~sircmpwn/hare

ref: 5b6e7a1ad54a68b09afb68e1be099a46052ef5a7 hare/fs/fs.ha -rw-r--r-- 5.9 KiB
5b6e7a1aDrew DeVault unix: declare fs dependency 3 months ago
                                                                                
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
use errors;
use io;
use path;

// Closes a filesystem. The fs cannot be used after this function is called.
export fn close(fs: *fs) void = {
	match (fs.close) {
		null => void,
		f: *closefunc => f(fs),
	};
};

// Opens a file. If no flags are provided, the default read/write mode is
// RDONLY.
export fn open(fs: *fs, path: str, flags: flags...) (*io::stream | error) = {
	return match (fs.open) {
		null => errors::unsupported,
		f: *openfunc => f(fs, path, flags...),
	};
};

// Creates a new file and opens it for writing. If no flags are provided, the
// default read/write mode is WRONLY.
//
// Only the permission bits of the mode are used. If other bits are set, they
// are discarded.
export fn create(
	fs: *fs,
	path: str,
	mode: mode,
	flags: flags...
) (*io::stream | error) = {
	mode &= 0o777;
	return match (fs.create) {
		null => errors::unsupported,
		f: *createfunc => f(fs, path, mode, flags...),
	};
};

// Removes a file.
export fn remove(fs: *fs, path: str) (void | error) = {
	return match (fs.remove) {
		null => errors::unsupported,
		f: *removefunc => f(fs, path),
	};
};

// Renames a file. This generally only works if the source and destination path
// are both on the same filesystem. See [[move]] for an implementation which
// falls back on a "copy & remove" procedure in this situation.
export fn rename(fs: *fs, oldpath: str, newpath: str) (void | error) = {
	return match (fs.rename) {
		null => errors::unsupported,
		f: *renamefunc => f(fs, oldpath, newpath),
	};
};

// Moves a file. This will use [[rename]] if possible, and will fall back to
// copy and remove if necessary.
export fn move(fs: *fs, oldpath: str, newpath: str) (void | error) = {
	match (rename(fs, oldpath, newpath)) {
		cannotrename => void, // Fallback
		errors::unsupported => void, // Fallback
		void => return, // Success
		err: error => return err,
	};
	// TODO:
	// - If an error occurs, remove the new file.
	// - Move non-regular files
	let st = fs::stat(fs, oldpath)?;
	assert(is_file(st.mode), "TODO: move non-regular files");
	let new = fs::create(fs, newpath, st.mode)?;
	defer io::close(new);
	let old = fs::open(fs, oldpath)?;
	defer io::close(old);
	io::copy(new, old)?;
	fs::remove(fs, oldpath)?;
};

// Returns an iterator for a path, which yields the contents of a directory.
// Pass empty string to yield from the root. The order in which entries are
// returned is undefined.
export fn iter(fs: *fs, path: str) (*iterator | error) = {
	return match (fs.iter) {
		null => errors::unsupported,
		f: *iterfunc => f(fs, path),
	};
};

// Obtains information about a file or directory. If the target is a symlink,
// information is returned about the link, not its target.
export fn stat(fs: *fs, path: str) (filestat | error) = {
	return match (fs.stat) {
		null => errors::unsupported,
		f: *statfunc => f(fs, path),
	};
};

// Opens a new filesystem for a subdirectory. The subdirectory must be closed
// separately from the parent filesystem, and its lifetime can outlive that of
// its parent.
export fn subdir(fs: *fs, path: str) (*fs | error) = {
	return match (fs.subdir) {
		null => errors::unsupported,
		f: *subdirfunc => f(fs, path),
	};
};

// Creates a directory.
export fn mkdir(fs: *fs, path: str) (void | error) = {
	return match (fs.mkdir) {
		null => errors::unsupported,
		f: *mkdirfunc => f(fs, path),
	};
};

// Makes a directory, and all non-extant directories in its path.
export fn mkdirs(fs: *fs, path: str) (void | error) = {
	let parent = path::dirname(path);
	if (path != parent) {
		match (mkdirs(fs, parent)) {
			errors::exists => void,
			err: error => return err,
			void => void,
		};
	};
	return match (mkdir(fs, path)) {
		errors::exists => void,
		err: error => err,
		void => void,
	};
};

// Removes a directory. The target directory must be empty; see [[rmdirall]] to
// remove its contents as well.
export fn rmdir(fs: *fs, path: str) (void | error) = {
	if (path == "") {
		return errors::invalid;
	};
	return match (fs.rmdir) {
		null => errors::unsupported,
		f: *rmdirfunc => f(fs, path),
	};
};

// Removes a directory, and anything in it.
export fn rmdirall(fs: *fs, path: str) (void | error) = {
	let it = iter(fs, path)?;
	for (true) match (next(it)) {
		ent: dirent => {
			if (ent.name == "." || ent.name == "..") {
				continue;
			};
			let p = path::join(path, ent.name);
			defer free(p);
			switch (ent.ftype & mode::DIR) {
				mode::DIR => rmdirall(fs, p)?,
				* => remove(fs, p)?,
			};
		},
		void => break,
	};
	if (path != "") {
		return rmdir(fs, path);
	};
};

// Creates a directory and returns a subdir for it. Some filesystems support
// doing this operation atomically, but if not, a fallback is used.
export fn mksubdir(fs: *fs, path: str) (*fs | error) = {
	return match (fs.mksubdir) {
		null => {
			mkdir(fs, path)?;
			subdir(fs, path);
		},
		f: *mksubdirfunc => f(fs, path),
	};
};

// Changes mode flags on a file or directory. Type bits are discared.
export fn chmod(fs: *fs, path: str, mode: mode) (void | error) = {
	mode &= 0o777;
	return match (fs.chmod) {
		f: *chmodfunc => f(fs, path, mode),
		null => errors::unsupported,
	};
};

// Changes ownership of a file.
export fn chown(fs: *fs, path: str, uid: uint, gid: uint) (void | error) = {
	return match (fs.chown) {
		f: *chownfunc => f(fs, path, uid, gid),
		null => errors::unsupported,
	};
};

// Resolves a path to its absolute, normalized value. This consoldates ./ and
// ../ sequences, roots the path, and returns a new path. The caller must free
// the return value.
export fn resolve(fs: *fs, path: str) str = {
	match (fs.resolve) {
		f: *resolvefunc => return f(fs, path),
		null => void,
	};
	abort(); // TODO
};

// Returns the next directory entry from an interator, or void if none remain.
// It is a programming error to call this again after it has returned void. The
// file stat returned may only have the type bits set on the file mode; callers
// should call [[fs::stat]] to obtain the detailed file mode.
export fn next(iter: *iterator) (dirent | void) = iter.next(iter);