use of java.nio.file.attribute.PosixFilePermission in project spring-boot by spring-projects.
the class JarWriter method setExecutableFilePermission.
private void setExecutableFilePermission(File file) {
try {
Path path = file.toPath();
Set<PosixFilePermission> permissions = new HashSet<>(Files.getPosixFilePermissions(path));
permissions.add(PosixFilePermission.OWNER_EXECUTE);
Files.setPosixFilePermissions(path, permissions);
} catch (Throwable ex) {
// Ignore and continue creating the jar
}
}
use of java.nio.file.attribute.PosixFilePermission in project camel by apache.
the class FileOperations method storeFile.
public boolean storeFile(String fileName, Exchange exchange) throws GenericFileOperationFailedException {
ObjectHelper.notNull(endpoint, "endpoint");
File file = new File(fileName);
// if an existing file already exists what should we do?
if (file.exists()) {
if (endpoint.getFileExist() == GenericFileExist.Ignore) {
// ignore but indicate that the file was written
LOG.trace("An existing file already exists: {}. Ignore and do not override it.", file);
return true;
} else if (endpoint.getFileExist() == GenericFileExist.Fail) {
throw new GenericFileOperationFailedException("File already exist: " + file + ". Cannot write new file.");
} else if (endpoint.getFileExist() == GenericFileExist.Move) {
// move any existing file first
doMoveExistingFile(fileName);
}
}
// Do an explicit test for a null body and decide what to do
if (exchange.getIn().getBody() == null) {
if (endpoint.isAllowNullBody()) {
LOG.trace("Writing empty file.");
try {
writeFileEmptyBody(file);
return true;
} catch (IOException e) {
throw new GenericFileOperationFailedException("Cannot store file: " + file, e);
}
} else {
throw new GenericFileOperationFailedException("Cannot write null body to file: " + file);
}
}
// 3. write stream to file
try {
// is there an explicit charset configured we must write the file as
String charset = endpoint.getCharset();
// we can optimize and use file based if no charset must be used, and the input body is a file
File source = null;
boolean fileBased = false;
if (charset == null) {
// if no charset, then we can try using file directly (optimized)
Object body = exchange.getIn().getBody();
if (body instanceof WrappedFile) {
body = ((WrappedFile<?>) body).getFile();
}
if (body instanceof File) {
source = (File) body;
fileBased = true;
}
}
if (fileBased) {
// okay we know the body is a file based
// so try to see if we can optimize by renaming the local work path file instead of doing
// a full file to file copy, as the local work copy is to be deleted afterwards anyway
// local work path
File local = exchange.getIn().getHeader(Exchange.FILE_LOCAL_WORK_PATH, File.class);
if (local != null && local.exists()) {
boolean renamed = writeFileByLocalWorkPath(local, file);
if (renamed) {
// try to keep last modified timestamp if configured to do so
keepLastModified(exchange, file);
// set permissions if the chmod option was set
if (ObjectHelper.isNotEmpty(endpoint.getChmod())) {
Set<PosixFilePermission> permissions = endpoint.getPermissions();
if (!permissions.isEmpty()) {
if (LOG.isTraceEnabled()) {
LOG.trace("Setting chmod: {} on file: {} ", PosixFilePermissions.toString(permissions), file);
}
Files.setPosixFilePermissions(file.toPath(), permissions);
}
}
// clear header as we have renamed the file
exchange.getIn().setHeader(Exchange.FILE_LOCAL_WORK_PATH, null);
// to the target.
return true;
}
} else if (source != null && source.exists()) {
// no there is no local work file so use file to file copy if the source exists
writeFileByFile(source, file);
// try to keep last modified timestamp if configured to do so
keepLastModified(exchange, file);
// set permissions if the chmod option was set
if (ObjectHelper.isNotEmpty(endpoint.getChmod())) {
Set<PosixFilePermission> permissions = endpoint.getPermissions();
if (!permissions.isEmpty()) {
if (LOG.isTraceEnabled()) {
LOG.trace("Setting chmod: {} on file: {} ", PosixFilePermissions.toString(permissions), file);
}
Files.setPosixFilePermissions(file.toPath(), permissions);
}
}
return true;
}
}
if (charset != null) {
// charset configured so we must use a reader so we can write with encoding
Reader in = exchange.getContext().getTypeConverter().tryConvertTo(Reader.class, exchange, exchange.getIn().getBody());
if (in == null) {
// okay no direct reader conversion, so use an input stream (which a lot can be converted as)
InputStream is = exchange.getIn().getMandatoryBody(InputStream.class);
in = new InputStreamReader(is);
}
// buffer the reader
in = IOHelper.buffered(in);
writeFileByReaderWithCharset(in, file, charset);
} else {
// fallback and use stream based
InputStream in = exchange.getIn().getMandatoryBody(InputStream.class);
writeFileByStream(in, file);
}
// try to keep last modified timestamp if configured to do so
keepLastModified(exchange, file);
// set permissions if the chmod option was set
if (ObjectHelper.isNotEmpty(endpoint.getChmod())) {
Set<PosixFilePermission> permissions = endpoint.getPermissions();
if (!permissions.isEmpty()) {
if (LOG.isTraceEnabled()) {
LOG.trace("Setting chmod: {} on file: {} ", PosixFilePermissions.toString(permissions), file);
}
Files.setPosixFilePermissions(file.toPath(), permissions);
}
}
return true;
} catch (IOException e) {
throw new GenericFileOperationFailedException("Cannot store file: " + file, e);
} catch (InvalidPayloadException e) {
throw new GenericFileOperationFailedException("Cannot store file: " + file, e);
}
}
use of java.nio.file.attribute.PosixFilePermission in project camel by apache.
the class FileProducerChmodOptionTest method runChmodCheck.
private void runChmodCheck(String routeSuffix, String expectedPermissions) throws Exception {
MockEndpoint mock = getMockEndpoint("mock:chmod" + routeSuffix);
mock.expectedMessageCount(1);
String testFileName = "chmod" + routeSuffix + ".txt";
String fullTestFileName = TEST_DIRECTORY + testFileName;
String testFileContent = "Writing file with chmod " + routeSuffix + " option at " + new Date();
mock.expectedFileExists(fullTestFileName, testFileContent);
template.sendBodyAndHeader("direct:write" + routeSuffix, testFileContent, Exchange.FILE_NAME, testFileName);
File f = new File(fullTestFileName);
Set<PosixFilePermission> permissions = Files.getPosixFilePermissions(f.toPath(), LinkOption.NOFOLLOW_LINKS);
assertEquals(expectedPermissions, PosixFilePermissions.toString(permissions));
assertEquals(expectedPermissions.replace("-", "").length(), permissions.size());
assertMockEndpointsSatisfied();
}
use of java.nio.file.attribute.PosixFilePermission in project jdk8u_jdk by JetBrains.
the class CustomLauncherTest method getLauncher.
private static String[] getLauncher() throws IOException {
String platform = getPlatform();
if (platform == null) {
return null;
}
String launcher = TEST_SRC + File.separator + platform + "-" + ARCH + File.separator + "launcher";
final FileSystem FS = FileSystems.getDefault();
Path launcherPath = FS.getPath(launcher);
final boolean hasLauncher = Files.isRegularFile(launcherPath, LinkOption.NOFOLLOW_LINKS) && Files.isReadable(launcherPath);
if (!hasLauncher) {
System.out.println("Launcher [" + launcher + "] does not exist. Skipping the test.");
return null;
}
// It is impossible to store an executable file in the source control
// We need to copy the launcher to the working directory
// and set the executable flag
Path localLauncherPath = FS.getPath(WORK_DIR, "launcher");
Files.copy(launcherPath, localLauncherPath, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
if (!Files.isExecutable(localLauncherPath)) {
Set<PosixFilePermission> perms = new HashSet<>(Files.getPosixFilePermissions(localLauncherPath, LinkOption.NOFOLLOW_LINKS));
perms.add(PosixFilePermission.OWNER_EXECUTE);
Files.setPosixFilePermissions(localLauncherPath, perms);
}
return new String[] { launcher, localLauncherPath.toAbsolutePath().toString() };
}
use of java.nio.file.attribute.PosixFilePermission in project jdk8u_jdk by JetBrains.
the class PosixFilePermissions method asFileAttribute.
/**
* Creates a {@link FileAttribute}, encapsulating a copy of the given file
* permissions, suitable for passing to the {@link java.nio.file.Files#createFile
* createFile} or {@link java.nio.file.Files#createDirectory createDirectory}
* methods.
*
* @param perms
* the set of permissions
*
* @return an attribute encapsulating the given file permissions with
* {@link FileAttribute#name name} {@code "posix:permissions"}
*
* @throws ClassCastException
* if the set contains elements that are not of type {@code
* PosixFilePermission}
*/
public static FileAttribute<Set<PosixFilePermission>> asFileAttribute(Set<PosixFilePermission> perms) {
// copy set and check for nulls (CCE will be thrown if an element is not
// a PosixFilePermission)
perms = new HashSet<PosixFilePermission>(perms);
for (PosixFilePermission p : perms) {
if (p == null)
throw new NullPointerException();
}
final Set<PosixFilePermission> value = perms;
return new FileAttribute<Set<PosixFilePermission>>() {
@Override
public String name() {
return "posix:permissions";
}
@Override
public Set<PosixFilePermission> value() {
return Collections.unmodifiableSet(value);
}
};
}
Aggregations