1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
const std = @import("std");
const zqlite = @import("zqlite");
const build_options = @import("build_options");
const testing = std.testing;
const StderrReadResult = union(enum) {
data: []const u8,
would_block,
eof,
};
const NonBlockingStderr = struct {
file: std.fs.File,
fn init(stderr: std.fs.File) !NonBlockingStderr {
// Make stderr non-blocking
const flags = try std.posix.fcntl(stderr.handle, std.posix.F.GETFL, 0);
_ = try std.posix.fcntl(stderr.handle, std.posix.F.SETFL, flags | 0x800); // O_NONBLOCK = 0x800
return NonBlockingStderr{ .file = stderr };
}
fn readChunk(self: NonBlockingStderr, buffer: []u8) !StderrReadResult {
const bytes_read = self.file.read(buffer) catch |err| switch (err) {
error.WouldBlock => return .would_block,
else => return err,
};
if (bytes_read == 0) return .eof;
return .{ .data = buffer[0..bytes_read] };
}
};
fn readCurrentStderr(allocator: std.mem.Allocator, child_process: *std.process.Child) ![]const u8 {
if (child_process.stderr) |stderr| {
const nb_stderr = NonBlockingStderr.init(stderr) catch |err| {
std.debug.print("Failed to make stderr non-blocking: {}\n", .{err});
return &[_]u8{};
};
var stderr_buffer = std.ArrayList(u8).init(allocator);
defer stderr_buffer.deinit();
var read_buffer: [1024]u8 = undefined;
while (true) {
switch (try nb_stderr.readChunk(&read_buffer)) {
.data => |chunk| {
stderr_buffer.appendSlice(chunk) catch break;
},
.would_block, .eof => break,
}
}
return stderr_buffer.toOwnedSlice() catch &[_]u8{};
}
return &[_]u8{};
}
fn testFileLifecycle(exe_path: []const u8, impl_name: []const u8) !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
var arena = std.heap.ArenaAllocator.init(gpa.allocator());
defer arena.deinit();
const allocator = arena.allocator();
std.debug.print("Testing {s} implementation\n", .{impl_name});
// Create temporary mount point
var tmp_dir = std.testing.tmpDir(.{});
defer tmp_dir.cleanup();
// Create temporary database file
const test_db_file = try tmp_dir.dir.createFile("test.db", .{});
test_db_file.close();
const test_db_path = try tmp_dir.dir.realpathAlloc(allocator, "test.db");
// Create temporary mount point
try tmp_dir.dir.makeDir("mount");
const mount_point = try tmp_dir.dir.realpathAlloc(allocator, "mount");
// Start sqlitefs process
var child_process = std.process.Child.init(&.{ exe_path, "-f", mount_point, test_db_path }, allocator);
child_process.stdout_behavior = .Pipe;
child_process.stderr_behavior = .Pipe;
try child_process.spawn();
defer _ = child_process.kill() catch {};
errdefer {
// Clean up FUSE mount point on error
var cleanup_mount = std.process.Child.init(&.{ "fusermount", "-u", mount_point }, allocator);
if (cleanup_mount.spawnAndWait()) |cleanup_result| {
if (cleanup_result.Exited != 0) {
std.debug.print("Warning: fusermount cleanup failed with exit code: {}\n", .{cleanup_result.Exited});
}
} else |err| {
std.debug.print("Warning: Failed to spawn fusermount cleanup: {}\n", .{err});
}
}
// Wait for FUSE initialization with timeout
const init_timeout_ms = 5000;
const start_time = std.time.milliTimestamp();
var fuse_initialized = false;
var all_stderr = std.ArrayList(u8).init(allocator);
defer all_stderr.deinit();
if (child_process.stderr) |stderr| {
const nb_stderr = NonBlockingStderr.init(stderr) catch |err| {
std.debug.print("Failed to make stderr non-blocking: {}\n", .{err});
return error.StderrConfigError;
};
while (std.time.milliTimestamp() - start_time < init_timeout_ms) {
var read_buffer: [256]u8 = undefined;
switch (try nb_stderr.readChunk(&read_buffer)) {
.data => |chunk| {
all_stderr.appendSlice(chunk) catch break;
// Check if we've seen the init message
if (std.mem.indexOf(u8, all_stderr.items, "FUSE_INIT_COMPLETE")) |_| {
fuse_initialized = true;
break;
}
},
.would_block => {
// No data available, sleep and try again
std.time.sleep(50 * std.time.ns_per_ms);
continue;
},
.eof => {
std.debug.print("Stderr EOF during init\n", .{});
break;
},
}
}
std.debug.print("Child stderr output: {s}\n", .{all_stderr.items});
}
if (!fuse_initialized) {
std.debug.print("FUSE initialization timed out after {}ms\n", .{init_timeout_ms});
return error.FuseInitTimeout;
}
std.debug.print("FUSE initialization completed successfully\n", .{});
// Check if our specific mount point is actually mounted
var mount_check = std.process.Child.init(&.{"mount"}, allocator);
mount_check.stdout_behavior = .Pipe;
mount_check.stderr_behavior = .Pipe;
_ = mount_check.spawnAndWait() catch {};
if (mount_check.stdout) |stdout| {
const mount_output = try stdout.readToEndAlloc(allocator, 4096);
// Filter for our specific mount point
if (std.mem.indexOf(u8, mount_output, mount_point)) |_| {
std.debug.print("Mount point {s} is mounted\n", .{mount_point});
} else {
std.debug.print("Mount point {s} is NOT mounted\n", .{mount_point});
}
// Show all mounts for debugging
std.debug.print("All mounts:\n{s}\n", .{mount_output});
}
// Test: Create a file through the filesystem
const test_file_path = try std.fs.path.join(allocator, &.{ mount_point, "test.txt" });
const test_content = "Hello, SQLiteFS!";
// Check if mount point is accessible
std.debug.print("Attempting to write to: {s}\n", .{test_file_path});
var mount_dir = std.fs.cwd().openDir(mount_point, .{}) catch |err| {
std.debug.print("Cannot open mount directory {s}: {}\n", .{ mount_point, err });
return err;
};
mount_dir.close();
// Write file through filesystem
std.fs.cwd().writeFile(.{ .sub_path = test_file_path, .data = test_content }) catch |err| {
std.debug.print("Failed to write file {s}: {}\n", .{ test_file_path, err });
return err;
};
std.debug.print("Successfully wrote file {s}\n", .{test_file_path});
// Immediately try to read the file back to verify FUSE is working
const immediate_read = std.fs.cwd().readFileAlloc(allocator, test_file_path, 1024) catch |err| {
std.debug.print("Cannot read file back immediately: {}\n", .{err});
std.debug.print("This suggests FUSE mount is not working\n", .{});
return err;
};
std.debug.print("Immediate read back successful, content: '{s}'\n", .{immediate_read});
// Verify file exists in database
const db_path_z = try allocator.dupeZ(u8, test_db_path);
const conn = try zqlite.Conn.init(db_path_z, zqlite.c.SQLITE_OPEN_READONLY);
errdefer {
// Create persistent copy of database for debugging
const debug_db_path = "/tmp/sqlitefs_test_failure.db";
conn.exec("VACUUM INTO ?", .{debug_db_path}) catch {};
// Read any remaining stderr output for debugging
const current_stderr = readCurrentStderr(allocator, &child_process) catch &[_]u8{};
std.debug.print("Error opening database. Debug copy saved to: {s}\n", .{debug_db_path});
std.debug.print("Additional stderr output: {s}\n", .{current_stderr});
}
defer conn.close();
var rows = try conn.rows("SELECT filecontent FROM files WHERE filename = ?", .{"test.txt"});
defer rows.deinit();
if (rows.next()) |row| {
const result = row.get([]const u8, 0);
try testing.expectEqualStrings(test_content, result);
} else {
// Create persistent copy of database for debugging
const debug_db_path = "/tmp/sqlitefs_test_failure.db";
conn.exec("VACUUM INTO ?", .{debug_db_path}) catch {};
// Read any remaining stderr output for debugging
const current_stderr = readCurrentStderr(allocator, &child_process) catch &[_]u8{};
std.debug.print("No row found for 'test.txt'. Debug copy saved to: {s}\n", .{debug_db_path});
std.debug.print("Additional stderr output: {s}\n", .{current_stderr});
try testing.expect(false); // No row found
}
// Verify file can be read back through filesystem
const read_content = try std.fs.cwd().readFileAlloc(allocator, test_file_path, 1024);
try testing.expectEqualStrings(test_content, read_content);
// Test: Delete the file
std.debug.print("Testing file deletion...\n", .{});
std.fs.cwd().deleteFile(test_file_path) catch |err| {
std.debug.print("Failed to delete file {s}: {}\n", .{ test_file_path, err });
return err;
};
std.debug.print("File deleted successfully\n", .{});
// Verify file no longer exists in filesystem
if (std.fs.cwd().readFileAlloc(allocator, test_file_path, 1024)) |_| {
std.debug.print("ERROR: File still readable after deletion!\n", .{});
try testing.expect(false);
} else |err| switch (err) {
error.FileNotFound => {
std.debug.print("Confirmed: File not found in filesystem after deletion\n", .{});
},
else => {
std.debug.print("Unexpected error reading deleted file: {}\n", .{err});
return err;
},
}
// Verify file was removed from database (while FUSE process is still running)
const db_path_z_concurrent = try allocator.dupeZ(u8, test_db_path);
const conn_concurrent = try zqlite.Conn.init(db_path_z_concurrent, zqlite.c.SQLITE_OPEN_READONLY);
defer conn_concurrent.close();
var rows_after_delete_concurrent = try conn_concurrent.rows("SELECT filename FROM files WHERE filename = ?", .{"test.txt"});
defer rows_after_delete_concurrent.deinit();
if (rows_after_delete_concurrent.next()) |_| {
std.debug.print("ERROR: File still exists in database after deletion (concurrent check)!\n", .{});
try testing.expect(false);
} else {
std.debug.print("Confirmed: File removed from database after deletion (concurrent check)\n", .{});
}
// Kill the child process
_ = child_process.kill() catch {};
_ = try child_process.wait();
// Clean up FUSE mount point (especially important for Haskell implementation)
var fusermount_cleanup = std.process.Child.init(&.{ "fusermount", "-u", mount_point }, allocator);
if (fusermount_cleanup.spawnAndWait()) |cleanup_result| {
if (cleanup_result.Exited != 0) {
std.debug.print("Warning: fusermount cleanup failed with exit code: {}\n", .{cleanup_result.Exited});
}
} else |err| {
std.debug.print("Warning: Failed to spawn fusermount cleanup: {}\n", .{err});
}
}
test "file lifecycle (zig)" {
const exe_path = build_options.sqlitefs_zig_exe_path;
try testFileLifecycle(exe_path, "Zig");
}
test "file lifecycle (haskell)" {
const haskell_exe_path = build_options.sqlitefs_haskell_exe_path;
try testFileLifecycle(haskell_exe_path, "Haskell");
}
|