/** * Like the unix command of the same name, creates an empty file or updates the last modified * timestamp of the existing file at the given path to the current system time. */ @SuppressWarnings("GoodTime") // reading system time without TimeSource public static void touch(Path path) throws IOException { checkNotNull(path); try { Files.setLastModifiedTime(path, FileTime.fromMillis(System.currentTimeMillis())); } catch (NoSuchFileException e) { try { Files.createFile(path); } catch (FileAlreadyExistsException ignore) { // The file didn't exist when we called setLastModifiedTime, but it did when we called // createFile, so something else created the file in between. The end result is // what we wanted: a new file that probably has its last modified time set to approximately // now. Or it could have an arbitrary last modified time set by the creator, but that's no // different than if another process set its last modified time to something else after we // created it here. } } }
/** * Like the unix command of the same name, creates an empty file or updates the last modified * timestamp of the existing file at the given path to the current system time. */ public static void touch(Path path) throws IOException { checkNotNull(path); try { Files.setLastModifiedTime(path, FileTime.fromMillis(System.currentTimeMillis())); } catch (NoSuchFileException e) { try { Files.createFile(path); } catch (FileAlreadyExistsException ignore) { // The file didn't exist when we called setLastModifiedTime, but it did when we called // createFile, so something else created the file in between. The end result is // what we wanted: a new file that probably has its last modified time set to approximately // now. Or it could have an arbitrary last modified time set by the creator, but that's no // different than if another process set its last modified time to something else after we // created it here. } } }
/** * Like the unix command of the same name, creates an empty file or updates the last modified * timestamp of the existing file at the given path to the current system time. */ public static void touch(Path path) throws IOException { checkNotNull(path); try { Files.setLastModifiedTime(path, FileTime.fromMillis(System.currentTimeMillis())); } catch (NoSuchFileException e) { try { Files.createFile(path); } catch (FileAlreadyExistsException ignore) { // The file didn't exist when we called setLastModifiedTime, but it did when we called // createFile, so something else created the file in between. The end result is // what we wanted: a new file that probably has its last modified time set to approximately // now. Or it could have an arbitrary last modified time set by the creator, but that's no // different than if another process set its last modified time to something else after we // created it here. } } }
@Override public FileTime creationTime() { if (info.getUpdateTime() == null) { return CloudStorageFileSystem.FILE_TIME_UNKNOWN; } return FileTime.fromMillis(info.getUpdateTime()); }
private void updateLastModified(final Path... paths) throws IOException { for (final Path path : paths) { Files.setLastModifiedTime(path, FileTime.fromMillis(System.currentTimeMillis() + 2000)); } }
protected Attributes(File file) { this.lastModifiedTime = FileTime.fromMillis(file.getLastModifiedTime()); this.lastAccessTime = FileTime.fromMillis(file.getLastAccessTime()); this.creationTime = FileTime.fromMillis(file.getCreationTime()); this.regularFile = file.isRegularFile(); this.directory = file.isDirectory(); this.symbolicLink = file.isSymbolicLink(); this.size = file.size(); this.fileKey = file.id(); }
private void updateLastModified(final Path... paths) throws IOException { for (final Path path : paths) { Files.setLastModifiedTime(path, FileTime.fromMillis(System.currentTimeMillis() + 2000)); } }
private void updateLastModified(final Path... paths) throws IOException { for (final Path path : paths) { Files.setLastModifiedTime(path, FileTime.fromMillis(System.currentTimeMillis() + 2000)); } }
protected void setLastModified(FileVersion reconstructedFileVersion, File reconstructedFilesAtFinalLocation) { // Using Files.setLastModifiedTime() instead of File.setLastModified() // due to pre-1970 issue. See #374 for details. try { FileTime newLastModifiedTime = FileTime.fromMillis(reconstructedFileVersion.getLastModified().getTime()); Files.setLastModifiedTime(reconstructedFilesAtFinalLocation.toPath(), newLastModifiedTime); } catch (IOException e) { logger.log(Level.WARNING, "Warning: Could not set last modified date for file " + reconstructedFilesAtFinalLocation + "; Ignoring error.", e); } }
public void testTouchTime() throws IOException { Path temp = createTempFile(); assertTrue(Files.exists(temp)); Files.setLastModifiedTime(temp, FileTime.fromMillis(0)); assertEquals(0, Files.getLastModifiedTime(temp).toMillis()); MoreFiles.touch(temp); assertThat(Files.getLastModifiedTime(temp).toMillis()).isNotEqualTo(0); }
@Before public void setUp() throws Exception { base = Files.createTempDirectory("tempDir", new FileAttribute<?>[0]); aaa = Files.createTempFile(base, "aaa", null, new FileAttribute<?>[0]); bbb = Files.createTempFile(base, "bbb", null, new FileAttribute<?>[0]); ccc = Files.createTempFile(base, "ccc", null, new FileAttribute<?>[0]); // lastModified granularity is 1 sec(!) on some file systems... final long now = System.currentTimeMillis(); Files.setLastModifiedTime(aaa, FileTime.fromMillis(now)); Files.setLastModifiedTime(bbb, FileTime.fromMillis(now + 1000)); Files.setLastModifiedTime(ccc, FileTime.fromMillis(now + 2000)); }
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); } }
@Test public void getNextGamePreviewImagePathOldestFileTest() throws IOException, InterruptedException { Files.createDirectories(TMP_PREVIEWS_FOLDER); Files.createFile(TMP_PREVIEWS_FOLDER.resolve("1.jpg")); Files.createFile(TMP_PREVIEWS_FOLDER.resolve("2.jpg")); Files.createFile(TMP_PREVIEWS_FOLDER.resolve("3.jpg")); Files.createFile(TMP_PREVIEWS_FOLDER.resolve("4.jpg")); Files.createFile(TMP_PREVIEWS_FOLDER.resolve("5.jpg")); final Path expectedOldestFile = TMP_PREVIEWS_FOLDER.resolve("3.jpg"); Files.setLastModifiedTime(expectedOldestFile, FileTime.fromMillis(0)); final Path imagePath = GamePreviewImageProvider.getNextGamePreviewImagePath(TMP_FOLDER); Assert.assertNotNull(imagePath); Assert.assertEquals(expectedOldestFile, imagePath); } }
@Test public void testAcceptsIfFileAgeEqualToDuration() { final IfLastModified filter = IfLastModified.createAgeCondition(Duration.parse("PT33S")); final DummyFileAttributes attrs = new DummyFileAttributes(); final long age = 33 * 1000; attrs.lastModified = FileTime.fromMillis(System.currentTimeMillis() - age); assertTrue(filter.accept(null, null, attrs)); }
@Test public void testDoesNotAcceptIfFileAgeLessThanDuration() { final IfLastModified filter = IfLastModified.createAgeCondition(Duration.parse("PT33S")); final DummyFileAttributes attrs = new DummyFileAttributes(); final long age = 33 * 1000 - 5; attrs.lastModified = FileTime.fromMillis(System.currentTimeMillis() - age); assertFalse(filter.accept(null, null, attrs)); }
@Test public void testAcceptsIfFileAgeExceedsDuration() { final IfLastModified filter = IfLastModified.createAgeCondition(Duration.parse("PT33S")); final DummyFileAttributes attrs = new DummyFileAttributes(); final long age = 33 * 1000 + 5; attrs.lastModified = FileTime.fromMillis(System.currentTimeMillis() - age); assertTrue(filter.accept(null, null, attrs)); }
@Test public void testNullness() throws NoSuchMethodException, SecurityException { new NullPointerTester() .ignore(CloudStorageFileAttributeView.class.getMethod("equals", Object.class)) .setDefault(FileTime.class, FileTime.fromMillis(0)) .testAllPublicInstanceMethods( Files.getFileAttributeView(path, CloudStorageFileAttributeView.class)); } }
@Test public void testAcceptCallsNestedConditionsOnlyIfPathAccepted() { final CountingCondition counter = new CountingCondition(true); final IfLastModified filter = IfLastModified.createAgeCondition(Duration.parse("PT33S"), counter); final DummyFileAttributes attrs = new DummyFileAttributes(); final long oldEnough = 33 * 1000 + 5; attrs.lastModified = FileTime.fromMillis(System.currentTimeMillis() - oldEnough); assertTrue(filter.accept(null, null, attrs)); assertEquals(1, counter.getAcceptCount()); assertTrue(filter.accept(null, null, attrs)); assertEquals(2, counter.getAcceptCount()); assertTrue(filter.accept(null, null, attrs)); assertEquals(3, counter.getAcceptCount()); final long tooYoung = 33 * 1000 - 5; attrs.lastModified = FileTime.fromMillis(System.currentTimeMillis() - tooYoung); assertFalse(filter.accept(null, null, attrs)); assertEquals(3, counter.getAcceptCount()); // no increase assertFalse(filter.accept(null, null, attrs)); assertEquals(3, counter.getAcceptCount()); assertFalse(filter.accept(null, null, attrs)); assertEquals(3, counter.getAcceptCount()); }
@Test public void testCompareRecentFirst() { final PathSorter sorter = PathSortByModificationTime.createSorter(true); final Path p1 = Paths.get("aaa"); final Path p2 = Paths.get("bbb"); final DummyFileAttributes a1 = new DummyFileAttributes(); final DummyFileAttributes a2 = new DummyFileAttributes(); a1.lastModified = FileTime.fromMillis(100); a2.lastModified = FileTime.fromMillis(222); assertEquals("same path, 2nd more recent", 1, sorter.compare(path(p1, a1), path(p1, a2))); assertEquals("path ignored, 2nd more recent", 1, sorter.compare(path(p1, a1), path(p2, a2))); assertEquals("path ignored, 2nd more recent", 1, sorter.compare(path(p2, a1), path(p1, a2))); assertEquals("same path, 1st more recent", -1, sorter.compare(path(p1, a2), path(p1, a1))); assertEquals("path ignored, 1st more recent", -1, sorter.compare(path(p1, a2), path(p2, a1))); assertEquals("path ignored, 1st more recent", -1, sorter.compare(path(p2, a2), path(p1, a1))); assertEquals("same path, same time", 0, sorter.compare(path(p1, a1), path(p1, a1))); assertEquals("p2 < p1, same time", 1, sorter.compare(path(p1, a1), path(p2, a1))); assertEquals("p2 < p1, same time", -1, sorter.compare(path(p2, a1), path(p1, a1))); }
@Test public void testCompareRecentLast() { final PathSorter sorter = PathSortByModificationTime.createSorter(false); final Path p1 = Paths.get("aaa"); final Path p2 = Paths.get("bbb"); final DummyFileAttributes a1 = new DummyFileAttributes(); final DummyFileAttributes a2 = new DummyFileAttributes(); a1.lastModified = FileTime.fromMillis(100); a2.lastModified = FileTime.fromMillis(222); assertEquals("same path, 2nd more recent", -1, sorter.compare(path(p1, a1), path(p1, a2))); assertEquals("path ignored, 2nd more recent", -1, sorter.compare(path(p1, a1), path(p2, a2))); assertEquals("path ignored, 2nd more recent", -1, sorter.compare(path(p2, a1), path(p1, a2))); assertEquals("same path, 1st more recent", 1, sorter.compare(path(p1, a2), path(p1, a1))); assertEquals("path ignored, 1st more recent", 1, sorter.compare(path(p1, a2), path(p2, a1))); assertEquals("path ignored, 1st more recent", 1, sorter.compare(path(p2, a2), path(p1, a1))); assertEquals("same path, same time", 0, sorter.compare(path(p1, a1), path(p1, a1))); assertEquals("p1 < p2, same time", -1, sorter.compare(path(p1, a1), path(p2, a1))); assertEquals("p1 < p2, same time", 1, sorter.compare(path(p2, a1), path(p1, a1))); }