Esempio n. 1
0
  @Internal("file.copy")
  public static LogicalVector fileCopy(
      @Current Context context,
      StringVector fromFiles,
      String to,
      boolean overwrite,
      final boolean recursive)
      throws FileSystemException {
    LogicalArrayVector.Builder result = new LogicalArrayVector.Builder();
    FileObject toFile = context.resolveFile(to);
    for (String from : fromFiles) {
      try {
        toFile.copyFrom(
            context.resolveFile(from),
            new FileSelector() {

              @Override
              public boolean traverseDescendents(FileSelectInfo fileInfo) throws Exception {
                return true;
              }

              @Override
              public boolean includeFile(FileSelectInfo fileInfo) throws Exception {
                return recursive;
              }
            });
        result.add(true);
      } catch (FileSystemException e) {
        result.add(false);
      }
    }
    return result.build();
  }
Esempio n. 2
0
 /**
  * ‘file.append’ attempts to append the files named by its second argument to those named by its
  * first. The R subscript recycling rule is used to align names given in vectors of different
  * lengths.
  */
 @Internal("file.append")
 @DataParallel
 public static boolean fileAppend(
     @Current Context context, String destFileName, String sourceFileName) {
   try {
     FileObject sourceFile = context.resolveFile(sourceFileName);
     if (!sourceFile.exists()) {
       return false;
     }
     FileObject destFile = context.resolveFile(destFileName);
     OutputStream out = destFile.getContent().getOutputStream(true);
     try {
       InputStream in = sourceFile.getContent().getInputStream();
       try {
         ByteStreams.copy(in, out);
       } finally {
         try {
           in.close();
         } catch (Exception ignored) {
         }
       }
     } finally {
       try {
         out.close();
       } catch (Exception ignored) {
       }
     }
     return true;
   } catch (Exception e) {
     return false;
   }
 }
Esempio n. 3
0
  /**
   * Creates the last element of the path, unless recursive = TRUE. Trailing path separators are
   * removed.
   *
   * @param context the current call Context
   * @param path the path
   * @param showWarnings should the warnings on failure be shown?
   * @param recursive Should elements of the path other than the last be created? If true, like
   *     Unix's mkdir -p
   * @param mode the file mode to be used on Unix-alikes: it will be coerced by as.octmode.
   *     (currently ignored by renjin)
   * @return true if the operation succeeded for each of the files attempted. Using a missing value
   *     for a path name will always be regarded as a failure. returns false if the directory
   *     already exists
   * @throws FileSystemException
   */
  @Internal("dir.create")
  public static SEXP dirCreate(
      @Current Context context, String path, boolean showWarnings, boolean recursive, int mode)
      throws FileSystemException {
    FileObject dir = context.resolveFile(path);
    dir.createFolder();

    // TODO: return correct value and implement warnings documented above

    context.setInvisibleFlag();
    return new LogicalArrayVector(true);
  }
Esempio n. 4
0
  @Invisible
  @Internal
  public static String setwd(@Current Context context, String workingDirectoryName)
      throws FileSystemException {
    FileObject newWorkingDirectory = context.resolveFile(workingDirectoryName);
    if (!newWorkingDirectory.exists() || newWorkingDirectory.getType() != FileType.FOLDER) {
      throw new EvalException("cannot change working directory");
    }

    String previous = context.getSession().getWorkingDirectory().getName().getURI();

    context.getSession().setWorkingDirectory(newWorkingDirectory);
    return previous;
  }
Esempio n. 5
0
  /**
   * Gets the type or storage mode of an object.
   *
   * @return unix-style file mode integer
   */
  private static int mode(FileObject file) throws FileSystemException {
    int access = 0;
    if (file.isReadable()) {
      access += 4;
    }

    if (file.isWriteable()) {
      access += 2;
    }
    if (file.getType() == FileType.FOLDER) {
      access += 1;
    }
    // i know this is braindead but i can't be bothered
    // to do octal math at the moment
    String digit = Integer.toString(access);
    String octalString = digit + digit + digit;

    return Integer.parseInt(octalString, 8);
  }
Esempio n. 6
0
  private static int checkAccess(FileObject file, int mode) throws FileSystemException {

    boolean ok = true;
    if ((mode & CHECK_ACCESS_EXISTENCE) != 0 && !file.exists()) {
      ok = false;
    }

    if ((mode & CHECK_ACCESS_READ) != 0 && !file.isReadable()) {
      ok = false;
    }

    if ((mode & CHECK_ACCESS_WRITE) != 0 & !file.isWriteable()) {
      ok = false;
    }

    // case CHECK_ACCESS_EXECUTE:
    //      return -1; // don't know if this is possible to check with VFS
    //  }
    return ok ? 0 : -1;
  }
Esempio n. 7
0
  @Internal("file.create")
  @DataParallel
  public static boolean fileCreate(
      @Current Context context, @Recycle String fileName, @Recycle(false) boolean showWarnings)
      throws IOException {
    try {
      FileObject file = context.resolveFile(fileName);
      // VFS will create the parent folder if it doesn't exist,
      // which the R method is not supposed to do
      if (!file.getParent().exists()) {
        throw new IOException("No such file or directory");
      }
      file.getContent().getOutputStream().close();
      return true;

    } catch (Exception e) {
      if (showWarnings) {
        Warning.invokeWarning(
            context, "cannot create file '%s', reason '%s'", fileName, e.getMessage());
      }
      return false;
    }
  }
Esempio n. 8
0
 private static void delete(FileObject file, boolean recursive) throws FileSystemException {
   if (file.exists()) {
     if (file.getType() == FileType.FILE) {
       file.delete();
     } else if (file.getType() == FileType.FOLDER) {
       if (file.getChildren().length == 0) {
         file.delete();
       } else if (recursive) {
         file.delete();
       }
     }
   }
 }
Esempio n. 9
0
  /** Helper function to extract a zip entry to the given folder. */
  private static void unzipExtract(
      ZipInputStream zin, ZipEntry entry, FileObject exdir, boolean junkpaths, boolean overwrite)
      throws IOException {
    if (junkpaths) {
      throw new EvalException("unzip(junpaths=false) not yet implemented");
    }

    FileObject exfile = exdir.resolveFile(entry.getName());
    if (exfile.exists() && !overwrite) {
      throw new EvalException(
          "file to be extracted '%s' already exists", exfile.getName().getURI());
    }
    OutputStream out = exfile.getContent().getOutputStream();
    try {

      byte buffer[] = new byte[64 * 1024];
      int bytesRead;
      while ((bytesRead = zin.read(buffer)) != -1) {
        out.write(buffer, 0, bytesRead);
      }
    } finally {
      out.close();
    }
  }
Esempio n. 10
0
  /**
   * Utility function to extract information about files on the user's file systems.
   *
   * @param context current call Context
   * @param paths the list of files for which to return information
   * @return list column-oriented table of file information
   * @throws FileSystemException
   */
  @Internal("file.info")
  public static ListVector fileInfo(@Current Context context, StringVector paths)
      throws FileSystemException {

    DoubleArrayVector.Builder size = new DoubleArrayVector.Builder();
    LogicalArrayVector.Builder isdir = new LogicalArrayVector.Builder();
    IntArrayVector.Builder mode =
        (IntArrayVector.Builder)
            new IntArrayVector.Builder()
                .setAttribute(Symbols.CLASS, StringVector.valueOf("octmode"));
    DoubleArrayVector.Builder mtime = new DoubleArrayVector.Builder();
    StringVector.Builder exe = new StringVector.Builder();

    for (String path : paths) {
      if (StringVector.isNA(path)) {
        throw new EvalException("invalid filename argument");
      }
      FileObject file = context.resolveFile(path);
      if (file.exists()) {
        if (file.getType() == FileType.FILE) {
          size.add((int) file.getContent().getSize());
        } else {
          size.add(0);
        }
        isdir.add(file.getType() == FileType.FOLDER);
        mode.add(mode(file));
        try {
          mtime.add(file.getContent().getLastModifiedTime());
        } catch (Exception e) {
          mtime.add(0);
        }
        exe.add(file.getName().getBaseName().endsWith(".exe") ? "yes" : "no");
      } else {
        size.addNA();
        isdir.addNA();
        mode.addNA();
        mtime.addNA();
        exe.addNA();
      }
    }

    return ListVector.newNamedBuilder()
        .add("size", size)
        .add("isdir", isdir)
        .add("mode", mode)
        .add("mtime", mtime)
        .add("ctime", mtime)
        .add("atime", mtime)
        .add("exe", exe)
        .build();
  }