private void addEmptyUsernameIfExists(Map<String, File> users) throws IOException { File emptyUsernameConfigFile = new File(usersDirectory, User.CONFIG_XML); if (emptyUsernameConfigFile.exists()) { File newEmptyUsernameDirectory = new File(usersDirectory, EMPTY_USERNAME_DIRECTORY_NAME); Files.createDirectory(newEmptyUsernameDirectory.toPath()); File newEmptyUsernameConfigFile = new File(newEmptyUsernameDirectory, User.CONFIG_XML); Files.move(emptyUsernameConfigFile.toPath(), newEmptyUsernameConfigFile.toPath()); users.put("", newEmptyUsernameDirectory); } }
long bytes = java.nio.file.Files.copy( new java.io.File("<filepath1>").toPath(), new java.io.File("<filepath2>").toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING, java.nio.file.StandardCopyOption.COPY_ATTRIBUTES, java.nio.file.LinkOption.NOFOLLOW_LINKS);
public static boolean isCaseSensitiveFS(File testDir) { if (testDir != null) { File caseCheckUpper = new File(testDir, "CaseCheck"); File caseCheckLow = new File(testDir, "casecheck"); try { makeDirs(testDir); if (caseCheckUpper.createNewFile()) { boolean caseSensitive = !caseCheckLow.exists(); LOG.debug("Filesystem at {} is {}case-sensitive", testDir.getAbsolutePath(), (caseSensitive ? "" : "NOT ")); return caseSensitive; } else { LOG.debug("Failed to create file: {}", caseCheckUpper.getAbsolutePath()); } } catch (Exception e) { LOG.debug("Failed to detect filesystem case-sensitivity by file creation", e); } finally { try { Files.deleteIfExists(caseCheckUpper.toPath()); Files.deleteIfExists(caseCheckLow.toPath()); } catch (Exception e) { // ignore } } } return IOCase.SYSTEM.isCaseSensitive(); }
@Test public void testRunVerticleWithoutMainVerticleInManifestButWithCustomCommand() throws Exception { // Copy the right manifest File manifest = new File("target/test-classes/META-INF/MANIFEST-Launcher-Default-Command.MF"); if (!manifest.isFile()) { throw new IllegalStateException("Cannot find the MANIFEST-Default-Command.MF file"); } File target = new File("target/test-classes/META-INF/MANIFEST.MF"); Files.copy(manifest.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING); Launcher launcher = new Launcher(); HelloCommand.called = false; String[] args = {"--name=vert.x"}; launcher.dispatch(args); assertWaitUntil(() -> HelloCommand.called); }
@Test public void testTodaysFilesPickedUp() throws IOException { final SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd", Locale.US); final String dirStruc = sdf.format(new Date()); final File directory = new File("target/test/data/in/" + dirStruc); deleteDirectory(directory); assertTrue("Unable to create test data directory " + directory.getAbsolutePath(), directory.exists() || directory.mkdirs()); final File inFile = new File("src/test/resources/hello.txt"); final Path inPath = inFile.toPath(); final File destFile = new File(directory, inFile.getName()); final Path targetPath = destFile.toPath(); Files.copy(inPath, targetPath); final TestRunner runner = TestRunners.newTestRunner(new GetFile()); runner.setProperty(GetFile.DIRECTORY, "target/test/data/in/${now():format('yyyy/MM/dd')}"); runner.run(); runner.assertAllFlowFilesTransferred(GetFile.REL_SUCCESS, 1); final List<MockFlowFile> successFiles = runner.getFlowFilesForRelationship(GetFile.REL_SUCCESS); successFiles.get(0).assertContentEquals("Hello, World!".getBytes("UTF-8")); }
@Test public void withFiles_savedToLocatorSpecifiedRelativeDir() throws Exception { String[] extensions = {"zip"}; Path workingDirPath = getWorkingDirectory().toPath(); Path subdirPath = workingDirPath.resolve("some").resolve("test").resolve("directory"); Path relativeDir = workingDirPath.relativize(subdirPath); // Expects locator to produce file in own working directory when connected via JMX gfshConnector.executeCommand("export logs --dir=" + relativeDir.toString()); assertThat(listFiles(getWorkingDirectory(), extensions, false)).isEmpty(); assertThat(listFiles(getWorkingDirectory(), extensions, true)).isNotEmpty(); assertThat(listFiles(subdirPath.toFile(), extensions, false)).isNotEmpty(); }
/** * Utility method that copy a file from its current location to the * provided target directory. * * @param file file that needs to be copied. * @param targetDirectory the destination directory * @throws IOException */ public static void copyFileToDirectory( File file, File targetDirectory ) throws IOException { if ( !targetDirectory.exists() ) { Files.createDirectories( targetDirectory.toPath() ); } if ( !targetDirectory.isDirectory() ) { throw new IllegalArgumentException( "Move target must be a directory, not " + targetDirectory ); } File target = new File( targetDirectory, file.getName() ); copyFile( file, target ); }
sei.fMask = SEE_MASK_NOCLOSEPROCESS; sei.lpVerb = "runas"; sei.lpFile = jenkinsExe.getAbsolutePath(); sei.lpParameters = "/redirect redirect.log "+command; sei.lpDirectory = pwd.getAbsolutePath(); sei.nShow = SW_HIDE; if (!Shell32.INSTANCE.ShellExecuteEx(sei)) return Kernel32Utils.waitForExitProcess(sei.hProcess); } finally { try (InputStream fin = Files.newInputStream(new File(pwd,"redirect.log").toPath())) { IOUtils.copy(fin, out.getLogger()); } catch (InvalidPathException e) {
@Override public Path discover(final String cgroup) { Preconditions.checkNotNull(cgroup, "cgroup required"); final File procMounts = new File(procDir, "mounts"); final File pidCgroups = new File(procDir, "cgroup"); final PidCgroupEntry pidCgroupsEntry = getCgroupEntry(pidCgroups, cgroup); final ProcMountsEntry procMountsEntry = getMountEntry(procMounts, cgroup); final File cgroupDir = new File( procMountsEntry.path.toString(), pidCgroupsEntry.path.toString() ); if (cgroupDir.exists() && cgroupDir.isDirectory()) { return cgroupDir.toPath(); } throw new RE("Invalid cgroup directory [%s]", cgroupDir); }
protected PrintWriter createWriter(String outdir) throws IOException { new File(outdir).mkdirs(); String jvmArg = System.getProperty(JVM_ARG); if (jvmArg != null && !jvmArg.trim().isEmpty()) { fileName = jvmArg; } return new PrintWriter(newBufferedWriter(new File(outdir, fileName).toPath(), UTF_8)); }
public static void moveFile(File src, File targetDirectory) throws IOException { if (!src.renameTo(new File(targetDirectory, src.getName()))) { // If rename fails we must do a true deep copy instead. Path sourcePath = src.toPath(); Path targetDirPath = targetDirectory.toPath(); try { Files.move(sourcePath, targetDirPath.resolve(sourcePath.getFileName()), StandardCopyOption.REPLACE_EXISTING); } catch (IOException ex) { throw new IOException("Failed to move " + src + " to " + targetDirectory + " - " + ex.getMessage()); } } }
private String createClassOutsideClasspath(String className) throws Exception { File dir = Files.createTempDirectory("vertx").toFile(); dir.deleteOnExit(); File source = new File(dir, className + ".java"); Files.write(source.toPath(), ("public class " + className + " extends io.vertx.core.AbstractVerticle {} ").getBytes()); URLClassLoader loader = new URLClassLoader(new URL[]{dir.toURI().toURL()}, Thread.currentThread().getContextClassLoader()); CompilingClassLoader compilingClassLoader = new CompilingClassLoader(loader, className + ".java"); compilingClassLoader.loadClass(className); byte[] bytes = compilingClassLoader.getClassBytes(className); assertNotNull(bytes); File classFile = new File(dir, className + ".class"); Files.write(classFile.toPath(), bytes); return dir.getAbsolutePath(); }
@Before public void setUp() throws Exception { testDirectory = testDirectory(); testDirectory.mkdirs(); File tableFile = new File(testDirectory, "dir-list" + SingleTableStore.SUFFIX); tablestore = SingleTableBuilder. binary(tableFile, Metadata.NoMeta.INSTANCE).build(); listing = new TableDirectoryListing(tablestore, testDirectory.toPath(), f -> Integer.parseInt(f.getName().split("\\.")[0]), false); listing.init(); tempFile = File.createTempFile("foo", "bar"); tempFile.deleteOnExit(); }
public void create() { checkFilesAccessibility(bundledPlugins, externalPlugins); reset(); MessageDigest md5Digest = DigestUtils.getMd5Digest(); try (ZipOutputStream zos = new ZipOutputStream(new DigestOutputStream(new BufferedOutputStream(new FileOutputStream(destZipFile)), md5Digest))) { for (GoPluginDescriptor agentPlugins : agentPlugins()) { String zipEntryPrefix = "external/"; if (agentPlugins.isBundledPlugin()) { zipEntryPrefix = "bundled/"; } zos.putNextEntry(new ZipEntry(zipEntryPrefix + new File(agentPlugins.pluginFileLocation()).getName())); Files.copy(new File(agentPlugins.pluginFileLocation()).toPath(), zos); zos.closeEntry(); } } catch (Exception e) { LOG.error("Could not create zip of plugins for agent to download.", e); } md5DigestOfPlugins = Hex.encodeHexString(md5Digest.digest()); }