Java Code Examples for java.nio.file.attribute.PosixFilePermissions#fromString()
The following examples show how to use
java.nio.file.attribute.PosixFilePermissions#fromString() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Aion.java From aion with MIT License | 7 votes |
private static void writeKeyToFile(final String path, final String fileName, final String key) throws IOException { Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxr-----"); FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms); Path p = Paths.get(path).resolve(fileName); Path keyFile; if (!java.nio.file.Files.exists(p)) { keyFile = java.nio.file.Files.createFile(p, attr); } else { keyFile = p; } FileOutputStream fos = new FileOutputStream(keyFile.toString()); fos.write(key.getBytes()); fos.close(); }
Example 2
Source File: PosixViewAttributeAction.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Override public PosixViewAttributeAction build() { if (Strings.isEmpty(basePath)) { LOGGER.error("Posix file attribute view action not valid because base path is empty."); return null; } if (filePermissions == null && Strings.isEmpty(filePermissionsString) && Strings.isEmpty(fileOwner) && Strings.isEmpty(fileGroup)) { LOGGER.error("Posix file attribute view not valid because nor permissions, user or group defined."); return null; } if (!FileUtils.isFilePosixAttributeViewSupported()) { LOGGER.warn("Posix file attribute view defined but it is not supported by this files system."); return null; } return new PosixViewAttributeAction(basePath, followLinks, maxDepth, pathConditions, subst != null ? subst : configuration.getStrSubstitutor(), filePermissions != null ? filePermissions : filePermissionsString != null ? PosixFilePermissions.fromString(filePermissionsString) : null, fileOwner, fileGroup); }
Example 3
Source File: LocalJavaKeyStoreProvider.java From big-c with Apache License 2.0 | 6 votes |
@Override protected void stashOriginalFilePermissions() throws IOException { // save off permissions in case we need to // rewrite the keystore in flush() if (!Shell.WINDOWS) { Path path = Paths.get(file.getCanonicalPath()); permissions = Files.getPosixFilePermissions(path); } else { // On Windows, the JDK does not support the POSIX file permission APIs. // Instead, we can do a winutils call and translate. String[] cmd = Shell.getGetPermissionCommand(); String[] args = new String[cmd.length + 1]; System.arraycopy(cmd, 0, args, 0, cmd.length); args[cmd.length] = file.getCanonicalPath(); String out = Shell.execCommand(args); StringTokenizer t = new StringTokenizer(out, Shell.TOKEN_SEPARATOR_REGEX); // The winutils output consists of 10 characters because of the leading // directory indicator, i.e. "drwx------". The JDK parsing method expects // a 9-character string, so remove the leading character. String permString = t.nextToken().substring(1); permissions = PosixFilePermissions.fromString(permString); } }
Example 4
Source File: LocalJavaKeyStoreProvider.java From hadoop with Apache License 2.0 | 6 votes |
@Override protected void stashOriginalFilePermissions() throws IOException { // save off permissions in case we need to // rewrite the keystore in flush() if (!Shell.WINDOWS) { Path path = Paths.get(file.getCanonicalPath()); permissions = Files.getPosixFilePermissions(path); } else { // On Windows, the JDK does not support the POSIX file permission APIs. // Instead, we can do a winutils call and translate. String[] cmd = Shell.getGetPermissionCommand(); String[] args = new String[cmd.length + 1]; System.arraycopy(cmd, 0, args, 0, cmd.length); args[cmd.length] = file.getCanonicalPath(); String out = Shell.execCommand(args); StringTokenizer t = new StringTokenizer(out, Shell.TOKEN_SEPARATOR_REGEX); // The winutils output consists of 10 characters because of the leading // directory indicator, i.e. "drwx------". The JDK parsing method expects // a 9-character string, so remove the leading character. String permString = t.nextToken().substring(1); permissions = PosixFilePermissions.fromString(permString); } }
Example 5
Source File: PathUtil.java From zstack with Apache License 2.0 | 5 votes |
public static void setFilePosixPermissions(String path, String perms) { try { Set<PosixFilePermission> s = PosixFilePermissions.fromString(perms); Files.setPosixFilePermissions(new File(path).toPath(), s); } catch (IOException ex) { logger.warn(String.format("set %s permission to %s: %s", path, perms, ex.getMessage())); } }
Example 6
Source File: FileUtilities.java From phoenicis with GNU Lesser General Public License v3.0 | 5 votes |
public void chmod(String filePath, String permissions) throws IOException { final Path path = Paths.get(filePath); if (!path.toFile().exists()) { throw new IllegalArgumentException(String.format("Path \"%s\" does not exist", path)); } final Set<PosixFilePermission> permissionsObj = PosixFilePermissions.fromString(permissions); Files.setPosixFilePermissions(path, permissionsObj); }
Example 7
Source File: LocalLocation.java From twill with Apache License 2.0 | 5 votes |
/** * Parses a three digit UNIX numeric permission representation to a set of {@link PosixFilePermission}. */ private Set<PosixFilePermission> parseNumericPermissions(String permission) { String posixPermission = ""; for (int i = 0; i < 3; i++) { int digit = permission.charAt(i) - '0'; if (digit < 0 || digit > 7) { throw new IllegalArgumentException("Invalid permission " + permission + ". Only digits between 0-7 are allowed."); } posixPermission += ((digit & 4) != 0) ? "r" : "-"; posixPermission += ((digit & 2) != 0) ? "w" : "-"; posixPermission += ((digit & 1) != 0) ? "x" : "-"; } return PosixFilePermissions.fromString(posixPermission); }
Example 8
Source File: LocalLocation.java From twill with Apache License 2.0 | 5 votes |
/** * Parses the given permission to a set of {@link PosixFilePermission}. * * @param permission the permission as passed to the {@link #createNew(String)} or {@link #getOutputStream(String)} * methods. * @return a new set of {@link PosixFilePermission}. */ private Set<PosixFilePermission> parsePermissions(String permission) { Set<PosixFilePermission> permissions; if (permission.length() == 3) { permissions = parseNumericPermissions(permission); } else if (permission.length() == 9) { permissions = PosixFilePermissions.fromString(permission); } else { throw new IllegalArgumentException("Invalid permission " + permission + ". Permission should either be a three digit or nine character string."); } return permissions; }
Example 9
Source File: FilePermissionsTest.java From git-client-plugin with MIT License | 5 votes |
private static void verifyFile(File repo, int staticPerm) throws IOException { String fileName = String.format("git-%03o.txt", staticPerm); File file = new File(repo, fileName); assertTrue("Missing " + file.getAbsolutePath(), file.exists()); String content = FileUtils.readFileToString(file, "UTF-8"); assertTrue(fileName + " wrong content: '" + content + "'", content.contains(fileName)); String rwx = permString(staticPerm); Set<PosixFilePermission> expected = PosixFilePermissions.fromString(rwx); Path path = FileSystems.getDefault().getPath(file.getPath()); PosixFileAttributes attrs = Files.getFileAttributeView(path, PosixFileAttributeView.class).readAttributes(); assertEquals(fileName + " OWNER_EXECUTE (execute) perm mismatch, expected: " + expected + ", was actually: " + attrs.permissions(), expected.contains(PosixFilePermission.OWNER_EXECUTE), attrs.permissions().contains(PosixFilePermission.OWNER_EXECUTE) ); }
Example 10
Source File: DremioFileSystem.java From dremio-oss with Apache License 2.0 | 5 votes |
@Override public boolean mkdirs(Path path, FsPermission fsPermission) throws IOException { String fsPerms = fsPermission.toString(); Set<PosixFilePermission> posixFilePermission = PosixFilePermissions.fromString(fsPerms.substring(1, fsPerms.length())); try (ContextClassLoaderSwapper swapper = ContextClassLoaderSwapper.newInstance(HadoopFileSystem.class.getClassLoader())) { return underLyingFs.mkdirs(com.dremio.io.file.Path.of(path.toUri()), posixFilePermission); } }
Example 11
Source File: CliGitAPIImpl.java From git-client-plugin with MIT License | 5 votes |
private File createTempFileInSystemDir(String prefix, String suffix) throws IOException { if (isWindows()) { return Files.createTempFile(prefix, suffix).toFile(); } Set<PosixFilePermission> ownerOnly = PosixFilePermissions.fromString("rw-------"); FileAttribute fileAttribute = PosixFilePermissions.asFileAttribute(ownerOnly); return Files.createTempFile(prefix, suffix, fileAttribute).toFile(); }
Example 12
Source File: FilePerm.java From servicecomb-java-chassis with Apache License 2.0 | 5 votes |
/** * 设置文件权限。前提:必须支持PosixFileAttributeView. */ public static void setFilePerm(File file, String perm) { if (filePermSupported()) { try { Set<PosixFilePermission> perms = PosixFilePermissions.fromString(perm); PosixFileAttributes attr = Files.readAttributes(file.toPath(), PosixFileAttributes.class); attr.permissions().clear(); Files.setPosixFilePermissions(file.toPath(), perms); } catch (IOException e) { throw new IllegalStateException(e); } } }
Example 13
Source File: HadoopPosixFileAttributes.java From jsr203-hadoop with Apache License 2.0 | 5 votes |
public HadoopPosixFileAttributes(HadoopFileSystem hdfs, Object fileKey, FileStatus fileStatus) throws IOException { super(fileKey, fileStatus); this.owner = hdfs.getUserPrincipalLookupService() .lookupPrincipalByGroupName(fileStatus.getOwner()); this.group = hdfs.getUserPrincipalLookupService() .lookupPrincipalByGroupName(fileStatus.getGroup()); FsPermission fsPermission = getFileStatus().getPermission(); String perms = fsPermission.getUserAction().SYMBOL + fsPermission.getGroupAction().SYMBOL + fsPermission.getOtherAction().SYMBOL; this.permissions = PosixFilePermissions.fromString(perms); }
Example 14
Source File: SymbolicLinkPreservingUntarTask.java From crate with Apache License 2.0 | 4 votes |
@TaskAction final void execute() { // ensure the target extraction path is empty getProject().delete(extractPath); try ( TarArchiveInputStream tar = new TarArchiveInputStream( new GzipCompressorInputStream(new FileInputStream(tarFile.getAsFile().get())) ) ) { final Path destinationPath = extractPath.get().getAsFile().toPath(); TarArchiveEntry entry = tar.getNextTarEntry(); while (entry != null) { final Path relativePath = transform.apply(entry.getName()); if (relativePath == null) { entry = tar.getNextTarEntry(); continue; } final Path destination = destinationPath.resolve(relativePath); final Path parent = destination.getParent(); if (Files.exists(parent) == false) { Files.createDirectories(parent); } if (entry.isDirectory()) { Files.createDirectory(destination); } else if (entry.isSymbolicLink()) { Files.createSymbolicLink(destination, Paths.get(entry.getLinkName())); } else { // copy the file from the archive using a small buffer to avoid heaping Files.createFile(destination); try (FileOutputStream fos = new FileOutputStream(destination.toFile())) { tar.transferTo(fos); } } if (entry.isSymbolicLink() == false) { // check if the underlying file system supports POSIX permissions final PosixFileAttributeView view = Files.getFileAttributeView(destination, PosixFileAttributeView.class); if (view != null) { final Set<PosixFilePermission> permissions = PosixFilePermissions.fromString( permissions((entry.getMode() >> 6) & 07) + permissions((entry.getMode() >> 3) & 07) + permissions( (entry.getMode() >> 0) & 07 ) ); Files.setPosixFilePermissions(destination, permissions); } } entry = tar.getNextTarEntry(); } } catch (final IOException e) { throw new GradleException("unable to extract tar [" + tarFile.getAsFile().get().toPath() + "]", e); } }
Example 15
Source File: FileHelper.java From qpid-broker-j with Apache License 2.0 | 4 votes |
public Path createNewFile(Path newFile, String posixFileAttributes) throws IOException { Set<PosixFilePermission> permissions = posixFileAttributes == null ? null : PosixFilePermissions.fromString(posixFileAttributes); return createNewFile(newFile, permissions ); }
Example 16
Source File: RollingRandomAccessFileManagerTest.java From logging-log4j2 with Apache License 2.0 | 4 votes |
@Test public void testRolloverRetainsFileAttributes() throws Exception { // Short-circuit if host doesn't support file attributes. if (!FileUtils.isFilePosixAttributeViewSupported()) { return; } // Create the initial file. final File file = File.createTempFile("log4j2", "test"); LockSupport.parkNanos(1000000); // 1 millisec // Set the initial file attributes. final String filePermissionsString = "rwxrwxrwx"; final Set<PosixFilePermission> filePermissions = PosixFilePermissions.fromString(filePermissionsString); FileUtils.defineFilePosixAttributeView(file.toPath(), filePermissions, null, null); // Create the manager. final RolloverStrategy rolloverStrategy = DefaultRolloverStrategy .newBuilder() .setMax("7") .setMin("1") .setFileIndex("max") .setStopCustomActionsOnError(false) .setConfig(new DefaultConfiguration()) .build(); final RollingRandomAccessFileManager manager = RollingRandomAccessFileManager.getRollingRandomAccessFileManager( file.getAbsolutePath(), Strings.EMPTY, true, true, RollingRandomAccessFileManager.DEFAULT_BUFFER_SIZE, new SizeBasedTriggeringPolicy(Long.MAX_VALUE), rolloverStrategy, null, null, filePermissionsString, null, null, null); assertNotNull(manager); manager.initialize(); // Trigger a rollover. manager.rollover(); // Verify the rolled over file attributes. final Set<PosixFilePermission> actualFilePermissions = Files .getFileAttributeView( Paths.get(manager.getFileName()), PosixFileAttributeView.class) .readAttributes() .permissions(); assertEquals(filePermissions, actualFilePermissions); }
Example 17
Source File: StoreConfig.java From ambry with Apache License 2.0 | 4 votes |
public StoreConfig(VerifiableProperties verifiableProperties) { storeKeyFactory = verifiableProperties.getString("store.key.factory", "com.github.ambry.commons.BlobIdFactory"); storeDataFlushIntervalSeconds = verifiableProperties.getLong("store.data.flush.interval.seconds", 60); storeIndexMaxMemorySizeBytes = verifiableProperties.getInt("store.index.max.memory.size.bytes", 20 * 1024 * 1024); storeDataFlushDelaySeconds = verifiableProperties.getInt("store.data.flush.delay.seconds", 5); storeIndexMaxNumberOfInmemElements = verifiableProperties.getInt("store.index.max.number.of.inmem.elements", 10000); storeIndexBloomMaxFalsePositiveProbability = verifiableProperties.getDoubleInRange("store.index.bloom.max.false.positive.probability", 0.01, 0.0, 1.0); storeMaxNumberOfEntriesToReturnFromJournal = verifiableProperties.getIntInRange("store.max.number.of.entries.to.return.from.journal", 5000, 1, 10000); storeDeletedMessageRetentionDays = verifiableProperties.getInt("store.deleted.message.retention.days", 7); storeContainerDeletionRetentionDays = verifiableProperties.getInt("store.container.deletion.retention.days", 14); storeHardDeleteOperationsBytesPerSec = verifiableProperties.getIntInRange("store.hard.delete.operations.bytes.per.sec", 100 * 1024, 1, Integer.MAX_VALUE); storeCompactionOperationsBytesPerSec = verifiableProperties.getIntInRange("store.compaction.operations.bytes.per.sec", 1 * 1024 * 1024, 1, Integer.MAX_VALUE); storeCompactionEnableDirectIO = verifiableProperties.getBoolean("store.compaction.enable.direct.io", false); storeCompactionMinBufferSize = verifiableProperties.getIntInRange("store.compaction.min.buffer.size", 10 * 1024 * 1024, 0, Integer.MAX_VALUE); storeCompactionFilter = verifiableProperties.getString("store.compaction.filter", "IndexSegmentValidEntryFilterWithoutUndelete"); storeEnableHardDelete = verifiableProperties.getBoolean("store.enable.hard.delete", false); storeSegmentSizeInBytes = verifiableProperties.getLongInRange("store.segment.size.in.bytes", Long.MAX_VALUE, 1, Long.MAX_VALUE); storeMinUsedCapacityToTriggerCompactionInPercentage = verifiableProperties.getInt("store.min.used.capacity.to.trigger.compaction.in.percentage", 50); storeCompactionTriggers = verifiableProperties.getString("store.compaction.triggers", "").split(","); storeCompactionCheckFrequencyInHours = verifiableProperties.getIntInRange("store.compaction.check.frequency.in.hours", 7 * 24, 1, 365 * 24); storeCompactionPolicyFactory = verifiableProperties.getString("store.compaction.policy.factory", "com.github.ambry.store.CompactAllPolicyFactory"); storeMinLogSegmentCountToReclaimToTriggerCompaction = verifiableProperties.getIntInRange("store.min.log.segment.count.to.reclaim.to.trigger.compaction", 1, 1, 1000); storeStatsBucketCount = verifiableProperties.getIntInRange("store.stats.bucket.count", 0, 0, 10000); storeStatsBucketSpanInMinutes = verifiableProperties.getLongInRange("store.stats.bucket.span.in.minutes", 60, 1, 10000); storeStatsRecentEntryProcessingIntervalInMinutes = verifiableProperties.getLongInRange("store.stats.recent.entry.processing.interval.in.minutes", 2, 1, 60); storeStatsWaitTimeoutInSecs = verifiableProperties.getLongInRange("store.stats.wait.timeout.in.secs", 2 * 60, 0, 30 * 60); storeStatsIndexEntriesPerSecond = verifiableProperties.getIntInRange("store.stats.index.entries.per.second", 240000, 1, Integer.MAX_VALUE); storeIndexPersistedEntryMinBytes = verifiableProperties.getInt("store.index.persisted.entry.min.bytes", 115); storeReplicaStatusDelegateEnable = verifiableProperties.getBoolean(storeReplicaStatusDelegateEnableName, false); storeReadOnlyEnableSizeThresholdPercentage = verifiableProperties.getIntInRange(storeReadOnlyEnableSizeThresholdPercentageName, 95, 0, 100); storeReadWriteEnableSizeThresholdPercentageDelta = verifiableProperties.getIntInRange(storeReadWriteEnableSizeThresholdPercentageDeltaName, 5, 0, storeReadOnlyEnableSizeThresholdPercentage); storeValidateAuthorization = verifiableProperties.getBoolean("store.validate.authorization", false); storeTtlUpdateBufferTimeSeconds = verifiableProperties.getIntInRange(storeTtlUpdateBufferTimeSecondsName, 60 * 60 * 24, 0, Integer.MAX_VALUE); storeIndexMemState = IndexMemState.valueOf( verifiableProperties.getString(storeIndexMemStateName, IndexMemState.MMAP_WITHOUT_FORCE_LOAD.name())); storeIoErrorCountToTriggerShutdown = verifiableProperties.getIntInRange("store.io.error.count.to.trigger.shutdown", Integer.MAX_VALUE, 1, Integer.MAX_VALUE); storeSetFilePermissionEnabled = verifiableProperties.getBoolean("store.set.file.permission.enabled", false); String storeDataFilePermissionStr = verifiableProperties.getString("store.data.file.permission", "rw-rw----"); storeDataFilePermission = PosixFilePermissions.fromString(storeDataFilePermissionStr); String storeOperationFilePermissionStr = verifiableProperties.getString("store.operation.file.permission", "rw-rw-r--"); storeOperationFilePermission = PosixFilePermissions.fromString(storeOperationFilePermissionStr); storeUuidBasedBloomFilterEnabled = verifiableProperties.getBoolean("store.uuid.based.bloom.filter.enabled", false); storeIndexRebuildBloomFilterEnabled = verifiableProperties.getBoolean("store.index.rebuild.bloom.filter.enabled", false); storeContainerDeletionEnabled = verifiableProperties.getBoolean("store.container.deletion.enabled", false); storeSetLocalPartitionStateEnabled = verifiableProperties.getBoolean("store.set.local.partition.state.enabled", false); }
Example 18
Source File: AbstractFileTests.java From pulsar with Apache License 2.0 | 4 votes |
protected static final FileAttribute<Set<PosixFilePermission>> getPermissions() { Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxrwxrwx"); return PosixFilePermissions.asFileAttribute(perms); }
Example 19
Source File: FilePerm.java From servicecomb-java-chassis with Apache License 2.0 | 4 votes |
/** * 获取默认Posix权限:640 */ public static Set<PosixFilePermission> getDefaultPosixPerm() { return PosixFilePermissions.fromString("rw-r-----"); }
Example 20
Source File: CliGitAPIImpl.java From git-client-plugin with MIT License | 4 votes |
/** * Create temporary file that is aware of the specific limitations * of command line git. * * For example, no temporary file name (Windows or Unix) may * include a percent sign in its path because ssh uses the percent * sign character as the start of token indicator for token * expansion. * * As another example, windows temporary files may not contain a * space, an open parenthesis, or a close parenthesis anywhere in * their path, otherwise they break ssh argument passing through * the GIT_SSH or SSH_ASKPASS environment variable. * * Package protected for testing. Not to be used outside this class * * @param prefix file name prefix for the generated temporary file (will be preceeded by "jenkins-gitclient-") * @param suffix file name suffix for the generated temporary file * @return temporary file * @throws IOException on error */ File createTempFile(String prefix, String suffix) throws IOException { String common_prefix = "jenkins-gitclient-"; if (prefix == null) { prefix = common_prefix; } else { prefix = common_prefix + prefix; } if (workspace == null) { return createTempFileInSystemDir(prefix, suffix); } File workspaceTmp = new File(workspace.getAbsolutePath() + "@tmp"); if (!workspaceTmp.isDirectory() && !workspaceTmp.mkdirs()) { if (!workspaceTmp.isDirectory()) { return createTempFileInSystemDir(prefix, suffix); } } Path tmpPath = Paths.get(workspaceTmp.getAbsolutePath()); if (workspaceTmp.getAbsolutePath().contains("%")) { // Avoid ssh token expansion on all platforms return createTempFileInSystemDir(prefix, suffix); } if (isWindows()) { /* Windows git fails its call to GIT_SSH if its absolute * path contains a space or parenthesis or pipe or question mark or asterisk. * Use system temp dir instead of workspace temp dir. */ if (workspaceTmp.getAbsolutePath().matches(".*[ ()|?*].*")) { return createTempFileInSystemDir(prefix, suffix); } return Files.createTempFile(tmpPath, prefix, suffix).toFile(); } else if (workspaceTmp.getAbsolutePath().contains("%")) { /* Avoid Linux expansion of % in ssh arguments */ return createTempFileInSystemDir(prefix, suffix); } // Unix specific if (workspaceTmp.getAbsolutePath().contains("`")) { // Avoid backquote shell expansion return createTempFileInSystemDir(prefix, suffix); } Set<PosixFilePermission> ownerOnly = PosixFilePermissions.fromString("rw-------"); FileAttribute fileAttribute = PosixFilePermissions.asFileAttribute(ownerOnly); return Files.createTempFile(tmpPath, prefix, suffix, fileAttribute).toFile(); }