Search in sources :

Example 51 with FileDoesNotExistException

use of alluxio.exception.FileDoesNotExistException in project alluxio by Alluxio.

the class AlluxioJniFuseFileSystem method truncateInternal.

private int truncateInternal(String path, long size) {
    // Truncate scenarios:
    // 1. Truncate size = 0, file does not exist => no-op
    // 2. Truncate size = 0, file exists and completed
    // => noop if file size = 0 or delete file
    // TODO(lu) delete open file entry if any, blocked by libfuse 3
    // 3. Truncate size = 0, file exists and is being written by current Fuse
    // => noop if written size = 0, otherwise delete file and update create file entry
    // 4. Truncate size = 0, file exists and is being written by other applications
    // => error out
    // 5. Truncate size != 0, file does not exist => error out
    // 6. Truncate size != 0, file is completed
    // => no-op if file size = truncate size, error out otherwise
    // 7. Truncate size != 0, file is being written by other applications
    // => error out, don't know exact file written size
    // 8. Truncate size != 0, file is being written by current Fuse
    // => no-op if written size = truncate size, error out otherwise
    // Error out in all other cases
    final AlluxioURI uri = mPathResolverCache.getUnchecked(path);
    URIStatus status = null;
    try {
        status = mFileSystem.getStatus(uri);
    } catch (FileDoesNotExistException | InvalidPathException e) {
        status = null;
    } catch (Throwable t) {
        LOG.error("Failed to truncate path {} to {}. Failed to get file status", path, size, t);
        return -ErrorCodes.EIO();
    }
    CreateFileEntry<FileOutStream> ce = mCreateFileEntries.getFirstByField(PATH_INDEX, path);
    if (size == 0) {
        if (status == null) {
            // Case 1: Truncate size = 0, file does not exist => no-op
            return 0;
        }
        try {
            // Case 2: Truncate size = 0, file exists and completed
            if (status.isCompleted()) {
                if (status.getLength() == 0) {
                    return 0;
                }
                // support open(O_WRONLY or O_RDWR) -> truncate() -> write()
                mFileSystem.delete(uri);
                // because concurrent read and delete are not supported
                return 0;
            }
            // Case 3: Truncate size = 0, file exists and is being written by current Fuse
            if (ce != null) {
                FileOutStream os = ce.getOut();
                long bytesWritten = os.getBytesWritten();
                if (bytesWritten == 0) {
                    return 0;
                }
                mCreateFileEntries.remove(ce);
                mFileSystem.delete(uri);
                os = mFileSystem.createFile(uri);
                final long fd = ce.getId();
                ce = new CreateFileEntry(fd, path, os);
                mCreateFileEntries.add(ce);
                mAuthPolicy.setUserGroupIfNeeded(uri);
                return 0;
            }
            // Case 4: Truncate size = 0, file exists and is being written by other applications
            LOG.error("Failed to truncate path {} to size 0, " + "file is being written in other applications", path);
            return -ErrorCodes.EOPNOTSUPP();
        } catch (Throwable t) {
            LOG.error("Failed to truncate path {} to {}. Failed to delete path {} from Alluxio", path, size, path);
            return -ErrorCodes.EIO();
        }
    }
    // Case 5: Truncate size != 0, file does not exist => error out
    if (status == null) {
        LOG.error("Cannot truncate non-existing file {} to size {}", path, size);
        return -ErrorCodes.EEXIST();
    }
    // Case 6: Truncate size != 0, file is completed
    if (status.isCompleted()) {
        if (size == status.getLength()) {
            return 0;
        }
        LOG.error("Cannot truncate file {} to non-zero size {}", path, size);
        return -ErrorCodes.EOPNOTSUPP();
    }
    // Case 7: Truncate size != 0, file is being written by other applications
    if (ce == null) {
        LOG.error("Cannot truncate {} to {}. " + "File is being written in other Fuse applications or APIs", path, size);
        return -ErrorCodes.EOPNOTSUPP();
    }
    // Case 8: Truncate size != 0, file is being written by current Fuse
    FileOutStream os = ce.getOut();
    long bytesWritten = os.getBytesWritten();
    if (bytesWritten == size) {
        return 0;
    }
    // error out otherwise
    LOG.error("Truncate file {} of size {} to size {} is not supported. " + "Alluxio supports sequential writes only and the written contents cannot be modified", path, bytesWritten, size);
    return -ErrorCodes.EOPNOTSUPP();
}
Also used : FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) FileOutStream(alluxio.client.file.FileOutStream) URIStatus(alluxio.client.file.URIStatus) InvalidPathException(java.nio.file.InvalidPathException) AlluxioURI(alluxio.AlluxioURI)

Example 52 with FileDoesNotExistException

use of alluxio.exception.FileDoesNotExistException in project alluxio by Alluxio.

the class AlluxioJniFuseFileSystem method getattrInternal.

private int getattrInternal(String path, FileStat stat) {
    final AlluxioURI uri = mPathResolverCache.getUnchecked(path);
    try {
        URIStatus status = null;
        // Handle special metadata cache operation
        if (mConf.getBoolean(PropertyKey.FUSE_SPECIAL_COMMAND_ENABLED) && mFuseShell.isSpecialCommand(uri)) {
            // TODO(lu) add cache for isFuseSpecialCommand if needed
            status = mFuseShell.runCommand(uri);
        } else {
            status = mFileSystem.getStatus(uri);
        }
        long size = status.getLength();
        if (!status.isCompleted()) {
            if (mCreateFileEntries.contains(PATH_INDEX, path)) {
                // Alluxio master will not update file length until file is completed
                // get file length from the current output stream
                CreateFileEntry<FileOutStream> ce = mCreateFileEntries.getFirstByField(PATH_INDEX, path);
                if (ce != null) {
                    FileOutStream os = ce.getOut();
                    size = os.getBytesWritten();
                }
            } else if (!AlluxioFuseUtils.waitForFileCompleted(mFileSystem, uri)) {
                // Always block waiting for file to be completed except when the file is writing
                // We do not want to block the writing process
                LOG.error("File {} is not completed", path);
            } else {
                // Update the file status after waiting
                status = mFileSystem.getStatus(uri);
                size = status.getLength();
            }
        }
        stat.st_size.set(size);
        // Sets block number to fulfill du command needs
        // `st_blksize` is ignored in `getattr` according to
        // https://github.com/libfuse/libfuse/blob/d4a7ba44b022e3b63fc215374d87ed9e930d9974/include/fuse.h#L302
        // According to http://man7.org/linux/man-pages/man2/stat.2.html,
        // `st_blocks` is the number of 512B blocks allocated
        stat.st_blocks.set((int) Math.ceil((double) size / 512));
        final long ctime_sec = status.getLastModificationTimeMs() / 1000;
        final long atime_sec = status.getLastAccessTimeMs() / 1000;
        // Keeps only the "residual" nanoseconds not caputred in citme_sec
        final long ctime_nsec = (status.getLastModificationTimeMs() % 1000) * 1_000_000L;
        final long atime_nsec = (status.getLastAccessTimeMs() % 1000) * 1_000_000L;
        stat.st_atim.tv_sec.set(atime_sec);
        stat.st_atim.tv_nsec.set(atime_nsec);
        stat.st_ctim.tv_sec.set(ctime_sec);
        stat.st_ctim.tv_nsec.set(ctime_nsec);
        stat.st_mtim.tv_sec.set(ctime_sec);
        stat.st_mtim.tv_nsec.set(ctime_nsec);
        if (mIsUserGroupTranslation) {
            // Translate the file owner/group to unix uid/gid
            // Show as uid==-1 (nobody) if owner does not exist in unix
            // Show as gid==-1 (nogroup) if group does not exist in unix
            stat.st_uid.set(mUidCache.get(status.getOwner()));
            stat.st_gid.set(mGidCache.get(status.getGroup()));
        } else {
            stat.st_uid.set(AlluxioFuseUtils.DEFAULT_UID);
            stat.st_gid.set(AlluxioFuseUtils.DEFAULT_GID);
        }
        int mode = status.getMode();
        if (status.isFolder()) {
            mode |= FileStat.S_IFDIR;
        } else {
            mode |= FileStat.S_IFREG;
        }
        stat.st_mode.set(mode);
        stat.st_nlink.set(1);
    } catch (FileDoesNotExistException | InvalidPathException e) {
        LOG.debug("Failed to get info of {}, path does not exist or is invalid", path);
        return -ErrorCodes.ENOENT();
    } catch (AccessControlException e) {
        LOG.error("Permission denied when getattr {}: ", path, e);
        return -ErrorCodes.EACCES();
    } catch (Throwable e) {
        LOG.error("Failed to getattr {}: ", path, e);
        return -ErrorCodes.EIO();
    }
    return 0;
}
Also used : FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) FileOutStream(alluxio.client.file.FileOutStream) AccessControlException(alluxio.exception.AccessControlException) URIStatus(alluxio.client.file.URIStatus) InvalidPathException(java.nio.file.InvalidPathException) AlluxioURI(alluxio.AlluxioURI)

Example 53 with FileDoesNotExistException

use of alluxio.exception.FileDoesNotExistException in project alluxio by Alluxio.

the class AlluxioFuseFileSystem method getattrInternal.

private int getattrInternal(String path, FileStat stat) {
    final AlluxioURI turi = mPathResolverCache.getUnchecked(path);
    try {
        URIStatus status = mFileSystem.getStatus(turi);
        if (!status.isCompleted()) {
            // We do not want to block the writing process
            if (!mOpenFiles.contains(PATH_INDEX, path) && !AlluxioFuseUtils.waitForFileCompleted(mFileSystem, turi)) {
                LOG.error("File {} is not completed", path);
            }
            status = mFileSystem.getStatus(turi);
        }
        long size = status.getLength();
        stat.st_size.set(size);
        // Sets block number to fulfill du command needs
        // `st_blksize` is ignored in `getattr` according to
        // https://github.com/libfuse/libfuse/blob/d4a7ba44b022e3b63fc215374d87ed9e930d9974/include/fuse.h#L302
        // According to http://man7.org/linux/man-pages/man2/stat.2.html,
        // `st_blocks` is the number of 512B blocks allocated
        stat.st_blocks.set((int) Math.ceil((double) size / 512));
        final long ctime_sec = status.getLastModificationTimeMs() / 1000;
        // Keeps only the "residual" nanoseconds not caputred in citme_sec
        final long ctime_nsec = (status.getLastModificationTimeMs() % 1000) * 1000;
        stat.st_ctim.tv_sec.set(ctime_sec);
        stat.st_ctim.tv_nsec.set(ctime_nsec);
        stat.st_mtim.tv_sec.set(ctime_sec);
        stat.st_mtim.tv_nsec.set(ctime_nsec);
        if (mIsUserGroupTranslation) {
            // Translate the file owner/group to unix uid/gid
            // Show as uid==-1 (nobody) if owner does not exist in unix
            // Show as gid==-1 (nogroup) if group does not exist in unix
            stat.st_uid.set(AlluxioFuseUtils.getUid(status.getOwner()));
            stat.st_gid.set(AlluxioFuseUtils.getGidFromGroupName(status.getGroup()));
        } else {
            stat.st_uid.set(UID);
            stat.st_gid.set(GID);
        }
        int mode = status.getMode();
        if (status.isFolder()) {
            mode |= FileStat.S_IFDIR;
        } else {
            mode |= FileStat.S_IFREG;
        }
        stat.st_mode.set(mode);
        stat.st_nlink.set(1);
    } catch (FileDoesNotExistException | InvalidPathException e) {
        LOG.debug("Failed to get info of {}, path does not exist or is invalid", path);
        return -ErrorCodes.ENOENT();
    } catch (Throwable t) {
        LOG.error("Failed to get info of {}", path, t);
        return AlluxioFuseUtils.getErrorCode(t);
    }
    return 0;
}
Also used : FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) URIStatus(alluxio.client.file.URIStatus) InvalidPathException(java.nio.file.InvalidPathException) AlluxioURI(alluxio.AlluxioURI)

Example 54 with FileDoesNotExistException

use of alluxio.exception.FileDoesNotExistException in project alluxio by Alluxio.

the class AlluxioJniFuseFileSystemTest method renameOldNotExist.

@Test
public void renameOldNotExist() throws Exception {
    AlluxioURI oldPath = BASE_EXPECTED_URI.join("/old");
    AlluxioURI newPath = BASE_EXPECTED_URI.join("/new");
    doThrow(new FileDoesNotExistException("File /old does not exist")).when(mFileSystem).rename(oldPath, newPath);
    assertEquals(-ErrorCodes.ENOENT(), mFuseFs.rename("/old", "/new"));
}
Also used : FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) AlluxioURI(alluxio.AlluxioURI) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 55 with FileDoesNotExistException

use of alluxio.exception.FileDoesNotExistException in project alluxio by Alluxio.

the class RmCommand method runPlainPath.

@Override
protected void runPlainPath(AlluxioURI path, CommandLine cl) throws AlluxioException, IOException {
    // TODO(calvin): Remove explicit state checking.
    boolean recursive = cl.hasOption(RECURSIVE_OPTION.getOpt()) || cl.hasOption(RECURSIVE_ALIAS_OPTION.getOpt());
    if (!mFileSystem.exists(path)) {
        throw new FileDoesNotExistException(ExceptionMessage.PATH_DOES_NOT_EXIST.getMessage(path));
    }
    if (!recursive && mFileSystem.getStatus(path).isFolder()) {
        throw new IOException(path.getPath() + " is a directory, to remove it," + " please use \"rm -R/-r/--recursive <path>\"");
    }
    boolean isAlluxioOnly = cl.hasOption(REMOVE_ALLUXIO_ONLY.getLongOpt());
    DeletePOptions options = DeletePOptions.newBuilder().setRecursive(recursive).setAlluxioOnly(isAlluxioOnly).setUnchecked(cl.hasOption(REMOVE_UNCHECKED_OPTION_CHAR)).build();
    mFileSystem.delete(path, options);
    if (!isAlluxioOnly) {
        System.out.println(path + " has been removed");
    } else {
        System.out.println(path + " has been removed only from Alluxio space");
    }
}
Also used : FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) DeletePOptions(alluxio.grpc.DeletePOptions) IOException(java.io.IOException)

Aggregations

FileDoesNotExistException (alluxio.exception.FileDoesNotExistException)126 AlluxioURI (alluxio.AlluxioURI)90 InvalidPathException (alluxio.exception.InvalidPathException)42 IOException (java.io.IOException)39 URIStatus (alluxio.client.file.URIStatus)34 Test (org.junit.Test)31 AlluxioException (alluxio.exception.AlluxioException)26 ArrayList (java.util.ArrayList)26 LockedInodePath (alluxio.master.file.meta.LockedInodePath)22 AccessControlException (alluxio.exception.AccessControlException)19 FileAlreadyExistsException (alluxio.exception.FileAlreadyExistsException)14 Inode (alluxio.master.file.meta.Inode)14 FileInfo (alluxio.wire.FileInfo)14 BlockInfoException (alluxio.exception.BlockInfoException)13 BaseIntegrationTest (alluxio.testutils.BaseIntegrationTest)10 UnavailableException (alluxio.exception.status.UnavailableException)9 BlockInfo (alluxio.wire.BlockInfo)9 FileBlockInfo (alluxio.wire.FileBlockInfo)9 DirectoryNotEmptyException (alluxio.exception.DirectoryNotEmptyException)8 InodeFile (alluxio.master.file.meta.InodeFile)7