use of java.nio.file.attribute.PosixFileAttributes in project syncany by syncany.
the class FileVersionComparator method captureFileProperties.
public FileProperties captureFileProperties(File file, FileChecksum knownChecksum, boolean forceChecksum) {
FileProperties fileProperties = new FileProperties();
fileProperties.relativePath = FileUtil.getRelativeDatabasePath(rootFolder, file);
Path filePath = null;
try {
filePath = Paths.get(file.getAbsolutePath());
fileProperties.exists = Files.exists(filePath, LinkOption.NOFOLLOW_LINKS);
} catch (InvalidPathException e) {
// This throws an exception if the filename is invalid,
// e.g. colon in filename on windows "file:name"
logger.log(Level.FINE, "InvalidPath", e);
logger.log(Level.WARNING, "- Path '{0}' is invalid on this file system. It cannot exist. ", file.getAbsolutePath());
fileProperties.exists = false;
return fileProperties;
}
if (!fileProperties.exists) {
return fileProperties;
}
try {
// Read operating system dependent file attributes
BasicFileAttributes fileAttributes = null;
if (EnvironmentUtil.isWindows()) {
DosFileAttributes dosAttrs = Files.readAttributes(filePath, DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
fileProperties.dosAttributes = FileUtil.dosAttrsToString(dosAttrs);
fileAttributes = dosAttrs;
} else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
PosixFileAttributes posixAttrs = Files.readAttributes(filePath, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
fileProperties.posixPermissions = PosixFilePermissions.toString(posixAttrs.permissions());
fileAttributes = posixAttrs;
} else {
fileAttributes = Files.readAttributes(filePath, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
}
fileProperties.lastModified = fileAttributes.lastModifiedTime().toMillis();
fileProperties.size = fileAttributes.size();
// Type
if (fileAttributes.isSymbolicLink()) {
fileProperties.type = FileType.SYMLINK;
fileProperties.linkTarget = FileUtil.readSymlinkTarget(file);
} else if (fileAttributes.isDirectory()) {
fileProperties.type = FileType.FOLDER;
fileProperties.linkTarget = null;
} else {
fileProperties.type = FileType.FILE;
fileProperties.linkTarget = null;
}
// Checksum
if (knownChecksum != null) {
fileProperties.checksum = knownChecksum;
} else {
if (fileProperties.type == FileType.FILE && forceChecksum) {
try {
if (fileProperties.size > 0) {
fileProperties.checksum = new FileChecksum(FileUtil.createChecksum(file, checksumAlgorithm));
} else {
fileProperties.checksum = null;
}
} catch (NoSuchAlgorithmException | IOException e) {
logger.log(Level.FINE, "Failed create checksum", e);
logger.log(Level.SEVERE, "SEVERE: Unable to create checksum for file {0}", file);
fileProperties.checksum = null;
}
} else {
fileProperties.checksum = null;
}
}
// Must be last (!), used for vanish-test later
fileProperties.exists = Files.exists(filePath, LinkOption.NOFOLLOW_LINKS);
fileProperties.locked = fileProperties.exists && FileUtil.isFileLocked(file);
return fileProperties;
} catch (IOException e) {
logger.log(Level.FINE, "Failed to read file", e);
logger.log(Level.SEVERE, "SEVERE: Cannot read file {0}. Assuming file is locked.", file);
fileProperties.exists = true;
fileProperties.locked = true;
return fileProperties;
}
}
use of java.nio.file.attribute.PosixFileAttributes in project tycho by eclipse.
the class TarGzArchiver method createTarEntry.
private TarArchiveEntry createTarEntry(File tarRootDir, File source) throws IOException {
String pathInTar = slashify(tarRootDir.toPath().relativize(source.toPath()));
log.debug("Adding entry " + pathInTar);
TarArchiveEntry tarEntry;
if (isSymbolicLink(source) && resolvesBelow(source, tarRootDir)) {
// only create symlink entry if link target is inside archive
tarEntry = new TarArchiveEntry(pathInTar, TarArchiveEntry.LF_SYMLINK);
tarEntry.setLinkName(slashify(getRelativeSymLinkTarget(source, source.getParentFile())));
} else {
tarEntry = new TarArchiveEntry(source, pathInTar);
}
PosixFileAttributes attrs = getAttributes(source);
if (attrs != null) {
tarEntry.setMode(FilePermissionHelper.toOctalFileMode(attrs.permissions()));
}
tarEntry.setModTime(source.lastModified());
return tarEntry;
}
use of java.nio.file.attribute.PosixFileAttributes in project samza by apache.
the class TestBlobStoreUtil method testAreSameFile.
@Test
public void testAreSameFile() throws IOException {
FileUtil fileUtil = new FileUtil();
// 1. test with sst file with same attributes
Path sstFile = Files.createTempFile("samza-testAreSameFiles-", ".sst");
PosixFileAttributes sstFileAttribs = Files.readAttributes(sstFile, PosixFileAttributes.class);
FileMetadata sstFileMetadata = new FileMetadata(sstFileAttribs.creationTime().toMillis(), sstFileAttribs.lastModifiedTime().toMillis(), sstFileAttribs.size(), sstFileAttribs.owner().toString(), sstFileAttribs.group().toString(), PosixFilePermissions.toString(sstFileAttribs.permissions()));
// checksum should be ignored for sst file. Set any dummy value
FileIndex sstFileIndex = new FileIndex(sstFile.getFileName().toString(), Collections.emptyList(), sstFileMetadata, 0L);
assertTrue(DirDiffUtil.areSameFile(false).test(sstFile.toFile(), sstFileIndex));
// 2. test with sst file with different timestamps
// Update last modified time
Files.setLastModifiedTime(sstFile, FileTime.fromMillis(System.currentTimeMillis() + 1000L));
assertTrue(DirDiffUtil.areSameFile(false).test(sstFile.toFile(), sstFileIndex));
// 3. test with non-sst files with same metadata and content
Path tmpFile = Files.createTempFile("samza-testAreSameFiles-", ".tmp");
fileUtil.writeToTextFile(tmpFile.toFile(), RandomStringUtils.random(1000), false);
PosixFileAttributes tmpFileAttribs = Files.readAttributes(tmpFile, PosixFileAttributes.class);
FileMetadata tmpFileMetadata = new FileMetadata(tmpFileAttribs.creationTime().toMillis(), tmpFileAttribs.lastModifiedTime().toMillis(), tmpFileAttribs.size(), tmpFileAttribs.owner().toString(), tmpFileAttribs.group().toString(), PosixFilePermissions.toString(tmpFileAttribs.permissions()));
FileIndex tmpFileIndex = new FileIndex(tmpFile.getFileName().toString(), Collections.emptyList(), tmpFileMetadata, FileUtils.checksumCRC32(tmpFile.toFile()));
assertTrue(DirDiffUtil.areSameFile(false).test(tmpFile.toFile(), tmpFileIndex));
// 4. test with non-sst files with different attributes
// change lastModifiedTime of local file
FileTime prevLastModified = tmpFileAttribs.lastModifiedTime();
Files.setLastModifiedTime(tmpFile, FileTime.fromMillis(System.currentTimeMillis() + 1000L));
assertTrue(DirDiffUtil.areSameFile(false).test(tmpFile.toFile(), tmpFileIndex));
// change content/checksum of local file
// reset attributes to match with remote file
Files.setLastModifiedTime(tmpFile, prevLastModified);
// new content
fileUtil.writeToTextFile(tmpFile.toFile(), RandomStringUtils.random(1000), false);
assertFalse(DirDiffUtil.areSameFile(false).test(tmpFile.toFile(), tmpFileIndex));
}
use of java.nio.file.attribute.PosixFileAttributes in project samza by apache.
the class TestBlobStoreUtil method testRestoreDirFailsRestoreOnNonRetriableExceptions.
@Test
public void testRestoreDirFailsRestoreOnNonRetriableExceptions() throws IOException {
Path restoreDirBasePath = Files.createTempDirectory(BlobStoreTestUtil.TEMP_DIR_PREFIX);
DirIndex mockDirIndex = mock(DirIndex.class);
when(mockDirIndex.getDirName()).thenReturn(DirIndex.ROOT_DIR_NAME);
FileIndex mockFileIndex = mock(FileIndex.class);
when(mockFileIndex.getFileName()).thenReturn("1.sst");
// setup mock file attributes. create a temp file to get current user/group/permissions so that they
// match with restored files.
File tmpFile = Paths.get(restoreDirBasePath.toString(), "tempfile-" + new Random().nextInt()).toFile();
tmpFile.createNewFile();
byte[] fileContents = "fileContents".getBytes();
PosixFileAttributes attrs = Files.readAttributes(tmpFile.toPath(), PosixFileAttributes.class);
FileMetadata fileMetadata = new // ctime mtime does not matter. size == 26
FileMetadata(// ctime mtime does not matter. size == 26
1234L, // ctime mtime does not matter. size == 26
1243L, // ctime mtime does not matter. size == 26
fileContents.length, attrs.owner().getName(), attrs.group().getName(), PosixFilePermissions.toString(attrs.permissions()));
when(mockFileIndex.getFileMetadata()).thenReturn(fileMetadata);
// delete so that it doesn't show up in restored dir contents.
Files.delete(tmpFile.toPath());
List<FileBlob> mockFileBlobs = new ArrayList<>();
FileBlob mockFileBlob = mock(FileBlob.class);
when(mockFileBlob.getBlobId()).thenReturn("fileBlobId");
when(mockFileBlob.getOffset()).thenReturn(0);
mockFileBlobs.add(mockFileBlob);
when(mockFileIndex.getBlobs()).thenReturn(mockFileBlobs);
CRC32 checksum = new CRC32();
checksum.update(fileContents);
when(mockFileIndex.getChecksum()).thenReturn(checksum.getValue());
when(mockDirIndex.getFilesPresent()).thenReturn(ImmutableList.of(mockFileIndex));
BlobStoreManager mockBlobStoreManager = mock(BlobStoreManager.class);
when(mockBlobStoreManager.get(anyString(), any(OutputStream.class), any(Metadata.class))).thenReturn(// non retriable error
FutureUtil.failedFuture(new IllegalArgumentException())).thenAnswer((Answer<CompletionStage<Void>>) invocationOnMock -> {
String blobId = invocationOnMock.getArgumentAt(0, String.class);
OutputStream outputStream = invocationOnMock.getArgumentAt(1, OutputStream.class);
outputStream.write(fileContents);
((FileOutputStream) outputStream).getFD().sync();
return CompletableFuture.completedFuture(null);
});
BlobStoreUtil blobStoreUtil = new BlobStoreUtil(mockBlobStoreManager, EXECUTOR, null, null);
try {
blobStoreUtil.restoreDir(restoreDirBasePath.toFile(), mockDirIndex, metadata).join();
fail("Should have failed on non-retriable errors during file restore");
} catch (CompletionException e) {
assertTrue(e.getCause() instanceof IllegalArgumentException);
}
}
use of java.nio.file.attribute.PosixFileAttributes in project samza by apache.
the class TestBlobStoreUtil method testRestoreDirRetriesFileRestoreOnRetriableExceptions.
@Test
public void testRestoreDirRetriesFileRestoreOnRetriableExceptions() throws IOException {
Path restoreDirBasePath = Files.createTempDirectory(BlobStoreTestUtil.TEMP_DIR_PREFIX);
DirIndex mockDirIndex = mock(DirIndex.class);
when(mockDirIndex.getDirName()).thenReturn(DirIndex.ROOT_DIR_NAME);
FileIndex mockFileIndex = mock(FileIndex.class);
when(mockFileIndex.getFileName()).thenReturn("1.sst");
// setup mock file attributes. create a temp file to get current user/group/permissions so that they
// match with restored files.
File tmpFile = Paths.get(restoreDirBasePath.toString(), "tempfile-" + new Random().nextInt()).toFile();
tmpFile.createNewFile();
byte[] fileContents = "fileContents".getBytes();
PosixFileAttributes attrs = Files.readAttributes(tmpFile.toPath(), PosixFileAttributes.class);
FileMetadata fileMetadata = new // ctime mtime does not matter. size == 26
FileMetadata(// ctime mtime does not matter. size == 26
1234L, // ctime mtime does not matter. size == 26
1243L, // ctime mtime does not matter. size == 26
fileContents.length, attrs.owner().getName(), attrs.group().getName(), PosixFilePermissions.toString(attrs.permissions()));
when(mockFileIndex.getFileMetadata()).thenReturn(fileMetadata);
// delete so that it doesn't show up in restored dir contents.
Files.delete(tmpFile.toPath());
List<FileBlob> mockFileBlobs = new ArrayList<>();
FileBlob mockFileBlob = mock(FileBlob.class);
when(mockFileBlob.getBlobId()).thenReturn("fileBlobId");
when(mockFileBlob.getOffset()).thenReturn(0);
mockFileBlobs.add(mockFileBlob);
when(mockFileIndex.getBlobs()).thenReturn(mockFileBlobs);
CRC32 checksum = new CRC32();
checksum.update(fileContents);
when(mockFileIndex.getChecksum()).thenReturn(checksum.getValue());
when(mockDirIndex.getFilesPresent()).thenReturn(ImmutableList.of(mockFileIndex));
BlobStoreManager mockBlobStoreManager = mock(BlobStoreManager.class);
when(mockBlobStoreManager.get(anyString(), any(OutputStream.class), any(Metadata.class))).thenAnswer(// first try, retriable error
(Answer<CompletionStage<Void>>) invocationOnMock -> {
String blobId = invocationOnMock.getArgumentAt(0, String.class);
OutputStream outputStream = invocationOnMock.getArgumentAt(1, OutputStream.class);
outputStream.write("bad-data".getBytes());
((FileOutputStream) outputStream).getFD().sync();
return FutureUtil.failedFuture(new RetriableException());
}).thenAnswer(// 2nd try
(Answer<CompletionStage<Void>>) invocationOnMock -> {
String blobId = invocationOnMock.getArgumentAt(0, String.class);
OutputStream outputStream = invocationOnMock.getArgumentAt(1, OutputStream.class);
outputStream.write(fileContents);
((FileOutputStream) outputStream).getFD().sync();
return CompletableFuture.completedFuture(null);
});
BlobStoreUtil blobStoreUtil = new BlobStoreUtil(mockBlobStoreManager, EXECUTOR, null, null);
blobStoreUtil.restoreDir(restoreDirBasePath.toFile(), mockDirIndex, metadata).join();
assertTrue(new DirDiffUtil().areSameDir(Collections.emptySet(), false).test(restoreDirBasePath.toFile(), mockDirIndex));
}
Aggregations