use of java.nio.file.attribute.FileTime in project jdk8u_jdk by JetBrains.
the class Basic method neq.
static void neq(long v1, TimeUnit u1, long v2, TimeUnit u2) {
FileTime t1 = FileTime.from(v1, u1);
FileTime t2 = FileTime.from(v2, u2);
if (t1.equals(t2))
throw new RuntimeException("should not be equal");
}
use of java.nio.file.attribute.FileTime in project jdk8u_jdk by JetBrains.
the class Basic method to.
static void to(long v, TimeUnit unit) {
FileTime t = FileTime.from(v, unit);
for (TimeUnit u : TimeUnit.values()) {
long result = t.to(u);
long expected = u.convert(v, unit);
if (result != expected) {
throw new RuntimeException("unexpected result");
}
}
}
use of java.nio.file.attribute.FileTime in project jdk8u_jdk by JetBrains.
the class TestExtraTime method testTimeConversions.
static void testTimeConversions(long from, long to, long step) {
ZipEntry ze = new ZipEntry("TestExtraTime.java");
for (long time = from; time <= to; time += step) {
ze.setTime(time);
FileTime lastModifiedTime = ze.getLastModifiedTime();
if (lastModifiedTime.toMillis() != time) {
throw new RuntimeException("setTime should make getLastModifiedTime " + "return the specified instant: " + time + " got: " + lastModifiedTime.toMillis());
}
if (ze.getTime() != time) {
throw new RuntimeException("getTime after setTime, expected: " + time + " got: " + ze.getTime());
}
}
}
use of java.nio.file.attribute.FileTime in project logging-log4j2 by apache.
the class IfLastModified method accept.
/*
* (non-Javadoc)
*
* @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#accept(java.nio.file.Path,
* java.nio.file.Path, java.nio.file.attribute.BasicFileAttributes)
*/
@Override
public boolean accept(final Path basePath, final Path relativePath, final BasicFileAttributes attrs) {
final FileTime fileTime = attrs.lastModifiedTime();
final long millis = fileTime.toMillis();
final long ageMillis = CLOCK.currentTimeMillis() - millis;
final boolean result = ageMillis >= age.toMillis();
final String match = result ? ">=" : "<";
final String accept = result ? "ACCEPTED" : "REJECTED";
LOGGER.trace("IfLastModified {}: {} ageMillis '{}' {} '{}'", accept, relativePath, ageMillis, match, age);
if (result) {
return IfAll.accept(nestedConditions, basePath, relativePath, attrs);
}
return result;
}
use of java.nio.file.attribute.FileTime in project lucene-solr by apache.
the class NativeFSLockFactory method obtainFSLock.
@Override
protected Lock obtainFSLock(FSDirectory dir, String lockName) throws IOException {
Path lockDir = dir.getDirectory();
// Ensure that lockDir exists and is a directory.
// note: this will fail if lockDir is a symlink
Files.createDirectories(lockDir);
Path lockFile = lockDir.resolve(lockName);
try {
Files.createFile(lockFile);
} catch (IOException ignore) {
// we must create the file to have a truly canonical path.
// if it's already created, we don't care. if it cant be created, it will fail below.
}
// fails if the lock file does not exist
final Path realPath = lockFile.toRealPath();
// used as a best-effort check, to see if the underlying file has changed
final FileTime creationTime = Files.readAttributes(realPath, BasicFileAttributes.class).creationTime();
if (LOCK_HELD.add(realPath.toString())) {
FileChannel channel = null;
FileLock lock = null;
try {
channel = FileChannel.open(realPath, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
lock = channel.tryLock();
if (lock != null) {
return new NativeFSLock(lock, channel, realPath, creationTime);
} else {
throw new LockObtainFailedException("Lock held by another program: " + realPath);
}
} finally {
if (lock == null) {
// not successful - clear up and move out
// TODO: addSuppressed
IOUtils.closeWhileHandlingException(channel);
// clear LOCK_HELD last
clearLockHeld(realPath);
}
}
} else {
throw new LockObtainFailedException("Lock held by this virtual machine: " + realPath);
}
}
Aggregations