Example usage for java.nio.file Files getPosixFilePermissions

List of usage examples for java.nio.file Files getPosixFilePermissions

Introduction

In this page you can find the example usage for java.nio.file Files getPosixFilePermissions.

Prototype

public static Set<PosixFilePermission> getPosixFilePermissions(Path path, LinkOption... options)
        throws IOException 

Source Link

Document

Returns a file's POSIX file permissions.

Usage

From source file:de.jwi.jfm.FileWrapper.java

public String getAttributes() throws IOException {
    FileSystem fileSystem = FileSystems.getDefault();
    Set<String> fileSystemViews = fileSystem.supportedFileAttributeViews();

    File file = getFile();//w  ww.  j  a va 2s  .com
    Path p = file.toPath();

    try {
        if (fileSystemViews.contains("posix")) {
            Set<PosixFilePermission> posixFilePermissions = Files.getPosixFilePermissions(p,
                    LinkOption.NOFOLLOW_LINKS);

            PosixFileAttributes attrs = Files.getFileAttributeView(p, PosixFileAttributeView.class)
                    .readAttributes();

            String owner = attrs.owner().toString();
            String group = attrs.group().toString();

            String permissions = PosixFilePermissions.toString(attrs.permissions());

            String res = String.format("%s %s %s", permissions, owner, group);
            return res;
        } else if (fileSystemViews.contains("dos")) {
            StringWriter sw = new StringWriter();
            DosFileAttributeView attributeView = Files.getFileAttributeView(p, DosFileAttributeView.class);
            DosFileAttributes dosFileAttributes = null;

            dosFileAttributes = attributeView.readAttributes();
            if (dosFileAttributes.isArchive()) {
                sw.append('A');
            }
            if (dosFileAttributes.isHidden()) {
                sw.append('H');
            }
            if (dosFileAttributes.isReadOnly()) {
                sw.append('R');
            }
            if (dosFileAttributes.isSystem()) {
                sw.append('S');
            }

            return sw.toString();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return "";
}

From source file:net.sourceforge.jencrypt.FileEncrypter.java

/**
 * Encrypt file [inputFile] relative to [pathToEncrypt] into the archive
 * specified by [outputFile]./*from  w  ww.j  a  v  a 2  s .c  om*/
 * 
 * @param pathToEncrypt
 * @param inputFile
 * @param outputFile
 * @throws Exception
 */
private void encryptAndArchivePath(String folderToEncryptString, String inputFileRelativePath, OutputStream os)
        throws Exception {

    // Full path to file to encrypt
    String fullSourcePath = folderToEncryptString + File.separator + inputFileRelativePath;

    // Path to encrypt is a folder if it ends with a file separator
    boolean pathIsFolder = (inputFileRelativePath
            .charAt(inputFileRelativePath.length() - 1) == File.separatorChar);

    // Use a Unix separator for storing file paths
    inputFileRelativePath = FilenameUtils.normalize(inputFileRelativePath, true);

    // To indicate a folder add a Unix file separator at the end
    if (pathIsFolder)
        inputFileRelativePath += Utils.UNIX_SEPARATOR;

    File inputFile = new File(fullSourcePath);
    Set<PosixFilePermission> perms = null;

    // Get the "Encrypting [file] ... " message
    String message = getCipherMessage(inputFile.getName(), commandline.getCipherMode());

    // Instantiate ProgressInfo
    ProgressInfo progress = new FileProgressInfo(message, inputFile.length(), config.getReadBufferSize());

    assert (inputFileRelativePath.length() > 0);

    byte[] pathLength = ByteBuffer.allocate(4).putInt(inputFileRelativePath.length()).array();
    byte[] pathLengthEnc = cryptoWrapperFileNamesAndSizes.cipherBytes(pathLength, Cipher.ENCRYPT_MODE);
    // write 4 bytes path size
    os.write(pathLengthEnc);

    byte[] fullPath = inputFileRelativePath.getBytes();
    byte[] fullPathEnc = cryptoWrapperFileNamesAndSizes.cipherBytes(fullPath, Cipher.ENCRYPT_MODE);
    // write full path of encrypted file
    os.write(fullPathEnc);

    // Retain file permissions on Unix flavours
    if (fileSystemIsPosixCompliant()) {
        perms = Files.getPosixFilePermissions(Paths.get(fullSourcePath), LinkOption.NOFOLLOW_LINKS);
    } else {
        /*
         * On non-POSIX file systems use default Unix (umask 022) file and
         * directory permissions
         */
        if (pathIsFolder)
            perms = PosixFilePermissions.fromString("rwxr-xr-x");
        else
            perms = PosixFilePermissions.fromString("rw-r--r--");
    }

    byte[] permBytes = Utils.permsToByte(perms);
    byte[] permBytesEnc = cryptoWrapperFileNamesAndSizes.cipherBytes(permBytes, Cipher.ENCRYPT_MODE);
    // write 2 bytes with the file's permissions
    os.write(permBytesEnc);

    // Start file encryption if the supplied relative path is not a folder
    if (!pathIsFolder) {

        long l = inputFile.length();
        byte[] fileSize = ByteBuffer.allocate(8).putLong(l).array();
        byte[] fileSizeEnc = cryptoWrapperFileNamesAndSizes.cipherBytes(fileSize, Cipher.ENCRYPT_MODE);
        // write 8 bytes filesize
        os.write(fileSizeEnc);

        cryptoWrapperFileContent.doCipherOperation(getInputFileStream(fullSourcePath), os,
                new File(fullSourcePath).length(), Cipher.ENCRYPT_MODE, progress);
    }
}

From source file:algorithm.MetsSubmissionInformationPackage.java

@SuppressWarnings("unchecked")
@Override//from   w  w  w  .ja v  a2s .c o m
public File encapsulate(File carrier, List<File> userPayloadList) throws IOException {
    List<File> payloadList = new ArrayList<File>();
    payloadList.addAll(userPayloadList);
    Mets metsDocument = new Mets();
    metsDocument.setID(carrier.getName() + "_SIP");
    metsDocument.setLABEL("PeriCAT information package");
    metsDocument.setTYPE("digital object and metadata files");
    // HEADER:
    Agent periPack = new Agent();
    periPack.setID("peripack agent");
    periPack.setROLE(Role.PRESERVATION);
    periPack.setOTHERTYPE("SOFTWARE");
    Name name = new Name();
    PCData nameString = new PCData();
    nameString.add("peripack");
    name.getContent().add(nameString);
    periPack.getContent().add(name);
    Note note = new Note();
    PCData noteString = new PCData();
    noteString.add(TOOL_DESCRIPTION);
    note.getContent().add(noteString);
    periPack.getContent().add(note);
    MetsHdr header = new MetsHdr();
    header.setID("mets header");
    header.setCREATEDATE(new Date());
    header.setLASTMODDATE(new Date());
    header.setRECORDSTATUS("complete");
    header.getContent().add(periPack);
    if (truePersonButton.isSelected()) {
        Agent person = new Agent();
        person.setID("peripack user");
        person.setROLE(Role.CREATOR);
        person.setTYPE(Type.INDIVIDUAL);
        Name personName = new Name();
        PCData personNameString = new PCData();
        personNameString.add(personField.getText());
        personName.getContent().add(personNameString);
        person.getContent().add(personName);
        header.getContent().add(person);
    }
    metsDocument.getContent().add(header);
    // FILE SECTION:
    FileSec fileSection = new FileSec();
    FileGrp carrierGroup = new FileGrp();
    carrierGroup.setID("carrier files");
    carrierGroup.setUSE("digital object");
    // carrier div for structural map:
    Div carrierDiv = new Div();
    carrierDiv.setID("carrier list");
    carrierDiv.setTYPE("carrier files");
    // back to file section:
    edu.harvard.hul.ois.mets.File metsCarrier = new edu.harvard.hul.ois.mets.File();
    metsCarrier.setID(carrier.getAbsolutePath());
    metsCarrier.setGROUPID("carrier");
    metsCarrier.setCHECKSUM("" + FileUtils.checksumCRC32(carrier));
    metsCarrier.setMIMETYPE(Files.probeContentType(carrier.toPath()));
    metsCarrier.setOWNERID(Files.getOwner(carrier.toPath(), LinkOption.NOFOLLOW_LINKS).toString());
    metsCarrier.setSIZE(Files.size(carrier.toPath()));
    metsCarrier.setUSE(Files.getPosixFilePermissions(carrier.toPath(), LinkOption.NOFOLLOW_LINKS).toString());
    FLocat fileLocation = new FLocat();
    fileLocation.setXlinkHref(carrier.getAbsolutePath());
    fileLocation.setLOCTYPE(Loctype.OTHER);
    fileLocation.setOTHERLOCTYPE("system file path");
    fileLocation.setXlinkTitle("original file path of the carrier");
    metsCarrier.getContent().add(fileLocation);
    carrierGroup.getContent().add(metsCarrier);
    // add structural map information:
    Fptr carrierFilePointer = new Fptr();
    carrierFilePointer.setFILEID(carrier.getAbsolutePath());
    carrierDiv.getContent().add(carrierFilePointer);
    fileSection.getContent().add(carrierGroup);
    FileGrp payloadGroup = new FileGrp();
    payloadGroup.setID("payload files");
    payloadGroup.setUSE("metadata");

    // payload div for structural map:
    Div payloadDiv = new Div();
    payloadDiv.setID("payload list");
    payloadDiv.setTYPE("payload files");
    // back to file section:
    for (File payload : payloadList) {
        edu.harvard.hul.ois.mets.File metsPayload = new edu.harvard.hul.ois.mets.File();
        metsPayload.setID(payload.getAbsolutePath());
        metsPayload.setGROUPID("payload");
        metsPayload.setCHECKSUM(DigestUtils.md5Hex(new FileInputStream(payload)));
        metsPayload.setCHECKSUMTYPE(Checksumtype.MD5);
        metsPayload.setMIMETYPE(Files.probeContentType(payload.toPath()));
        metsPayload.setOWNERID(Files.getOwner(payload.toPath(), LinkOption.NOFOLLOW_LINKS).toString());
        metsPayload.setSIZE(Files.size(payload.toPath()));
        metsPayload
                .setUSE(Files.getPosixFilePermissions(payload.toPath(), LinkOption.NOFOLLOW_LINKS).toString());
        FLocat fileLocation2 = new FLocat();
        fileLocation2.setXlinkHref(payload.getAbsolutePath());
        fileLocation2.setLOCTYPE(Loctype.OTHER);
        fileLocation2.setOTHERLOCTYPE("system file path");
        fileLocation2.setXlinkTitle("original file path of the payload");
        metsPayload.getContent().add(fileLocation2);
        payloadGroup.getContent().add(metsPayload);
        // add structural map information:
        Fptr payloadFilePointer = new Fptr();
        payloadFilePointer.setFILEID(payload.getAbsolutePath());
        payloadDiv.getContent().add(payloadFilePointer);
    }
    fileSection.getContent().add(payloadGroup);
    metsDocument.getContent().add(fileSection);
    // STRUCTURAL MAP:
    StructMap structuralMap = new StructMap();
    structuralMap.setID("structural map");
    Div encapsulatedFiles = new Div();
    encapsulatedFiles.setID("peripack files");
    encapsulatedFiles.setTYPE("encapsulated files");
    structuralMap.getContent().add(encapsulatedFiles);
    encapsulatedFiles.getContent().add(carrierDiv);
    encapsulatedFiles.getContent().add(payloadDiv);
    metsDocument.getContent().add(structuralMap);
    File metsFile = new File(OUTPUT_DIRECTORY + "mets.xml");
    FileOutputStream outputStream = new FileOutputStream(metsFile);
    try {
        metsDocument.write(new MetsWriter(outputStream));
    } catch (MetsException e) {
    }
    outputStream.close();
    payloadList.add(metsFile);
    File outputFile = null;
    if (zipButton.isSelected()) {
        outputFile = new ZipPackaging().encapsulate(carrier, payloadList);

    } else if (tarButton.isSelected()) {
        outputFile = new TarPackaging().encapsulate(carrier, payloadList);
    }
    metsFile.delete();
    return outputFile;
}