Search in sources :

Example 26 with BasicFileAttributeView

use of java.nio.file.attribute.BasicFileAttributeView in project knime-core by knime.

the class FileUtil method touchTempFiles.

/**
 * Called on regular basis to update temp files according to {@link #UPDATE_TEMP_FILES_FREQUENCY_HOURS}.
 */
private static void touchTempFiles() {
    LOGGER.debug("Updating the last access time of created temp files to prevent deletion by the OS.");
    final Set<Path> paths = new HashSet<>(TEMP_FILES.size());
    synchronized (TEMP_FILES) {
        // clean TEMP_FILES map, i.e., remove stale entries
        TEMP_FILES.keySet().removeIf(Predicate.not(File::exists));
        // add all TEMP_FILES and their children to the paths set
        for (final File f : TEMP_FILES.keySet()) {
            try (final Stream<Path> stream = Files.walk(f.toPath())) {
                stream.forEach(paths::add);
            } catch (IOException ex) {
                LOGGER.debug(String.format("Error when attempting to collect temp files (children of \"%s\").", f.getAbsolutePath()), ex);
            }
        }
    }
    // attempt to touch all paths in the paths set
    final FileTime now = FileTime.from(ZonedDateTime.now().toInstant());
    long successCounter = 0;
    long failCounter = 0;
    boolean warningLogged = false;
    for (final Path p : paths) {
        try {
            BasicFileAttributeView attView = Files.getFileAttributeView(p, BasicFileAttributeView.class);
            if (attView == null) {
                if (!warningLogged) {
                    // NOSONAR -- 3 nested leves, still readable, eh
                    LOGGER.warnWithFormat("Unable to set file attributes on temp file %s, " + "file system not supporting %s", p, BasicFileAttributeView.class.getName());
                    warningLogged = true;
                    failCounter += 1;
                }
            } else {
                // Mac looks at the file's 'lastAccessTime' (not 'lastModifiedTime')
                attView.setTimes(null, now, null);
                successCounter += 1;
            }
        } catch (IOException ex) {
            LOGGER.debug(String.format("Error when attempting to touch temp file \"%s\"", p), ex);
        }
    }
    LOGGER.debugWithFormat("Finished updating 'lastAccessTime' attribute of temp files (%d succeeded, %d failed)", successCounter, failCounter);
}
Also used : Path(java.nio.file.Path) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) FileTime(java.nio.file.attribute.FileTime) IOException(java.io.IOException) File(java.io.File) HashSet(java.util.HashSet)

Example 27 with BasicFileAttributeView

use of java.nio.file.attribute.BasicFileAttributeView in project sonarqube by SonarSource.

the class GlobalTempFolderProviderTest method setFileCreationDate.

private void setFileCreationDate(File f, long time) throws IOException {
    BasicFileAttributeView attributes = Files.getFileAttributeView(f.toPath(), BasicFileAttributeView.class);
    FileTime creationTime = FileTime.fromMillis(time);
    attributes.setTimes(creationTime, creationTime, creationTime);
}
Also used : BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) FileTime(java.nio.file.attribute.FileTime)

Example 28 with BasicFileAttributeView

use of java.nio.file.attribute.BasicFileAttributeView in project judge by zjnu-acm.

the class CopyHelper method preVisitDirectory.

@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
    Path resolve = resolve(dir);
    if (!Files.exists(resolve)) {
        Files.createDirectories(resolve);
        BasicFileAttributeView view = Files.getFileAttributeView(resolve, BasicFileAttributeView.class);
        view.setTimes(attrs.lastModifiedTime(), attrs.lastAccessTime(), attrs.creationTime());
    }
    return FileVisitResult.CONTINUE;
}
Also used : Path(java.nio.file.Path) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView)

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