use of java.nio.file.attribute.BasicFileAttributes in project intellij-community by JetBrains.
the class Digester method digestRegularFile.
public static long digestRegularFile(File file, boolean normalize) throws IOException {
Path path = file.toPath();
BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
if (attrs.isSymbolicLink()) {
Path target = Files.readSymbolicLink(path);
if (target.isAbsolute())
throw new IOException("Absolute link: " + file + " -> " + target);
return digestStream(new ByteArrayInputStream(target.toString().getBytes("UTF-8"))) | LINK_MASK;
}
if (attrs.isDirectory())
return DIRECTORY;
try (InputStream in = new BufferedInputStream(Utils.newFileInputStream(file, normalize))) {
return digestStream(in);
}
}
use of java.nio.file.attribute.BasicFileAttributes in project Glowstone by GlowstoneMC.
the class GlowServer method checkTransfer.
private void checkTransfer(String name, String suffix, Environment environment) {
// todo: import things like per-dimension villages.dat when those are implemented
Path srcPath = new File(new File(getWorldContainer(), name), "DIM" + environment.getId()).toPath();
Path destPath = new File(getWorldContainer(), name + suffix).toPath();
if (Files.exists(srcPath) && !Files.exists(destPath)) {
logger.info("Importing " + destPath + " from " + srcPath);
try {
Files.walkFileTree(srcPath, new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
Path target = destPath.resolve(srcPath.relativize(dir));
if (!Files.exists(target)) {
Files.createDirectory(target);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.copy(file, destPath.resolve(srcPath.relativize(file)), StandardCopyOption.COPY_ATTRIBUTES);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
logger.warning("Importing file " + srcPath.relativize(file) + " + failed: " + exc);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
});
Files.copy(srcPath.resolve("../level.dat"), destPath.resolve("level.dat"));
} catch (IOException e) {
logger.log(Level.WARNING, "Import of " + srcPath + " failed", e);
}
}
}
use of java.nio.file.attribute.BasicFileAttributes in project asterixdb by apache.
the class LocalFileSystemUtils method traverse.
public static void traverse(final List<File> files, File root, final String expression, final LinkedList<Path> dirs) throws IOException {
final Path path = root.toPath();
if (!Files.exists(path)) {
throw new RuntimeDataException(ErrorCode.UTIL_LOCAL_FILE_SYSTEM_UTILS_PATH_NOT_FOUND, path.toString());
}
if (!Files.isDirectory(path)) {
validateAndAdd(path, expression, files);
}
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes attrs) throws IOException {
if (!Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {
return FileVisitResult.TERMINATE;
}
if (Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS)) {
if (dirs != null) {
dirs.add(path);
}
//get immediate children files
File[] content = path.toFile().listFiles();
for (File file : content) {
if (!file.isDirectory()) {
validateAndAdd(file.toPath(), expression, files);
}
}
} else {
// Path is a file, add to list of files if it matches the expression
validateAndAdd(path, expression, files);
}
return FileVisitResult.CONTINUE;
}
});
}
use of java.nio.file.attribute.BasicFileAttributes in project beam by apache.
the class ApexYarnLauncher method createJar.
/**
* Create a jar file from the given directory.
* @param dir source directory
* @param jarFile jar file name
* @throws IOException when file cannot be created
*/
public static void createJar(File dir, File jarFile) throws IOException {
final Map<String, ?> env = Collections.singletonMap("create", "true");
if (jarFile.exists() && !jarFile.delete()) {
throw new RuntimeException("Failed to remove " + jarFile);
}
URI uri = URI.create("jar:" + jarFile.toURI());
try (final FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
File manifestFile = new File(dir, JarFile.MANIFEST_NAME);
Files.createDirectory(zipfs.getPath("META-INF"));
try (final OutputStream out = Files.newOutputStream(zipfs.getPath(JarFile.MANIFEST_NAME))) {
if (!manifestFile.exists()) {
new Manifest().write(out);
} else {
FileUtils.copyFile(manifestFile, out);
}
}
final java.nio.file.Path root = dir.toPath();
Files.walkFileTree(root, new java.nio.file.SimpleFileVisitor<Path>() {
String relativePath;
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
relativePath = root.relativize(dir).toString();
if (!relativePath.isEmpty()) {
if (!relativePath.endsWith("/")) {
relativePath += "/";
}
if (!relativePath.equals("META-INF/")) {
final Path dstDir = zipfs.getPath(relativePath);
Files.createDirectory(dstDir);
}
}
return super.preVisitDirectory(dir, attrs);
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
String name = relativePath + file.getFileName();
if (!JarFile.MANIFEST_NAME.equals(name)) {
try (final OutputStream out = Files.newOutputStream(zipfs.getPath(name))) {
FileUtils.copyFile(file.toFile(), out);
}
}
return super.visitFile(file, attrs);
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
relativePath = root.relativize(dir.getParent()).toString();
if (!relativePath.isEmpty() && !relativePath.endsWith("/")) {
relativePath += "/";
}
return super.postVisitDirectory(dir, exc);
}
});
}
}
use of java.nio.file.attribute.BasicFileAttributes in project cloudstack by apache.
the class HypervisorUtils method checkVolumeFileForActivity.
public static void checkVolumeFileForActivity(final String filePath, int timeoutSeconds, long inactiveThresholdMilliseconds, long minimumFileSize) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
throw new CloudRuntimeException("File " + file.getAbsolutePath() + " not found");
}
if (file.length() < minimumFileSize) {
s_logger.debug("VM disk file too small, fresh clone? skipping modify check");
return;
}
int waitedSeconds = 0;
int intervalSeconds = 1;
while (true) {
BasicFileAttributes attrs = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
long modifyIdle = System.currentTimeMillis() - attrs.lastModifiedTime().toMillis();
long accessIdle = System.currentTimeMillis() - attrs.lastAccessTime().toMillis();
if (modifyIdle > inactiveThresholdMilliseconds && accessIdle > inactiveThresholdMilliseconds) {
s_logger.debug("File " + filePath + " has not been accessed or modified for at least " + inactiveThresholdMilliseconds + " ms");
return;
} else {
s_logger.debug("File was modified " + modifyIdle + "ms ago, accessed " + accessIdle + "ms ago, waiting for inactivity threshold of " + inactiveThresholdMilliseconds + "ms or timeout of " + timeoutSeconds + "s (waited " + waitedSeconds + "s)");
}
try {
TimeUnit.SECONDS.sleep(intervalSeconds);
} catch (InterruptedException ex) {
throw new CloudRuntimeException("Interrupted while waiting for activity on " + filePath + " to subside", ex);
}
waitedSeconds += intervalSeconds;
if (waitedSeconds >= timeoutSeconds) {
throw new CloudRuntimeException("Reached timeout while waiting for activity on " + filePath + " to subside");
}
}
}
Aggregations