Update of /cvsroot/nutch/nutch/src/java/net/nutch/util
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1162

Modified Files:
        LocalFileSystem.java 
Log Message:
This patch fixes several problems in LocalFileSystem:

* use copy & delete instead of rename. Rename is not guaranteed to
  work across filesystems.

  NOTE: this incurs a performance penalty for some of the NDFS operations.
  We need to find a better solution for this.

* return NDFSFile[] as a result from listFiles - even though the official
  interface is File[], clients can benefit from additional information.

* use recursive deletion for directories - File.delete() doesn't work here.



Index: LocalFileSystem.java
===================================================================
RCS file: /cvsroot/nutch/nutch/src/java/net/nutch/util/LocalFileSystem.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** LocalFileSystem.java        4 Oct 2004 15:42:58 -0000       1.3
--- LocalFileSystem.java        7 Oct 2004 21:27:15 -0000       1.4
***************
*** 8,11 ****
--- 8,15 ----
  import java.nio.channels.*;
  
+ import net.nutch.fs.NDFSFile;
+ import net.nutch.fs.NDFSFileInfo;
+ import net.nutch.io.UTF8;
+ 
  /****************************************************************
   * Implement the NutchFileSystem interface for the local disk.
***************
*** 19,22 ****
--- 23,28 ----
      TreeMap nonsharedLockDataSet = new TreeMap();
      TreeMap lockObjSet = new TreeMap();
+     // by default use copy/delete instead of rename
+     boolean useCopyForRename = true;
  
      /**
***************
*** 24,27 ****
--- 30,39 ----
      public LocalFileSystem() throws IOException {
          super();
+         // if you find an OS which reliably supports non-POSIX
+         // rename(2) across filesystems / volumes, you can
+         // uncomment this.
+         // String os = System.getProperty("os.name");
+         // if (os.toLowerCase().indexOf("os-with-super-rename") != -1)
+         //     useCopyForRename = false;
      }
  
***************
*** 213,217 ****
       */
      public boolean rename(File src, File dst) throws IOException {
!         return src.renameTo(dst);
      }
  
--- 225,232 ----
       */
      public boolean rename(File src, File dst) throws IOException {
!         if (useCopyForRename) {
!             FileUtil.copyContents(this, src, dst, true);
!             return fullyDelete(src);
!         } else return src.renameTo(dst);
      }
  
***************
*** 220,224 ****
       */
      public boolean delete(File f) throws IOException {
!         return f.delete();
      }
  
--- 235,241 ----
       */
      public boolean delete(File f) throws IOException {
!         if (f.isFile()) {
!             return f.delete();
!         } else return fullyDelete(f);
      }
  
***************
*** 244,248 ****
       */
      public File[] listFiles(File f) throws IOException {
!         return f.listFiles();
      }
  
--- 261,274 ----
       */
      public File[] listFiles(File f) throws IOException {
!         File[] files = f.listFiles();
!         if (files == null) return null;
!         NDFSFile[] nfiles = new NDFSFile[files.length];
!         for (int i = 0; i < files.length; i++) {
!             long len = files[i].length();
!             UTF8 name = new UTF8(files[i].toString());
!             NDFSFileInfo info = new NDFSFileInfo(name, len, len, 
files[i].isDirectory());
!             nfiles[i] = new NDFSFile(info);
!         }
!         return nfiles;
      }
  
***************
*** 303,307 ****
      public void addLocalFile(File src, File dst) throws IOException {
          if (! src.equals(dst)) {
!             src.renameTo(dst);
          }
      }
--- 329,336 ----
      public void addLocalFile(File src, File dst) throws IOException {
          if (! src.equals(dst)) {
!             if (useCopyForRename) {
!                 FileUtil.copyContents(this, src, dst, true);
!                 fullyDelete(src);
!             } else src.renameTo(dst);
          }
      }
***************
*** 361,363 ****
--- 390,417 ----
          return "LocalFS";
      }
+     
+     /**
+      * Implement our own version instead of using the one in FileUtil,
+      * to avoid infinite recursion.
+      * @param dir
+      * @return
+      * @throws IOException
+      */
+     private boolean fullyDelete(File dir) throws IOException {
+         File contents[] = dir.listFiles();
+         if (contents != null) {
+             for (int i = 0; i < contents.length; i++) {
+                 if (contents[i].isFile()) {
+                     if (! contents[i].delete()) {
+                         return false;
+                     }
+                 } else {
+                     if (! fullyDelete(contents[i])) {
+                         return false;
+                     }
+                 }
+             }
+         }
+         return dir.delete();
+     }
  }



-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal
Use IT products in your business? Tell us what you think of them. Give us
Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl
_______________________________________________
Nutch-cvs mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/nutch-cvs

Reply via email to