Java Code Examples for java.nio.file.attribute.BasicFileAttributes#lastModifiedTime()

The following examples show how to use java.nio.file.attribute.BasicFileAttributes#lastModifiedTime() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
public boolean hasNext() {

      while (true) {
        if (!dirStreamIterator.hasNext()) {
          nextMFile = null;
          return false;
        }

        try {
          Path nextPath = dirStreamIterator.next();
          BasicFileAttributes attr = Files.readAttributes(nextPath, BasicFileAttributes.class);
          if (attr.isDirectory())
            continue; // LOOK fix this

          FileTime last = attr.lastModifiedTime();
          long millisSinceModified = now - last.toMillis();
          if (millisSinceModified < olderThanMillis)
            continue;
          nextMFile = new MFileOS7(nextPath, attr);
          return true;

        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
    }
 
Example 2
/**
 * Create a DirectoryBuilder for the named directory
 * 
 * @param topCollectionName from config, name of the collection
 * @param dir covers this directory
 * @param attr file attributes, may be null
 */
public DirectoryBuilder(String topCollectionName, Path dir, BasicFileAttributes attr, String suffix)
    throws IOException {
  this.topCollectionName = topCollectionName;
  this.dir = dir;
  this.partitionName = DirectoryCollection.makeCollectionName(topCollectionName, dir);
  this.suffix = suffix;

  if (attr == null)
    attr = Files.readAttributes(this.dir, BasicFileAttributes.class);
  if (!attr.isDirectory())
    throw new IllegalArgumentException("DirectoryPartitionBuilder needs a directory");
  dirLastModified = attr.lastModifiedTime();

  // see if we can find the index
  findIndex();
}
 
Example 3
private DirectoryBuilder(String topCollectionName, Path indexFile, long indexLastModified, String suffix)
    throws IOException {
  this.topCollectionName = topCollectionName;
  if (Files.exists(indexFile)) {
    this.index = indexFile;
    this.indexLastModified = FileTime.fromMillis(indexLastModified);
  }

  this.dir = indexFile.getParent();
  this.partitionName = DirectoryCollection.makeCollectionName(topCollectionName, dir);

  BasicFileAttributes attr = Files.readAttributes(this.dir, BasicFileAttributes.class);
  if (!attr.isDirectory())
    throw new IllegalArgumentException("DirectoryPartition needs a directory");
  dirLastModified = attr.lastModifiedTime();

  this.suffix = suffix;
}
 
Example 4
@Override
public GenerationStatisticsBuilder add(String path) {
    Path file = Paths.get(path);
    this.fileCounter++;

    try {
        BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class);
        if (minResultTime == null || minResultTime.toMillis() > attr.lastModifiedTime().toMillis()) {
            minResultTime = attr.lastModifiedTime();
        }

        if (maxResultTime == null || maxResultTime.toMillis() < attr.lastModifiedTime().toMillis()) {
            maxResultTime = attr.lastModifiedTime();
        }
    } catch (IOException e) {
        LOGGER.error("can't read file attributes", e);
    }
    return this;
}
 
Example 5
Source Project: jesterj   File: FileScanner.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * A default, reusable and overridable means of adding file attributes to a document
 *
 * @param attributes The attributes of the scanned file as returned by
 *                   {@link java.nio.file.Files#getFileAttributeView(Path, Class, LinkOption...)}
 * @param doc        The document representing the scanned file to which attributes should be added.
 */
default void addAttrs(BasicFileAttributes attributes, DocumentImpl doc) {
  if (attributes != null) {
    FileTime modifiedTime = attributes.lastModifiedTime();
    FileTime accessTime = attributes.lastAccessTime();
    FileTime creationTime = attributes.creationTime();
    if (modifiedTime != null) {
      doc.put("modified", String.valueOf(modifiedTime.toMillis()));
    }
    if (accessTime != null) {
      doc.put("accessed", String.valueOf(accessTime.toMillis()));
    }
    if (creationTime != null) {
      doc.put("created", String.valueOf(creationTime.toMillis()));
    }
    doc.put("file_size", String.valueOf(attributes.size()));
  }
}
 
Example 6
protected FileTime determineTime(Path path) {
    FileTime result = FileTime.fromMillis(0);
    try {
        BasicFileAttributes attributes = Files.readAttributes(path, BasicFileAttributes.class);
        FileTime creationTime = attributes.creationTime();
        if (creationTime.equals(result)) {
            // file system doesn't support creation time and didn't give modification time automatically
            result = attributes.lastModifiedTime();
        } else {
            result = creationTime;
        }
    } catch (IOException e) {
        // ignore
    }
    return result;
}
 
Example 7
public boolean hasNext() {

      while (true) {
        if (!dirStreamIterator.hasNext()) {
          nextMFile = null;
          return false;
        }

        try {
          Path nextPath = dirStreamIterator.next();
          BasicFileAttributes attr = Files.readAttributes(nextPath, BasicFileAttributes.class);

          if (wantSubdirs && attr.isDirectory()) { // dont filter subdirectories
            subdirs.add(new OneDirIterator(nextPath, subdirs));
            continue;
          }

          if (!matcher.matches(nextPath)) // otherwise apply the filter specified by the specp
            continue;

          if (olderThanMillis > 0) {
            FileTime last = attr.lastModifiedTime();
            long millisSinceModified = now - last.toMillis();
            if (millisSinceModified < olderThanMillis)
              continue;
          }
          nextMFile = new MFileOS7(nextPath, attr);
          return true;

        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
    }
 
Example 8
/**
 * Find the index file, using its canonical name
 * 
 * @return true if found
 */
public boolean findIndex() throws IOException {
  Path indexPath = Paths.get(dir.toString(), partitionName + suffix);
  if (Files.exists(indexPath)) {
    this.index = indexPath;
    BasicFileAttributes attr = Files.readAttributes(indexPath, BasicFileAttributes.class);
    this.indexLastModified = attr.lastModifiedTime();
    this.indexSize = attr.size();
    return true;
  }
  return false;
}
 
Example 9
public boolean hasNext() {
  while (true) {
    // if (debug && count % 100 == 0) System.out.printf("%d ", count);
    count++;
    if (!dirStreamIterator.hasNext()) {
      nextMFile = null;
      return false;
    }

    long now = System.currentTimeMillis();
    try {
      Path nextPath = dirStreamIterator.next();
      BasicFileAttributes attr = Files.readAttributes(nextPath, BasicFileAttributes.class);
      if (attr.isDirectory())
        continue;
      FileTime last = attr.lastModifiedTime();
      long millisSinceModified = now - last.toMillis();
      if (millisSinceModified < olderThanMillis)
        continue;
      nextMFile = new MFileOS7(nextPath, attr);
      return true;

    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
}
 
Example 10
public static FileTime readModifiedTime() throws LoggingUpdaterException {

        String carbonConfigDirPath = CarbonUtils.getCarbonConfigDirPath();
        File log4j2File = new File(carbonConfigDirPath + File.separator + "log4j2.properties");
        try {
            BasicFileAttributes log4j2FileAttributes
                    = Files.getFileAttributeView(log4j2File.toPath(), BasicFileAttributeView.class).readAttributes();
            return log4j2FileAttributes.lastModifiedTime();
        } catch (IOException e) {
            throw new LoggingUpdaterException("Error while reading log4j2.properties", e);
        }
    }
 
Example 11
FileTimeTester(Path path) throws IOException {
  this.path = path;

  BasicFileAttributes attrs = attrs();
  accessTime = attrs.lastAccessTime();
  modifiedTime = attrs.lastModifiedTime();
}
 
Example 12
public static void main(String[] args)
    throws InterruptedException, IOException {

    // Use 'which' to verify that 'nc' is available and skip the test
    // if it is not.
    Process proc = Runtime.getRuntime().exec("which nc");
    InputStream stdout = proc.getInputStream();
    int b = stdout.read();
    proc.destroy();
    if (b == -1) {
        System.err.println("Netcat command unavailable; skipping test.");
        return;
    }

    // Create a new sub-directory of the nominal test directory in which
    // 'nc' will create the socket file.
    String testSubDir = System.getProperty("test.dir", ".")
        + File.separator + TEST_SUB_DIR;
    Path socketTestDir = Paths.get(testSubDir);
    Files.createDirectory(socketTestDir);

    // Set the path of the socket file.
    String socketFilePath = testSubDir + File.separator
        + SOCKET_FILE_NAME;

    // Create a process which executes the nc (netcat) utility to create
    // a socket file at the indicated location.
    FileSystem fs = FileSystems.getDefault();
    try (WatchService ws = fs.newWatchService()) {
        // Watch the test sub-directory to receive notification when an
        // entry, i.e., the socket file, is added to the sub-directory.
        WatchKey wk = socketTestDir.register(ws,
                StandardWatchEventKinds.ENTRY_CREATE);

        // Execute the 'nc' command.
        proc = Runtime.getRuntime().exec(CMD_BASE + " " + socketFilePath);

        // Wait until the socket file is created.
        WatchKey key = ws.take();
        if (key != wk) {
            throw new RuntimeException("Unknown entry created - expected: "
                + wk.watchable() + ", actual: " + key.watchable());
        }
        wk.cancel();
    }

    // Verify that the socket file in fact exists.
    Path socketPath = fs.getPath(socketFilePath);
    if (!Files.exists(socketPath)) {
        throw new RuntimeException("Socket file " + socketFilePath
            + " was not created by \"nc\" command.");
    }

    // Retrieve the most recent access and modification times of the
    // socket file; print the values.
    BasicFileAttributeView attributeView = Files.getFileAttributeView(
            socketPath, BasicFileAttributeView.class);
    BasicFileAttributes oldAttributes = attributeView.readAttributes();
    FileTime oldAccessTime = oldAttributes.lastAccessTime();
    FileTime oldModifiedTime = oldAttributes.lastModifiedTime();
    System.out.println("Old times: " + oldAccessTime
        + " " + oldModifiedTime);

    // Calculate the time to which the access and modification times of the
    // socket file will be changed.
    FileTime newFileTime =
        FileTime.fromMillis(oldAccessTime.toMillis() + 1066);

    try {
        // Set the access and modification times of the socket file.
        attributeView.setTimes(newFileTime, newFileTime, null);

        // Retrieve the updated access and modification times of the
        // socket file; print the values.
        FileTime newAccessTime = null;
        FileTime newModifiedTime = null;
        BasicFileAttributes newAttributes = attributeView.readAttributes();
        newAccessTime = newAttributes.lastAccessTime();
        newModifiedTime = newAttributes.lastModifiedTime();
        System.out.println("New times: " + newAccessTime + " "
            + newModifiedTime);

        // Verify that the updated times have the expected values.
        if ((newAccessTime != null && !newAccessTime.equals(newFileTime))
            || (newModifiedTime != null
                && !newModifiedTime.equals(newFileTime))) {
            throw new RuntimeException("Failed to set correct times.");
        }
    } finally {
        // Destry the process running netcat and delete the socket file.
        proc.destroy();
        Files.delete(socketPath);
    }
}
 
Example 13
public static <T> MCRFileAttributes<T> fromAttributes(BasicFileAttributes attrs, String md5) {
    return new MCRFileAttributes<>(FileType.fromAttribute(attrs), attrs.size(), (T) attrs.fileKey(), md5,
        attrs.creationTime(), attrs.lastModifiedTime(), attrs.lastAccessTime());
}
 
Example 14
public static long getLastModified(Path filePath) throws IOException {
    BasicFileAttributes attr = Files.readAttributes(filePath, BasicFileAttributes.class);
    FileTime modifiedTime = attr.lastModifiedTime();
    return modifiedTime.toMillis();
}
 
Example 15
Source Project: ecs-sync   File: PreserveFiltersTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testPreserveFileAttributes() throws Exception {
    // can only change ownership if root
    boolean isRoot = "root".equals(System.getProperty("user.name"));
    if (isRoot) log.warn("detected root execution");

    Integer uid = 1111, gid = 2222;
    Set<PosixFilePermission> permissions = new HashSet<>(Arrays.asList(PosixFilePermission.OWNER_READ,
            PosixFilePermission.OTHERS_READ, PosixFilePermission.OTHERS_WRITE, PosixFilePermission.OTHERS_EXECUTE));
    String mode = "0407"; // matches permissions above

    // write 10 files
    writeTestFiles(sourceDir, 10, 10 * 1024, uid, gid, permissions);

    SyncOptions options = new SyncOptions().withThreadCount(16);

    // sync 10 files to a test target
    FilesystemConfig fsConfig = new FilesystemConfig();
    fsConfig.setPath(sourceDir.getPath());

    TestConfig testConfig = new TestConfig().withReadData(true).withDiscardData(false);

    EcsSync sync = new EcsSync();
    sync.setSyncConfig(new SyncConfig().withOptions(options).withSource(fsConfig).withTarget(testConfig)
            .withFilters(Collections.singletonList(new PreserveFileAttributesConfig())));
    sync.run();

    Assert.assertEquals(0, sync.getStats().getObjectsFailed());
    Assert.assertEquals(10, sync.getStats().getObjectsComplete());

    TestStorage testStorage = (TestStorage) sync.getTarget();

    // NOTE: new ClarityNow! DataMover spec only records to the hundredth of a second
    for (SyncObject object : testStorage.getRootObjects()) {
        File file = new File(sourceDir, object.getRelativePath());
        BasicFileAttributes attributes = Files.getFileAttributeView(file.toPath(), BasicFileAttributeView.class,
                LinkOption.NOFOLLOW_LINKS).readAttributes();
        FileTime mtime = attributes.lastModifiedTime();
        FileTime atime = attributes.lastAccessTime();
        FileTime crtime = attributes.creationTime();
        String mtimeSecs = object.getMetadata().getUserMetadataValue(PreserveFilters.META_MTIME);
        if (mtime == null) Assert.assertNull(mtimeSecs);
        else Assert.assertEquals(mtime.toMillis() / 10, (long) (Double.parseDouble(mtimeSecs) * 100D));
        String atimeSecs = object.getMetadata().getUserMetadataValue(PreserveFilters.META_ATIME);
        if (atime == null) Assert.assertNull(atimeSecs);
        else // atime is affected by reading the file times
            Assert.assertTrue(Math.abs(atime.toMillis() - (long) (Double.parseDouble(atimeSecs) * 1000D)) < 1000);
        String crtimeSecs = object.getMetadata().getUserMetadataValue(PreserveFilters.META_CRTIME);
        if (crtime == null) Assert.assertNull(crtimeSecs);
        else Assert.assertEquals(crtime.toMillis() / 10, (long) (Double.parseDouble(crtimeSecs) * 100D));

        // if possible, check ctime
        FileTime ctime = (FileTime) Files.getAttribute(file.toPath(), "unix:ctime");
        String ctimeSecs = object.getMetadata().getUserMetadataValue(PreserveFilters.META_CTIME);
        if (ctime == null) Assert.assertNull(ctimeSecs);
        else Assert.assertEquals(ctime.toMillis() / 10, (long) (Double.parseDouble(ctimeSecs) * 100D));

        // check permissions
        if (isRoot) {
            Assert.assertEquals(uid.toString(), object.getMetadata().getUserMetadataValue(PreserveFilters.META_OWNER));
            Assert.assertEquals(gid.toString(), object.getMetadata().getUserMetadataValue(PreserveFilters.META_GROUP));
        }
        Assert.assertEquals(mode, object.getMetadata().getUserMetadataValue(PreserveFilters.META_PERMISSIONS));
    }
}
 
Example 16
Source Project: ecs-sync   File: PreserveFiltersTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testRestoreFileAttributes() throws Exception {
    // can only change ownership if root
    boolean isRoot = "root".equals(System.getProperty("user.name"));
    if (isRoot) log.warn("detected root execution");

    // write 10 files
    Integer uid = 1111, gid = 2222;
    Set<PosixFilePermission> permissions = new HashSet<>(Arrays.asList(PosixFilePermission.OWNER_READ,
            PosixFilePermission.OTHERS_READ, PosixFilePermission.OTHERS_WRITE, PosixFilePermission.OTHERS_EXECUTE));

    // write 10 files
    writeTestFiles(sourceDir, 10, 10 * 1024, uid, gid, permissions);

    SyncOptions options = new SyncOptions().withThreadCount(16).withVerify(true);

    // sync files to a test target
    FilesystemConfig fsConfig = new FilesystemConfig();
    fsConfig.setPath(sourceDir.getPath());

    TestConfig testConfig = new TestConfig().withReadData(true).withDiscardData(false);

    EcsSync sync = new EcsSync();
    sync.setSyncConfig(new SyncConfig().withOptions(options).withSource(fsConfig).withTarget(testConfig)
            .withFilters(Collections.singletonList(new PreserveFileAttributesConfig())));
    sync.run();

    Assert.assertEquals(0, sync.getStats().getObjectsFailed());
    Assert.assertEquals(10, sync.getStats().getObjectsComplete());

    // wait a tick to make sure mtimes are different
    Thread.sleep(1000);

    TestStorage testStorage = (TestStorage) sync.getTarget();

    // sync from test target back to filesystem in a separate dir
    fsConfig.setPath(targetDir.getPath());

    options.setVerify(false); // verification will screw up atime
    sync = new EcsSync();
    sync.setSyncConfig(new SyncConfig().withOptions(options).withTarget(fsConfig)
            .withFilters(Collections.singletonList(new RestoreFileAttributesConfig())));
    sync.setSource(testStorage);
    sync.run();

    Assert.assertEquals(0, sync.getStats().getObjectsFailed());
    Assert.assertEquals(10, sync.getStats().getObjectsComplete());

    // NOTE: new ClarityNow! DataMover spec only records to the hundredth of a second
    for (File sourceFile : sourceDir.listFiles()) {
        File targetFile = new File(targetDir, sourceFile.getName());
        BasicFileAttributes attributes = Files.getFileAttributeView(sourceFile.toPath(),
                BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS).readAttributes();
        // java's time parsing is only millisecond-accurate
        FileTime sourceMtime = FileTime.fromMillis(attributes.lastModifiedTime().toMillis());
        FileTime sourceAtime = FileTime.fromMillis(attributes.lastAccessTime().toMillis());
        FileTime sourceCrtime = FileTime.fromMillis(attributes.creationTime().toMillis());
        attributes = Files.getFileAttributeView(targetFile.toPath(),
                BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS).readAttributes();
        FileTime targetMtime = attributes.lastModifiedTime();
        FileTime targetAtime = attributes.lastAccessTime();
        FileTime targetCrtime = attributes.creationTime();
        Assert.assertEquals(sourceMtime.toMillis() / 10, targetMtime.toMillis() /10);
        // atime is affected by reading the file times
        Assert.assertTrue(Math.abs(sourceAtime.toMillis() - targetAtime.toMillis()) <= 2000);
        Assert.assertEquals(sourceCrtime.toMillis() / 10, targetCrtime.toMillis() / 10);

        // check permissions
        if (isRoot) Assert.assertEquals(uid, Files.getAttribute(targetFile.toPath(), "unix:uid"));
        if (isRoot) Assert.assertEquals(gid, Files.getAttribute(targetFile.toPath(), "unix:gid"));
        Assert.assertEquals(permissions, Files.getPosixFilePermissions(targetFile.toPath()));
    }
}
 
Example 17
private final synchronized void loadMetadataIfNecessary()
{
    if (cachedMetadata == null)
    {
        cachedMetadata   = metadataLoader.loadMetadata(metadataReference);
        contentIsInPlace = false;

        if (contentReference != null)
        {
            try
            {
                final Path                path       = contentReference.toPath();
                final BasicFileAttributes attributes = Files.readAttributes(path, BasicFileAttributes.class);

                // If not set in the metadata file, set the creation timestamp to what's on disk
                if (!cachedMetadata.getProperties().containsKey(ContentModel.PROP_CREATED.toString()) &&
                    !cachedMetadata.getProperties().containsKey(ContentModel.PROP_CREATED.toPrefixString(namespaceService)) &&
                    attributes.creationTime() != null)
                {
                    final Date created = new Date(attributes.creationTime().toMillis());
                    cachedMetadata.addProperty(ContentModel.PROP_CREATED.toString(), created);
                }

                // If not set in the metadata file, set the modification timestamp to what's on disk
                if (!cachedMetadata.getProperties().containsKey(ContentModel.PROP_MODIFIED.toString()) &&
                    !cachedMetadata.getProperties().containsKey(ContentModel.PROP_MODIFIED.toPrefixString(namespaceService)) &&
                    attributes.lastModifiedTime() != null)
                {
                    final Date modified = new Date(attributes.lastModifiedTime().toMillis());
                    cachedMetadata.addProperty(ContentModel.PROP_MODIFIED.toString(), modified);
                }

                // If an in-place import is possible, attempt to construct a content URL
                if (!contentReference.isDirectory() && isInContentStore(configuredContentStore, contentReference))
                {
                    final ContentData contentData = buildContentProperty(mimeTypeService, configuredContentStore, contentReference);

                    if (contentData != null)
                    {
                        // We have valid in-place content
                        contentIsInPlace = true;
                        cachedMetadata.addProperty(ContentModel.PROP_CONTENT.toString(), contentData);
                    }
                    else
                    {
                        if (warn(FilesystemBulkImportItem.log)) warn (FilesystemBulkImportItem.log, "Unable to in-place import '" + getFileName(contentReference) + "'. Will stream it instead.");
                    }
                }
            }
            catch (final IOException ioe)
            {
                // Not much we can do in this case - log it and keep on truckin'
                if (warn(FilesystemBulkImportItem.log)) warn(FilesystemBulkImportItem.log, "Unable to read file attributes for " + contentReference.getAbsolutePath() + ". Creation and modification timestamps will be system generated.", ioe);
            }
        }
    }
}
 
Example 18
@Test
public void file_last_modified_nio() throws IOException {

	BasicFileAttributes attr = Files.readAttributes(source,
			BasicFileAttributes.class);

	FileTime lastModified = attr.lastModifiedTime();

	assertEquals(1389664624000l, lastModified.toMillis());
}