2424import java .io .File ;
2525import java .io .FileNotFoundException ;
2626import java .io .IOException ;
27- import java .nio .file .AccessDeniedException ;
28- import java .nio .file .FileSystemException ;
2927import java .nio .file .Files ;
3028import java .nio .file .InvalidPathException ;
3129import java .nio .file .LinkOption ;
32- import java .nio .file .NoSuchFileException ;
3330import java .nio .file .Paths ;
3431import java .nio .file .StandardCopyOption ;
3532import java .nio .file .attribute .BasicFileAttributes ;
@@ -54,12 +51,6 @@ public class JavaIoFileSystem extends AbstractFileSystem {
5451
5552 private final Clock clock ;
5653
57- protected static final String ERR_IS_DIRECTORY = " (Is a directory)" ;
58- protected static final String ERR_DIRECTORY_NOT_EMPTY = " (Directory not empty)" ;
59- protected static final String ERR_FILE_EXISTS = " (File exists)" ;
60- protected static final String ERR_NO_SUCH_FILE_OR_DIR = " (No such file or directory)" ;
61- protected static final String ERR_NOT_A_DIRECTORY = " (Not a directory)" ;
62-
6354 public JavaIoFileSystem (DigestHashFunction hashFunction ) {
6455 super (hashFunction );
6556 this .clock = new JavaClock ();
@@ -289,12 +280,8 @@ public void createSymbolicLink(
289280 Files .createSymbolicLink (
290281 nioPath ,
291282 Paths .get (StringEncoding .internalToPlatform (targetFragment .getSafePathString ())));
292- } catch (java .nio .file .FileAlreadyExistsException e ) {
293- throw new IOException (linkPath + ERR_FILE_EXISTS , e );
294- } catch (java .nio .file .AccessDeniedException e ) {
295- throw new IOException (linkPath + ERR_PERMISSION_DENIED , e );
296- } catch (java .nio .file .NoSuchFileException e ) {
297- throw new FileNotFoundException (linkPath + ERR_NO_SUCH_FILE_OR_DIR );
283+ } catch (IOException e ) {
284+ throw translateNioToIoException (linkPath , e );
298285 }
299286 }
300287
@@ -305,10 +292,8 @@ public PathFragment readSymbolicLink(PathFragment path) throws IOException {
305292 try {
306293 String link = Files .readSymbolicLink (nioPath ).toString ();
307294 return PathFragment .create (StringEncoding .platformToInternal (link ));
308- } catch (java .nio .file .NotLinkException e ) {
309- throw new NotASymlinkException (path , e );
310- } catch (java .nio .file .NoSuchFileException e ) {
311- throw new FileNotFoundException (path + ERR_NO_SUCH_FILE_OR_DIR );
295+ } catch (IOException e ) {
296+ throw translateNioToIoException (path , e );
312297 } finally {
313298 profiler .logSimpleTask (startTime , ProfilerTask .VFS_READLINK , path .getPathString ());
314299 }
@@ -318,32 +303,11 @@ public PathFragment readSymbolicLink(PathFragment path) throws IOException {
318303 public void renameTo (PathFragment sourcePath , PathFragment targetPath ) throws IOException {
319304 java .nio .file .Path source = getNioPath (sourcePath );
320305 java .nio .file .Path target = getNioPath (targetPath );
321- // Replace NIO exceptions with the types used by the native Unix filesystem implementation where
322- // necessary.
323306 try {
324307 Files .move (
325308 source , target , StandardCopyOption .ATOMIC_MOVE , StandardCopyOption .REPLACE_EXISTING );
326- } catch (NoSuchFileException originalException ) {
327- FileNotFoundException newException =
328- new FileNotFoundException (originalException .getMessage () + ERR_NO_SUCH_FILE_OR_DIR );
329- newException .initCause (originalException );
330- throw newException ;
331- } catch (AccessDeniedException originalException ) {
332- FileAccessException newException =
333- new FileAccessException (originalException .getMessage () + ERR_PERMISSION_DENIED );
334- newException .initCause (originalException );
335- throw newException ;
336- } catch (FileSystemException e ) {
337- // Rewrite exception messages to be identical to the ones produced by the native Unix
338- // filesystem implementation. Bazel forces the root locale for the JVM, so the error messages
339- // should be stable.
340- String filesPart = sourcePath + " -> " + targetPath ;
341- throw switch (e .getReason ()) {
342- case "Directory not empty" -> new IOException (filesPart + ERR_DIRECTORY_NOT_EMPTY , e );
343- case "Not a directory" -> new IOException (filesPart + ERR_NOT_A_DIRECTORY , e );
344- case "Is a directory" -> new IOException (filesPart + ERR_IS_DIRECTORY , e );
345- default -> e ;
346- };
309+ } catch (IOException e ) {
310+ throw translateNioToIoException (sourcePath , e );
347311 }
348312 }
349313
0 commit comments