/// attempts to implement tab complete by matching the provided string /// with an entry at the given parent path std::string tabCompleteTeaSafeEntry(teasafe::TeaSafe &theBfs, std::string const &path) { // make sure path begins with a slash std::string thePath(*path.begin() != '/' ? "/" : ""); thePath.append(path); // find out the parent of the thing we're trying to tab complete // the item we're trying to tab-complete should be a child of this parent boost::filesystem::path bp(path); auto parentPath(bp.parent_path().string()); // get the parent folder auto folder = theBfs.getFolder(parentPath); // iterate over entries in folder auto & entries = folder.listAllEntries(); for (auto const &it : entries) { // try to match the entry with the thing that we want to tab-complete auto extracted(it.second->filename().substr(0, bp.filename().string().length())); if(extracted == bp.filename()) { return it.second->filename(); // match, return name of entry } } return bp.filename().string(); // no match, return non tab-completed token }
// list the directory contents static int teasafe_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t, struct fuse_file_info *) { try { auto folder(TeaSafe_DATA->getFolder(path)); auto & infos(folder.listAllEntries()); filler(buf, ".", NULL, 0); /* Current directory (.) */ filler(buf, "..", NULL, 0); for(auto const &it : infos) { struct stat stbuf; if (it.second->type() == teasafe::EntryType::FileType) { stbuf.st_mode = S_IFREG | 0755; stbuf.st_nlink = 1; stbuf.st_size = it.second->size(); } else { stbuf.st_mode = S_IFDIR | 0744; stbuf.st_nlink = 3; } filler(buf, it.second->filename().c_str(), &stbuf, 0); } } catch (teasafe::TeaSafeException const &e) { return detail::exceptionDispatch(e); } return 0; }
void CoreFS::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 KnoxCryptException(KnoxCryptError::NotFound); } /* auto childInfo(parentEntry->getEntryInfo(boost::filesystem::path(thePath).filename().string())); if (!childInfo) { throw KnoxCryptException(KnoxCryptError::NotFound); } if (childInfo->type() == EntryType::FileType) { throw KnoxCryptException(KnoxCryptError::NotFound); }*/ auto boostPath = ::boost::filesystem::path(thePath); if (removalType == FolderRemovalType::MustBeEmpty) { auto childEntry(parentEntry->getFolder(boostPath.filename().string())); if (!childEntry->listAllEntries().empty()) { throw KnoxCryptException(KnoxCryptError::FolderNotEmpty); } } try { parentEntry->removeFolder(boostPath.filename().string()); } catch (...) { throw KnoxCryptException(KnoxCryptError::NotFound); } // also remove entry and its parent from parent cache this->removeFolderFromCache(boostPath); this->removeFolderFromCache(boostPath.parent_path()); // need to also check if this now f***s up the cached file resetCachedFile(thePath); }
/// the 'ls' command for listing dir contents void com_ls(teasafe::TeaSafe &theBfs, std::string const &path) { // make sure path begins with a slash std::string thePath(*path.begin() != '/' ? "/" : ""); thePath.append(path); // iterate over entries in folder and print filenames of each auto folder = theBfs.getFolder(thePath); auto & entries = folder.listAllEntries(); for (auto const &it : entries) { if (it.second->type() == teasafe::EntryType::FileType) { std::cout<<boost::format("%1% %|30t|%2%\n") % it.second->filename() % "<F>"; } else { std::cout<<boost::format("%1% %|30t|%2%\n") % it.second->filename() % "<D>"; } } }