@Override public Void invoke(File f, VirtualChannel channel) throws IOException { Files.move(fileToPath(reading(f)), fileToPath(creating(new File(target.remote))), LinkOption.NOFOLLOW_LINKS); return null; } }
@Override public String invoke(File f, VirtualChannel channel) throws IOException, InterruptedException { return new String(Files.readAllBytes(fileToPath(reading(f)))); } }
/** * Creates an empty file if nonexistent or truncates the existing file. * Note: The behavior of this method in the case where the file already * exists is unlike the POSIX <code>touch</code> utility which merely * updates the file's access and/or modification time. */ public static void touch(@Nonnull File file) throws IOException { Files.newOutputStream(fileToPath(file)).close(); }
private boolean tryCopyWithAttributes(File f, Path targetPath) { try { Files.copy(fileToPath(f), targetPath, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { LOGGER.log(Level.FINE, "Unable to copy: {0}", e.getMessage()); return false; } return true; } @Override
@Override public Void invoke(File f, VirtualChannel channel) throws IOException { File targetFile = new File(target.remote); File targetDir = targetFile.getParentFile(); filterNonNull().mkdirs(targetDir); Files.createDirectories(fileToPath(targetDir)); Files.copy(fileToPath(reading(f)), fileToPath(writing(targetFile)), StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING); return null; } }
@Override public Void invoke(File f, VirtualChannel channel) throws IOException { try (InputStream fis = Files.newInputStream(fileToPath(reading(f)))) { org.apache.commons.io.IOUtils.copy(fis, out); return null; } finally { out.close(); } } }
@Override public Void invoke(File f, VirtualChannel channel) throws IOException { if(!f.exists()) { Files.newOutputStream(fileToPath(creating(f))).close(); } if(!stating(f).setLastModified(timestamp)) throw new IOException("Failed to set the timestamp of "+f+" to "+timestamp); return null; } }
@Override public Void invoke(File f, VirtualChannel channel) throws IOException { mkdirs(f.getParentFile()); try (OutputStream fos = Files.newOutputStream(fileToPath(writing(f))); Writer w = encoding != null ? new OutputStreamWriter(fos, encoding) : new OutputStreamWriter(fos)) { w.write(content); } return null; } }
@Override public Void invoke(File f, VirtualChannel channel) throws IOException { Util.deleteRecursive(fileToPath(f), path -> deleting(path.toFile())); return null; } }
@Override public Void invoke(File f, VirtualChannel channel) throws IOException { Util.deleteContentsRecursive(fileToPath(f), path -> deleting(path.toFile())); return null; } }
@Override public OutputStream invoke(File f, VirtualChannel channel) throws IOException, InterruptedException { f = f.getAbsoluteFile(); mkdirs(f.getParentFile()); return new RemoteOutputStream(Files.newOutputStream(fileToPath(writing(f)))); } }
/** * Deletes the given directory (including its contents) recursively. * It does not take no for an answer - if necessary, it will have multiple * attempts at deleting things. * * @throws IOException * if the operation fails. */ public static void deleteRecursive(@Nonnull File dir) throws IOException { deleteRecursive(fileToPath(dir), PathRemover.PathChecker.ALLOW_ALL); }
private File createDirectoryForNewUser(String userId) throws IOException { try { Path tempDirectory = Files.createTempDirectory(Util.fileToPath(usersDirectory), generatePrefix(userId)); return tempDirectory.toFile(); } catch (IOException e) { LOGGER.log(Level.SEVERE, "Error creating directory for user: " + userId, e); throw e; } }
/** * Deletes the contents of the given directory (but not the directory itself) * recursively. * It does not take no for an answer - if necessary, it will have multiple * attempts at deleting things. * * @throws IOException * if the operation fails. */ public static void deleteContentsRecursive(@Nonnull File file) throws IOException { deleteContentsRecursive(fileToPath(file), PathRemover.PathChecker.ALLOW_ALL); }
/** * Computes the MD5 digest of a file. * @param file a file * @return a 32-character string * @throws IOException in case reading fails * @since 1.525 */ @Nonnull public static String getDigestOf(@Nonnull File file) throws IOException { // Note: getDigestOf() closes the input stream. return getDigestOf(Files.newInputStream(fileToPath(file))); }
@Override public Void invoke(File f, VirtualChannel channel) throws IOException, InterruptedException { try (InputStream fis = Files.newInputStream(fileToPath(reading(f))); OutputStream out = p.getOut()) { org.apache.commons.io.IOUtils.copy(fis, out); } catch (Exception x) { p.error(x); } return null; } }
private boolean mkdirs(File dir) throws IOException { if (dir.exists()) return false; filterNonNull().mkdirs(dir); Files.createDirectories(fileToPath(dir)); return true; }
/** * Deletes this file (and does not take no for an answer). * If necessary, it will have multiple attempts at deleting things. * * @param f a file to delete * @throws IOException if it exists but could not be successfully deleted */ public static void deleteFile(@Nonnull File f) throws IOException { newPathRemover(PathRemover.PathChecker.ALLOW_ALL).forceRemoveFile(fileToPath(f)); }
/** * Creates a new {@link jenkins.util.io.LinesStream} of the file. * <p> * Note: The caller is responsible for closing the returned * <code>LinesStream</code>. * @throws IOException if the file cannot be converted to a * {@link java.nio.file.Path} or if the file cannot be opened for reading * @since 2.111 */ @CreatesObligation public @Nonnull LinesStream linesStream() throws IOException { return new LinesStream(Util.fileToPath(file)); }
/** * Reads this file. */ public InputStream read() throws IOException, InterruptedException { if(channel==null) { return Files.newInputStream(fileToPath(reading(new File(remote)))); } final Pipe p = Pipe.createRemoteToLocal(); actAsync(new Read(p)); return p.getIn(); } private class Read extends SecureFileCallable<Void> {