Exemplo n.º 1
0
    EntryInfo
    TeaSafe::getInfo(std::string const &path)
    {
        StateLock lock(m_stateMutex);
        auto thePath(path);
        char ch = *path.rbegin();
        // ignore trailing slash, but only if folder type
        // an entry of file type should never have a trailing
        // slash and is allowed to fail in this case
        if (ch == '/') {
            std::string(path.begin(), path.end() - 1).swap(thePath);
        }

        auto parentEntry(doGetParentCompoundFolder(thePath));
        if (!parentEntry) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }

        auto fname(boost::filesystem::path(thePath).filename().string());
        auto childInfo = parentEntry->getEntryInfo(boost::filesystem::path(thePath).filename().string());

        if (!childInfo) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }
        return *childInfo;
    }
Exemplo n.º 2
0
    TeaSafeFolder
    TeaSafe::getTeaSafeFolder(std::string const &path)
    {
        StateLock lock(m_stateMutex);
        std::string thePath(path);
        char ch = *path.rbegin();
        // ignore trailing slash, but only if folder type
        // an entry of file type should never have a trailing
        // slash and is allowed to fail in this case
        if (ch == '/') {
            std::string(path.begin(), path.end() - 1).swap(thePath);
        }

        SharedTeaSafeFolder parentEntry = doGetParentTeaSafeFolder(thePath);
        if (!parentEntry) {
            return *m_rootFolder;
        }

        SharedEntryInfo childInfo = parentEntry->getEntryInfo(boost::filesystem::path(path).filename().string());
        if (!childInfo) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }
        if (childInfo->type() == EntryType::FileType) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }
        return parentEntry->getTeaSafeFolder(boost::filesystem::path(path).filename().string());


    }
Exemplo n.º 3
0
    FileDevice
    TeaSafe::openFile(std::string const &path, OpenDisposition const &openMode)
    {
        StateLock lock(m_stateMutex);
        char ch = *path.rbegin();
        if (ch == '/') {
            throw TeaSafeException(TeaSafeError::NotFound);
        }

        auto parentEntry(doGetParentCompoundFolder(path));
        if (!parentEntry) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }

        /*
        auto childInfo(parentEntry->getEntryInfo(boost::filesystem::path(path).filename().string()));
        if (!childInfo) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }
        if (childInfo->type() == EntryType::FolderType) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }*/

        auto fe(this->setAndGetCachedFile(path, parentEntry, openMode));
        return FileDevice(fe);
    }
Exemplo n.º 4
0
    void
    TeaSafe::removeFile(std::string const &path)
    {
        StateLock lock(m_stateMutex);
        auto thePath(path);
        char ch = *path.rbegin();
        // ignore trailing slash
        if (ch == '/') {
            std::string(path.begin(), path.end() - 1).swap(thePath);
        }
        auto parentEntry(doGetParentCompoundFolder(thePath));
        if (!parentEntry) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }

        /*
        auto childInfo(parentEntry->getEntryInfo(boost::filesystem::path(thePath).filename().string()));
        if (!childInfo) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }
        if (childInfo->type() == EntryType::FolderType) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }*/

        try {
            parentEntry->removeFile(boost::filesystem::path(thePath).filename().string());
        } catch (...) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }

        // also remove it from the cache if it exists
        this->removeFileFromFileCache(path);

    }
Exemplo n.º 5
0
    TeaSafeFileDevice
    TeaSafe::openFile(std::string const &path, OpenDisposition const &openMode)
    {
        StateLock lock(m_stateMutex);
        char ch = *path.rbegin();
        if (ch == '/') {
            throw TeaSafeException(TeaSafeError::NotFound);
        }

        SharedTeaSafeFolder parentEntry = doGetParentTeaSafeFolder(path);
        if (!parentEntry) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }

        SharedEntryInfo childInfo = parentEntry->getEntryInfo(boost::filesystem::path(path).filename().string());
        if (!childInfo) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }
        if (childInfo->type() == EntryType::FolderType) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }

        TeaSafeFile fe = parentEntry->getTeaSafeFile(boost::filesystem::path(path).filename().string(), openMode);

        return TeaSafeFileDevice(fe);
    }
Exemplo n.º 6
0
 void
 TeaSafe::throwIfAlreadyExists(std::string const &path) const
 {
     // throw if already exists
     boost::filesystem::path processedPath(path);
     if (doFileExists(processedPath.string())) {
         throw TeaSafeException(TeaSafeError::AlreadyExists);
     }
     if (doFolderExists(processedPath.string())) {
         throw TeaSafeException(TeaSafeError::AlreadyExists);
     }
 }
Exemplo n.º 7
0
    void
    TeaSafe::renameEntry(std::string const &src, std::string const &dst)
    {
        StateLock lock(m_stateMutex);
        std::string srcPath(src);
        char ch = *src.rbegin();
        // ignore trailing slash
        if (ch == '/') {
            std::string(src.begin(), src.end() - 1).swap(srcPath);
        }
        std::string dstPath(dst);
        char chDst = *dst.rbegin();
        // ignore trailing slash
        if (chDst == '/') {
            std::string(dst.begin(), dst.end() - 1).swap(dstPath);
        }

        // throw if source parent doesn't exist
        SharedTeaSafeFolder parentSrc = doGetParentTeaSafeFolder(srcPath);
        if (!parentSrc) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }

        // throw if destination parent doesn't exist
        SharedTeaSafeFolder parentDst = doGetParentTeaSafeFolder(dstPath);
        if (!parentSrc) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }

        // throw if destination already exists
        throwIfAlreadyExists(dstPath);

        // throw if source doesn't exist
        std::string const filename = boost::filesystem::path(srcPath).filename().string();
        SharedEntryInfo childInfo = parentSrc->getEntryInfo(filename);
        if (!childInfo) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }

        // do moving / renaming
        // (i) Remove original entry metadata entry
        // (ii) Add new metadata entry with new file name
        parentSrc->putMetaDataOutOfUse(filename);
        std::string dstFilename = boost::filesystem::path(dstPath).filename().string();
        parentDst->writeNewMetaDataForEntry(dstFilename, childInfo->type(), childInfo->firstFileBlock());

    }
Exemplo n.º 8
0
    void
    TeaSafe::removeFolder(std::string const &path, FolderRemovalType const &removalType)
    {
        StateLock lock(m_stateMutex);
        auto thePath(path);
        char ch = *path.rbegin();
        // ignore trailing slash, but only if folder type
        // an entry of file type should never have a trailing
        // slash and is allowed to fail in this case
        if (ch == '/') {
            std::string(path.begin(), path.end() - 1).swap(thePath);
        }

        auto parentEntry(doGetParentCompoundFolder(thePath));
        if (!parentEntry) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }

        /*
        auto childInfo(parentEntry->getEntryInfo(boost::filesystem::path(thePath).filename().string()));
        if (!childInfo) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }
        if (childInfo->type() == EntryType::FileType) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }*/

        if (removalType == FolderRemovalType::MustBeEmpty) {

            auto childEntry(parentEntry->getFolder(boost::filesystem::path(thePath).filename().string()));
            if (!childEntry->listAllEntries().empty()) {
                throw TeaSafeException(TeaSafeError::FolderNotEmpty);
            }
        }

        try {
            parentEntry->removeFolder(boost::filesystem::path(thePath).filename().string());
        } catch (...) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }

        // also remove entry from parent cache
        this->removeDeletedParentFromCache(boost::filesystem::path(thePath));

    }
Exemplo n.º 9
0
    void
    TeaSafe::truncateFile(std::string const &path, std::ios_base::streamoff offset)
    {
        StateLock lock(m_stateMutex);
        SharedTeaSafeFolder parentEntry = doGetParentTeaSafeFolder(path);
        if (!parentEntry) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }

        SharedEntryInfo childInfo = parentEntry->getEntryInfo(boost::filesystem::path(path).filename().string());
        if (!childInfo) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }
        if (childInfo->type() == EntryType::FolderType) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }

        TeaSafeFile fe = parentEntry->getTeaSafeFile(boost::filesystem::path(path).filename().string(), OpenDisposition::buildOverwriteDisposition());
        fe.truncate(offset);
    }
Exemplo n.º 10
0
    void
    TeaSafe::addFile(std::string const &path)
    {
        StateLock lock(m_stateMutex);
        std::string thePath(path);
        char ch = *path.rbegin();
        // file entries with trailing slash should throw
        if (ch == '/') {
            throw TeaSafeException(TeaSafeError::IllegalFilename);
        }

        SharedTeaSafeFolder parentEntry = doGetParentTeaSafeFolder(thePath);
        if (!parentEntry) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }

        // throw if already exists
        throwIfAlreadyExists(path);

        parentEntry->addTeaSafeFile(boost::filesystem::path(thePath).filename().string());
    }
Exemplo n.º 11
0
    void
    TeaSafe::truncateFile(std::string const &path, std::ios_base::streamoff offset)
    {
        StateLock lock(m_stateMutex);
        auto parentEntry(doGetParentCompoundFolder(path));
        if (!parentEntry) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }

        /*
        auto childInfo(parentEntry->getEntryInfo(boost::filesystem::path(path).filename().string()));
        if (!childInfo) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }
        if (childInfo->type() == EntryType::FolderType) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }*/

        auto fe(this->setAndGetCachedFile(path, parentEntry, OpenDisposition::buildOverwriteDisposition()));
        fe->truncate(offset);
    }
Exemplo n.º 12
0
    void
    TeaSafe::addFolder(std::string const &path) const
    {
        StateLock lock(m_stateMutex);
        std::string thePath(path);
        char ch = *path.rbegin();
        // ignore trailing slash
        if (ch == '/') {
            std::string(path.begin(), path.end() - 1).swap(thePath);
        }

        SharedTeaSafeFolder parentEntry = doGetParentTeaSafeFolder(thePath);
        if (!parentEntry) {
            throw TeaSafeException(TeaSafeError::NotFound);
        }

        // throw if already exists
        throwIfAlreadyExists(path);

        parentEntry->addTeaSafeFolder(boost::filesystem::path(thePath).filename().string());
    }