Search in sources :

Example 11 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project alluxio by Alluxio.

the class FileUtils method getLocalFileMode.

/**
   * Gets local file's permission mode.
   *
   * @param filePath the file path
   * @return the file mode in short, e.g. 0777
   * @throws IOException when fails to get the permission
   */
public static short getLocalFileMode(String filePath) throws IOException {
    Set<PosixFilePermission> permission = Files.readAttributes(Paths.get(filePath), PosixFileAttributes.class).permissions();
    // Translate posix file permissions to short mode.
    int mode = 0;
    for (PosixFilePermission action : PosixFilePermission.values()) {
        mode = mode << 1;
        mode += permission.contains(action) ? 1 : 0;
    }
    return (short) mode;
}
Also used : PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) PosixFilePermission(java.nio.file.attribute.PosixFilePermission)

Example 12 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project ats-framework by Axway.

the class LocalFileSystemOperations method getPermissions.

private String getPermissions(String sourceFile) {
    int ownerPermissions = 0;
    int groupPermissions = 0;
    int othersPermissions = 0;
    try {
        Path path = Paths.get(sourceFile);
        PosixFileAttributes attr;
        attr = Files.readAttributes(path, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
        Set<PosixFilePermission> filePermissions = attr.permissions();
        if (filePermissions.contains(PosixFilePermission.OWNER_READ)) {
            ownerPermissions += 4;
        }
        if (filePermissions.contains(PosixFilePermission.OWNER_WRITE)) {
            ownerPermissions += 2;
        }
        if (filePermissions.contains(PosixFilePermission.OWNER_EXECUTE)) {
            ownerPermissions += 1;
        }
        if (filePermissions.contains(PosixFilePermission.GROUP_READ)) {
            groupPermissions += 4;
        }
        if (filePermissions.contains(PosixFilePermission.GROUP_WRITE)) {
            groupPermissions += 2;
        }
        if (filePermissions.contains(PosixFilePermission.GROUP_EXECUTE)) {
            groupPermissions += 1;
        }
        if (filePermissions.contains(PosixFilePermission.OTHERS_READ)) {
            othersPermissions += 4;
        }
        if (filePermissions.contains(PosixFilePermission.OTHERS_WRITE)) {
            othersPermissions += 2;
        }
        if (filePermissions.contains(PosixFilePermission.OTHERS_EXECUTE)) {
            othersPermissions += 1;
        }
    } catch (IOException ioe) {
        throw new FileSystemOperationException("Could not get permissions for file '" + sourceFile + "'", ioe);
    }
    return String.valueOf(ownerPermissions) + String.valueOf(groupPermissions) + String.valueOf(othersPermissions);
}
Also used : Path(java.nio.file.Path) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) IOException(java.io.IOException) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) PosixFilePermission(java.nio.file.attribute.PosixFilePermission)

Example 13 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project ignite by apache.

the class LocalFileSystemUtils method posixAttributesToMap.

/**
     * Convert POSIX attributes to property map.
     *
     * @param attrs Attributes view.
     * @return IGFS properties map.
     */
public static Map<String, String> posixAttributesToMap(PosixFileAttributes attrs) {
    if (attrs == null)
        return null;
    Map<String, String> props = U.newHashMap(3);
    props.put(IgfsUtils.PROP_USER_NAME, attrs.owner().getName());
    props.put(IgfsUtils.PROP_GROUP_NAME, attrs.group().getName());
    int perm = 0;
    for (PosixFilePermission p : attrs.permissions()) perm |= (1 << 8 - p.ordinal());
    props.put(IgfsUtils.PROP_PERMISSION, '0' + Integer.toOctalString(perm));
    return props;
}
Also used : PosixFilePermission(java.nio.file.attribute.PosixFilePermission)

Example 14 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project ignite by apache.

the class LocalFileSystemUtils method updateProperties.

/**
     * Update file properties.
     *
     * @param file File.
     * @param grp Group.
     * @param perm Permissions.
     */
public static void updateProperties(File file, String grp, String perm) {
    PosixFileAttributeView attrs = Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class);
    if (attrs == null)
        throw new UnsupportedOperationException("Posix file attributes not available");
    if (grp != null) {
        try {
            UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
            GroupPrincipal grp0 = lookupService.lookupPrincipalByGroupName(grp);
            attrs.setGroup(grp0);
        } catch (IOException e) {
            throw new IgfsException("Update the '" + IgfsUtils.PROP_GROUP_NAME + "' property is failed.", e);
        }
    }
    if (perm != null) {
        int perm0 = Integer.parseInt(perm, 8);
        Set<PosixFilePermission> permSet = new HashSet<>(9);
        for (int i = 0; i < LocalFileSystemUtils.POSIX_PERMISSIONS.length; ++i) {
            if ((perm0 & (1 << i)) != 0)
                permSet.add(LocalFileSystemUtils.POSIX_PERMISSIONS[i]);
        }
        try {
            attrs.setPermissions(permSet);
        } catch (IOException e) {
            throw new IgfsException("Update the '" + IgfsUtils.PROP_PERMISSION + "' property is failed.", e);
        }
    }
}
Also used : UserPrincipalLookupService(java.nio.file.attribute.UserPrincipalLookupService) IgfsException(org.apache.ignite.igfs.IgfsException) GroupPrincipal(java.nio.file.attribute.GroupPrincipal) IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView) HashSet(java.util.HashSet)

Example 15 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project jabref by JabRef.

the class FileSaveSession method commit.

@Override
public void commit(Path file) throws SaveException {
    if (file == null) {
        return;
    }
    if (backup && Files.exists(file)) {
        Path backupFile = FileUtil.addExtension(file, BACKUP_EXTENSION);
        FileUtil.copyFile(file, backupFile, true);
    }
    try {
        // Always use a lock file
        try {
            if (FileBasedLock.createLockFile(file)) {
                // Oops, the lock file already existed. Try to wait it out:
                if (!FileBasedLock.waitForFileLock(file)) {
                    throw SaveException.FILE_LOCKED;
                }
            }
        } catch (IOException ex) {
            LOGGER.error("Error when creating lock file.", ex);
        }
        // Try to save file permissions to restore them later (by default: 664)
        Set<PosixFilePermission> oldFilePermissions = EnumSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.GROUP_READ, PosixFilePermission.GROUP_WRITE, PosixFilePermission.OTHERS_READ);
        if (FileUtil.isPosixCompilant && Files.exists(file)) {
            try {
                oldFilePermissions = Files.getPosixFilePermissions(file);
            } catch (IOException exception) {
                LOGGER.warn("Error getting file permissions.", exception);
            }
        }
        FileUtil.copyFile(temporaryFile, file, true);
        // Restore file permissions
        if (FileUtil.isPosixCompilant) {
            try {
                Files.setPosixFilePermissions(file, oldFilePermissions);
            } catch (IOException exception) {
                throw new SaveException(exception);
            }
        }
    } finally {
        FileBasedLock.deleteLockFile(file);
    }
    try {
        Files.deleteIfExists(temporaryFile);
    } catch (IOException e) {
        LOGGER.warn("Cannot delete temporary file", e);
    }
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission)

Aggregations

PosixFilePermission (java.nio.file.attribute.PosixFilePermission)33 Path (java.nio.file.Path)18 IOException (java.io.IOException)10 Test (org.junit.Test)7 File (java.io.File)6 HashSet (java.util.HashSet)6 PosixFileAttributes (java.nio.file.attribute.PosixFileAttributes)5 Set (java.util.Set)4 GroupPrincipal (java.nio.file.attribute.GroupPrincipal)3 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)3 TransferSettings (org.syncany.plugins.transfer.TransferSettings)3 TestClient (org.syncany.tests.util.TestClient)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 RandomAccessFile (java.io.RandomAccessFile)2 UserPrincipal (java.nio.file.attribute.UserPrincipal)2 Date (java.util.Date)2 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)2 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)2