Search in sources :

Example 16 with BasicFileAttributeView

use of java.nio.file.attribute.BasicFileAttributeView in project packr by libgdx.

the class ArchiveUtils method setLastModifiedTime.

private static void setLastModifiedTime(Path path, FileTime lastModifiedTime) throws IOException {
    if (Files.isSymbolicLink(path)) {
        return;
    }
    BasicFileAttributeView pathAttributeView = Files.getFileAttributeView(path, BasicFileAttributeView.class, NOFOLLOW_LINKS);
    final BasicFileAttributes fileAttributes = pathAttributeView.readAttributes();
    pathAttributeView.setTimes(lastModifiedTime, fileAttributes.lastAccessTime(), fileAttributes.creationTime());
}
Also used : BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 17 with BasicFileAttributeView

use of java.nio.file.attribute.BasicFileAttributeView in project sakuli by ConSol.

the class LogCleanUpServiceImplTest method createFileWithDate.

private Path createFileWithDate(Path path, FileTime todayMinus15) throws IOException {
    Path file = Files.createFile(path);
    BasicFileAttributeView fileAttributeView = Files.getFileAttributeView(file, BasicFileAttributeView.class);
    fileAttributeView.setTimes(todayMinus15, todayMinus15, todayMinus15);
    return file;
}
Also used : Path(java.nio.file.Path) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView)

Example 18 with BasicFileAttributeView

use of java.nio.file.attribute.BasicFileAttributeView in project mycore by MyCoRe-Org.

the class MCRPathXML method getDirectoryXML.

/**
 * Sends the contents of an MCRDirectory as XML data to the client
 */
public static Document getDirectoryXML(MCRPath path, BasicFileAttributes attr) throws IOException {
    LOGGER.debug("MCRDirectoryXML: start listing of directory {}", path);
    Element root = new Element("mcr_directory");
    Document doc = new org.jdom2.Document(root);
    addString(root, "uri", path.toUri().toString(), false);
    addString(root, "ownerID", path.getOwner(), false);
    MCRPath relativePath = path.getRoot().relativize(path);
    boolean isRoot = relativePath.toString().isEmpty();
    addString(root, "name", (isRoot ? "" : relativePath.getFileName().toString()), true);
    addString(root, "path", toStringValue(relativePath), true);
    if (!isRoot) {
        addString(root, "parentPath", toStringValue(relativePath.getParent()), true);
    }
    addBasicAttributes(root, attr, path);
    Element numChildren = new Element("numChildren");
    Element here = new Element("here");
    root.addContent(numChildren);
    numChildren.addContent(here);
    Element nodes = new Element("children");
    root.addContent(nodes);
    SortedMap<MCRPath, MCRFileAttributes<?>> directories = new TreeMap<>();
    SortedMap<MCRPath, MCRFileAttributes<?>> files = new TreeMap<>();
    try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(path)) {
        LOGGER.debug(() -> "Opened DirectoryStream for " + path);
        Function<Path, MCRFileAttributes<?>> attrResolver = p -> {
            try {
                return Files.readAttributes(p, MCRFileAttributes.class);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        };
        if (dirStream instanceof SecureDirectoryStream) {
            // fast path
            LOGGER.debug(() -> "Using SecureDirectoryStream code path for " + path);
            attrResolver = p -> {
                try {
                    BasicFileAttributeView attributeView = ((SecureDirectoryStream<Path>) dirStream).getFileAttributeView(p.getFileName(), BasicFileAttributeView.class);
                    return (MCRFileAttributes<?>) attributeView.readAttributes();
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            };
        }
        for (Path child : dirStream) {
            MCRFileAttributes<?> childAttrs;
            try {
                childAttrs = attrResolver.apply(child);
            } catch (UncheckedIOException e) {
                throw e.getCause();
            }
            if (childAttrs.isDirectory()) {
                directories.put(MCRPath.toMCRPath(child), childAttrs);
            } else {
                files.put(MCRPath.toMCRPath(child), childAttrs);
            }
        }
    }
    // store current directory statistics
    addString(here, "directories", Integer.toString(directories.size()), false);
    addString(here, "files", Integer.toString(files.size()), false);
    for (Map.Entry<MCRPath, MCRFileAttributes<?>> dirEntry : directories.entrySet()) {
        Element child = new Element("child");
        child.setAttribute("type", "directory");
        addString(child, "name", dirEntry.getKey().getFileName().toString(), true);
        addString(child, "uri", dirEntry.getKey().toUri().toString(), false);
        nodes.addContent(child);
        addBasicAttributes(child, dirEntry.getValue(), dirEntry.getKey());
    }
    for (Map.Entry<MCRPath, MCRFileAttributes<?>> fileEntry : files.entrySet()) {
        Element child = new Element("child");
        child.setAttribute("type", "file");
        addString(child, "name", fileEntry.getKey().getFileName().toString(), true);
        addString(child, "uri", fileEntry.getKey().toUri().toString(), false);
        nodes.addContent(child);
        addAttributes(child, fileEntry.getValue(), fileEntry.getKey());
    }
    LOGGER.debug("MCRDirectoryXML: end listing of directory {}", path);
    return doc;
}
Also used : Path(java.nio.file.Path) MCRConstants(org.mycore.common.MCRConstants) FileTime(java.nio.file.attribute.FileTime) Function(java.util.function.Function) SEPARATOR_STRING(org.mycore.datamodel.niofs.MCRAbstractFileSystem.SEPARATOR_STRING) Document(org.jdom2.Document) DirectoryStream(java.nio.file.DirectoryStream) MCRCategLinkServiceFactory(org.mycore.datamodel.classifications2.MCRCategLinkServiceFactory) SecureDirectoryStream(java.nio.file.SecureDirectoryStream) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) Map(java.util.Map) Path(java.nio.file.Path) MCRCategLinkReference(org.mycore.datamodel.classifications2.MCRCategLinkReference) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) Files(java.nio.file.Files) Collection(java.util.Collection) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) IOException(java.io.IOException) SEPARATOR(org.mycore.datamodel.niofs.MCRAbstractFileSystem.SEPARATOR) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) UncheckedIOException(java.io.UncheckedIOException) TimeUnit(java.util.concurrent.TimeUnit) Logger(org.apache.logging.log4j.Logger) TreeMap(java.util.TreeMap) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) LogManager(org.apache.logging.log4j.LogManager) SortedMap(java.util.SortedMap) Element(org.jdom2.Element) Element(org.jdom2.Element) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Document(org.jdom2.Document) TreeMap(java.util.TreeMap) SecureDirectoryStream(java.nio.file.SecureDirectoryStream) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Example 19 with BasicFileAttributeView

use of java.nio.file.attribute.BasicFileAttributeView in project mycore by MyCoRe-Org.

the class MCRFileSystemProvider method copy.

/* (non-Javadoc)
     * @see java.nio.file.spi.FileSystemProvider#copy(java.nio.file.Path, java.nio.file.Path, java.nio.file.CopyOption[])
     */
@Override
public void copy(Path source, Path target, CopyOption... options) throws IOException {
    if (isSameFile(source, target)) {
        // that was easy
        return;
    }
    checkCopyOptions(options);
    HashSet<CopyOption> copyOptions = Sets.newHashSet(options);
    boolean createNew = !copyOptions.contains(StandardCopyOption.REPLACE_EXISTING);
    MCRPath src = MCRFileSystemUtils.checkPathAbsolute(source);
    MCRPath tgt = MCRFileSystemUtils.checkPathAbsolute(target);
    MCRFilesystemNode srcNode = resolvePath(src);
    // checkParent of target;
    if (tgt.getNameCount() == 0 && srcNode instanceof MCRDirectory) {
        MCRDirectory tgtDir = MCRDirectory.getRootDirectory(tgt.getOwner());
        if (tgtDir != null) {
            if (tgtDir.hasChildren() && copyOptions.contains(StandardCopyOption.REPLACE_EXISTING)) {
                throw new DirectoryNotEmptyException(tgt.toString());
            }
        } else {
            // TODO: handle StandardCopyOption.COPY_ATTRIBUTES
            tgtDir = new MCRDirectory(tgt.getOwner());
        }
        // created new root component
        return;
    }
    MCRDirectory tgtParentDir = (MCRDirectory) resolvePath(tgt.getParent());
    if (srcNode instanceof MCRFile) {
        MCRFile srcFile = (MCRFile) srcNode;
        MCRFile targetFile = MCRFileSystemUtils.getMCRFile(tgt, true, createNew);
        targetFile.setContentFrom(srcFile.getContentAsInputStream());
        if (copyOptions.contains(StandardCopyOption.COPY_ATTRIBUTES)) {
            @SuppressWarnings("unchecked") MCRMD5AttributeView<String> srcAttrView = Files.getFileAttributeView(src, MCRMD5AttributeView.class, (LinkOption[]) null);
            File targetLocalFile = targetFile.getLocalFile();
            BasicFileAttributeView targetBasicFileAttributeView = Files.getFileAttributeView(targetLocalFile.toPath(), BasicFileAttributeView.class);
            MCRFileAttributes<String> srcAttr = srcAttrView.readAllAttributes();
            targetFile.adjustMetadata(srcAttr.lastModifiedTime(), srcFile.getMD5(), srcFile.getSize());
            targetBasicFileAttributeView.setTimes(srcAttr.lastModifiedTime(), srcAttr.lastAccessTime(), srcAttr.creationTime());
        }
    } else if (srcNode instanceof MCRDirectory) {
        MCRFilesystemNode child = tgtParentDir.getChild(tgt.getFileName().toString());
        if (child != null) {
            if (!copyOptions.contains(StandardCopyOption.REPLACE_EXISTING)) {
                throw new FileAlreadyExistsException(tgtParentDir.toString(), tgt.getFileName().toString(), null);
            }
            if (child instanceof MCRFile) {
                throw new NotDirectoryException(tgt.toString());
            }
            MCRDirectory tgtDir = (MCRDirectory) child;
            if (tgtDir.hasChildren() && copyOptions.contains(StandardCopyOption.REPLACE_EXISTING)) {
                throw new DirectoryNotEmptyException(tgt.toString());
            }
        // TODO: handle StandardCopyOption.COPY_ATTRIBUTES
        } else {
            // simply create directory
            @SuppressWarnings("unused") MCRDirectory tgtDir = new MCRDirectory(tgt.getFileName().toString(), tgtParentDir);
        // TODO: handle StandardCopyOption.COPY_ATTRIBUTES
        }
    }
}
Also used : MCRFile(org.mycore.datamodel.ifs.MCRFile) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) CopyOption(java.nio.file.CopyOption) StandardCopyOption(java.nio.file.StandardCopyOption) MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) LinkOption(java.nio.file.LinkOption) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) NotDirectoryException(java.nio.file.NotDirectoryException) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRFile(org.mycore.datamodel.ifs.MCRFile) File(java.io.File)

Example 20 with BasicFileAttributeView

use of java.nio.file.attribute.BasicFileAttributeView in project Malai by arnobl.

the class ShowFileInformation method doActionBody.

// The operation that executes the action.
@Override
protected void doActionBody() {
    if (path == null)
        textArea.setText("");
    else {
        // Showing some properties of the selected file.
        final BasicFileAttributeView attrV = Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
        String txt = path.toString();
        if (attrV != null) {
            try {
                BasicFileAttributes attr = attrV.readAttributes();
                txt += "\n" + attr.size() + " bytes";
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        textArea.setText(txt);
    }
}
Also used : BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)28 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)13 Path (java.nio.file.Path)11 FileTime (java.nio.file.attribute.FileTime)11 IOException (java.io.IOException)7 Test (org.junit.Test)7 File (java.io.File)3 NoSuchFileException (java.nio.file.NoSuchFileException)3 FileOwnerAttributeView (java.nio.file.attribute.FileOwnerAttributeView)3 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)3 InputStream (java.io.InputStream)2 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)2 FileStore (java.nio.file.FileStore)2 SecureDirectoryStream (java.nio.file.SecureDirectoryStream)2 StandardCopyOption (java.nio.file.StandardCopyOption)2 DateFormat (java.text.DateFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2