File Scanning on a Unix Box

R

Rishi Dhupar

Hi,
I have written this recursion File scan, that is working perfectly in
Windows but of course just won't work in Unix.

Pretty simple, just goes thru directories and subdirectories and adds
any File it finds to a vector of type FileInfo. Any idea on how to get
to this to work on a Unix box?

Also should I be running the scan on the mounted name or the file
system name? Both do not seem to be working. Not too familiar with how
Unix filesystems work. For instance I run it on "FileScan.jar /boot"
Program says it isn't a valid directory, but f I run "FileScan.jar /"
Doesn't give error of not a directory, Java gives this error.

Exception in thread "main" java.lang.NullPointerException
at java.io.File.File(java.lang.String, java.lang.String)
(/usr/lib/libgcj.so.
5.0.0)
at MyFileStructure.build(java.io.File) (Unknown Source)
at MyFileStructure.build(java.io.File) (Unknown Source)
at MyFileStructure.build(java.io.File) (Unknown Source)
at MyFileStructure.build(java.io.File) (Unknown Source)
at MyFileStructure.build(java.io.File) (Unknown Source)
at MyFileStructure.build() (Unknown Source)
at Main.main(java.lang.String[]) (Unknown Source)

private FileInfo build(File f) {
if (f.getPath().indexOf("System Volume Information")!= -1) return
null;
if (!f.exists()) return null;
if (!f.isDirectory()) return null;

// f is an existing directory
String path = f.getPath();
String name = f.getName();
//FileInfo mdir = new FileInfo(path, name);

// Loop thru files and directories in this path
String[] files = f.list();
for (int i = 0; i < files.length; i++) {
File f2 = new File(path, files);
if (f2.isFile()) {
//mdir.addFile(new FileInfo(path, files,f2.length()));
this.addFile(new FileInfo((path + "\\"), files,f2.length()));
} else if (f2.isDirectory()) {
File f3 = new File(path, files);
FileInfo m = build(f3); // recursive call
//if (m != null) { mdir.addDir(m); }
}
}
return mdir;
}



Thanks you very much
 
S

shakah

Rishi said:
Hi,
I have written this recursion File scan, that is working perfectly in
Windows but of course just won't work in Unix.

Pretty simple, just goes thru directories and subdirectories and adds
any File it finds to a vector of type FileInfo. Any idea on how to get
to this to work on a Unix box?

Also should I be running the scan on the mounted name or the file
system name? Both do not seem to be working. Not too familiar with how
Unix filesystems work. For instance I run it on "FileScan.jar /boot"
Program says it isn't a valid directory, but f I run "FileScan.jar /"
Doesn't give error of not a directory, Java gives this error.

Exception in thread "main" java.lang.NullPointerException
at java.io.File.File(java.lang.String, java.lang.String)
(/usr/lib/libgcj.so.
5.0.0)
at MyFileStructure.build(java.io.File) (Unknown Source)
at MyFileStructure.build(java.io.File) (Unknown Source)
at MyFileStructure.build(java.io.File) (Unknown Source)
at MyFileStructure.build(java.io.File) (Unknown Source)
at MyFileStructure.build(java.io.File) (Unknown Source)
at MyFileStructure.build() (Unknown Source)
at Main.main(java.lang.String[]) (Unknown Source)

private FileInfo build(File f) {
if (f.getPath().indexOf("System Volume Information")!= -1) return
null;
if (!f.exists()) return null;
if (!f.isDirectory()) return null;

// f is an existing directory
String path = f.getPath();
String name = f.getName();
//FileInfo mdir = new FileInfo(path, name);

// Loop thru files and directories in this path
String[] files = f.list();
for (int i = 0; i < files.length; i++) {
File f2 = new File(path, files);
if (f2.isFile()) {
//mdir.addFile(new FileInfo(path, files,f2.length()));
this.addFile(new FileInfo((path + "\\"), files,f2.length()));
} else if (f2.isDirectory()) {
File f3 = new File(path, files);
FileInfo m = build(f3); // recursive call
//if (m != null) { mdir.addDir(m); }
}
}
return mdir;
}



Thanks you very much



Use java.io.File.separator() or java.io.File.separatorChar() instead of
your hard-coded "\\" string when building the filenames.
 
R

Rishi Dhupar

Are you referring to this line?

this.addFile(new FileInfo((path + "\\"), files,f2.length()));

If so that shouldn't have anything to do with the reason the program is
getting a null pointer exception. I use \\ just to append it to the
path for other parts of the program that I use to compare items
together. But thanks for the tip I will actually use it.

I know on windows I had an issue with System Volume Information
directory, as this returns null and Java messes up. Is there some
directory that could cause this on a Unix box that I should remove from
my scanning?
 
S

shakah

Rishi said:
Hi,
I have written this recursion File scan, that is working perfectly in
Windows but of course just won't work in Unix.

Pretty simple, just goes thru directories and subdirectories and adds
any File it finds to a vector of type FileInfo. Any idea on how to get
to this to work on a Unix box?

Also should I be running the scan on the mounted name or the file
system name? Both do not seem to be working. Not too familiar with how
Unix filesystems work. For instance I run it on "FileScan.jar /boot"
Program says it isn't a valid directory, but f I run "FileScan.jar /"
Doesn't give error of not a directory, Java gives this error.

Exception in thread "main" java.lang.NullPointerException
at java.io.File.File(java.lang.String, java.lang.String)
(/usr/lib/libgcj.so.
5.0.0)
at MyFileStructure.build(java.io.File) (Unknown Source)
at MyFileStructure.build(java.io.File) (Unknown Source)
at MyFileStructure.build(java.io.File) (Unknown Source)
at MyFileStructure.build(java.io.File) (Unknown Source)
at MyFileStructure.build(java.io.File) (Unknown Source)
at MyFileStructure.build() (Unknown Source)
at Main.main(java.lang.String[]) (Unknown Source)

private FileInfo build(File f) {
if (f.getPath().indexOf("System Volume Information")!= -1) return
null;
if (!f.exists()) return null;
if (!f.isDirectory()) return null;

// f is an existing directory
String path = f.getPath();
String name = f.getName();
//FileInfo mdir = new FileInfo(path, name);

// Loop thru files and directories in this path
String[] files = f.list();
for (int i = 0; i < files.length; i++) {
File f2 = new File(path, files);
if (f2.isFile()) {
//mdir.addFile(new FileInfo(path, files,f2.length()));
this.addFile(new FileInfo((path + "\\"), files,f2.length()));
} else if (f2.isDirectory()) {
File f3 = new File(path, files);
FileInfo m = build(f3); // recursive call
//if (m != null) { mdir.addDir(m); }
}
}
return mdir;
}



Thanks you very much


Use code like:
new FileInfo((path + java.io.File.pathSeparator())

in place of:
new FileInfo((path + "\\")
 
N

Nigel Wade

Rishi said:
Hi,
I have written this recursion File scan, that is working perfectly in
Windows but of course just won't work in Unix.

Pretty simple, just goes thru directories and subdirectories and adds
any File it finds to a vector of type FileInfo. Any idea on how to get
to this to work on a Unix box?

Also should I be running the scan on the mounted name or the file
system name? Both do not seem to be working. Not too familiar with how
Unix filesystems work. For instance I run it on "FileScan.jar /boot"
Program says it isn't a valid directory, but f I run "FileScan.jar /"
Doesn't give error of not a directory, Java gives this error.

Exception in thread "main" java.lang.NullPointerException
at java.io.File.File(java.lang.String, java.lang.String)
(/usr/lib/libgcj.so.
5.0.0)
at MyFileStructure.build(java.io.File) (Unknown Source)
at MyFileStructure.build(java.io.File) (Unknown Source)
at MyFileStructure.build(java.io.File) (Unknown Source)
at MyFileStructure.build(java.io.File) (Unknown Source)
at MyFileStructure.build(java.io.File) (Unknown Source)
at MyFileStructure.build() (Unknown Source)
at Main.main(java.lang.String[]) (Unknown Source)

private FileInfo build(File f) {
if (f.getPath().indexOf("System Volume Information")!= -1) return
null;
if (!f.exists()) return null;
if (!f.isDirectory()) return null;

// f is an existing directory
String path = f.getPath();
String name = f.getName();
//FileInfo mdir = new FileInfo(path, name);

// Loop thru files and directories in this path
String[] files = f.list();
for (int i = 0; i < files.length; i++) {
File f2 = new File(path, files);
if (f2.isFile()) {
//mdir.addFile(new FileInfo(path, files,f2.length()));
this.addFile(new FileInfo((path + "\\"), files,f2.length()));
} else if (f2.isDirectory()) {
File f3 = new File(path, files);
FileInfo m = build(f3); // recursive call
//if (m != null) { mdir.addDir(m); }
}
}
return mdir;
}



Thanks you very much


UNIX has a thing called "security", something which Windows hasn't really
got to grips with yet.

If you don't have read permission on a directory you can't read the
contents, and the line String[] files = f.list(); will return null. You
blithely go on to use files.length on the next line without bothering to
test whether files is null.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top