Skip to content

Commit

Permalink
Automatic code cleanup.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 445931356
  • Loading branch information
Googler authored and copybara-github committed May 2, 2022
1 parent 20d7888 commit 691fc5b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -687,8 +687,7 @@ public static void writeContentAsLatin1(Path outputFile, String content) throws
}

/**
* Writes the specified String using the specified encoding to the file.
* Follows symbolic links.
* Writes the specified String using the specified encoding to the file. Follows symbolic links.
*
* @throws IOException if there was an error
*/
Expand All @@ -698,28 +697,26 @@ public static void writeContent(Path outputFile, Charset charset, String content
}

/**
* Writes lines to file using the given encoding, ending every line with a
* line break '\n' character.
* Writes the specified byte array to the output file. Follows symbolic links.
*
* @throws IOException if there was an error
*/
@ThreadSafe // but not atomic
public static void writeLinesAs(Path file, Charset charset, String... lines)
throws IOException {
writeLinesAs(file, charset, Arrays.asList(lines));
public static void writeContent(Path outputFile, byte[] content) throws IOException {
asByteSink(outputFile).write(content);
}

/**
* Appends lines to file using the given encoding, ending every line with a
* line break '\n' character.
* Writes lines to file using the given encoding, ending every line with a line break '\n'
* character.
*/
@ThreadSafe // but not atomic
public static void appendLinesAs(Path file, Charset charset, String... lines)
throws IOException {
appendLinesAs(file, charset, Arrays.asList(lines));
public static void writeLinesAs(Path file, Charset charset, String... lines) throws IOException {
writeLinesAs(file, charset, Arrays.asList(lines));
}

/**
* Writes lines to file using the given encoding, ending every line with a
* line break '\n' character.
* Writes lines to file using the given encoding, ending every line with a line break '\n'
* character.
*/
@ThreadSafe // but not atomic
public static void writeLinesAs(Path file, Charset charset, Iterable<String> lines)
Expand All @@ -729,23 +726,23 @@ public static void writeLinesAs(Path file, Charset charset, Iterable<String> lin
}

/**
* Appends lines to file using the given encoding, ending every line with a
* line break '\n' character.
* Appends lines to file using the given encoding, ending every line with a line break '\n'
* character.
*/
@ThreadSafe // but not atomic
public static void appendLinesAs(Path file, Charset charset, Iterable<String> lines)
throws IOException {
file.getParentDirectory().createDirectoryAndParents();
asByteSink(file, true).asCharSink(charset).writeLines(lines);
public static void appendLinesAs(Path file, Charset charset, String... lines) throws IOException {
appendLinesAs(file, charset, Arrays.asList(lines));
}

/**
* Writes the specified byte array to the output file. Follows symbolic links.
*
* @throws IOException if there was an error
* Appends lines to file using the given encoding, ending every line with a line break '\n'
* character.
*/
public static void writeContent(Path outputFile, byte[] content) throws IOException {
asByteSink(outputFile).write(content);
@ThreadSafe // but not atomic
public static void appendLinesAs(Path file, Charset charset, Iterable<String> lines)
throws IOException {
file.getParentDirectory().createDirectoryAndParents();
asByteSink(file, true).asCharSink(charset).writeLines(lines);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,9 @@ protected void createSymbolicLink(PathFragment linkPath, PathFragment targetFrag
try {
Files.createSymbolicLink(nioPath, Paths.get(targetFragment.getSafePathString()));
} catch (java.nio.file.FileAlreadyExistsException e) {
throw new IOException(linkPath + ERR_FILE_EXISTS);
throw new IOException(linkPath + ERR_FILE_EXISTS, e);
} catch (java.nio.file.AccessDeniedException e) {
throw new IOException(linkPath + ERR_PERMISSION_DENIED);
throw new IOException(linkPath + ERR_PERMISSION_DENIED, e);
} catch (java.nio.file.NoSuchFileException e) {
throw new FileNotFoundException(linkPath + ERR_NO_SUCH_FILE_OR_DIR);
}
Expand All @@ -299,7 +299,7 @@ protected PathFragment readSymbolicLink(PathFragment path) throws IOException {
String link = Files.readSymbolicLink(nioPath).toString();
return PathFragment.create(link);
} catch (java.nio.file.NotLinkException e) {
throw new NotASymlinkException(path);
throw new NotASymlinkException(path, e);
} catch (java.nio.file.NoSuchFileException e) {
throw new FileNotFoundException(path + ERR_NO_SUCH_FILE_OR_DIR);
} finally {
Expand Down Expand Up @@ -348,9 +348,9 @@ protected boolean delete(PathFragment path) throws IOException {
try {
return Files.deleteIfExists(nioPath);
} catch (java.nio.file.DirectoryNotEmptyException e) {
throw new IOException(path.getPathString() + ERR_DIRECTORY_NOT_EMPTY);
throw new IOException(path.getPathString() + ERR_DIRECTORY_NOT_EMPTY, e);
} catch (java.nio.file.AccessDeniedException e) {
throw new IOException(path.getPathString() + ERR_PERMISSION_DENIED);
throw new IOException(path.getPathString() + ERR_PERMISSION_DENIED, e);
} catch (java.nio.file.AtomicMoveNotSupportedException
| java.nio.file.FileAlreadyExistsException
| java.nio.file.FileSystemLoopException
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/com/google/devtools/build/lib/vfs/Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,16 +279,6 @@ public FileStatus stat() throws IOException {
return fileSystem.stat(asFragment(), true);
}

/** Like stat(), but returns null on file-nonexistence instead of throwing. */
public FileStatus statNullable() {
return statNullable(Symlinks.FOLLOW);
}

/** Like stat(), but returns null on file-nonexistence instead of throwing. */
public FileStatus statNullable(Symlinks symlinks) {
return fileSystem.statNullable(asFragment(), symlinks.toBoolean());
}

/**
* Returns the status of a file, optionally following symbolic links.
*
Expand All @@ -302,6 +292,16 @@ public FileStatus stat(Symlinks followSymlinks) throws IOException {
return fileSystem.stat(asFragment(), followSymlinks.toBoolean());
}

/** Like stat(), but returns null on file-nonexistence instead of throwing. */
public FileStatus statNullable() {
return statNullable(Symlinks.FOLLOW);
}

/** Like stat(), but returns null on file-nonexistence instead of throwing. */
public FileStatus statNullable(Symlinks symlinks) {
return fileSystem.statNullable(asFragment(), symlinks.toBoolean());
}

/**
* Like {@link #stat}, but may return null if the file is not found (corresponding to {@code
* ENOENT} and {@code ENOTDIR} in Unix's stat(2) function) instead of throwing. Follows symbolic
Expand Down
36 changes: 18 additions & 18 deletions src/main/java/com/google/devtools/build/lib/vfs/PathFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,6 @@ public PathFragment getRelative(PathFragment other) {
return getRelative(otherStr, other.getDriveStrLength(), OS.needsToNormalizeSuffix(otherStr));
}

public static boolean isNormalizedRelativePath(String path) {
int driveStrLength = OS.getDriveStrLength(path);
int normalizationLevel = OS.needsToNormalize(path);
return driveStrLength == 0 && normalizationLevel == OsPathPolicy.NORMALIZED;
}

public static boolean containsSeparator(String path) {
return path.lastIndexOf(SEPARATOR_CHAR) != -1;
}

/**
* Returns a {@link PathFragment} instance representing the relative path between this {@link
* PathFragment} and the given path.
Expand Down Expand Up @@ -245,6 +235,16 @@ private PathFragment getRelative(String other, int otherDriveStrLength, int norm
return makePathFragment(newPath, getDriveStrLength());
}

public static boolean isNormalizedRelativePath(String path) {
int driveStrLength = OS.getDriveStrLength(path);
int normalizationLevel = OS.needsToNormalize(path);
return driveStrLength == 0 && normalizationLevel == OsPathPolicy.NORMALIZED;
}

public static boolean containsSeparator(String path) {
return path.lastIndexOf(SEPARATOR_CHAR) != -1;
}

public PathFragment getChild(String baseName) {
checkBaseName(baseName);
String newPath;
Expand Down Expand Up @@ -685,23 +685,23 @@ public boolean containsUplevelReferences() {
}

/**
* Returns true if the passed path contains uplevel references ".." or single-dot references "."
* Returns true if the passed path contains uplevel references "..".
*
* <p>This is useful to check a string for normalization before constructing a PathFragment, since
* <p>This is useful to check a string for '..' segments before constructing a PathFragment, since
* these are always normalized and will throw uplevel references away.
*/
public static boolean isNormalized(String path) {
return isNormalizedImpl(path, /* lookForSameLevelReferences= */ true);
public static boolean containsUplevelReferences(String path) {
return !isNormalizedImpl(path, /* lookForSameLevelReferences= */ false);
}

/**
* Returns true if the passed path contains uplevel references "..".
* Returns true if the passed path contains uplevel references ".." or single-dot references "."
*
* <p>This is useful to check a string for '..' segments before constructing a PathFragment, since
* <p>This is useful to check a string for normalization before constructing a PathFragment, since
* these are always normalized and will throw uplevel references away.
*/
public static boolean containsUplevelReferences(String path) {
return !isNormalizedImpl(path, /* lookForSameLevelReferences= */ false);
public static boolean isNormalized(String path) {
return isNormalizedImpl(path, /* lookForSameLevelReferences= */ true);
}

private enum NormalizedImplState {
Expand Down

0 comments on commit 691fc5b

Please sign in to comment.