Skip to content

Commit fb35da9

Browse files
committed
Fixup access flags for checking files (without actually creating them)
1 parent ffc436b commit fb35da9

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

packages/duckdb-wasm/src/bindings/runtime.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ export enum FileFlags {
5858
FILE_FLAGS_FILE_CREATE_NEW = 1 << 4,
5959
//! Open file in append mode
6060
FILE_FLAGS_APPEND = 1 << 5,
61+
FILE_FLAGS_PRIVATE = 1 << 6,
62+
FILE_FLAGS_NULL_IF_NOT_EXISTS = 1 << 7,
63+
FILE_FLAGS_PARALLEL_ACCESS = 1 << 8,
64+
FILE_FLAGS_EXCLUSIVE_CREATE = 1 << 9,
65+
FILE_FLAGS_NULL_IF_EXISTS = 1 << 10
6166
}
6267

6368
/** Configuration for the AWS S3 Filesystem */

packages/duckdb-wasm/src/bindings/runtime_browser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,11 @@ export const BROWSER_RUNTIME: DuckDBRuntime & {
381381
return result;
382382
}
383383

384+
// Depending on file flags, return nullptr
385+
if (flags & FileFlags.FILE_FLAGS_NULL_IF_NOT_EXISTS) {
386+
return 0;
387+
}
388+
384389
// Fall back to empty buffered file in the browser
385390
console.warn(`Buffering missing file: ${file.fileName}`);
386391
const result = mod._malloc(2 * 8);

packages/duckdb-wasm/src/bindings/runtime_node.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ export const NODE_RUNTIME: DuckDBRuntime & {
7676
case DuckDBDataProtocol.NODE_FS: {
7777
let fd = NODE_RUNTIME._files?.get(file.dataUrl!);
7878
if (fd === null || fd === undefined) {
79+
// Depending on file flags, return nullptr
80+
if (flags & FileFlags.FILE_FLAGS_NULL_IF_NOT_EXISTS) {
81+
return 0;
82+
}
83+
7984
fd = fs.openSync(
8085
file.dataUrl!,
8186
fs.constants.O_CREAT | fs.constants.O_RDWR,
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
diff --git a/src/storage/storage_manager.cpp b/src/storage/storage_manager.cpp
2-
index cb6c654e5f..a6b2af3b85 100644
2+
index 45d42174f1..10b3ce47b1 100644
33
--- a/src/storage/storage_manager.cpp
44
+++ b/src/storage/storage_manager.cpp
5-
@@ -160,9 +160,12 @@ void SingleFileStorageManager::LoadDatabase(StorageOptions storage_options) {
5+
@@ -162,9 +162,15 @@ void SingleFileStorageManager::LoadDatabase(StorageOptions storage_options) {
66
row_group_size, STANDARD_VECTOR_SIZE);
77
}
88
}
99
- // Check if the database file already exists.
1010
- // Note: a file can also exist if there was a ROLLBACK on a previous transaction creating that file.
1111
- if (!read_only && !fs.FileExists(path)) {
12-
+ auto db_file_handle = fs.OpenFile(path, FileFlags::FILE_FLAGS_READ | FileFlags::FILE_FLAGS_NULL_IF_NOT_EXISTS);
13-
+ bool is_empty_file = db_file_handle->GetFileSize() == 0;
14-
+ db_file_handle.reset();
12+
+ bool is_empty_file = true;
13+
+ auto db_file_handle = fs.OpenFile(path, FileFlags::FILE_FLAGS_READ | FileFlags::FILE_FLAGS_NULL_IF_NOT_EXISTS);
14+
+ if (db_file_handle && db_file_handle->GetFileSize() != 0) {
15+
+ is_empty_file = false;
16+
+ db_file_handle.reset();
17+
+ }
1518
+
16-
+ // first check if the database exists
17-
+ if (!read_only && ( !fs.FileExists(path) || ( options.use_direct_io && is_empty_file )) ) {
19+
+ // first check if the database exists
20+
+ if (!read_only && (!fs.FileExists(path) || (options.use_direct_io && is_empty_file))) {
1821
// file does not exist and we are in read-write mode
1922
// create a new file
2023

0 commit comments

Comments
 (0)