@k13engineering/linux-fuse 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,149 @@
1
+ import {
2
+ AT_FCWD,
3
+ MOUNT_ATTR_NODEV,
4
+ MOUNT_ATTR_NODIRATIME,
5
+ MOUNT_ATTR_NOEXEC,
6
+ MOUNT_ATTR_NOSUID,
7
+ MOUNT_ATTR_NOSYMFOLLOW,
8
+ MOUNT_ATTR_RDONLY,
9
+ MOVE_MOUNT_F_EMPTY_PATH
10
+ } from "./constants.js";
11
+ import { move_mount, umount } from "./syscalls.js";
12
+
13
+ /*type TRequiredFuseMountOptions = {
14
+ rootmode: number;
15
+ user_id: number;
16
+ group_id: number;
17
+ };*/
18
+
19
+ /*type TMountAttributes = {
20
+ MOUNT_ATTR_RDONLY?: boolean;
21
+ MOUNT_ATTR_NOSUID?: boolean;
22
+ MOUNT_ATTR_NODEV?: boolean;
23
+ MOUNT_ATTR_NOEXEC?: boolean;
24
+ MOUNT_ATTR_NOSYMFOLLOW?: boolean;
25
+ MOUNT_ATTR_NODIRATIME?: boolean;
26
+
27
+ customFlags?: bigint;
28
+ };*/
29
+
30
+ const knownMountAttributesBitmask = MOUNT_ATTR_RDONLY
31
+ | MOUNT_ATTR_NOSUID
32
+ | MOUNT_ATTR_NODEV
33
+ | MOUNT_ATTR_NOEXEC
34
+ | MOUNT_ATTR_NOSYMFOLLOW
35
+ | MOUNT_ATTR_NODIRATIME;
36
+
37
+ // eslint-disable-next-line max-statements, complexity
38
+ const mountAttributesToBitmask = ({ mountAttributes }/*: { mountAttributes: TMountAttributes }*/) => {
39
+ let bitmask = 0n;
40
+
41
+ if (mountAttributes.MOUNT_ATTR_RDONLY) {
42
+ bitmask |= MOUNT_ATTR_RDONLY;
43
+ }
44
+
45
+ if (mountAttributes.MOUNT_ATTR_NOSUID) {
46
+ bitmask |= MOUNT_ATTR_NOSUID;
47
+ }
48
+
49
+ if (mountAttributes.MOUNT_ATTR_NODEV) {
50
+ bitmask |= MOUNT_ATTR_NODEV;
51
+ }
52
+
53
+ if (mountAttributes.MOUNT_ATTR_NOEXEC) {
54
+ bitmask |= MOUNT_ATTR_NOEXEC;
55
+ }
56
+
57
+ if (mountAttributes.MOUNT_ATTR_NOSYMFOLLOW) {
58
+ bitmask |= MOUNT_ATTR_NOSYMFOLLOW;
59
+ }
60
+
61
+ if (mountAttributes.MOUNT_ATTR_NODIRATIME) {
62
+ bitmask |= MOUNT_ATTR_NODIRATIME;
63
+ }
64
+
65
+ if (mountAttributes.customFlags !== undefined) {
66
+
67
+ if ((mountAttributes.customFlags & knownMountAttributesBitmask) !== 0n) {
68
+ throw Error(`customFlags contains known mount attribute flags`);
69
+ }
70
+
71
+ bitmask |= mountAttributes.customFlags;
72
+ }
73
+
74
+ return bitmask;
75
+ };
76
+
77
+
78
+ /*type TUnmountResult = {
79
+ error: Error | undefined;
80
+ };*/
81
+
82
+ const MNT_FORCE = 1n;
83
+ const MNT_DETACH = 2n;
84
+
85
+ // for now, only export this simple variant for use with fuse
86
+ const moveMount = ({
87
+ mountFd,
88
+ newMountPoint,
89
+ }/*: {
90
+ mountFd: number;
91
+ newMountPoint: string;
92
+ }*/) => {
93
+ const { errno: moveMountErrno } = move_mount({
94
+ fromFd: mountFd,
95
+ fromPath: "",
96
+ toFd: Number(AT_FCWD),
97
+ toPath: newMountPoint,
98
+ flags: MOVE_MOUNT_F_EMPTY_PATH,
99
+ });
100
+
101
+ if (moveMountErrno !== undefined) {
102
+ return {
103
+ error: Error(`move_mount syscall failed with errno ${moveMountErrno}`)
104
+ };
105
+ }
106
+
107
+ return {
108
+ error: undefined
109
+ };
110
+ };
111
+
112
+ const unmount = ({ mountPoint, force, detach }/*: { mountPoint: string, force: boolean, detach: boolean }*/)/*: TUnmountResult*/ => {
113
+
114
+ let flags = 0n;
115
+
116
+ if (force) {
117
+ flags |= MNT_FORCE;
118
+ }
119
+
120
+ if (detach) {
121
+ flags |= MNT_DETACH;
122
+ }
123
+
124
+ const { errno } = umount({
125
+ target: mountPoint,
126
+ flags,
127
+ });
128
+
129
+ if (errno !== undefined) {
130
+ return {
131
+ error: Error(`umount syscall failed with errno ${errno}`)
132
+ };
133
+ }
134
+
135
+ return {
136
+ error: undefined
137
+ };
138
+ };
139
+
140
+ export {
141
+ mountAttributesToBitmask,
142
+ moveMount,
143
+ unmount,
144
+ };
145
+
146
+ /*export type {
147
+ TRequiredFuseMountOptions,
148
+ TMountAttributes,
149
+ };*/