/** * @param msg Error message. * @return Checked exception. */ private static IgniteCheckedException fsException(String msg) { return new IgniteCheckedException(new IgfsException(msg)); }
/** * Converts any passed exception to IGFS exception. * * @param err Initial exception. * @return Converted IGFS exception. */ public static IgfsException toIgfsException(Throwable err) { IgfsException err0 = err instanceof IgfsException ? (IgfsException)err : null; IgfsException igfsErr = X.cause(err, IgfsException.class); while (igfsErr != null && igfsErr != err0) { err0 = igfsErr; igfsErr = X.cause(err, IgfsException.class); } // If initial exception is already IGFS exception and no inner stuff exists, just return it unchanged. if (err0 != err) { if (err0 != null) // Dealing with a kind of IGFS error, wrap it once again, preserving message and root cause. err0 = newIgfsException(err0.getClass(), err0.getMessage(), err0); else { if (err instanceof ClusterTopologyServerNotFoundException) err0 = new IgfsException("Cache server nodes not found.", err); else // Unknown error nature. err0 = new IgfsException("Generic IGFS error occurred.", err); } } return err0; }
/** * Heuristically checks if exception was caused by invalid HDFS version and returns appropriate exception. * * @param e Exception to check. * @param msg Detailed error message. * @return Appropriate exception. */ private IgfsException handleSecondaryFsError(IOException e, String msg) { if (e instanceof FileNotFoundException) return new IgfsPathNotFoundException(e); else return new IgfsException(msg, e); }
/** * Converts any passed exception to IGFS exception. * * @param err Initial exception. * @return Converted IGFS exception. */ @SuppressWarnings("ThrowableResultOfMethodCallIgnored") public static IgfsException toIgfsException(Throwable err) { IgfsException err0 = err instanceof IgfsException ? (IgfsException)err : null; IgfsException igfsErr = X.cause(err, IgfsException.class); while (igfsErr != null && igfsErr != err0) { err0 = igfsErr; igfsErr = X.cause(err, IgfsException.class); } // If initial exception is already IGFS exception and no inner stuff exists, just return it unchanged. if (err0 != err) { if (err0 != null) // Dealing with a kind of IGFS error, wrap it once again, preserving message and root cause. err0 = newIgfsException(err0.getClass(), err0.getMessage(), err0); else { if (err instanceof ClusterTopologyServerNotFoundException) err0 = new IgfsException("Cache server nodes not found.", err); else // Unknown error nature. err0 = new IgfsException("Generic IGFS error occurred.", err); } } return err0; }
/** * Create IGFS path for file. * * @param f File object. * @return IFGS path. * @throws IgfsException If failed. */ private IgfsPath igfsPath(File f) throws IgfsException { String path = f.getAbsolutePath(); if (workDir != null) { if (!path.startsWith(workDir)) throw new IgfsException("Path is not located in the work directory [workDir=" + workDir + ", path=" + path + ']'); path = path.substring(workDir.length(), path.length()); } return new IgfsPath(path); }
/** * Get POSIX attributes for file. * * @param file File. * @return PosixFileAttributes. */ @Nullable public static PosixFileAttributes posixAttributes(File file) { PosixFileAttributes attrs = null; try { PosixFileAttributeView view = Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class); if (view != null) attrs = view.readAttributes(); } catch (IOException e) { throw new IgfsException("Failed to read POSIX attributes: " + file.getAbsolutePath(), e); } return attrs; }
/** * Get POSIX attributes for file. * * @param file File. * @return BasicFileAttributes. */ @Nullable public static BasicFileAttributes basicAttributes(File file) { BasicFileAttributes attrs = null; try { BasicFileAttributeView view = Files.getFileAttributeView(file.toPath(), BasicFileAttributeView.class); if (view != null) attrs = view.readAttributes(); } catch (IOException e) { throw new IgfsException("Failed to read basic file attributes: " + file.getAbsolutePath(), e); } return attrs; }
/** {@inheritDoc} */ @Override public long usedSpaceSize() { Path p = fileForPath(IgfsPath.ROOT).toPath(); try { LocalFileSystemSizeVisitor visitor = new LocalFileSystemSizeVisitor(); Files.walkFileTree(p, visitor); return visitor.size(); } catch (IOException e) { throw new IgfsException("Failed to calculate used space size.", e); } }
/** {@inheritDoc} */ @Override public void rename(IgfsPath src, IgfsPath dest) { File srcFile = fileForPath(src); File destFile = fileForPath(dest); if (!srcFile.exists()) throw new IgfsPathNotFoundException("Failed to perform rename because source path not found: " + src); if (srcFile.isDirectory() && destFile.isFile()) throw new IgfsPathIsNotDirectoryException("Failed to perform rename because destination path is " + "directory and source path is file [src=" + src + ", dest=" + dest + ']'); try { if (destFile.isDirectory()) Files.move(srcFile.toPath(), destFile.toPath().resolve(srcFile.getName())); else if(!srcFile.renameTo(destFile)) throw new IgfsException("Failed to perform rename (underlying file system returned false) " + "[src=" + src + ", dest=" + dest + ']'); } catch (IOException e) { throw handleSecondaryFsError(e, "Failed to rename [src=" + src + ", dest=" + dest + ']'); } }
throw new IgfsException("Update the '" + IgfsUtils.PROP_GROUP_NAME + "' property is failed.", e); throw new IgfsException("Update the '" + IgfsUtils.PROP_PERMISSION + "' property is failed.", e);
/** {@inheritDoc} */ @Override public void rename(IgfsPath src, IgfsPath dest) { // Delegate to the secondary file system. try { if (!fileSystemForUser().rename(convert(src), convert(dest))) throw new IgfsException("Failed to rename (secondary file system returned false) " + "[src=" + src + ", dest=" + dest + ']'); } catch (IOException e) { throw handleSecondaryFsError(e, "Failed to rename file [src=" + src + ", dest=" + dest + ']'); } }
/** * Internal create routine. * * @param path Path. * @param overwrite Overwrite flag. * @return Output stream. */ private OutputStream create0(IgfsPath path, boolean overwrite) { File file = fileForPath(path); boolean exists = file.exists(); if (exists) { if (!overwrite) throw new IgfsPathAlreadyExistsException("Failed to create a file because it already exists: " + path); } else { File parent = file.getParentFile(); if (!mkdirs0(parent)) throw new IgfsException("Failed to create parent directory for file (underlying file system " + "returned false): " + path); } try { return new FileOutputStream(file); } catch (IOException e) { throw handleSecondaryFsError(e, "Failed to create file [path=" + path + ", overwrite=" + overwrite + ']'); } }
/** * Cast IO exception to IGFS exception. * * @param msg Error message. * @param e IO exception. * @return IGFS exception. */ public static IgfsException cast(String msg, IOException e) { if (e instanceof FileNotFoundException) return new IgfsPathNotFoundException(e); else if (e instanceof ParentNotDirectoryException) return new IgfsParentNotDirectoryException(msg, e); else if (e instanceof PathIsNotEmptyDirectoryException) return new IgfsDirectoryNotEmptyException(e); else if (e instanceof PathExistsException) return new IgfsPathAlreadyExistsException(msg, e); else return new IgfsException(msg, e); }
/** * Constructs file output stream. * * @param igfsCtx IGFS context. * @param path Path to stored file. * @param fileInfo File info to write binary data to. * @param bufSize The size of the buffer to be used. * @param mode Grid IGFS mode. * @param batch Optional secondary file system batch. */ IgfsOutputStreamImpl(IgfsContext igfsCtx, IgfsPath path, IgfsEntryInfo fileInfo, int bufSize, IgfsMode mode, @Nullable IgfsFileWorkerBatch batch) { super(igfsCtx, path, bufSize, batch); assert fileInfo != null && fileInfo.isFile() : "Unexpected file info: " + fileInfo; assert mode != null && mode != PROXY && (mode == PRIMARY && batch == null || batch != null); // File hasn't been locked. if (fileInfo.lockId() == null) throw new IgfsException("Failed to acquire file lock (concurrently modified?): " + path); synchronized (mux) { this.fileInfo = fileInfo; this.mode = mode; streamRange = initialStreamRange(fileInfo); writeFut = igfsCtx.data().writeStart(fileInfo.id()); } }
throw new IgfsException("Not enough space reserved to store data [id=" + id + ", reservedLen=" + reservedLen + ", remainderLen=" + remainderLen + ", data.length=" + srcLen + ']');
/** * Run client task. * * @param task Task. * @return Result. */ <T> T runClientTask(IgfsClientAbstractCallable<T> task) { try { return (cfg.isColocateMetadata()) ? clientCompute().affinityCall(metaCacheName, IgfsUtils.ROOT_ID, task) : clientCompute().call(task); } catch (Exception e) { if (X.hasCause(e, ClusterTopologyException.class)) throw new IgfsException("Failed to execute operation because there are no IGFS metadata nodes." , e); IgfsException igfsEx = X.cause(e, IgfsException.class); if (igfsEx != null) throw igfsEx; throw e; } }
/** * @param errCode Error code. * @param err Error. * @throws IgniteCheckedException Based on error code. */ public static void throwError(Integer errCode, String err) throws IgniteCheckedException { assert err != null; assert errCode != -1; if (errCode == ERR_FILE_NOT_FOUND) throw new IgfsPathNotFoundException(err); else if (errCode == ERR_PATH_ALREADY_EXISTS) throw new IgfsPathAlreadyExistsException(err); else if (errCode == ERR_DIRECTORY_NOT_EMPTY) throw new IgfsDirectoryNotEmptyException(err); else if (errCode == ERR_PARENT_NOT_DIRECTORY) throw new IgfsParentNotDirectoryException(err); else if (errCode == ERR_INVALID_HDFS_VERSION) throw new IgfsInvalidHdfsVersionException(err); else if (errCode == ERR_CORRUPTED_FILE) throw new IgfsCorruptedFileException(err); else if (errCode == ERR_IGFS_GENERIC) throw new IgfsException(err); throw new IgniteCheckedException(err); }
throw new IgfsException("Cannot remove root directory");
throw new IgfsException("File is already opened for write: " + path);
throw new IgfsException("Failed to get affinity (range is too wide)" + " [info=" + info + ", start=" + start + ", len=" + len + ']');