use of java.nio.file.attribute.PosixFilePermission in project jdk8u_jdk by JetBrains.
the class PosixFilePermissions method asFileAttribute.
/**
* Creates a {@link FileAttribute}, encapsulating a copy of the given file
* permissions, suitable for passing to the {@link java.nio.file.Files#createFile
* createFile} or {@link java.nio.file.Files#createDirectory createDirectory}
* methods.
*
* @param perms
* the set of permissions
*
* @return an attribute encapsulating the given file permissions with
* {@link FileAttribute#name name} {@code "posix:permissions"}
*
* @throws ClassCastException
* if the set contains elements that are not of type {@code
* PosixFilePermission}
*/
public static FileAttribute<Set<PosixFilePermission>> asFileAttribute(Set<PosixFilePermission> perms) {
// copy set and check for nulls (CCE will be thrown if an element is not
// a PosixFilePermission)
perms = new HashSet<PosixFilePermission>(perms);
for (PosixFilePermission p : perms) {
if (p == null)
throw new NullPointerException();
}
final Set<PosixFilePermission> value = perms;
return new FileAttribute<Set<PosixFilePermission>>() {
@Override
public String name() {
return "posix:permissions";
}
@Override
public Set<PosixFilePermission> value() {
return Collections.unmodifiableSet(value);
}
};
}
use of java.nio.file.attribute.PosixFilePermission in project ignite by apache.
the class IgfsLocalSecondaryFileSystemTestAdapter method permissions.
/**
* {@inheritDoc}
*/
@Override
public String permissions(String path) throws IOException {
Path p = path(path);
PosixFileAttributeView attrView = Files.getFileAttributeView(p, PosixFileAttributeView.class);
if (attrView == null)
throw new UnsupportedOperationException("Posix file attributes not available");
int perm = 0;
for (PosixFilePermission pfp : attrView.readAttributes().permissions()) perm |= (1 << 8 - pfp.ordinal());
return '0' + Integer.toOctalString(perm);
}
use of java.nio.file.attribute.PosixFilePermission in project syncany by syncany.
the class ChangedAttributesScenarioTest method testNewFileWithDifferingAttributes.
@Test
public void testNewFileWithDifferingAttributes() throws Exception {
// Setup
TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();
TestClient clientA = new TestClient("A", testConnection);
TestClient clientB = new TestClient("B", testConnection);
// Create new file with differing attributes
clientA.createNewFile("file1.jpg");
FileUtils.copyFile(clientA.getLocalFile("file1.jpg"), clientB.getLocalFile("file1.jpg"));
// Client B's attributes differ!
File aFile = clientA.getLocalFile("file1.jpg");
Path aFilePath = Paths.get(aFile.getAbsolutePath());
Object aReadOnlyAttribute = null;
Set<PosixFilePermission> aPosixFilePermissions = null;
// Client B's attributes differ!
File bFile = clientB.getLocalFile("file1.jpg");
Path bFilePath = Paths.get(bFile.getAbsolutePath());
if (EnvironmentUtil.isWindows()) {
aReadOnlyAttribute = Files.getAttribute(aFilePath, "dos:readonly");
Files.setAttribute(bFilePath, "dos:readonly", true);
} else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
aPosixFilePermissions = Files.getPosixFilePermissions(aFilePath);
Files.setPosixFilePermissions(bFilePath, PosixFilePermissions.fromString("rwxrwxrwx"));
}
clientA.upWithForceChecksum();
// This is the key operation
DownOperationResult downResult = clientB.down();
// Test 1: Check result sets for inconsistencies
assertTrue("File should be downloaded.", downResult.getChangeSet().hasChanges());
// Test 2: file1.jpg permissions (again!
if (EnvironmentUtil.isWindows()) {
Object bReadOnlyAttribute = Files.getAttribute(aFilePath, "dos:readonly");
assertEquals("Read-only should be true.", aReadOnlyAttribute, bReadOnlyAttribute);
} else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
Set<PosixFilePermission> bPosixFilePermissions = Files.getPosixFilePermissions(aFilePath);
assertEquals("Should be rwxrwxrwx.", PosixFilePermissions.toString(aPosixFilePermissions), PosixFilePermissions.toString(bPosixFilePermissions));
}
// Test 3: The rest
assertFileListEquals(clientA.getLocalFilesExcludeLockedAndNoRead(), clientB.getLocalFilesExcludeLockedAndNoRead());
assertSqlDatabaseEquals(clientA.getDatabaseFile(), clientB.getDatabaseFile());
// Tear down
clientA.deleteTestData();
clientB.deleteTestData();
}
use of java.nio.file.attribute.PosixFilePermission 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();
}
use of java.nio.file.attribute.PosixFilePermission in project gradle by gradle.
the class ChmodBenchmark method createFileJava7SetMixedPermission.
@Benchmark
public void createFileJava7SetMixedPermission(Blackhole blackhole) throws IOException {
int incrementAndGet = counter.incrementAndGet();
Path file = Files.createFile(tempDirPath.resolve("file-" + incrementAndGet));
Set<PosixFilePermission> permissionsToSet;
if (incrementAndGet % 2 == 0) {
permissionsToSet = DEFAULT_JAVA7_FILE_PERMISSIONS;
} else {
permissionsToSet = WEIRD_JAVA7_FILE_PERMISSIONS;
}
Files.setPosixFilePermissions(file, permissionsToSet);
blackhole.consume(file);
}
Aggregations