use of java.nio.file.attribute.DosFileAttributes in project ats-framework by Axway.
the class LocalFileSystemOperations method setFileHiddenAttribute.
@Override
public void setFileHiddenAttribute(String sourceFile, boolean hidden) {
sourceFile = IoUtils.normalizeFilePath(sourceFile, osType);
checkFileExistence(new File(sourceFile));
final String errMsg = "Could not " + (hidden ? "set" : "unset") + " the hidden attribute of file '" + sourceFile + "'";
if (OperatingSystemType.getCurrentOsType().isWindows()) {
try {
Path path = Paths.get(sourceFile);
DosFileAttributes attr;
attr = Files.readAttributes(path, DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
boolean goHidden = attr.isHidden();
if (!hidden && goHidden) {
Files.setAttribute(path, "dos:hidden", false, LinkOption.NOFOLLOW_LINKS);
} else if (hidden && !goHidden) {
Files.setAttribute(path, "dos:hidden", true, LinkOption.NOFOLLOW_LINKS);
}
} catch (IOException e) {
throw new FileSystemOperationException(errMsg, e);
}
} else if (OperatingSystemType.getCurrentOsType().isUnix()) {
// a '.' prefix makes the file hidden
String filePath = IoUtils.getFilePath(sourceFile);
String fileName = IoUtils.getFileName(sourceFile);
if (hidden) {
if (fileName.startsWith(".")) {
log.warn("File '" + sourceFile + "' is already hidden. No changes are made!");
return;
} else {
fileName = "." + fileName;
}
} else {
if (!fileName.startsWith(".")) {
log.warn("File '" + sourceFile + "' is already NOT hidden. No changes are made!");
return;
} else {
fileName = fileName.substring(1);
}
}
renameFile(sourceFile, filePath + fileName, false);
} else {
throw new FileSystemOperationException(errMsg + ": Unknown OS type");
}
}
use of java.nio.file.attribute.DosFileAttributes in project bazel by bazelbuild.
the class WindowsFileSystem method stat.
@Override
protected FileStatus stat(Path path, boolean followSymlinks) throws IOException {
File file = getIoFile(path);
final DosFileAttributes attributes;
try {
attributes = getAttribs(file, followSymlinks);
} catch (IOException e) {
throw new FileNotFoundException(path + ERR_NO_SUCH_FILE_OR_DIR);
}
final boolean isSymbolicLink = !followSymlinks && fileIsSymbolicLink(file);
FileStatus status = new FileStatus() {
@Override
public boolean isFile() {
return attributes.isRegularFile() || (isSpecialFile() && !isDirectory());
}
@Override
public boolean isSpecialFile() {
return attributes.isOther();
}
@Override
public boolean isDirectory() {
return attributes.isDirectory();
}
@Override
public boolean isSymbolicLink() {
return isSymbolicLink;
}
@Override
public long getSize() throws IOException {
return attributes.size();
}
@Override
public long getLastModifiedTime() throws IOException {
return attributes.lastModifiedTime().toMillis();
}
@Override
public long getLastChangeTime() {
// This is the best we can do with Java NIO...
return attributes.lastModifiedTime().toMillis();
}
@Override
public long getNodeId() {
// TODO(bazel-team): Consider making use of attributes.fileKey().
return -1;
}
};
return status;
}
use of java.nio.file.attribute.DosFileAttributes in project jimfs by google.
the class DosAttributeProviderTest method testAttributes.
@Test
public void testAttributes() {
DosFileAttributes attrs = provider.readAttributes(file);
assertThat(attrs.isHidden()).isFalse();
assertThat(attrs.isArchive()).isFalse();
assertThat(attrs.isReadOnly()).isFalse();
assertThat(attrs.isSystem()).isFalse();
file.setAttribute("dos", "hidden", true);
attrs = provider.readAttributes(file);
assertThat(attrs.isHidden()).isTrue();
assertThat(attrs.isArchive()).isFalse();
assertThat(attrs.isReadOnly()).isFalse();
assertThat(attrs.isSystem()).isFalse();
}
use of java.nio.file.attribute.DosFileAttributes 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.DosFileAttributes in project syncany by syncany.
the class FolderPermissionChangeScenarioTest method testFolderPermissionChange.
@Test
public void testFolderPermissionChange() throws Exception {
// Setup
TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();
TestClient clientA = new TestClient("A", testConnection);
TestClient clientB = new TestClient("B", testConnection);
// Run
clientA.createNewFolder("Somefolder");
clientA.upWithForceChecksum();
clientB.createNewFolder("Somefolder");
if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
Set<PosixFilePermission> expectedPosixPermissions = Files.getPosixFilePermissions(clientB.getLocalFile("Somefolder").toPath());
Set<PosixFilePermission> changedPosixPermissions = PosixFilePermissions.fromString("rwxrwxrwx");
Files.setPosixFilePermissions(clientB.getLocalFile("Somefolder").toPath(), changedPosixPermissions);
clientB.down();
assertEquals(1, clientB.getLocalFiles().size());
assertEquals(PosixFilePermissions.toString(expectedPosixPermissions), PosixFilePermissions.toString(Files.getPosixFilePermissions(clientB.getLocalFile("Somefolder").toPath())));
} else if (EnvironmentUtil.isWindows()) {
DosFileAttributes expectedDosAttrs = Files.readAttributes(clientB.getLocalFile("Somefolder").toPath(), DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
Files.setAttribute(clientB.getLocalFile("Somefolder").toPath(), "dos:hidden", true);
Files.setAttribute(clientB.getLocalFile("Somefolder").toPath(), "dos:archive", true);
Files.setAttribute(clientB.getLocalFile("Somefolder").toPath(), "dos:system", true);
Files.setAttribute(clientB.getLocalFile("Somefolder").toPath(), "dos:readonly", true);
clientB.down();
assertEquals(1, clientB.getLocalFiles().size());
DosFileAttributes actualDosAttrs = Files.readAttributes(clientB.getLocalFile("Somefolder").toPath(), DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
assertEquals(LimitedDosFileAttributes.toString(expectedDosAttrs), LimitedDosFileAttributes.toString(actualDosAttrs));
}
// Tear down
clientA.deleteTestData();
clientB.deleteTestData();
}
Aggregations