Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Reproducing du/ls in Java

Reply
Thread Tools

Reproducing du/ls in Java

 
 
Oreo
Guest
Posts: n/a
 
      11-29-2004
Hi there,

I am writing an application that needs to compute the exact size of a
file. I need for this function to work in Windows and UNIX.

One of my issues is that the length() method provided by the File
class does not return the exact size for all files.

The essence of my problem is that I cannot properly compute the size
of a directory or symbolic links in UNIX properly.

File.class() returns 0 for the size of directories regardless of OS.
This appears to be correct on Windows (right-click -> properties on an
empty directory in Windows). However this seems to be incorrect
according to Linux.

When compared to the output of ls, the File.length() method will not
return the proper length of directories (whether on a FAT or reiserfs
file system) or a symbolic link in UNIX. The length of a UNIX
directory is not zero, and the size of symbolic link is the length of
the filename that it points to.

When the windows partition is mounted in Linux, the size of the
directories on the windows partition are reported as being 4096 by
file system utilities ls and du. This contrasts the value of zero
reported by the Microsoft 98 Windows Explorer.

Detailed examples of my problem can be found at the following thread:
http://forum.java.sun.com/thread.jspa?threadID=574755

How can I compute a function that will be able to compute the size of
any file under any operating system?

I would like to avoid his path:
1. Determine if the class is being run in Windows or UNIX
2. If running in UNIX,
a. If a directory of symbolic link: execute : du -sb [file]
| awk '{print $1}'
or ls -al [file ] | awk ' { print $5 }'
b. Otherwise: File.length()
3. If running Windows, simply class File.length() (?)


By taking this route I need to perform an if() statement every time
the size of any file is being computed, and also execute an expensive
command for some special cases (when the file is a directory of link).
The expensive commands will be common, and I would like to avoid
having to do that.

- Oreo
 
Reply With Quote
 
 
 
 
Sudsy
Guest
Posts: n/a
 
      11-29-2004
Oreo wrote:
<snip>
> When compared to the output of ls, the File.length() method will not
> return the proper length of directories (whether on a FAT or reiserfs
> file system) or a symbolic link in UNIX. The length of a UNIX
> directory is not zero, and the size of symbolic link is the length of
> the filename that it points to.


WRONG! Take a look at this output from a Linux system:
$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Oct 26 13:43 /bin/sh -> bash

That's showing a file size of 4 bytes. It's most empatically NOT
the size of /bin/bash.
As for the correct size of directories, I tested out the following
very simple code and it returned the same value as what ls returned
(not surprising, as it's just mapping to a stat(2) system call
under the covers), namely 5120 bytes.

import java.io.*;

public class DirUsage {
public static void main( String args[] ) {
File f = new File( "." );
System.out.println( "Size = " + f.length() );
}
}

--
Java/J2EE/JSP/Struts/Tiles/C/UNIX consulting and remote development.

 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      11-29-2004
On Mon, 29 Nov 2004 09:31:32 -0500, Sudsy wrote:
> Oreo wrote:
>> The length of a UNIX directory is not zero, and the size of
>> symbolic link is the length of the filename that it points to.

>
> WRONG! Take a look at this output from a Linux system:
> $ ls -l /bin/sh
> lrwxrwxrwx 1 root root 4 Oct 26 13:43 /bin/sh -> bash
>
> That's showing a file size of 4 bytes. It's most empatically NOT
> the size of /bin/bash.


Well, he did say the length of the filename, not the length of the file...

It might also be worth pointing out that some linux filesystems can
store symlinks in the inode itself without requiring a separate data
block. These are called "fast symlinks" and ls will show you their
size just as if a data block had been allocated to hold the filename.

To the OP: most file operations provided by the operating system act
on the link target, not the link itself, and that's what Java
provides. However ls uses lstat() to get the size of the symlink
itself, but will show the size of the link target if you specify -L.

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
Ann
Guest
Posts: n/a
 
      11-29-2004

"Oreo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> Hi there,
>
> I am writing an application that needs to compute the exact size of a
> file. I need for this function to work in Windows and UNIX.
>
> One of my issues is that the length() method provided by the File
> class does not return the exact size for all files.
>
> The essence of my problem is that I cannot properly compute the size
> of a directory or symbolic links in UNIX properly.
>
> File.class() returns 0 for the size of directories regardless of OS.
> This appears to be correct on Windows (right-click -> properties on an
> empty directory in Windows). However this seems to be incorrect
> according to Linux.
>
> When compared to the output of ls, the File.length() method will not
> return the proper length of directories (whether on a FAT or reiserfs
> file system) or a symbolic link in UNIX. The length of a UNIX
> directory is not zero, and the size of symbolic link is the length of
> the filename that it points to.
>
> When the windows partition is mounted in Linux, the size of the
> directories on the windows partition are reported as being 4096 by
> file system utilities ls and du. This contrasts the value of zero
> reported by the Microsoft 98 Windows Explorer.
>
> Detailed examples of my problem can be found at the following thread:
> http://forum.java.sun.com/thread.jspa?threadID=574755
>
> How can I compute a function that will be able to compute the size of
> any file under any operating system?
>
> I would like to avoid his path:
> 1. Determine if the class is being run in Windows or UNIX
> 2. If running in UNIX,
> a. If a directory of symbolic link: execute : du -sb [file]
> | awk '{print $1}'
> or ls -al [file ] | awk ' { print $5 }'
> b. Otherwise: File.length()
> 3. If running Windows, simply class File.length() (?)
>
>
> By taking this route I need to perform an if() statement every time
> the size of any file is being computed, and also execute an expensive
> command for some special cases (when the file is a directory of link).
> The expensive commands will be common, and I would like to avoid
> having to do that.
>
> - Oreo


What about the "master file table" ?

And, since there is extra overhead in a directory, do you want
to assign a fraction of that overhead to your filesize too?


 
Reply With Quote
 
Oreo
Guest
Posts: n/a
 
      11-29-2004
Sudsy <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Oreo wrote:
> <snip>
> > When compared to the output of ls, the File.length() method will not
> > return the proper length of directories (whether on a FAT or reiserfs
> > file system) or a symbolic link in UNIX. The length of a UNIX
> > directory is not zero, and the size of symbolic link is the length of
> > the filename that it points to.

>
> WRONG! Take a look at this output from a Linux system:
> $ ls -l /bin/sh
> lrwxrwxrwx 1 root root 4 Oct 26 13:43 /bin/sh -> bash
>
> That's showing a file size of 4 bytes. It's most empatically NOT
> the size of /bin/bash.
> As for the correct size of directories, I tested out the following
> very simple code and it returned the same value as what ls returned
> (not surprising, as it's just mapping to a stat(2) system call
> under the covers), namely 5120 bytes.
>
> import java.io.*;
>
> public class DirUsage {
> public static void main( String args[] ) {
> File f = new File( "." );
> System.out.println( "Size = " + f.length() );
> }
> }



I am computing the size of the symbolic link itself, not the file it points to.
 
Reply With Quote
 
Oreo
Guest
Posts: n/a
 
      11-30-2004
"Ann" <(E-Mail Removed)> wrote in message news:<OnIqd.174555$R05.129476@attbi_s53>...
> "Oreo" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) m...
> > Hi there,
> >
> > I am writing an application that needs to compute the exact size of a
> > file. I need for this function to work in Windows and UNIX.
> >
> > One of my issues is that the length() method provided by the File
> > class does not return the exact size for all files.
> >
> > The essence of my problem is that I cannot properly compute the size
> > of a directory or symbolic links in UNIX properly.
> >
> > File.class() returns 0 for the size of directories regardless of OS.
> > This appears to be correct on Windows (right-click -> properties on an
> > empty directory in Windows). However this seems to be incorrect
> > according to Linux.
> >
> > When compared to the output of ls, the File.length() method will not
> > return the proper length of directories (whether on a FAT or reiserfs
> > file system) or a symbolic link in UNIX. The length of a UNIX
> > directory is not zero, and the size of symbolic link is the length of
> > the filename that it points to.
> >
> > When the windows partition is mounted in Linux, the size of the
> > directories on the windows partition are reported as being 4096 by
> > file system utilities ls and du. This contrasts the value of zero
> > reported by the Microsoft 98 Windows Explorer.
> >
> > Detailed examples of my problem can be found at the following thread:
> > http://forum.java.sun.com/thread.jspa?threadID=574755
> >
> > How can I compute a function that will be able to compute the size of
> > any file under any operating system?
> >
> > I would like to avoid his path:
> > 1. Determine if the class is being run in Windows or UNIX
> > 2. If running in UNIX,
> > a. If a directory of symbolic link: execute : du -sb [file]
> > | awk '{print $1}'
> > or ls -al [file ] | awk ' { print $5 }'
> > b. Otherwise: File.length()
> > 3. If running Windows, simply class File.length() (?)
> >
> >
> > By taking this route I need to perform an if() statement every time
> > the size of any file is being computed, and also execute an expensive
> > command for some special cases (when the file is a directory of link).
> > The expensive commands will be common, and I would like to avoid
> > having to do that.
> >
> > - Oreo

>
> What about the "master file table" ?
>
> And, since there is extra overhead in a directory, do you want
> to assign a fraction of that overhead to your filesize too?


How can I access the master file table? A search did not produce any
results, other than some recovery software.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reproducing c function in java jimgardener Java 3 06-26-2008 07:11 PM
Reproducing white at ISO 100 ronviers@gmail.com Digital Photography 22 03-24-2006 06:23 AM
Trouble Reproducing the Color Purple powrwrap@aol.com Digital Photography 29 01-01-2006 08:53 PM
Size of digital images for reproducing in newspapers and magazines. Eric Clive Digital Photography 3 07-28-2004 04:01 PM
reproducing HttpContext behavior John ASP .Net 0 12-14-2003 04:00 PM



Advertisments