/** * A wrapper for {@link File#list()}. This java.io API returns null * when a dir is not a directory or for any I/O error. Instead of having * null check everywhere File#list() is used, we will add utility API * to get around this problem. For the majority of cases where we prefer * an IOException to be thrown. * @param dir directory for which listing should be performed * @return list of file names or empty string list * @exception AccessDeniedException for unreadable directory * @exception IOException for invalid directory or for bad disk */ public static String[] list(File dir) throws IOException { if (!canRead(dir)) { throw new AccessDeniedException(dir.toString(), null, FSExceptionMessages.PERMISSION_DENIED); } String[] fileNames = dir.list(); if(fileNames == null) { throw new IOException("Invalid directory or I/O error occurred for dir: " + dir.toString()); } return fileNames; }
/** * Checks that the current running process can read, write, and execute the * given directory by using methods of the File object. * * @param dir File to check * @throws DiskErrorException if dir is not readable, not writable, or not * executable */ private static void checkAccessByFileMethods(File dir) throws DiskErrorException { if (!dir.isDirectory()) { throw new DiskErrorException("Not a directory: " + dir.toString()); } if (!FileUtil.canRead(dir)) { throw new DiskErrorException("Directory is not readable: " + dir.toString()); } if (!FileUtil.canWrite(dir)) { throw new DiskErrorException("Directory is not writable: " + dir.toString()); } if (!FileUtil.canExecute(dir)) { throw new DiskErrorException("Directory is not executable: " + dir.toString()); } }
synchronized boolean moveTmpSegmentToCurrent(File tmpFile, File finalFile, long endTxId) throws IOException { final boolean success; if (endTxId <= committedTxnId.get()) { if (!finalFile.getParentFile().exists()) { LOG.error(finalFile.getParentFile() + " doesn't exist. Aborting tmp " + "segment move to current directory ; journal id: " + journalId); return false; } Files.move(tmpFile.toPath(), finalFile.toPath(), StandardCopyOption.ATOMIC_MOVE); if (finalFile.exists() && FileUtil.canRead(finalFile)) { success = true; } else { success = false; LOG.warn("Unable to move edits file from " + tmpFile + " to " + finalFile + " ; journal id: " + journalId); } } else { success = false; LOG.error("The endTxId of the temporary file is not less than the " + "last committed transaction id. Aborting move to final file" + finalFile + " ; journal id: " + journalId); } return success; }
/** * Determine the checkpoint time of the specified StorageDirectory * * @param sd StorageDirectory to check * @return If file exists and can be read, last checkpoint time. If not, 0L. * @throws IOException On errors processing file pointed to by sd */ static long readCheckpointTime(StorageDirectory sd) throws IOException { File timeFile = NNStorage.getStorageFile(sd, NameNodeFile.TIME); long timeStamp = 0L; if (timeFile.exists() && FileUtil.canRead(timeFile)) { DataInputStream in = new DataInputStream(new FileInputStream(timeFile)); try { timeStamp = in.readLong(); in.close(); in = null; } finally { IOUtils.cleanupWithLogger(LOG, in); } } return timeStamp; }
/** * @return The first image file with the given txid and image type. */ public File getFsImageName(long txid, NameNodeFile nnf) { for (Iterator<StorageDirectory> it = dirIterator(NameNodeDirType.IMAGE); it.hasNext();) { StorageDirectory sd = it.next(); File fsImage = getStorageFile(sd, nnf, txid); if (FileUtil.canRead(sd.getRoot()) && fsImage.exists()) { return fsImage; } } return null; }
/** * @return The first image file whose txid is the same with the given txid and * image type is one of the given types. */ public File getFsImage(long txid, EnumSet<NameNodeFile> nnfs) { for (Iterator<StorageDirectory> it = dirIterator(NameNodeDirType.IMAGE); it.hasNext();) { StorageDirectory sd = it.next(); for (NameNodeFile nnf : nnfs) { File fsImage = getStorageFile(sd, nnf, txid); if (FileUtil.canRead(sd.getRoot()) && fsImage.exists()) { return fsImage; } } } return null; }
/** * Return the first readable storage file of the given name * across any of the 'current' directories in SDs of the * given type, or null if no such file exists. */ private File findFile(NameNodeDirType dirType, String name) { for (StorageDirectory sd : dirIterable(dirType)) { File candidate = new File(sd.getCurrentDir(), name); if (FileUtil.canRead(sd.getCurrentDir()) && candidate.exists()) { return candidate; } } return null; }
log.getEndTxId()); if (finalEditsFile.exists() && FileUtil.canRead(finalEditsFile)) { LOG.info("Skipping download of remote edit log " + log + " since it's" + " already stored locally at " + finalEditsFile);
if (f.exists() && FileUtil.canRead(f)) { LOG.info("Skipping download of remote edit log " + log + " since it already is stored locally at " + f);
private void validate(final List<String> values) throws IllegalArgumentException { for (String file : values) { File f = new File(file); if (!FileUtil.canRead(f)) { fail("File: " + f.getAbsolutePath() + " does not exist, or is not readable."); } } }
/** * Determine the checkpoint time of the specified StorageDirectory * * @param sd StorageDirectory to check * @return If file exists and can be read, last checkpoint time. If not, 0L. * @throws IOException On errors processing file pointed to by sd */ static long readCheckpointTime(StorageDirectory sd) throws IOException { File timeFile = NNStorage.getStorageFile(sd, NameNodeFile.TIME); long timeStamp = 0L; if (timeFile.exists() && FileUtil.canRead(timeFile)) { DataInputStream in = new DataInputStream(new FileInputStream(timeFile)); try { timeStamp = in.readLong(); in.close(); in = null; } finally { IOUtils.cleanup(LOG, in); } } return timeStamp; }
/** * @return The first image file with the given txid and image type. */ public File getFsImageName(long txid, NameNodeFile nnf) { for (Iterator<StorageDirectory> it = dirIterator(NameNodeDirType.IMAGE); it.hasNext();) { StorageDirectory sd = it.next(); File fsImage = getStorageFile(sd, nnf, txid); if (FileUtil.canRead(sd.getRoot()) && fsImage.exists()) { return fsImage; } } return null; }
/** * Determine the checkpoint time of the specified StorageDirectory * * @param sd StorageDirectory to check * @return If file exists and can be read, last checkpoint time. If not, 0L. * @throws IOException On errors processing file pointed to by sd */ static long readCheckpointTime(StorageDirectory sd) throws IOException { File timeFile = NNStorage.getStorageFile(sd, NameNodeFile.TIME); long timeStamp = 0L; if (timeFile.exists() && FileUtil.canRead(timeFile)) { DataInputStream in = new DataInputStream(new FileInputStream(timeFile)); try { timeStamp = in.readLong(); in.close(); in = null; } finally { IOUtils.cleanup(LOG, in); } } return timeStamp; }
/** * @return The first image file with the given txid and image type. */ public File getFsImageName(long txid, NameNodeFile nnf) { for (Iterator<StorageDirectory> it = dirIterator(NameNodeDirType.IMAGE); it.hasNext();) { StorageDirectory sd = it.next(); File fsImage = getStorageFile(sd, nnf, txid); if (FileUtil.canRead(sd.getRoot()) && fsImage.exists()) { return fsImage; } } return null; }
/** * @return The first image file whose txid is the same with the given txid and * image type is one of the given types. */ public File getFsImage(long txid, EnumSet<NameNodeFile> nnfs) { for (Iterator<StorageDirectory> it = dirIterator(NameNodeDirType.IMAGE); it.hasNext();) { StorageDirectory sd = it.next(); for (NameNodeFile nnf : nnfs) { File fsImage = getStorageFile(sd, nnf, txid); if (FileUtil.canRead(sd.getRoot()) && fsImage.exists()) { return fsImage; } } } return null; }
/** * Return the first readable storage file of the given name * across any of the 'current' directories in SDs of the * given type, or null if no such file exists. */ private File findFile(NameNodeDirType dirType, String name) { for (StorageDirectory sd : dirIterable(dirType)) { File candidate = new File(sd.getCurrentDir(), name); if (FileUtil.canRead(sd.getCurrentDir()) && candidate.exists()) { return candidate; } } return null; }
/** * @return The first image file whose txid is the same with the given txid and * image type is one of the given types. */ public File getFsImage(long txid, EnumSet<NameNodeFile> nnfs) { for (Iterator<StorageDirectory> it = dirIterator(NameNodeDirType.IMAGE); it.hasNext();) { StorageDirectory sd = it.next(); for (NameNodeFile nnf : nnfs) { File fsImage = getStorageFile(sd, nnf, txid); if (FileUtil.canRead(sd.getRoot()) && fsImage.exists()) { return fsImage; } } } return null; }
/** * Return the first readable storage file of the given name * across any of the 'current' directories in SDs of the * given type, or null if no such file exists. */ private File findFile(NameNodeDirType dirType, String name) { for (StorageDirectory sd : dirIterable(dirType)) { File candidate = new File(sd.getCurrentDir(), name); if (FileUtil.canRead(sd.getCurrentDir()) && candidate.exists()) { return candidate; } } return null; }
/** * Checks that the current running process can read, write, and execute the * given directory by using methods of the File object. * * @param dir File to check * @throws DiskErrorException if dir is not readable, not writable, or not * executable */ private static void checkAccessByFileMethods(File dir) throws DiskErrorException { if (!FileUtil.canRead(dir)) { throw new DiskErrorException("Directory is not readable: " + dir.toString()); } if (!FileUtil.canWrite(dir)) { throw new DiskErrorException("Directory is not writable: " + dir.toString()); } if (!FileUtil.canExecute(dir)) { throw new DiskErrorException("Directory is not executable: " + dir.toString()); } } }
/** * @return a debug string which can help diagnose an error of why * a given directory might have a permissions error in the context * of a test case */ private String createPermissionsDiagnosisString(File path) { StringBuilder sb = new StringBuilder(); while (path != null) { sb.append("path '" + path + "': ").append("\n"); sb.append("\tabsolute:").append(path.getAbsolutePath()).append("\n"); sb.append("\tpermissions: "); sb.append(path.isDirectory() ? "d": "-"); sb.append(FileUtil.canRead(path) ? "r" : "-"); sb.append(FileUtil.canWrite(path) ? "w" : "-"); sb.append(FileUtil.canExecute(path) ? "x" : "-"); sb.append("\n"); path = path.getParentFile(); } return sb.toString(); }