Search in sources :

Example 51 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project aion by aionnetwork.

the class Aion method writeKeyToFile.

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

Example 52 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project athenz by yahoo.

the class ZMSFileChangeLogStoreCommonTest method testSetFilePermissionsException.

@Test
public void testSetFilePermissionsException() throws IOException {
    ZMSFileChangeLogStoreCommon fstore = new ZMSFileChangeLogStoreCommon(FSTORE_PATH);
    FilesHelper helper = Mockito.mock(FilesHelper.class);
    Mockito.when(helper.setPosixFilePermissions(any(), any())).thenThrow(new IOException("io exception"));
    fstore.filesHelper = helper;
    try {
        File file = new File(FSTORE_PATH, "domain");
        Set<PosixFilePermission> perms = EnumSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_EXECUTE);
        fstore.setupFilePermissions(file, perms);
        fail();
    } catch (Exception ex) {
        assertTrue(true);
    }
}
Also used : IOException(java.io.IOException) FilesHelper(com.yahoo.athenz.common.server.util.FilesHelper) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Test(org.testng.annotations.Test)

Example 53 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project engineblock by engineblock.

the class DockerMetricsHelper method setupGrafanaFiles.

private void setupGrafanaFiles(String ip) {
    File grafanaDir = new File(userHome, ".eb/grafana");
    grafanaDir.mkdir();
    Path grafanaDirPath = Paths.get(userHome, ".eb/grafana");
    Set<PosixFilePermission> perms = new HashSet<>();
    perms.add(PosixFilePermission.OWNER_READ);
    perms.add(PosixFilePermission.OWNER_WRITE);
    perms.add(PosixFilePermission.OWNER_EXECUTE);
    try {
        Files.setPosixFilePermissions(grafanaDirPath, perms);
    } catch (IOException e) {
        logger.error("failed to set permissions on prom backup directory (~/.eb/prometheus)");
        e.printStackTrace();
        System.exit(1);
    }
}
Also used : Path(java.nio.file.Path) PosixFilePermission(java.nio.file.attribute.PosixFilePermission)

Example 54 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project engineblock by engineblock.

the class DockerMetricsHelper method setupPromFiles.

private void setupPromFiles(String ip) {
    String datasource = EngineBlockFiles.readFile("docker/prometheus/prometheus.yml");
    if (ip == null) {
        logger.error("IP for graphite container not found");
        System.exit(1);
    }
    datasource = datasource.replace("!!!GRAPHITE_IP!!!", ip);
    File prometheusDir = new File(userHome, ".eb/prometheus");
    prometheusDir.mkdir();
    new File(userHome, ".eb/prometheus-conf").mkdir();
    Path prometheusDirPath = Paths.get(userHome, ".eb/prometheus");
    Set<PosixFilePermission> perms = new HashSet<>();
    perms.add(PosixFilePermission.OTHERS_READ);
    perms.add(PosixFilePermission.OTHERS_WRITE);
    perms.add(PosixFilePermission.OTHERS_EXECUTE);
    try {
        Files.setPosixFilePermissions(prometheusDirPath, perms);
    } catch (IOException e) {
        logger.error("failed to set permissions on prom backup directory (~/.eb/prometheus)");
        e.printStackTrace();
        System.exit(1);
    }
    try (PrintWriter out = new PrintWriter(new FileWriter(userHome + "/.eb/prometheus-conf/prometheus.yml", false))) {
        out.println(datasource);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        logger.error("error writing prometheus yaml file to ~/.prometheus");
        System.exit(1);
    } catch (IOException e) {
        e.printStackTrace();
        logger.error("creating file in ~/.prometheus");
        System.exit(1);
    }
}
Also used : Path(java.nio.file.Path) PosixFilePermission(java.nio.file.attribute.PosixFilePermission)

Example 55 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project cloudconductor-agent-redhat by cinovo.

the class FileHelper method isFileMode.

/**
 * Check if a file has a specific file mode or not
 *
 * @param file the file to check
 * @param fileMode the file mode to check
 * @return file mode is equal or not
 * @throws IOException on error
 */
public static boolean isFileMode(File file, String fileMode) throws IOException {
    PosixFileAttributeView view = FileHelper.getFileAttributes(file);
    Set<PosixFilePermission> fileModeSet = PosixFilePermissions.fromString(fileMode);
    if (view.readAttributes().permissions().equals(fileModeSet)) {
        return true;
    }
    return false;
}
Also used : PosixFilePermission(java.nio.file.attribute.PosixFilePermission) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Aggregations

PosixFilePermission (java.nio.file.attribute.PosixFilePermission)139 Path (java.nio.file.Path)69 IOException (java.io.IOException)41 File (java.io.File)40 HashSet (java.util.HashSet)40 Test (org.junit.Test)35 Set (java.util.Set)22 FileOutputStream (java.io.FileOutputStream)17 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)14 PosixFileAttributes (java.nio.file.attribute.PosixFileAttributes)11 FileNotFoundException (java.io.FileNotFoundException)8 ArrayList (java.util.ArrayList)7 RandomAccessFile (java.io.RandomAccessFile)6 UserPrincipal (java.nio.file.attribute.UserPrincipal)6 OutputStream (java.io.OutputStream)5 SimpleDateFormat (java.text.SimpleDateFormat)5 BufferedWriter (java.io.BufferedWriter)4 InputStream (java.io.InputStream)4 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)4 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)4