-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
117 lines (94 loc) · 3.34 KB
/
example.js
File metadata and controls
117 lines (94 loc) · 3.34 KB
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
const { FuseFS } = require('./index.js');
const path = require('path');
const os = require('os');
const fs = require('fs');
function findFiles(dirPath) {
const results = [];
const entries = fs.readdirSync(dirPath, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dirPath, entry.name);
if (entry.isDirectory()) {
results.push(...findFiles(fullPath));
} else {
results.push(fullPath);
}
}
return results;
}
async function main() {
const fs_impl = new FuseFS();
try {
// Subscribe to file system events
fs_impl.on((event) => console.log(`${event.eventType} event:\n- Path: ${event.path}\n- Type: ${event.objectType}`));
console.log('Creating initial files and directories...');
// Add some virtual files before mounting
await fs_impl.addFile("hello.txt", Buffer.from("Hello, World!\n"));
await fs_impl.addFile("data.bin", Buffer.from([1, 2, 3, 4, 5]));
await fs_impl.addDirectory("subdir");
await fs_impl.addFile("subdir/test.txt", Buffer.from("Test file in subdir\n"));
// Mount the filesystem with 100MB RAM allocation
const RAM_SIZE = 100 * 1024 * 1024; // 100MB in bytes
const mountPath = path.join(os.homedir(), 'projected-fs-mount');
// Ensure mount point is clean
if (fs.existsSync(mountPath)) {
fs.rmSync(mountPath, { recursive: true, force: true });
}
// Create a fresh mount directory
fs.mkdirSync(mountPath, { recursive: true });
// Now mount with the prepared directory
await fs_impl.mount(mountPath, RAM_SIZE);
console.log('Filesystem mounted at ' + mountPath);
console.log('RAM allocated: ' + (RAM_SIZE / 1024 / 1024) + 'MB');
// Test file operations while mounted
setTimeout(async () => {
console.log('\nTesting file operations...');
// Create a new file
console.log('Adding dynamic.txt...');
await fs_impl.addFile("dynamic.txt", Buffer.from("Added while mounted!\n"));
// Create a new directory
setTimeout(async () => {
console.log('\nCreating new directory...');
await fs_impl.addDirectory("newdir");
// Create a file in the new directory
setTimeout(async () => {
console.log('\nAdding file in new directory...');
await fs_impl.addFile("newdir/nested.txt", Buffer.from("Nested file\n"));
// Remove files and directory
setTimeout(async () => {
console.log('\nRemoving files and directories...');
await fs_impl.removePath("hello.txt");
await fs_impl.removePath("newdir/nested.txt");
await fs_impl.removePath("newdir");
try {
console.log('\nListing files in mounted directory...');
const files = findFiles(mountPath);
console.log(files);
} catch (error) {
console.error('Error listing files:', error);
}
}, 2000);
}, 2000);
}, 2000);
}, 2000);
// Handle graceful shutdown
process.on('SIGINT', async () => {
console.log('\nUnmounting filesystem...');
try {
await fs_impl.unmount();
console.log('Filesystem unmounted successfully');
process.exit(0);
} catch (error) {
console.error('Error unmounting:', error);
process.exit(1);
}
});
console.log('\nPress Ctrl+C to unmount and exit');
console.log('Try modifying files in the mounted directory to see events...');
// Keep the process alive
process.stdin.resume();
} catch (error) {
console.error('Error:', error);
process.exit(1);
}
}
main();