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);
}
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);
}
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;
}
Aggregations