/** * Gets the default file system URI that is used for paths and file systems * that do not specify and explicit scheme. * * <p>As an example, assume the default file system URI is set to {@code 'hdfs://someserver:9000/'}. * A file path of {@code '/user/USERNAME/in.txt'} is interpreted as * {@code 'hdfs://someserver:9000/user/USERNAME/in.txt'}. * * @return The default file system URI */ public static URI getDefaultFsUri() { return defaultScheme != null ? defaultScheme : LocalFileSystem.getLocalFsURI(); }
@Override public boolean delete(final Path f, final boolean recursive) throws IOException { final File file = pathToFile(f); if (file.isFile()) { return file.delete(); } else if ((!recursive) && file.isDirectory()) { File[] containedFiles = file.listFiles(); if (containedFiles == null) { throw new IOException("Directory " + file.toString() + " does not exist or an I/O error occurred"); } else if (containedFiles.length != 0) { throw new IOException("Directory " + file.toString() + " is not empty"); } } return delete(file); }
@Override public FSDataOutputStream create(final Path filePath, final WriteMode overwrite) throws IOException { checkNotNull(filePath, "filePath"); if (exists(filePath) && overwrite == WriteMode.NO_OVERWRITE) { throw new FileAlreadyExistsException("File already exists: " + filePath); } final Path parent = filePath.getParent(); if (parent != null && !mkdirs(parent)) { throw new IOException("Mkdirs failed to create " + parent); } final File file = pathToFile(filePath); return new LocalDataOutputStream(file); }
/** * Recursively creates the directory specified by the provided path. * * @return <code>true</code>if the directories either already existed or have been created successfully, * <code>false</code> otherwise * @throws IOException * thrown if an error occurred while creating the directory/directories */ @Override public boolean mkdirs(final Path f) throws IOException { checkNotNull(f, "path is null"); return mkdirsInternal(pathToFile(f)); }
final LocalFileSystem lfs = new LocalFileSystem(); assertFalse(lfs.exists(pathtotmpdir)); assertTrue(tempdir.mkdirs()); assertTrue(lfs.exists(pathtotmpdir)); final FileStatus localstatus1 = lfs.getFileStatus(pathtotmpdir); final FileStatus[] statusforfiles = lfs.listStatus(pathtotmpdir); lfs.delete(pathtotmpdir, true); assertFalse(lfs.exists(pathtotmpdir)); assertFalse(tempdir.exists()); lfs.mkdirs(pathtotmpdir); final FSDataOutputStream lfsoutput1 = lfs.create(pathtotestfile1, WriteMode.NO_OVERWRITE); assertTrue(testfile2.createNewFile()); assertTrue(lfs.exists(pathtotestfile2)); assertEquals(lfs.getFileStatus(pathtotestfile1).getLen(), testfile1.length()); assertEquals(lfs.listStatus(pathtotestfile1)[0].getLen(), testfile1.length()); final FSDataInputStream lfsinput2 = lfs.open(pathtotestfile2); assertEquals(lfsinput2.read(testbytestest), 5); lfsinput2.close();
@Override public FileSystem create(URI fsUri) { return LocalFileSystem.getSharedInstance(); } }
@Override public FileStatus[] listStatus(final Path f) throws IOException { final File localf = pathToFile(f); FileStatus[] results; if (!localf.exists()) { return null; } if (localf.isFile()) { return new FileStatus[] { new LocalFileStatus(localf, this) }; } final String[] names = localf.list(); if (names == null) { return null; } results = new FileStatus[names.length]; for (int i = 0; i < names.length; i++) { results[i] = getFileStatus(new Path(f, names[i])); } return results; }
@Override public boolean exists(Path f) throws IOException { final File path = pathToFile(f); return path.exists(); }
@Override public FSDataInputStream open(final Path f, final int bufferSize) throws IOException { return open(f); }
/** * Deletes the given file or directory. * * @param f * the file to be deleted * @return <code>true</code> if all files were deleted successfully, <code>false</code> otherwise * @throws IOException * thrown if an error occurred while deleting the files/directories */ private boolean delete(final File f) throws IOException { if (f.isDirectory()) { final File[] files = f.listFiles(); if (files != null) { for (File file : files) { final boolean del = delete(file); if (!del) { return false; } } } } else { return f.delete(); } // Now directory is empty return f.delete(); }
/** * Converts the given Path to a File for this file system. * * <p>If the path is not absolute, it is interpreted relative to this FileSystem's working directory. */ public File pathToFile(Path path) { if (!path.isAbsolute()) { path = new Path(getWorkingDirectory(), path); } return new File(path.toUri().getPath()); }
@Override public boolean mkdirs(Path f) throws IOException { mkdirsTriggerLatch.trigger(); try { mkdirsAwaitLatch.await(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException("interrupted"); } return super.mkdirs(f); } }
@Override public FileStatus getFileStatus(Path f) throws IOException { LocalFileStatus status = (LocalFileStatus) super.getFileStatus(f); return new LocalFileStatus(status.getFile(), this); }
private boolean mkdirsInternal(File file) throws IOException { if (file.isDirectory()) { return true; } else if (file.exists() && !file.isDirectory()) { // Important: The 'exists()' check above must come before the 'isDirectory()' check to // be safe when multiple parallel instances try to create the directory // exists and is not a directory -> is a regular file throw new FileAlreadyExistsException(file.getAbsolutePath()); } else { File parent = file.getParentFile(); return (parent == null || mkdirsInternal(parent)) && (file.mkdir() || file.isDirectory()); } }
/** * Returns a reference to the {@link FileSystem} instance for accessing the local file system. * * @return a reference to the {@link FileSystem} instance for accessing the local file system. */ public static FileSystem getLocalFileSystem() { return FileSystemSafetyNet.wrapWithSafetyNetWhenActivated(LocalFileSystem.getSharedInstance()); }
@Override public FileStatus[] listStatus(final Path f) throws IOException { final File localf = pathToFile(f); FileStatus[] results; if (!localf.exists()) { return null; } if (localf.isFile()) { return new FileStatus[] { new LocalFileStatus(localf, this) }; } final String[] names = localf.list(); if (names == null) { return null; } results = new FileStatus[names.length]; for (int i = 0; i < names.length; i++) { results[i] = getFileStatus(new Path(f, names[i])); } return results; }
@Override public boolean rename(final Path src, final Path dst) throws IOException { final File srcFile = pathToFile(src); final File dstFile = pathToFile(dst); final File dstParent = dstFile.getParentFile(); // Files.move fails if the destination directory doesn't exist //noinspection ResultOfMethodCallIgnored -- we don't care if the directory existed or was created dstParent.mkdirs(); try { Files.move(srcFile.toPath(), dstFile.toPath(), StandardCopyOption.REPLACE_EXISTING); return true; } catch (NoSuchFileException | AccessDeniedException | DirectoryNotEmptyException | SecurityException ex) { // catch the errors that are regular "move failed" exceptions and return false return false; } }
/** * Recursively creates the directory specified by the provided path. * * @return <code>true</code>if the directories either already existed or have been created successfully, * <code>false</code> otherwise * @throws IOException * thrown if an error occurred while creating the directory/directories */ @Override public boolean mkdirs(final Path f) throws IOException { checkNotNull(f, "path is null"); return mkdirsInternal(pathToFile(f)); }
@Override public FSDataInputStream open(Path f) throws IOException { streamOpenCounter++; return super.open(f); }
@Override public boolean delete(Path f, boolean recursive) throws IOException { deleteTriggerLatch.trigger(); try { deleteAwaitLatch.await(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException("interrupted"); } return super.delete(f, recursive); }