diff --git a/extension/httpfs/src/cached_file_manager.cpp b/extension/httpfs/src/cached_file_manager.cpp index b06c33c566..2881e21028 100644 --- a/extension/httpfs/src/cached_file_manager.cpp +++ b/extension/httpfs/src/cached_file_manager.cpp @@ -25,10 +25,11 @@ std::unique_ptr CachedFileManager::getCachedFileInfo(HTTPFileInfo* htt auto fileName = FileSystem::getFileName(httpFileInfo->path); auto cacheFilePath = getCachedFilePath(fileName, transactionID); if (!vfs->fileOrPathExists(cacheFilePath)) { - auto cacheFileInfo = vfs->openFile(cacheFilePath, O_CREAT | O_RDWR); + auto cacheFileInfo = vfs->openFile(cacheFilePath, + FileFlags::CREATE_IF_NOT_EXISTS | FileFlags::READ_ONLY | FileFlags::WRITE); downloadFile(httpFileInfo, cacheFileInfo.get()); } - return vfs->openFile(cacheFilePath, O_RDONLY); + return vfs->openFile(cacheFilePath, FileFlags::READ_ONLY); } void CachedFileManager::cleanUP(main::ClientContext* context) { diff --git a/extension/httpfs/src/httpfs.cpp b/extension/httpfs/src/httpfs.cpp index c1bc4f060d..2eccbaa4d3 100644 --- a/extension/httpfs/src/httpfs.cpp +++ b/extension/httpfs/src/httpfs.cpp @@ -137,7 +137,7 @@ bool HTTPFileSystem::canHandleFile(const std::string& path) const { bool HTTPFileSystem::fileOrPathExists(const std::string& path, main::ClientContext* context) { try { - auto fileInfo = openFile(path, O_RDONLY, context, FileLockType::READ_LOCK); + auto fileInfo = openFile(path, FileFlags::READ_ONLY, context, FileLockType::READ_LOCK); auto httpFileInfo = fileInfo->constPtrCast(); if (httpFileInfo->length == 0) { return false; diff --git a/src/binder/bind/bind_import_database.cpp b/src/binder/bind/bind_import_database.cpp index b37ec73931..f66115c2c9 100644 --- a/src/binder/bind/bind_import_database.cpp +++ b/src/binder/bind/bind_import_database.cpp @@ -23,7 +23,8 @@ static std::string getQueryFromFile(common::VirtualFileSystem* vfs, const std::s if (!vfs->fileOrPathExists(filePath, context)) { throw BinderException(stringFormat("File {} does not exist.", filePath)); } - auto fileInfo = vfs->openFile(filePath, O_RDONLY + auto fileInfo = vfs->openFile(filePath, FileFlags::READ_ONLY +// TODO(Ziyi): We need to handle O_BINARY here. #ifdef _WIN32 | _O_BINARY #endif diff --git a/src/catalog/catalog.cpp b/src/catalog/catalog.cpp index 786248a38f..501aff4351 100644 --- a/src/catalog/catalog.cpp +++ b/src/catalog/catalog.cpp @@ -413,8 +413,8 @@ static void writeMagicBytes(Serializer& serializer) { void Catalog::saveToFile(const std::string& directory, common::VirtualFileSystem* fs, common::FileVersionType versionType) { auto catalogPath = StorageUtils::getCatalogFilePath(fs, directory, versionType); - Serializer serializer( - std::make_unique(fs->openFile(catalogPath, O_WRONLY | O_CREAT))); + Serializer serializer(std::make_unique(fs->openFile(catalogPath, + FileFlags::CREATE_IF_NOT_EXISTS | FileFlags::READ_ONLY | FileFlags::WRITE))); writeMagicBytes(serializer); serializer.serializeValue(StorageVersionInfo::getStorageVersion()); tables->serialize(serializer); @@ -426,8 +426,8 @@ void Catalog::saveToFile(const std::string& directory, common::VirtualFileSystem void Catalog::readFromFile(const std::string& directory, common::VirtualFileSystem* fs, common::FileVersionType versionType, main::ClientContext* context) { auto catalogPath = StorageUtils::getCatalogFilePath(fs, directory, versionType); - Deserializer deserializer( - std::make_unique(fs->openFile(catalogPath, O_RDONLY, context))); + Deserializer deserializer(std::make_unique( + fs->openFile(catalogPath, FileFlags::READ_ONLY, context))); validateMagicBytes(deserializer); storage_version_t savedStorageVersion; deserializer.deserializeValue(savedStorageVersion); diff --git a/src/include/storage/file_handle.h b/src/include/storage/file_handle.h index e6a45ec116..ae795f8af2 100644 --- a/src/include/storage/file_handle.h +++ b/src/include/storage/file_handle.h @@ -66,9 +66,7 @@ class FileHandle { protected: virtual common::page_idx_t addNewPageWithoutLock(); - void constructExistingFileHandle(const std::string& path, common::VirtualFileSystem* vfs, - main::ClientContext* context); - void constructNewFileHandle(const std::string& path, common::VirtualFileSystem* vfs, + void constructFileHandle(const std::string& path, common::VirtualFileSystem* vfs, main::ClientContext* context); protected: diff --git a/src/main/attached_database.cpp b/src/main/attached_database.cpp index 93fc5e02a7..3b7dae5b2e 100644 --- a/src/main/attached_database.cpp +++ b/src/main/attached_database.cpp @@ -30,8 +30,9 @@ void AttachedKuzuDatabase::initCatalog(const std::string& path, ClientContext* c } static void validateEmptyWAL(const std::string& path, ClientContext* context) { - auto walFile = context->getVFSUnsafe()->openFile( - path + "/" + common::StorageConstants::WAL_FILE_SUFFIX, O_RDONLY, context); + auto walFile = + context->getVFSUnsafe()->openFile(path + "/" + common::StorageConstants::WAL_FILE_SUFFIX, + common::FileFlags::READ_ONLY, context); if (walFile->getFileSize() > 0) { throw common::RuntimeException( common::stringFormat("Cannot attach a remote kuzu database with non-empty wal file.")); diff --git a/src/processor/operator/simple/export_db.cpp b/src/processor/operator/simple/export_db.cpp index b729ee17e0..bcb2fee92a 100644 --- a/src/processor/operator/simple/export_db.cpp +++ b/src/processor/operator/simple/export_db.cpp @@ -1,7 +1,5 @@ #include "processor/operator/simple/export_db.h" -#include - #include #include "catalog/catalog.h" @@ -26,7 +24,7 @@ using std::stringstream; static void writeStringStreamToFile(VirtualFileSystem* vfs, std::string ssString, const std::string& path) { - auto fileInfo = vfs->openFile(path, O_WRONLY | O_CREAT); + auto fileInfo = vfs->openFile(path, FileFlags::WRITE | FileFlags::CREATE_IF_NOT_EXISTS); fileInfo->writeFile(reinterpret_cast(ssString.c_str()), ssString.size(), 0 /* offset */); } diff --git a/src/storage/file_handle.cpp b/src/storage/file_handle.cpp index c5d549548b..e61a9a78e1 100644 --- a/src/storage/file_handle.cpp +++ b/src/storage/file_handle.cpp @@ -1,7 +1,5 @@ #include "storage/file_handle.h" -#include - #include #include @@ -15,38 +13,35 @@ namespace storage { FileHandle::FileHandle(const std::string& path, uint8_t flags, VirtualFileSystem* vfs, main::ClientContext* context) : flags{flags} { - if (!isNewTmpFile()) { - constructExistingFileHandle(path, vfs, context); - } else { - constructNewFileHandle(path, vfs, context); - } + constructFileHandle(path, vfs, context); } -void FileHandle::constructExistingFileHandle(const std::string& path, VirtualFileSystem* vfs, +void FileHandle::constructFileHandle(const std::string& path, VirtualFileSystem* vfs, main::ClientContext* context) { + if (isNewTmpFile()) { + numPages = 0; + pageCapacity = 0; + fileInfo = std::make_unique(path, nullptr /* fileSystem */); + return; + } int openFlags; if (isReadOnlyFile()) { openFlags = FileFlags::READ_ONLY; } else { - openFlags = FileFlags::WRITE | FileFlags::READ_ONLY | - ((createFileIfNotExists()) ? FileFlags::CREATE_IF_NOT_EXISTS : 0x00000000); + openFlags = FileFlags::WRITE | FileFlags::READ_ONLY; + if (createFileIfNotExists()) { + openFlags |= FileFlags::CREATE_IF_NOT_EXISTS; + } } fileInfo = vfs->openFile(path, openFlags, context); auto fileLength = fileInfo->getFileSize(); - numPages = ceil((double)fileLength / (double)getPageSize()); + numPages = ceil(static_cast(fileLength) / static_cast(getPageSize())); pageCapacity = 0; while (pageCapacity < numPages) { pageCapacity += StorageConstants::PAGE_GROUP_SIZE; } } -void FileHandle::constructNewFileHandle(const std::string& path, VirtualFileSystem* vfs, - main::ClientContext* context) { - fileInfo = vfs->openFile(path, O_CREAT | O_RDWR, context); - numPages = 0; - pageCapacity = 0; -} - page_idx_t FileHandle::addNewPage() { return addNewPages(1 /* numNewPages */); } diff --git a/src/storage/storage_manager.cpp b/src/storage/storage_manager.cpp index bbea8b7990..e7d7bd1494 100644 --- a/src/storage/storage_manager.cpp +++ b/src/storage/storage_manager.cpp @@ -98,7 +98,7 @@ void StorageManager::recover(main::ClientContext& clientContext) { WALReplayMode::RECOVERY_CHECKPOINT); walReplayer->replay(); // Truncate .wal and .shadow to empty. Remove catalog and stats wal files. - auto walFileInfo = vfs->openFile(walFilePath, O_RDWR); + auto walFileInfo = vfs->openFile(walFilePath, FileFlags::READ_ONLY | FileFlags::WRITE); if (walFileInfo->getFileSize() > 0) { walFileInfo->truncate(0); } diff --git a/src/storage/wal/wal.cpp b/src/storage/wal/wal.cpp index 40b2ba5828..5e91b186b4 100644 --- a/src/storage/wal/wal.cpp +++ b/src/storage/wal/wal.cpp @@ -1,7 +1,5 @@ #include "storage/wal/wal.h" -#include - #include "binder/ddl/bound_alter_info.h" #include "catalog/catalog_entry/sequence_catalog_entry.h" #include "common/file_system/file_info.h" @@ -22,7 +20,9 @@ WAL::WAL(const std::string& directory, bool readOnly, BufferManager& bufferManag : directory{directory}, bufferManager{bufferManager}, vfs{vfs} { auto fileInfo = vfs->openFile(vfs->joinPath(directory, std::string(StorageConstants::WAL_FILE_SUFFIX)), - readOnly ? O_RDONLY : O_CREAT | O_RDWR, context); + readOnly ? FileFlags::READ_ONLY : + FileFlags::CREATE_IF_NOT_EXISTS | FileFlags::READ_ONLY | FileFlags::WRITE, + context); bufferedWriter = std::make_shared(std::move(fileInfo)); shadowingFH = bufferManager.getBMFileHandle( vfs->joinPath(directory, std::string(StorageConstants::SHADOWING_SUFFIX)), diff --git a/src/storage/wal_replayer.cpp b/src/storage/wal_replayer.cpp index a6fb0b28a4..fd8225a782 100644 --- a/src/storage/wal_replayer.cpp +++ b/src/storage/wal_replayer.cpp @@ -41,7 +41,7 @@ void WALReplayer::replay() { if (!clientContext.getVFSUnsafe()->fileOrPathExists(walFilePath, &clientContext)) { return; } - auto fileInfo = clientContext.getVFSUnsafe()->openFile(walFilePath, O_RDONLY); + auto fileInfo = clientContext.getVFSUnsafe()->openFile(walFilePath, FileFlags::READ_ONLY); auto walFileSize = fileInfo->getFileSize(); // Check if the wal file is empty or corrupted. so nothing to read. if (walFileSize == 0) { diff --git a/tools/shell/shell_runner.cpp b/tools/shell/shell_runner.cpp index 60d3c83d6b..44b33c8d86 100644 --- a/tools/shell/shell_runner.cpp +++ b/tools/shell/shell_runner.cpp @@ -1,5 +1,3 @@ -#include - #include #include "args.hxx" @@ -64,9 +62,10 @@ int main(int argc, char* argv[]) { pathToHistory += "history.txt"; auto localFileSystem = std::make_unique(); try { - localFileSystem->openFile(pathToHistory, O_CREAT); + localFileSystem->openFile(pathToHistory, + FileFlags::CREATE_IF_NOT_EXISTS | FileFlags::WRITE | FileFlags::READ_ONLY); } catch (Exception& e) { - std::cerr << "Invalid path to directory for history file" << '\n'; + std::cerr << "Invalid path " + pathToHistory + " to directory for history file" << '\n'; return 1; } std::cout << "Opened the database at path: " << databasePath << " in "