# HG changeset patch # User Markus Bröker # Date 1287946681 -7200 # Node ID a8c2649cf277dc3dcededb331d16d8cb069116e8 # Parent 75133486ba7ec8eb6a51025e34fd29d0ccd2b9a0 FileTraverser Demo added Proper handling of symlinks in java apps committer: Markus Bröker diff --git a/filetraverser/FileTraverser.java b/filetraverser/FileTraverser.java new file mode 100644 --- /dev/null +++ b/filetraverser/FileTraverser.java @@ -0,0 +1,55 @@ +/** + * FileTraverser.java + * Copyright (C) 2010 Markus Broeker + */ + +import java.io.File; +import java.util.List; +import java.util.ArrayList; + +public class FileTraverser { + + /** + * @Params folder: the starting directory, the starting folder + * @Params recursive: list subfolders recursively or not + * @Params list: the list to store the data + */ + public static void find(String folder, boolean recursive, List list) { + + File directory = new File (folder); + if (!directory.isDirectory()) + return; + else + list.add(directory.getPath()); + + /* prevent linux recursion problems when using symlinks */ + if (Unix.isLink(folder)) { + return; + } + + File[] files = directory.listFiles(); + + for (File f : files) { + if (f.isDirectory() && recursive) { + find(f.getPath(), recursive, list); + } else + list.add(f.getPath()); + } + } + + public static void main(String[] args) { + List list = new ArrayList(); + String homeDir = System.getProperty("user.home"); + boolean recursive = false; + + if (args.length == 2) { + homeDir = args[0]; + recursive = Integer.parseInt(args[1]) != 0 ? true : false; + } + + find(homeDir, recursive, list); + for (String s : list) { + System.out.println ("List: " + s); + } + } +} diff --git a/filetraverser/Makefile b/filetraverser/Makefile new file mode 100644 --- /dev/null +++ b/filetraverser/Makefile @@ -0,0 +1,46 @@ + CC = gcc + JAVA = java +JAVAH = javah +JAVAC = javac + TC = Unix + LIB = lib/lib$(TC).so + +SOURCES = FileTraverser.java +SOURCES += $(TC).java + +OBJECTS = unix.o + +ifdef JAVA_HOME + JAVA_DIR="$(JAVA_HOME)" +else + JAVA_DIR=/usr/lib/jvm/java-6-sun +endif + +INCLUDE = -I$(JAVA_DIR)/include -I$(JAVA_DIR)/include/linux -Iinclude + +.SUFFIXES: .java + +.c.o: + @echo "JAVA_HOME=$(JAVA_DIR)" + $(CC) -c $(CFLAGS) $(INCLUDE) $< -o $@ + +all: $(TC).class $(LIB) + +$(TC).class: $(SOURCES) + $(JAVAC) -d . $(SOURCES) + $(JAVAH) -jni -d include $(TC) + +$(LIB): $(OBJECTS) + $(CC) -shared -Wl,-soname,lib$(TC).so.1,-rpath,lib $(OBJECTS) -o $@ + +.PHONY: clean + +clean: + rm -f include/$(TC).h *.class *~ $(LIB) + rm -f $(OBJECTS) + +debug: all + LD_LIBRARY_PATH=lib $(JAVA) -cp . FileTraverser + +run: all + $(JAVA) -Djava.library.path=lib -cp . FileTraverser $(HOME) 1 diff --git a/filetraverser/Unix.java b/filetraverser/Unix.java new file mode 100644 --- /dev/null +++ b/filetraverser/Unix.java @@ -0,0 +1,17 @@ +public class Unix { + public static native int isUnixLink(String name); + + public static boolean isLink(String name) { + if (isUnixLink(name) == 1) + return true; + return false; + } + + static { + try { + System.loadLibrary("Unix"); + } catch (UnsatisfiedLinkError ule) { + ule.printStackTrace(); + } + } +} diff --git a/filetraverser/include/.keep b/filetraverser/include/.keep new file mode 100644 diff --git a/filetraverser/lib/.keep b/filetraverser/lib/.keep new file mode 100644 diff --git a/filetraverser/unix.c b/filetraverser/unix.c new file mode 100644 --- /dev/null +++ b/filetraverser/unix.c @@ -0,0 +1,24 @@ +/** + * unix.c + * Copyright (C) 2010 Markus Broeker + */ + +#include +#include +#include +#include +#include + +int isUnixLink (const char *name) +{ + struct stat st; + + assert (lstat (name, &st) == 0); + return (S_ISLNK (st.st_mode)); +} + +JNIEXPORT jint JNICALL Java_Unix_isUnixLink (JNIEnv * env, jclass jc, jstring s) +{ + const char *c_string = (*env)->GetStringUTFChars (env, s, NULL); + return isUnixLink (c_string); +}