CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/File/VFS/VFS.cpp
Views: 1401
#include <cstring>12#include "Common/Log.h"3#include "Common/File/VFS/VFS.h"4#include "Common/File/FileUtil.h"5#include "Common/File/AndroidStorage.h"6#include "Common/StringUtils.h"78VFS g_VFS;910void VFS::Register(const char *prefix, VFSBackend *reader) {11if (reader) {12entries_.push_back(VFSEntry{ prefix, reader });13DEBUG_LOG(Log::IO, "Registered VFS for prefix %s: %s", prefix, reader->toString().c_str());14} else {15ERROR_LOG(Log::IO, "Trying to register null VFS backend for prefix %s", prefix);16}17}1819void VFS::Clear() {20for (auto &entry : entries_) {21delete entry.reader;22}23entries_.clear();24}2526// TODO: Use Path more.27static bool IsLocalAbsolutePath(std::string_view path) {28bool isUnixLocal = path[0] == '/';29#ifdef _WIN3230bool isWindowsLocal = (isalpha(path[0]) && path[1] == ':') || startsWith(path, "\\\\") || startsWith(path, "//");31#else32bool isWindowsLocal = false;33#endif34bool isContentURI = Android_IsContentUri(path);35return isUnixLocal || isWindowsLocal || isContentURI;36}3738// The returned data should be free'd with delete[].39uint8_t *VFS::ReadFile(const char *filename, size_t *size) {40if (IsLocalAbsolutePath(filename)) {41// Local path, not VFS.42// INFO_LOG(Log::IO, "Not a VFS path: %s . Reading local file.", filename);43return File::ReadLocalFile(Path(filename), size);44}4546int fn_len = (int)strlen(filename);47bool fileSystemFound = false;48for (const auto &entry : entries_) {49int prefix_len = (int)strlen(entry.prefix);50if (prefix_len >= fn_len) continue;51if (0 == memcmp(filename, entry.prefix, prefix_len)) {52fileSystemFound = true;53// INFO_LOG(Log::IO, "Prefix match: %s (%s) -> %s", entries[i].prefix, filename, filename + prefix_len);54uint8_t *data = entry.reader->ReadFile(filename + prefix_len, size);55if (data)56return data;57else58continue;59// Else try the other registered file systems.60}61}62if (!fileSystemFound) {63ERROR_LOG(Log::IO, "Missing filesystem for '%s'", filename);64} // Otherwise, the file was just missing. No need to log.65return nullptr;66}6768bool VFS::GetFileListing(const char *path, std::vector<File::FileInfo> *listing, const char *filter) {69if (IsLocalAbsolutePath(path)) {70// Local path, not VFS.71// INFO_LOG(Log::IO, "Not a VFS path: %s . Reading local directory.", path);72File::GetFilesInDir(Path(std::string(path)), listing, filter);73return true;74}7576int fn_len = (int)strlen(path);77bool fileSystemFound = false;78for (const auto &entry : entries_) {79int prefix_len = (int)strlen(entry.prefix);80if (prefix_len >= fn_len) continue;81if (0 == memcmp(path, entry.prefix, prefix_len)) {82fileSystemFound = true;83if (entry.reader->GetFileListing(path + prefix_len, listing, filter)) {84return true;85}86}87}8889if (!fileSystemFound) {90ERROR_LOG(Log::IO, "Missing filesystem for %s", path);91} // Otherwise, the file was just missing. No need to log.92return false;93}9495bool VFS::GetFileInfo(const char *path, File::FileInfo *info) {96if (IsLocalAbsolutePath(path)) {97// Local path, not VFS.98// INFO_LOG(Log::IO, "Not a VFS path: %s . Getting local file info.", path);99return File::GetFileInfo(Path(std::string(path)), info);100}101102bool fileSystemFound = false;103int fn_len = (int)strlen(path);104for (const auto &entry : entries_) {105int prefix_len = (int)strlen(entry.prefix);106if (prefix_len >= fn_len) continue;107if (0 == memcmp(path, entry.prefix, prefix_len)) {108fileSystemFound = true;109if (entry.reader->GetFileInfo(path + prefix_len, info))110return true;111else112continue;113}114}115if (!fileSystemFound) {116ERROR_LOG(Log::IO, "Missing filesystem for '%s'", path);117} // Otherwise, the file was just missing. No need to log.118return false;119}120121122