本文整理匯總了Java中java.nio.file.attribute.PosixFileAttributeView.setPermissions方法的典型用法代碼示例。如果您正苦於以下問題:Java PosixFileAttributeView.setPermissions方法的具體用法?Java PosixFileAttributeView.setPermissions怎麽用?Java PosixFileAttributeView.setPermissions使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.nio.file.attribute.PosixFileAttributeView
的用法示例。
在下文中一共展示了PosixFileAttributeView.setPermissions方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: addExecutablePermission
import java.nio.file.attribute.PosixFileAttributeView; //導入方法依賴的package包/類
/**
* Adds the executable file permission.
*
* @param file the path to the file
*/
private void addExecutablePermission(String file) {
final Path path = Paths.get(file);
if (Files.exists(path)) {
try {
PosixFileAttributeView attr = Files.getFileAttributeView(path, PosixFileAttributeView.class);
Set<PosixFilePermission> permissions = attr.readAttributes().permissions();
if(permissions.add(PosixFilePermission.OWNER_EXECUTE)) {
logger.info(String.format("Added +x permission to file: %s", file));
}
attr.setPermissions(permissions);
} catch (IOException e) {
logger.error(String.format("Failed to add the executable permissions to file: %s", file));
}
}
}
示例2: setPermissions
import java.nio.file.attribute.PosixFileAttributeView; //導入方法依賴的package包/類
/**
* Sets permissions on a {@link Resource} - doesn't do anything
* for unsupported resource types.
*
* <p>Supported types are:</p>
* <ul>
* <li>any {@link FileProvider}</li>
* <li>{@link ArchiveResource}</li>
* </ul>
*
* @param r the resource to set permissions for
* @param permissions the permissions
* @param posixNotSupportedCallback optional callback that is
* invoked for a file provider resource if the file-system holding
* the file doesn't support PosixFilePermissions. The Path
* corresponding to the file is passed to the callback.
* @throws IOException if something goes wrong
*/
public static void setPermissions(Resource r, Set<PosixFilePermission> permissions,
Consumer<Path> posixNotSupportedCallback)
throws IOException {
FileProvider f = r.as(FileProvider.class);
if (f != null) {
Path p = f.getFile().toPath();
PosixFileAttributeView view =
Files.getFileAttributeView(p, PosixFileAttributeView.class);
if (view != null) {
view.setPermissions(permissions);
} else if (posixNotSupportedCallback != null) {
posixNotSupportedCallback.accept(p);
}
} else if (r instanceof ArchiveResource) {
((ArchiveResource) r).setMode(modeFromPermissions(permissions,
FileType.of(r)));
}
}
示例3: makeWritable
import java.nio.file.attribute.PosixFileAttributeView; //導入方法依賴的package包/類
private void makeWritable(Path file) throws IOException {
FileStore fileStore = Files.getFileStore(file);
if (IS_WINDOWS && fileStore.supportsFileAttributeView(DosFileAttributeView.class)) {
DosFileAttributeView dosAttribs =
Files.getFileAttributeView(file, DosFileAttributeView.class);
if (dosAttribs != null) {
dosAttribs.setReadOnly(false);
}
} else if (fileStore.supportsFileAttributeView(PosixFileAttributeView.class)) {
PosixFileAttributeView posixAttribs =
Files.getFileAttributeView(file, PosixFileAttributeView.class);
if (posixAttribs != null) {
posixAttribs.setPermissions(EnumSet.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE));
}
}
}
示例4: setPosixFilePermissions
import java.nio.file.attribute.PosixFileAttributeView; //導入方法依賴的package包/類
@Override
public void setPosixFilePermissions(Path path, Set<PosixFilePermission> permissions) throws XenonException {
if (permissions == null) {
throw new IllegalArgumentException("Permissions is null!");
}
Path absPath = toAbsolutePath(path);
assertPathExists(absPath);
try {
PosixFileAttributeView view = Files.getFileAttributeView(javaPath(absPath), PosixFileAttributeView.class);
view.setPermissions(javaPermissions(permissions));
} catch (IOException e) {
throw new XenonException(ADAPTOR_NAME, "Failed to set permissions " + absPath, e);
}
}
示例5: save
import java.nio.file.attribute.PosixFileAttributeView; //導入方法依賴的package包/類
/** Write the keystore to the given config directory. */
void save(Path configDir) throws Exception {
char[] password = this.keystorePassword.get().getPassword();
SimpleFSDirectory directory = new SimpleFSDirectory(configDir);
// write to tmp file first, then overwrite
String tmpFile = KEYSTORE_FILENAME + ".tmp";
try (IndexOutput output = directory.createOutput(tmpFile, IOContext.DEFAULT)) {
CodecUtil.writeHeader(output, KEYSTORE_FILENAME, FORMAT_VERSION);
output.writeByte(password.length == 0 ? (byte)0 : (byte)1);
output.writeString(type);
output.writeString(secretFactory.getAlgorithm());
ByteArrayOutputStream keystoreBytesStream = new ByteArrayOutputStream();
keystore.get().store(keystoreBytesStream, password);
byte[] keystoreBytes = keystoreBytesStream.toByteArray();
output.writeInt(keystoreBytes.length);
output.writeBytes(keystoreBytes, keystoreBytes.length);
CodecUtil.writeFooter(output);
}
Path keystoreFile = keystorePath(configDir);
Files.move(configDir.resolve(tmpFile), keystoreFile, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
PosixFileAttributeView attrs = Files.getFileAttributeView(keystoreFile, PosixFileAttributeView.class);
if (attrs != null) {
// don't rely on umask: ensure the keystore has minimal permissions
attrs.setPermissions(PosixFilePermissions.fromString("rw-------"));
}
}
示例6: chmod
import java.nio.file.attribute.PosixFileAttributeView; //導入方法依賴的package包/類
private static void chmod(final Path file, final String perms) throws IOException {
final PosixFileAttributeView view =
Files.getFileAttributeView(file, PosixFileAttributeView.class);
if (view == null) {
// OS (Windows) doesn't support POSIX file attributes
return;
}
view.setPermissions(PosixFilePermissions.fromString("rwxr-xr-x"));
}
示例7: unTarGz
import java.nio.file.attribute.PosixFileAttributeView; //導入方法依賴的package包/類
public static void unTarGz(Path file, Path target) throws IOException {
try (final InputStream is = newInputStream(file)) {
final InputStream gzip = new GZIPInputStream(is);
final TarArchiveInputStream archive = new TarArchiveInputStream(gzip);
TarArchiveEntry entry = archive.getNextTarEntry();
while (entry != null) {
final Path entryFile = target.resolve(REMOVE_LEADING_GO_PATH_PATTERN.matcher(entry.getName()).replaceFirst("")).toAbsolutePath();
if (entry.isDirectory()) {
createDirectoriesIfRequired(entryFile);
} else {
ensureParentOf(entryFile);
try (final OutputStream os = newOutputStream(entryFile)) {
copy(archive, os);
}
final PosixFileAttributeView view = getFileAttributeView(entryFile, PosixFileAttributeView.class);
if (view != null) {
final int mode = entry.getMode();
final Set<PosixFilePermission> perms = new HashSet<>();
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.GROUP_READ);
perms.add(PosixFilePermission.OTHERS_READ);
//noinspection OctalInteger,ResultOfMethodCallIgnored,IncompatibleBitwiseMaskOperation
if ((mode | 0001) > 0) {
perms.add(PosixFilePermission.OWNER_EXECUTE);
}
//noinspection OctalInteger,ResultOfMethodCallIgnored,IncompatibleBitwiseMaskOperation
if ((mode | 0100) > 0) {
perms.add(PosixFilePermission.GROUP_EXECUTE);
perms.add(PosixFilePermission.OTHERS_EXECUTE);
}
view.setPermissions(perms);
}
}
entry = archive.getNextTarEntry();
}
}
}
示例8: updatePermissions
import java.nio.file.attribute.PosixFileAttributeView; //導入方法依賴的package包/類
static final void updatePermissions(Path fileOrDir) throws IOException {
PosixFileAttributeView posixView = Files.getFileAttributeView(fileOrDir, PosixFileAttributeView.class);
if (posixView == null) {
LOGGER.log(Level.INFO, "Cannot set file permissions.");
return;
}
if (Files.isDirectory(fileOrDir)) {
posixView.setPermissions(DIR_PERMISSIONS);
} else {
posixView.setPermissions(FILE_PERMISSIONS);
}
}
示例9: extract
import java.nio.file.attribute.PosixFileAttributeView; //導入方法依賴的package包/類
@Override
public void extract(Path archive, Path destination, MessageListener messageListener)
throws IOException {
GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(Files.newInputStream(archive));
try (TarArchiveInputStream in = new TarArchiveInputStream(gzipIn)) {
TarArchiveEntry entry;
while ((entry = in.getNextTarEntry()) != null) {
final Path entryTarget = destination.resolve(entry.getName());
if (messageListener != null) {
messageListener.message(entryTarget + "\n");
}
if (entry.isDirectory()) {
if (!Files.exists(entryTarget)) {
Files.createDirectories(entryTarget);
}
} else if (entry.isFile()) {
if (!Files.exists(entryTarget.getParent())) {
Files.createDirectories(entryTarget.getParent());
}
try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(entryTarget))) {
IOUtils.copy(in, out);
PosixFileAttributeView attributeView =
Files.getFileAttributeView(entryTarget, PosixFileAttributeView.class);
if (attributeView != null) {
attributeView.setPermissions(PosixUtil.getPosixFilePermissions(entry.getMode()));
}
}
} else {
// we don't know what kind of entry this is (we only process directories and files).
if (messageListener != null) {
messageListener.message("Skipping entry (unknown type): " + entry.getName() + "\n");
}
}
}
}
}
示例10: extract
import java.nio.file.attribute.PosixFileAttributeView; //導入方法依賴的package包/類
@Override
public void extract(Path archive, Path destination, MessageListener messageListener)
throws IOException {
// Use ZipFile instead of ZipArchiveInputStream so that we can obtain file permissions
// on unix-like systems via getUnixMode(). ZipArchiveInputStream doesn't have access to
// all the zip file data and will return "0" for any call to getUnixMode().
try (ZipFile zipFile = new ZipFile(archive.toFile())) {
Enumeration<ZipArchiveEntry> zipEntries = zipFile.getEntries();
while (zipEntries.hasMoreElements()) {
ZipArchiveEntry entry = zipEntries.nextElement();
final Path entryTarget = destination.resolve(entry.getName());
if (messageListener != null) {
messageListener.message(entryTarget + "\n");
}
if (entry.isDirectory()) {
if (!Files.exists(entryTarget)) {
Files.createDirectories(entryTarget);
}
} else {
if (!Files.exists(entryTarget.getParent())) {
Files.createDirectories(entryTarget.getParent());
}
try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(entryTarget))) {
try (InputStream in = zipFile.getInputStream(entry)) {
IOUtils.copy(in, out);
PosixFileAttributeView attributeView =
Files.getFileAttributeView(entryTarget, PosixFileAttributeView.class);
if (attributeView != null) {
attributeView.setPermissions(
PosixUtil.getPosixFilePermissions(entry.getUnixMode()));
}
}
}
}
}
}
}
示例11: main
import java.nio.file.attribute.PosixFileAttributeView; //導入方法依賴的package包/類
public static void main(String[] args) {
try {
Path path = Paths.get("metadata.txt");
Files.deleteIfExists(path);
Files.createFile(path);
BasicFileAttributeView basicView = Files.getFileAttributeView(path, BasicFileAttributeView.class);
BasicFileAttributes basicAttributes = basicView.readAttributes();
boolean isDirectory = basicAttributes.isDirectory();
FileTime lastModifiedTime = basicAttributes.lastModifiedTime();
System.out.println(isDirectory);
System.out.println(lastModifiedTime);
PosixFileAttributeView posixView = Files.getFileAttributeView(path, PosixFileAttributeView.class);
PosixFileAttributes posixAttributes = posixView.readAttributes();
GroupPrincipal group = posixAttributes.group();
Set<PosixFilePermission> permissions = posixAttributes.permissions();
permissions.add(PosixFilePermission.OWNER_EXECUTE);
posixView.setPermissions(permissions);
System.out.println(group);
} catch (IOException e) {
// Log and Handle exception
e.printStackTrace();
}
}
示例12: testView
import java.nio.file.attribute.PosixFileAttributeView; //導入方法依賴的package包/類
@Test
public void testView() throws IOException {
file.setAttribute("owner", "owner", createUserPrincipal("user"));
PosixFileAttributeView view =
provider.view(
fileLookup(),
ImmutableMap.of(
"basic", new BasicAttributeProvider().view(fileLookup(), NO_INHERITED_VIEWS),
"owner", new OwnerAttributeProvider().view(fileLookup(), NO_INHERITED_VIEWS)));
assertNotNull(view);
assertThat(view.name()).isEqualTo("posix");
assertThat(view.getOwner()).isEqualTo(createUserPrincipal("user"));
PosixFileAttributes attrs = view.readAttributes();
assertThat(attrs.fileKey()).isEqualTo(0);
assertThat(attrs.owner()).isEqualTo(createUserPrincipal("user"));
assertThat(attrs.group()).isEqualTo(createGroupPrincipal("group"));
assertThat(attrs.permissions()).isEqualTo(PosixFilePermissions.fromString("rw-r--r--"));
view.setOwner(createUserPrincipal("root"));
assertThat(view.getOwner()).isEqualTo(createUserPrincipal("root"));
assertThat(file.getAttribute("owner", "owner")).isEqualTo(createUserPrincipal("root"));
view.setGroup(createGroupPrincipal("root"));
assertThat(view.readAttributes().group()).isEqualTo(createGroupPrincipal("root"));
assertThat(file.getAttribute("posix", "group")).isEqualTo(createGroupPrincipal("root"));
view.setPermissions(PosixFilePermissions.fromString("rwx------"));
assertThat(view.readAttributes().permissions())
.isEqualTo(PosixFilePermissions.fromString("rwx------"));
assertThat(file.getAttribute("posix", "permissions"))
.isEqualTo(PosixFilePermissions.fromString("rwx------"));
}
示例13: defineFilePosixAttributeView
import java.nio.file.attribute.PosixFileAttributeView; //導入方法依賴的package包/類
/**
* Define file posix attribute view on a path/file.
*
* @param path Target path
* @param filePermissions Permissions to apply
* @param fileOwner File owner
* @param fileGroup File group
* @throws IOException If IO error during definition of file attribute view
*/
public static void defineFilePosixAttributeView(final Path path,
final Set<PosixFilePermission> filePermissions,
final String fileOwner,
final String fileGroup) throws IOException {
final PosixFileAttributeView view = Files.getFileAttributeView(path, PosixFileAttributeView.class);
if (view != null) {
final UserPrincipalLookupService lookupService = FileSystems.getDefault()
.getUserPrincipalLookupService();
if (fileOwner != null) {
final UserPrincipal userPrincipal = lookupService.lookupPrincipalByName(fileOwner);
if (userPrincipal != null) {
// If not sudoers member, it will throw Operation not permitted
// Only processes with an effective user ID equal to the user ID
// of the file or with appropriate privileges may change the ownership of a file.
// If _POSIX_CHOWN_RESTRICTED is in effect for path
view.setOwner(userPrincipal);
}
}
if (fileGroup != null) {
final GroupPrincipal groupPrincipal = lookupService.lookupPrincipalByGroupName(fileGroup);
if (groupPrincipal != null) {
// The current user id should be members of this group,
// if not will raise Operation not permitted
view.setGroup(groupPrincipal);
}
}
if (filePermissions != null) {
view.setPermissions(filePermissions);
}
}
}
示例14: chmod
import java.nio.file.attribute.PosixFileAttributeView; //導入方法依賴的package包/類
@Override
public void chmod(File f, int mode) throws IOException {
PosixFileAttributeView fileAttributeView = Files.getFileAttributeView(f.toPath(), PosixFileAttributeView.class);
fileAttributeView.setPermissions(convertToPermissionsSet(mode));
}
示例15: chmod
import java.nio.file.attribute.PosixFileAttributeView; //導入方法依賴的package包/類
public void chmod(File f, int mode) throws IOException {
PosixFileAttributeView fileAttributeView = Files.getFileAttributeView(f.toPath(), PosixFileAttributeView.class);
fileAttributeView.setPermissions(convertToPermissionsSet(mode));
}