Search in sources :

Example 1 with UserPrincipal

use of java.nio.file.attribute.UserPrincipal in project jimfs by google.

the class JimfsUnixLikeFileSystemTest method testCopy_toDifferentFileSystem_copyAttributes.

@Test
public void testCopy_toDifferentFileSystem_copyAttributes() throws IOException {
    try (FileSystem fs2 = Jimfs.newFileSystem(UNIX_CONFIGURATION)) {
        Path foo = fs.getPath("/foo");
        byte[] bytes = { 0, 1, 2, 3, 4 };
        Files.write(foo, bytes);
        Files.getFileAttributeView(foo, BasicFileAttributeView.class).setTimes(FileTime.fromMillis(0), FileTime.fromMillis(1), FileTime.fromMillis(2));
        UserPrincipal owner = fs.getUserPrincipalLookupService().lookupPrincipalByName("foobar");
        Files.setOwner(foo, owner);
        assertThatPath(foo).attribute("owner:owner").is(owner);
        Path foo2 = fs2.getPath("/foo");
        Files.copy(foo, foo2, COPY_ATTRIBUTES);
        assertThatPath(foo).exists();
        // when copying with COPY_ATTRIBUTES to a different FileSystem, only basic attributes (that
        // is, file times) can actually be copied
        assertThatPath(foo2).exists().and().attribute("lastModifiedTime").is(FileTime.fromMillis(0)).and().attribute("lastAccessTime").is(FileTime.fromMillis(1)).and().attribute("creationTime").is(FileTime.fromMillis(2)).and().attribute("owner:owner").isNot(owner).and().attribute("owner:owner").isNot(fs2.getUserPrincipalLookupService().lookupPrincipalByName("foobar")).and().containsBytes(// do this last; it updates the access time
        bytes);
    }
}
Also used : Path(java.nio.file.Path) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) FileSystem(java.nio.file.FileSystem) UserPrincipal(java.nio.file.attribute.UserPrincipal) Test(org.junit.Test)

Example 2 with UserPrincipal

use of java.nio.file.attribute.UserPrincipal in project jimfs by google.

the class OwnerAttributeProvider method set.

@Override
public void set(File file, String view, String attribute, Object value, boolean create) {
    if (attribute.equals("owner")) {
        checkNotCreate(view, attribute, create);
        UserPrincipal user = checkType(view, attribute, value, UserPrincipal.class);
        // TODO(cgdecker): Do we really need to do this? Any reason not to allow any UserPrincipal?
        if (!(user instanceof UserLookupService.JimfsUserPrincipal)) {
            user = createUserPrincipal(user.getName());
        }
        file.setAttribute("owner", "owner", user);
    }
}
Also used : UserPrincipal(java.nio.file.attribute.UserPrincipal) UserLookupService.createUserPrincipal(com.google.common.jimfs.UserLookupService.createUserPrincipal)

Example 3 with UserPrincipal

use of java.nio.file.attribute.UserPrincipal in project OpenClinica by OpenClinica.

the class SystemController method displayOwnerShipForTomcatSubDirectories.

public ArrayList<HashMap<String, Object>> displayOwnerShipForTomcatSubDirectories(File dir) throws IOException {
    ArrayList<HashMap<String, Object>> listOfHashMaps = new ArrayList<>();
    HashMap<String, Object> hashMap = null;
    File[] files = dir.listFiles();
    for (File file : files) {
        if (file.isDirectory()) {
            hashMap = new HashMap<String, Object>();
            hashMap.put("Read Access", getReadAccess(file));
            hashMap.put("Write Access", getWriteAccess(file));
            Path path = Paths.get(file.getCanonicalPath());
            FileOwnerAttributeView ownerAttributeView = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
            UserPrincipal owner = ownerAttributeView.getOwner();
            // hashMap.put("ownership", owner.getName());
            hashMap.put("Folder Name", file.getName());
            listOfHashMaps.add(hashMap);
            int dirCount = getNumberOfSubFolders(file.getCanonicalPath().toString());
            if (dirCount != 0) {
            // hashMap.put("Sub Folders", displayOwnerShipForTomcatSubDirectories(file));
            }
        }
    }
    return listOfHashMaps;
}
Also used : Path(java.nio.file.Path) FileOwnerAttributeView(java.nio.file.attribute.FileOwnerAttributeView) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) File(java.io.File) UserPrincipal(java.nio.file.attribute.UserPrincipal)

Example 4 with UserPrincipal

use of java.nio.file.attribute.UserPrincipal in project java-chassis by ServiceComb.

the class TestFortifyUtils method testIsInSecureResult.

@Test
public void testIsInSecureResult() {
    Path file = new File("src/test/resources/config/test.1.properties").toPath();
    new MockUp<FortifyUtils>() {

        @SuppressWarnings("unused")
        public boolean isInSecureDir(Path file, UserPrincipal user, int symlinkDepth) {
            return false;
        }
    };
    FortifyUtils.isInSecureResult(file);
    Assert.assertNotEquals(false, FortifyUtils.isInSecureResult(file));
}
Also used : Path(java.nio.file.Path) MockUp(mockit.MockUp) File(java.io.File) UserPrincipal(java.nio.file.attribute.UserPrincipal) Test(org.junit.Test)

Example 5 with UserPrincipal

use of java.nio.file.attribute.UserPrincipal in project java-chassis by ServiceComb.

the class TestFortifyUtils method testIsInSecureDir.

@Test
public void testIsInSecureDir() {
    Path file = new File("src/test/resources/config/test.1.properties").toPath();
    UserPrincipal user = null;
    int symlinkDepth = 5;
    FortifyUtils.isInSecureDir(file, user, symlinkDepth);
    Assert.assertNotEquals(false, FortifyUtils.isInSecureDir(file, user, symlinkDepth));
}
Also used : Path(java.nio.file.Path) File(java.io.File) UserPrincipal(java.nio.file.attribute.UserPrincipal) Test(org.junit.Test)

Aggregations

UserPrincipal (java.nio.file.attribute.UserPrincipal)19 Path (java.nio.file.Path)13 Test (org.junit.Test)7 File (java.io.File)6 IOException (java.io.IOException)4 GroupPrincipal (java.nio.file.attribute.GroupPrincipal)4 UserPrincipalLookupService (java.nio.file.attribute.UserPrincipalLookupService)4 ArrayList (java.util.ArrayList)4 FileOwnerAttributeView (java.nio.file.attribute.FileOwnerAttributeView)3 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)3 UserLookupService.createUserPrincipal (com.google.common.jimfs.UserLookupService.createUserPrincipal)2 FileSystem (java.nio.file.FileSystem)2 AclEntry (java.nio.file.attribute.AclEntry)2 BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)2 PosixFilePermission (java.nio.file.attribute.PosixFilePermission)2 HashMap (java.util.HashMap)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 UserLookupService.createGroupPrincipal (com.google.common.jimfs.UserLookupService.createGroupPrincipal)1 FileSystemException (io.vertx.core.file.FileSystemException)1 AclEntryFlag (java.nio.file.attribute.AclEntryFlag)1