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(); } } } }
/** * 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(); }
@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; }
/** * 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); }