/** * Get POSIX attributes for file. * * @param file File. * @return PosixFileAttributes. */ @Nullable public static PosixFileAttributes posixAttributes(File file) { PosixFileAttributes attrs = null; try { PosixFileAttributeView view = Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class); if (view != null) attrs = view.readAttributes(); } catch (IOException e) { throw new IgfsException("Failed to read POSIX attributes: " + file.getAbsolutePath(), e); } return attrs; }
/** * Get POSIX attributes for file. * * @param file File. * @return BasicFileAttributes. */ @Nullable public static BasicFileAttributes basicAttributes(File file) { BasicFileAttributes attrs = null; try { BasicFileAttributeView view = Files.getFileAttributeView(file.toPath(), BasicFileAttributeView.class); if (view != null) attrs = view.readAttributes(); } catch (IOException e) { throw new IgfsException("Failed to read basic file attributes: " + file.getAbsolutePath(), e); } return attrs; }
/** * Changes the local file's user. * * @param path that will change owner * @param user the new user */ public static void changeLocalFileUser(String path, String user) throws IOException { UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService(); PosixFileAttributeView view = Files.getFileAttributeView(Paths.get(path), PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS); UserPrincipal userPrincipal = lookupService.lookupPrincipalByName(user); view.setOwner(userPrincipal); }
/** * Changes the local file's group. * * @param path that will change owner * @param group the new group */ public static void changeLocalFileGroup(String path, String group) throws IOException { UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService(); PosixFileAttributeView view = Files.getFileAttributeView(Paths.get(path), PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS); GroupPrincipal groupPrincipal = lookupService.lookupPrincipalByGroupName(group); view.setGroup(groupPrincipal); }
/** * Sets the {@link Path}'s last modified time and last access time to * the given valid times. * * @param mtime the modification time to set (only if no less than zero). * @param atime the access time to set (only if no less than zero). * @throws IOException if setting the times fails. */ @Override public void setTimes(Path p, long mtime, long atime) throws IOException { try { BasicFileAttributeView view = Files.getFileAttributeView( pathToFile(p).toPath(), BasicFileAttributeView.class); FileTime fmtime = (mtime >= 0) ? FileTime.fromMillis(mtime) : null; FileTime fatime = (atime >= 0) ? FileTime.fromMillis(atime) : null; view.setTimes(fmtime, fatime, null); } catch (NoSuchFileException e) { throw new FileNotFoundException("File " + p + " does not exist"); } }
PosixFileAttributeView attrs = Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class);
private void setFileCreationDate(File f, long time) throws IOException { BasicFileAttributeView attributes = Files.getFileAttributeView(f.toPath(), BasicFileAttributeView.class); FileTime creationTime = FileTime.fromMillis(time); attributes.setTimes(creationTime, creationTime, creationTime); } }
/** {@inheritDoc} */ @Override public void setTimes(IgfsPath path, long modificationTime, long accessTime) throws IgniteException { Path p = fileForPath(path).toPath(); if (!Files.exists(p)) throw new IgfsPathNotFoundException("Failed to set times (path not found): " + path); try { Files.getFileAttributeView(p, BasicFileAttributeView.class) .setTimes( (modificationTime >= 0) ? FileTime.from(modificationTime, TimeUnit.MILLISECONDS) : null, (accessTime >= 0) ? FileTime.from(accessTime, TimeUnit.MILLISECONDS) : null, null); } catch (IOException e) { throw new IgniteException("Failed to set times for path: " + path, e); } }
@Test public void testEquals_equalsTester() { new EqualsTester() .addEqualityGroup( Files.getFileAttributeView( Paths.get(URI.create("gs://red/rum")), CloudStorageFileAttributeView.class), Files.getFileAttributeView( Paths.get(URI.create("gs://red/rum")), CloudStorageFileAttributeView.class)) .addEqualityGroup( Files.getFileAttributeView( Paths.get(URI.create("gs://red/lol/dog")), CloudStorageFileAttributeView.class)) .testEquals(); }
public static void setFilePermissions(Path filePath) throws IOException { if ("root".equalsIgnoreCase(ZaleniumConfiguration.getCurrentUser())) { UserPrincipal hostUid = filePath.getFileSystem() .getUserPrincipalLookupService().lookupPrincipalByName(ZaleniumConfiguration.getHostUid()); Files.setOwner(filePath, hostUid); GroupPrincipal hostGid = filePath.getFileSystem() .getUserPrincipalLookupService().lookupPrincipalByGroupName(ZaleniumConfiguration.getHostGid()); Files.getFileAttributeView(filePath, PosixFileAttributeView.class).setGroup(hostGid); } }
private void tryDos(Path p, boolean failIfDosIsNotSupported) { log("Falling back to DosFileAttributeView"); boolean readOnly = !isWritable(); DosFileAttributeView view = Files.getFileAttributeView(p, DosFileAttributeView.class); if (view != null) { try { view.setReadOnly(readOnly); } catch (IOException ioe) { maybeThrowException(ioe, "Failed to set permissions on '%s' due to %s", p, ioe.getMessage()); } catch (SecurityException uoe) { maybeThrowException(null, "the SecurityManager denies role accessUserInformation or write access for SecurityManager.checkWrite for resource '%s'", p); } } else { String msg = String.format( "the associated path '%s' does not support the DosFileAttributeView", p); if (failIfDosIsNotSupported) { throw new BuildException(msg); } log("Warning: " + msg, Project.MSG_ERR); } }
@Test public void testReadAttributes_pseudoDirectory() throws IOException { Path dir = Paths.get(URI.create("gs://red/rum/")); CloudStorageFileAttributeView lazyAttributes = Files.getFileAttributeView(dir, CloudStorageFileAttributeView.class); assertThat(lazyAttributes.readAttributes()) .isInstanceOf(CloudStoragePseudoDirectoryAttributes.class); }
public Void perform() { try { Path target = vertx.resolveFile(path).toPath(); UserPrincipalLookupService service = target.getFileSystem().getUserPrincipalLookupService(); UserPrincipal userPrincipal = user == null ? null : service.lookupPrincipalByName(user); GroupPrincipal groupPrincipal = group == null ? null : service.lookupPrincipalByGroupName(group); if (groupPrincipal != null) { PosixFileAttributeView view = Files.getFileAttributeView(target, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS); if (view == null) { throw new FileSystemException("Change group of file not supported"); } view.setGroup(groupPrincipal); } if (userPrincipal != null) { Files.setOwner(target, userPrincipal); } } catch (SecurityException e) { throw new FileSystemException("Accessed denied for chown on " + path); } catch (IOException e) { throw new FileSystemException(e); } return null; } };
@Test public void testReadAttributes() throws IOException { Files.write(path, HAPPY, CloudStorageOptions.withCacheControl("potato")); CloudStorageFileAttributeView lazyAttributes = Files.getFileAttributeView(path, CloudStorageFileAttributeView.class); assertThat(lazyAttributes.readAttributes().cacheControl().get()).isEqualTo("potato"); }
/** {@inheritDoc} */ @Override public T2<Long, Long> times(String path) throws IOException { BasicFileAttributes attrs = Files.getFileAttributeView(path(path), BasicFileAttributeView.class) .readAttributes(); return new T2<>(attrs.lastModifiedTime().toMillis(), attrs.lastAccessTime().toMillis()); }
@Test public void testName() throws IOException { Files.write(path, HAPPY, CloudStorageOptions.withCacheControl("potato")); CloudStorageFileAttributeView lazyAttributes = Files.getFileAttributeView(path, CloudStorageFileAttributeView.class); assertThat(lazyAttributes.name()).isEqualTo("gcs"); }
/** {@inheritDoc} */ @Override public Map<String, String> properties(final String path) throws IOException { Path p = path(path); PosixFileAttributes attrs = Files.getFileAttributeView(p, PosixFileAttributeView.class).readAttributes(); Map<String, String> props = new HashMap<>(); props.put(IgfsUtils.PROP_USER_NAME, attrs.owner().getName()); props.put(IgfsUtils.PROP_GROUP_NAME, attrs.group().getName()); props.put(IgfsUtils.PROP_PERMISSION, permissions(path)); return props; }
@Test public void testReadAttributes_notFound_throwsNoSuchFileException() throws IOException { CloudStorageFileAttributeView lazyAttributes = Files.getFileAttributeView(path, CloudStorageFileAttributeView.class); thrown.expect(NoSuchFileException.class); lazyAttributes.readAttributes(); }
/** {@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); }
@Test public void testNullness() throws NoSuchMethodException, SecurityException { new NullPointerTester() .ignore(CloudStorageFileAttributeView.class.getMethod("equals", Object.class)) .setDefault(FileTime.class, FileTime.fromMillis(0)) .testAllPublicInstanceMethods( Files.getFileAttributeView(path, CloudStorageFileAttributeView.class)); } }