public static boolean copy(File src, File dest, CopyProgressListener l, boolean overwrite)
     throws IOException {
   if (dest.exists()) {
     if (!dest.isFile()) {
       throw new IOException("impossible to copy: destination is not a file: " + dest);
     }
     if (overwrite) {
       if (!dest.canWrite()) {
         dest.delete();
       } // if dest is writable, the copy will overwrite it without requiring a delete
     } else {
       Message.verbose(dest + " already exists, nothing done");
       return false;
     }
   }
   copy(new FileInputStream(src), dest, l);
   long srcLen = src.length();
   long destLen = dest.length();
   if (srcLen != destLen) {
     dest.delete();
     throw new IOException(
         "size of source file "
             + src.toString()
             + "("
             + srcLen
             + ") differs from size of dest file "
             + dest.toString()
             + "("
             + destLen
             + ") - please retry");
   }
   dest.setLastModified(src.lastModified());
   return true;
 }
  public static void symlink(File src, File dest, CopyProgressListener l, boolean overwrite)
      throws IOException {
    try {
      if (dest.exists()) {
        if (!dest.isFile()) {
          throw new IOException("impossible to copy: destination is not a file: " + dest);
        }
        if (!overwrite) {
          Message.verbose(dest + " already exists, nothing done");
          return;
        }
      }
      if (dest.getParentFile() != null) {
        dest.getParentFile().mkdirs();
      }

      Runtime runtime = Runtime.getRuntime();
      Message.verbose("executing 'ln -s -f " + src.getAbsolutePath() + " " + dest.getPath() + "'");
      Process process =
          runtime.exec(new String[] {"ln", "-s", "-f", src.getAbsolutePath(), dest.getPath()});

      if (process.waitFor() != 0) {
        InputStream errorStream = process.getErrorStream();
        InputStreamReader isr = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(isr);

        StringBuffer error = new StringBuffer();
        String line;
        while ((line = br.readLine()) != null) {
          error.append(line);
          error.append('\n');
        }

        throw new IOException("error symlinking " + src + " to " + dest + ":\n" + error);
      }

      // check if the creation of the symbolic link was successful
      if (!dest.exists()) {
        throw new IOException("error symlinking: " + dest + " doesn't exists");
      }

      // check if the result is a true symbolic link
      if (dest.getAbsolutePath().equals(dest.getCanonicalPath())) {
        dest.delete(); // just make sure we do delete the invalid symlink!
        throw new IOException("error symlinking: " + dest + " isn't a symlink");
      }
    } catch (IOException x) {
      Message.verbose("symlink failed; falling back to copy");
      StringWriter buffer = new StringWriter();
      x.printStackTrace(new PrintWriter(buffer));
      Message.debug(buffer.toString());
      copy(src, dest, l, overwrite);
    } catch (InterruptedException x) {
      Thread.currentThread().interrupt();
    }
  }
Exemplo n.º 3
0
 protected void saveFile(String name, InputStream input) throws IOException {
   FileUtil.copy(input, new File(this.dir, name), null);
 }
 public static void copy(InputStream src, OutputStream dest, CopyProgressListener l)
     throws IOException {
   copy(src, dest, l, true);
 }
 public static void copy(InputStream src, File dest, CopyProgressListener l) throws IOException {
   if (dest.getParentFile() != null) {
     dest.getParentFile().mkdirs();
   }
   copy(src, new FileOutputStream(dest), l);
 }