java.nio.file.CopyOption Java Examples

The following examples show how to use java.nio.file.CopyOption. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: sftp-fs   Author: robtimus   File: SFTPFileSystemTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCopyReplaceNonEmptyDirAllowedDifferentFileSystems() throws IOException {
    addDirectory("/foo");
    addFile("/foo/bar");
    addFile("/baz");

    CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING };
    FileSystemException exception = assertThrows(FileSystemException.class,
            () -> fileSystem.copy(createPath("/baz"), createPath(fileSystem2, "/foo"), options));
    assertEquals("/foo", exception.getFile());

    verify(getExceptionFactory()).createDeleteException(eq("/foo"), any(SftpException.class), eq(true));
    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertTrue(Files.isRegularFile(getPath("/baz")));
}
 
Example #2
Source Project: mycore   Author: MyCoRe-Org   File: MCRFileSystemProvider.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public void move(Path source, Path target, CopyOption... options) throws IOException {
    HashSet<CopyOption> copyOptions = Sets.newHashSet(options);
    if (copyOptions.contains(StandardCopyOption.ATOMIC_MOVE)) {
        throw new AtomicMoveNotSupportedException(source.toString(), target.toString(),
            "ATOMIC_MOVE not supported yet");
    }
    if (Files.isDirectory(source)) {
        MCRPath src = MCRFileSystemUtils.checkPathAbsolute(source);
        MCRDirectory srcRootDirectory = MCRFileSystemUtils.getFileCollection(src.getOwner());
        if (srcRootDirectory.hasChildren()) {
            throw new IOException("Directory is not empty");
        }
    }
    copy(source, target, options);
    delete(source);
}
 
Example #3
Source Project: mycore   Author: MyCoRe-Org   File: MCRFileSystemProvider.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public void move(Path source, Path target, CopyOption... options) throws IOException {
    HashSet<CopyOption> copyOptions = Sets.newHashSet(options);
    if (copyOptions.contains(StandardCopyOption.ATOMIC_MOVE)) {
        throw new AtomicMoveNotSupportedException(source.toString(), target.toString(),
            "ATOMIC_MOVE not supported yet");
    }
    if (Files.isDirectory(source)) {
        MCRPath src = MCRFileSystemUtils.checkPathAbsolute(source);
        MCRDirectory srcRootDirectory = getRootDirectory(src);
        if (srcRootDirectory.hasChildren()) {
            throw new IOException("Directory is not empty");
        }
    }
    copy(source, target, options);
    delete(source);
}
 
Example #4
Source Project: sftp-fs   Author: robtimus   File: SFTPFileSystemTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testMoveNonEmptyDirDifferentFileSystem() throws IOException {
    addDirectory("/foo");
    addDirectory("/baz");
    addFile("/baz/qux");

    CopyOption[] options = {};
    FileSystemException exception = assertThrows(FileSystemException.class,
            () -> fileSystem.move(createPath("/baz"), createPath(fileSystem2, "/foo/bar"), options));
    assertEquals("/baz", exception.getFile());

    verify(getExceptionFactory()).createDeleteException(eq("/baz"), any(SftpException.class), eq(true));
    assertTrue(Files.isDirectory(getPath("/baz")));
    assertTrue(Files.isRegularFile(getPath("/baz/qux")));
    assertEquals(1, getChildCount("/baz"));

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isDirectory(getPath("/foo/bar")));
    assertFalse(Files.isRegularFile(getPath("/foo/bar/qux")));
    assertEquals(1, getChildCount("/foo"));
    assertEquals(0, getChildCount("/foo/bar"));
}
 
Example #5
Source Project: sftp-fs   Author: robtimus   File: SFTPFileSystemTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testMoveReplaceFileDifferentFileSystem() throws IOException {
    addDirectory("/foo");
    addDirectory("/foo/bar");
    addFile("/baz");

    CopyOption[] options = {};
    FileAlreadyExistsException exception = assertThrows(FileAlreadyExistsException.class,
            () -> fileSystem.move(createPath("/baz"), createPath(fileSystem2, "/foo/bar"), options));
    assertEquals("/foo/bar", exception.getFile());

    verify(getExceptionFactory(), never()).createMoveException(anyString(), anyString(), any(SftpException.class));
    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isDirectory(getPath("/foo/bar")));
    assertTrue(Files.isRegularFile(getPath("/baz")));
}
 
Example #6
Source Project: sftp-fs   Author: robtimus   File: SFTPFileSystemTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCopySFTPFailure() throws IOException {
    addDirectory("/foo");
    addFile("/foo/bar");

    // failure: target parent does not exist

    CopyOption[] options = {};
    NoSuchFileException exception = assertThrows(NoSuchFileException.class,
            () -> fileSystem.copy(createPath("/foo/bar"), createPath("/baz/bar"), options));
    assertEquals("/baz/bar", exception.getFile());

    verify(getExceptionFactory()).createNewOutputStreamException(eq("/baz/bar"), any(SftpException.class), anyCollection());
    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertFalse(Files.exists(getPath("/baz")));
    assertFalse(Files.exists(getPath("/baz/bar")));
}
 
Example #7
Source Project: sftp-fs   Author: robtimus   File: SFTPFileSystemTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCopyReplaceNonEmptyDirAllowed() throws IOException {
    addDirectory("/foo");
    addFile("/foo/bar");
    addFile("/baz");

    CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING };
    FileSystemException exception = assertThrows(FileSystemException.class,
            () -> fileSystem.copy(createPath("/baz"), createPath("/foo"), options));
    assertEquals("/foo", exception.getFile());

    verify(getExceptionFactory()).createDeleteException(eq("/foo"), any(SftpException.class), eq(true));
    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertTrue(Files.isRegularFile(getPath("/baz")));
}
 
Example #8
Source Project: sftp-fs   Author: robtimus   File: SFTPFileSystemTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testMoveSFTPFailure() throws IOException {
    addDirectory("/foo");
    addFile("/foo/bar");

    // failure: non-existing target parent

    CopyOption[] options = {};
    FileSystemException exception = assertThrows(FileSystemException.class,
            () -> fileSystem.move(createPath("/foo/bar"), createPath("/baz/bar"), options));
    assertEquals("/foo/bar", exception.getFile());
    assertEquals("/baz/bar", exception.getOtherFile());

    verify(getExceptionFactory()).createMoveException(eq("/foo/bar"), eq("/baz/bar"), any(SftpException.class));
    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertFalse(Files.exists(getPath("/baz")));
}
 
Example #9
Source Project: copybara   Author: google   File: CopyMoveVisitor.java    License: Apache License 2.0 5 votes vote down vote up
CopyMoveVisitor(Path before, Path after, @Nullable PathMatcher pathMatcher, boolean overwrite, boolean isCopy) {
  this.before = before;
  this.after = after;
  this.pathMatcher = pathMatcher;
  this.isCopy = isCopy;
  if (overwrite) {
    moveMode = new CopyOption[]{LinkOption.NOFOLLOW_LINKS, StandardCopyOption.REPLACE_EXISTING};
  } else {
    moveMode = new CopyOption[]{LinkOption.NOFOLLOW_LINKS};
  }
}
 
Example #10
Source Project: sftp-fs   Author: robtimus   File: SFTPFileSystemTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testMoveReplaceFileAllowed() throws IOException {
    addDirectory("/foo");
    addFile("/foo/bar");
    addFile("/baz");

    CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING };
    fileSystem.move(createPath("/baz"), createPath("/foo/bar"), options);

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertFalse(Files.exists(getPath("/baz")));
}
 
Example #11
Source Project: sftp-fs   Author: robtimus   File: SFTPFileSystemTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCopyReplaceEmptyDirAllowedDifferentFileSystems() throws IOException {
    addDirectory("/foo");
    addDirectory("/baz");

    CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING };
    fileSystem.copy(createPath("/baz"), createPath(fileSystem2, "/foo"), options);

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isDirectory(getPath("/foo")));
}
 
Example #12
Source Project: marathonv5   Author: jalian-systems   File: Copy.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Copy source file to target location.
 *
 * @return
 */
static boolean copyFile(Path source, Path target) {
    CopyOption[] options = new CopyOption[] { COPY_ATTRIBUTES, REPLACE_EXISTING };
    target = getUnique(target);
    try {
        Files.copy(source, target, options);
        return true;
    } catch (Exception x) {
        System.err.format("Unable to copy: %s: %s%n", source, x);
        return false;
    }
}
 
Example #13
Source Project: AndroidRipper   Author: reverse-unina   File: AndroidRipperStarter.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
protected void createAPKs(String testSuitePath, String appPackage, String appMainActivity, String extractorClass,
		String toolsPath, String debugKeyStorePath, String autAPK, String tempPath) {
	// replace strings
	println("Editing 'Configuration.java'");
	replaceStringsInFile(
			testSuitePath + "/smali/it/unina/android/ripper/configuration/Configuration.smali.template",
			testSuitePath + "/smali/it/unina/android/ripper/configuration/Configuration.smali", appPackage,
			appMainActivity);
	println("Editing 'AndroidManifest.xml'");
	replaceStringsInFile(testSuitePath + "/AndroidManifest.xml.template", testSuitePath + "/AndroidManifest.xml",
			appPackage, appMainActivity);

	try {
		println("Cleaning apks...");
		println("Building AndroidRipper...");

		execCommand("java -jar " + toolsPath + "apktool.jar b " + testSuitePath + " -o " + tempPath + "/ar.apk");

		println("Signing AndroidRipper...");

		execCommand("jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore " + debugKeyStorePath
				+ "/debug.keystore -storepass android -keypass android " + tempPath + "/ar.apk androiddebugkey");
		execCommand("zipalign 4 " + tempPath + "/ar.apk " + tempPath + "/ripper.apk");

		Files.copy(FileSystems.getDefault().getPath(autAPK),
				FileSystems.getDefault().getPath(tempPath + "/temp.apk"),
				(CopyOption) StandardCopyOption.REPLACE_EXISTING);

		println("Signing AUT...");
		ZipUtils.deleteFromZip(tempPath + "/temp.apk");
		execCommand("jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore " + debugKeyStorePath
				+ "/debug.keystore -storepass android -keypass android " + tempPath
				+ "/temp.apk androiddebugkey");
		execCommand("jarsigner -verify " + tempPath + "/temp.apk");
		execCommand("zipalign -v 4 " + tempPath + "/temp.apk " + tempPath + "/aut.apk");
		
	} catch (Throwable t) {
		throw new RipperRuntimeException(AndroidRipperStarter.class, "createAPKs", "apk build failed", t);
	}
}
 
Example #14
Source Project: sftp-fs   Author: robtimus   File: SFTPFileSystemTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testMoveNonEmptyDirSameParent() throws IOException {
    addDirectory("/foo");
    addFile("/foo/bar");

    CopyOption[] options = {};
    try {
        fileSystem.move(createPath("/foo"), createPath("/baz"), options);
    } finally {
        assertFalse(Files.exists(getPath("/foo")));
        assertTrue(Files.isDirectory(getPath("/baz")));
        assertTrue(Files.isRegularFile(getPath("/baz/bar")));
    }
}
 
Example #15
Source Project: sftp-fs   Author: robtimus   File: SFTPFileSystemTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testMoveFileDifferentFileSystem() throws IOException {
    addDirectory("/foo");
    addFile("/baz");

    CopyOption[] options = {};
    fileSystem.move(createPath("/baz"), createPath(fileSystem2, "/foo/bar"), options);

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertFalse(Files.exists(getPath("/baz")));
}
 
Example #16
Source Project: sftp-fs   Author: robtimus   File: SFTPFileSystemTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testMoveFile() throws IOException {
    addDirectory("/foo");
    addFile("/baz");

    CopyOption[] options = {};
    fileSystem.move(createPath("/baz"), createPath("/foo/bar"), options);

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertFalse(Files.exists(getPath("/baz")));
}
 
Example #17
Source Project: sftp-fs   Author: robtimus   File: SFTPFileSystemTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCopyEmptyDir() throws IOException {
    addDirectory("/foo");
    addDirectory("/baz");

    CopyOption[] options = {};
    fileSystem.copy(createPath("/baz"), createPath("/foo/bar"), options);

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isDirectory(getPath("/foo/bar")));
    assertTrue(Files.isDirectory(getPath("/baz")));

    assertEquals(0, getChildCount("/foo/bar"));
}
 
Example #18
Source Project: hkxpack   Author: Dexesttp   File: FileUtils.java    License: MIT License 5 votes vote down vote up
/**
 * Convert a given resource to a temporary {@link File}.
 * @param resourceName the resource to convert
 * @return the {@link File} object pointing to the temporary File.
 * @throws Exception
 */
public static File resourceToTemporaryFile(final String resourceName) throws IOException {
	InputStream inputStream = FileUtils.class.getResourceAsStream(resourceName);
	File tempFile = File.createTempFile(resourceName, ".tmp");
    Files.copy(inputStream, tempFile.toPath(), new CopyOption[]{StandardCopyOption.REPLACE_EXISTING});
    inputStream.close();
    return tempFile;
}
 
Example #19
Source Project: sftp-fs   Author: robtimus   File: SFTPFileSystemTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testMoveEmptyDir() throws IOException {
    addDirectory("/foo");
    addDirectory("/baz");

    CopyOption[] options = {};
    fileSystem.move(createPath("/baz"), createPath("/foo/bar"), options);

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isDirectory(getPath("/foo/bar")));
    assertFalse(Files.exists(getPath("/baz")));
}
 
Example #20
Source Project: sftp-fs   Author: robtimus   File: SFTPFileSystemTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCopyFileDifferentFileSystems() throws IOException {
    addDirectory("/foo");
    addFile("/baz");

    CopyOption[] options = {};
    fileSystem.copy(createPath("/baz"), createPath(fileSystem2, "/foo/bar"), options);

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertTrue(Files.isRegularFile(getPath("/baz")));
}
 
Example #21
Source Project: sftp-fs   Author: robtimus   File: SFTPFileSystemTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testMoveReplaceEmptyDirDifferentFileSystem() throws IOException {
    addDirectory("/foo");
    addFile("/baz");

    CopyOption[] options = {};
    FileAlreadyExistsException exception = assertThrows(FileAlreadyExistsException.class,
            () -> fileSystem.move(createPath("/baz"), createPath(fileSystem2, "/foo"), options));
    assertEquals("/foo", exception.getFile());

    verify(getExceptionFactory(), never()).createMoveException(anyString(), anyString(), any(SftpException.class));
    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/baz")));
}
 
Example #22
Source Project: sftp-fs   Author: robtimus   File: SFTPFileSystemTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testMoveEmptyDirDifferentFileSystem() throws IOException {
    addDirectory("/foo");
    addDirectory("/baz");

    CopyOption[] options = {};
    fileSystem.move(createPath("/baz"), createPath(fileSystem2, "/foo/bar"), options);

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isDirectory(getPath("/foo/bar")));
    assertFalse(Files.exists(getPath("/baz")));
}
 
Example #23
Source Project: manifold   Author: manifold-systems   File: PathUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean renameTo( Path from, Path to, CopyOption... options )
{
  try
  {
    return Files.move( from, to, options ) != null;
  }
  catch( IOException e )
  {
    return false;
  }
}
 
Example #24
Source Project: sftp-fs   Author: robtimus   File: SFTPFileSystemTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testMoveNonEmptyRoot() throws IOException {
    addDirectory("/foo");

    CopyOption[] options = {};
    DirectoryNotEmptyException exception = assertThrows(DirectoryNotEmptyException.class,
            () -> fileSystem.move(createPath("/"), createPath("/baz"), options));
    assertEquals("/", exception.getFile());

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertFalse(Files.exists(getPath("/baz")));
}
 
Example #25
Source Project: mycore   Author: MyCoRe-Org   File: MCRFileSystemProvider.java    License: GNU General Public License v3.0 5 votes vote down vote up
private void checkCopyOptions(CopyOption[] options) {
    for (CopyOption option : options) {
        if (!SUPPORTED_COPY_OPTIONS.contains(option)) {
            throw new UnsupportedOperationException("Unsupported copy option: " + option);
        }
    }
}
 
Example #26
Source Project: sftp-fs   Author: robtimus   File: SFTPFileSystemTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testMoveReplaceEmptyDir() throws IOException {
    addDirectory("/foo");
    addFile("/baz");

    CopyOption[] options = {};
    FileSystemException exception = assertThrows(FileSystemException.class,
            () -> fileSystem.move(createPath("/baz"), createPath("/foo"), options));
    assertEquals("/baz", exception.getFile());
    assertEquals("/foo", exception.getOtherFile());

    verify(getExceptionFactory()).createMoveException(eq("/baz"), eq("/foo"), any(SftpException.class));
    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/baz")));
}
 
Example #27
Source Project: baratine   Author: baratine   File: JFileSystemProvider.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void copy(Path source, Path target, CopyOption... options)
  throws IOException
{
  JPath jSource = (JPath) source;
  JPath jTarget = (JPath) target;

  try (InputStream is = jSource.getBfsFile().openRead()) {
    if (is == null) {
      throw new IOException(L.l("unable to read from file {0}", source.toUri()));
    }

    ArrayList<WriteOption> bfsOptionList = new ArrayList<>();
    for (CopyOption option : options) {
      if (option == StandardCopyOption.REPLACE_EXISTING) {
        bfsOptionList.add(WriteOption.Standard.OVERWRITE);
      }
    }

    WriteOption []bfsOptions = new WriteOption[bfsOptionList.size()];
    bfsOptionList.toArray(bfsOptions);

    try (OutputStream os = jTarget.getBfsFile().openWrite(bfsOptions)) {
      if (os == null) {
        throw new IOException(L.l("unable to write to file {0}", target.toUri()));
      }

      IoUtil.copy(is, os);
    }
  }
}
 
Example #28
Source Project: lucene-solr   Author: apache   File: WindowsFS.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void move(Path source, Path target, CopyOption... options) throws IOException {
  synchronized (openFiles) {
    checkDeleteAccess(source);
    Object key = getKeyOrNull(target);
    super.move(source, target, options);
    if (key != null) {
      Object newKey = getKey(target);
      if (newKey.equals(key) == false) {
        // we need to transfer ownership here if we have open files on this file since the getKey() method will
        // return a different i-node next time we call it with the target path and our onClose method will
        // trip an assert
        Map<Path, Integer> map = openFiles.get(key);
        if (map != null) {
          Integer v = map.remove(target);
          if (v != null) {
            Map<Path, Integer> pathIntegerMap = openFiles.computeIfAbsent(newKey, k -> new HashMap<>());
            Integer existingValue = pathIntegerMap.getOrDefault(target, 0);
            pathIntegerMap.put(target, existingValue + v);
          }
          if (map.isEmpty()) {
            openFiles.remove(key);
          }
        }
      }
    }
  }
}
 
Example #29
Source Project: sftp-fs   Author: robtimus   File: SFTPFileSystemTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCopyReplaceFileAllowedDifferentFileSystems() throws IOException {
    addDirectory("/foo");
    addFile("/foo/bar");
    addFile("/baz");

    CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING };
    fileSystem.copy(createPath("/baz"), createPath(fileSystem2, "/foo/bar"), options);

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
}
 
Example #30
Source Project: lucene-solr   Author: apache   File: VerboseFS.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void copy(Path source, Path target, CopyOption... options) throws IOException {
  Throwable exception = null;
  try {
    super.copy(source, target, options);
  } catch (Throwable t) {
    exception = t;
  } finally {
    sop("copy" + Arrays.toString(options) + ": " + path(source) + " -> " + path(target), exception);
  }
}