FileTraverser Demo added
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Sun, 24 Oct 2010 20:58:01 +0200
changeset 151 a8c2649cf277
parent 150 75133486ba7e
child 152 53ca9bff8b40
FileTraverser Demo added Proper handling of symlinks in java apps committer: Markus Bröker <mbroeker@largo.homelinux.org>
filetraverser/FileTraverser.java
filetraverser/Makefile
filetraverser/Unix.java
filetraverser/include/.keep
filetraverser/lib/.keep
filetraverser/unix.c
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<String> 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<String> list = new ArrayList<String>();
+        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);
+        }
+    }
+}
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
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();
+        }
+    }
+}
new file mode 100644
new file mode 100644
new file mode 100644
--- /dev/null
+++ b/filetraverser/unix.c
@@ -0,0 +1,24 @@
+/**
+ * unix.c
+ * Copyright (C) 2010 Markus Broeker
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <assert.h>
+#include <jni.h>
+
+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);
+}