Search in sources :

Example 26 with PosixFilePermission

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
    }
}
Also used : Path(java.nio.file.Path) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) HashSet(java.util.HashSet)

Example 27 with PosixFilePermission

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);
    }
}
Also used : Set(java.util.Set) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) InvalidPayloadException(org.apache.camel.InvalidPayloadException) WrappedFile(org.apache.camel.WrappedFile) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) WrappedFile(org.apache.camel.WrappedFile)

Example 28 with PosixFilePermission

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();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) File(java.io.File) Date(java.util.Date)

Example 29 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project POL-POM-5 by PlayOnLinux.

the class FileUtilitiesTest method testOneCaseIntToPosixFilePermission.

private void testOneCaseIntToPosixFilePermission(int mode, PosixFilePermission... permissions) {
    List<PosixFilePermission> permissionsSet = new ArrayList<>(fileUtilities.intToPosixFilePermission(mode));
    assertEquals(permissions.length, permissionsSet.size());
    for (PosixFilePermission permission : permissions) {
        assertTrue(permissionsSet.contains(permission));
    }
}
Also used : ArrayList(java.util.ArrayList) PosixFilePermission(java.nio.file.attribute.PosixFilePermission)

Example 30 with PosixFilePermission

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() };
}
Also used : Path(java.nio.file.Path) FileSystem(java.nio.file.FileSystem) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) HashSet(java.util.HashSet)

Aggregations

PosixFilePermission (java.nio.file.attribute.PosixFilePermission)36 Path (java.nio.file.Path)21 IOException (java.io.IOException)10 Test (org.junit.Test)7 File (java.io.File)6 PosixFileAttributes (java.nio.file.attribute.PosixFileAttributes)6 HashSet (java.util.HashSet)6 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)4 Set (java.util.Set)4 GroupPrincipal (java.nio.file.attribute.GroupPrincipal)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