use of java.nio.file.attribute.BasicFileAttributes in project buck by facebook.
the class ProjectWorkspace method setUp.
/**
* This will copy the template directory, renaming files named {@code foo.fixture} to {@code foo}
* in the process. Files whose names end in {@code .expected} will not be copied.
*/
@SuppressWarnings("PMD.EmptyCatchBlock")
public ProjectWorkspace setUp() throws IOException {
MoreFiles.copyRecursively(templatePath, destPath, BUILD_FILE_RENAME);
// Stamp the buck-out directory if it exists and isn't stamped already
try (OutputStream outputStream = new BufferedOutputStream(Channels.newOutputStream(Files.newByteChannel(destPath.resolve(BuckConstant.getBuckOutputPath().resolve(".currentversion")), ImmutableSet.<OpenOption>of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE))))) {
outputStream.write(BuckVersion.getVersion().getBytes(Charsets.UTF_8));
} catch (FileAlreadyExistsException | NoSuchFileException e) {
// If the current version file already exists we don't need to create it
// If buck-out doesn't exist we don't need to stamp it
}
if (Platform.detect() == Platform.WINDOWS) {
// Hack for symlinks on Windows.
SimpleFileVisitor<Path> copyDirVisitor = new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
// On NTFS length of path must be greater than 0 and less than 4096.
if (attrs.size() > 0 && attrs.size() <= 4096) {
String linkTo = new String(Files.readAllBytes(path), UTF_8);
Path linkToFile;
try {
linkToFile = templatePath.resolve(linkTo);
} catch (InvalidPathException e) {
// link.
return FileVisitResult.CONTINUE;
}
if (Files.isRegularFile(linkToFile)) {
Files.copy(linkToFile, path, StandardCopyOption.REPLACE_EXISTING);
} else if (Files.isDirectory(linkToFile)) {
Files.delete(path);
MoreFiles.copyRecursively(linkToFile, path);
}
}
return FileVisitResult.CONTINUE;
}
};
Files.walkFileTree(destPath, copyDirVisitor);
}
if (!Files.exists(getPath(".buckconfig.local"))) {
manageLocalConfigs = true;
// Disable the directory cache by default. Tests that want to enable it can call
// `enableDirCache` on this object. Only do this if a .buckconfig.local file does not already
// exist, however (we assume the test knows what it is doing at that point).
addBuckConfigLocalOption("cache", "mode", "");
// Limit the number of threads by default to prevent multiple integration tests running at the
// same time from creating a quadratic number of threads. Tests can disable this using
// `disableThreadLimitOverride`.
addBuckConfigLocalOption("build", "threads", "2");
}
isSetUp = true;
return this;
}
use of java.nio.file.attribute.BasicFileAttributes in project buck by facebook.
the class FakeProjectFilesystemTest method testReadFileAttributes.
@Test
public void testReadFileAttributes() throws IOException {
FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
filesystem.touch(Paths.get("foo"));
filesystem.mkdirs(Paths.get("bar"));
filesystem.touch(Paths.get("bar/aaaa"));
BasicFileAttributes attrs;
attrs = filesystem.readAttributes(Paths.get("foo"), BasicFileAttributes.class);
assertFalse(attrs.isDirectory());
assertTrue(attrs.isRegularFile());
attrs = filesystem.readAttributes(Paths.get("bar"), BasicFileAttributes.class);
assertTrue(attrs.isDirectory());
assertFalse(attrs.isRegularFile());
attrs = filesystem.readAttributes(Paths.get("bar/aaaa"), BasicFileAttributes.class);
assertFalse(attrs.isDirectory());
assertTrue(attrs.isRegularFile());
}
use of java.nio.file.attribute.BasicFileAttributes in project buck by facebook.
the class ProjectGenerator method addCoreDataModelBuildPhase.
private void addCoreDataModelBuildPhase(PBXGroup targetGroup, Iterable<AppleWrapperResourceArg> dataModels) throws IOException {
for (final AppleWrapperResourceArg dataModel : dataModels) {
// Core data models go in the resources group also.
PBXGroup resourcesGroup = targetGroup.getOrCreateChildGroupByName("Resources");
if (CoreDataModelDescription.isVersionedDataModel(dataModel)) {
// It's safe to do I/O here to figure out the current version because we're returning all
// the versions and the file pointing to the current version from
// getInputsToCompareToOutput(), so the rule will be correctly detected as stale if any of
// them change.
final String currentVersionFileName = ".xccurrentversion";
final String currentVersionKey = "_XCCurrentVersionName";
final XCVersionGroup versionGroup = resourcesGroup.getOrCreateChildVersionGroupsBySourceTreePath(new SourceTreePath(PBXReference.SourceTree.SOURCE_ROOT, pathRelativizer.outputDirToRootRelative(dataModel.path), Optional.empty()));
projectFilesystem.walkRelativeFileTree(dataModel.path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
if (dir.equals(dataModel.path)) {
return FileVisitResult.CONTINUE;
}
versionGroup.getOrCreateFileReferenceBySourceTreePath(new SourceTreePath(PBXReference.SourceTree.SOURCE_ROOT, pathRelativizer.outputDirToRootRelative(dir), Optional.empty()));
return FileVisitResult.SKIP_SUBTREE;
}
});
Path currentVersionPath = dataModel.path.resolve(currentVersionFileName);
try (InputStream in = projectFilesystem.newFileInputStream(currentVersionPath)) {
NSObject rootObject;
try {
rootObject = PropertyListParser.parse(in);
} catch (IOException e) {
throw e;
} catch (Exception e) {
rootObject = null;
}
if (!(rootObject instanceof NSDictionary)) {
throw new HumanReadableException("Malformed %s file.", currentVersionFileName);
}
NSDictionary rootDictionary = (NSDictionary) rootObject;
NSObject currentVersionName = rootDictionary.objectForKey(currentVersionKey);
if (!(currentVersionName instanceof NSString)) {
throw new HumanReadableException("Malformed %s file.", currentVersionFileName);
}
PBXFileReference ref = versionGroup.getOrCreateFileReferenceBySourceTreePath(new SourceTreePath(PBXReference.SourceTree.SOURCE_ROOT, pathRelativizer.outputDirToRootRelative(dataModel.path.resolve(currentVersionName.toString())), Optional.empty()));
versionGroup.setCurrentVersion(Optional.of(ref));
} catch (NoSuchFileException e) {
if (versionGroup.getChildren().size() == 1) {
versionGroup.setCurrentVersion(Optional.of(Iterables.get(versionGroup.getChildren(), 0)));
}
}
} else {
resourcesGroup.getOrCreateFileReferenceBySourceTreePath(new SourceTreePath(PBXReference.SourceTree.SOURCE_ROOT, pathRelativizer.outputDirToRootRelative(dataModel.path), Optional.empty()));
}
}
}
use of java.nio.file.attribute.BasicFileAttributes in project buck by facebook.
the class ArchiveStep method getAllInputs.
private ImmutableList<String> getAllInputs() throws IOException {
ImmutableList.Builder<String> allInputs = ImmutableList.builder();
// found from a recursive search.
for (Path input : inputs) {
if (filesystem.isDirectory(input)) {
// We make sure to sort the files we find under the directories so that we get
// deterministic output.
final Set<String> dirFiles = new TreeSet<>();
filesystem.walkFileTree(filesystem.resolve(input), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
dirFiles.add(file.toString());
return FileVisitResult.CONTINUE;
}
});
allInputs.addAll(dirFiles);
} else {
allInputs.add(input.toString());
}
}
return allInputs.build();
}
use of java.nio.file.attribute.BasicFileAttributes in project buck by facebook.
the class JarDirectoryStepHelper method addFilesInDirectoryToJar.
/**
* @param directory that must not contain symlinks with loops.
* @param jar is the file being written.
*/
private static void addFilesInDirectoryToJar(final ProjectFilesystem filesystem, final Path directory, CustomZipOutputStream jar, final Set<String> alreadyAddedEntries, final Iterable<Pattern> blacklist, final JavacEventSink eventSink) throws IOException {
// Since filesystem traversals can be non-deterministic, sort the entries we find into
// a tree map before writing them out.
final Map<String, Pair<JarEntry, Optional<Path>>> entries = Maps.newTreeMap();
filesystem.walkFileTree(directory, EnumSet.of(FileVisitOption.FOLLOW_LINKS), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
String relativePath = MorePaths.pathWithUnixSeparators(MorePaths.relativize(directory, file));
// Skip re-reading the manifest
if (JarFile.MANIFEST_NAME.equals(relativePath)) {
return FileVisitResult.CONTINUE;
}
// Check if the entry belongs to the blacklist and it should be excluded from the Jar.
if (shouldEntryBeRemovedFromJar(eventSink, relativePath, blacklist)) {
return FileVisitResult.CONTINUE;
}
JarEntry entry = new JarEntry(relativePath);
String entryName = entry.getName();
// We want deterministic JARs, so avoid mtimes.
entry.setTime(ZipConstants.getFakeTime());
// those repeatedly would be lame, so don't do that.
if (!isDuplicateAllowed(entryName) && !alreadyAddedEntries.add(entryName)) {
if (!entryName.endsWith("/")) {
eventSink.reportEvent(determineSeverity(entry), "Duplicate found when adding directory to jar: %s", relativePath);
}
return FileVisitResult.CONTINUE;
}
entries.put(entry.getName(), new Pair<>(entry, Optional.of(file)));
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
String relativePath = MorePaths.pathWithUnixSeparators(MorePaths.relativize(directory, dir));
if (relativePath.isEmpty()) {
// root of the tree. Skip.
return FileVisitResult.CONTINUE;
}
String entryName = relativePath.replace('\\', '/') + "/";
if (alreadyAddedEntries.contains(entryName)) {
return FileVisitResult.CONTINUE;
}
JarEntry entry = new JarEntry(entryName);
// We want deterministic JARs, so avoid mtimes.
entry.setTime(ZipConstants.getFakeTime());
entries.put(entry.getName(), new Pair<>(entry, Optional.empty()));
return FileVisitResult.CONTINUE;
}
});
// Write the entries out using the iteration order of the tree map above.
for (Pair<JarEntry, Optional<Path>> entry : entries.values()) {
jar.putNextEntry(entry.getFirst());
if (entry.getSecond().isPresent()) {
Files.copy(entry.getSecond().get(), jar);
}
jar.closeEntry();
}
}
Aggregations