summaryrefslogtreecommitdiffstats
path: root/contrib/restricted/wavm/Include/WAVM/VFS/VFS.h
blob: bc277d29ca3bfe4c20e84d44803115ab2f4d8c98 (plain) (blame)
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
#pragma once

#include <string>
#include "WAVM/Inline/BasicTypes.h"
#include "WAVM/Inline/Time.h"

namespace WAVM { namespace VFS {

	enum class FileAccessMode
	{
		none,
		readOnly,
		writeOnly,
		readWrite,
	};

	enum class FileCreateMode
	{
		createAlways,
		createNew,
		openAlways,
		openExisting,
		truncateExisting,
	};

	enum class SeekOrigin
	{
		begin,
		cur,
		end
	};

	enum class SyncType
	{
		contents,
		contentsAndMetadata
	};

	enum class FileType
	{
		unknown,
		blockDevice,
		characterDevice,
		directory,
		file,
		datagramSocket,
		streamSocket,
		symbolicLink,
		pipe
	};

	enum class VFDSync
	{
		none,
		contentsAfterWrite,
		contentsAndMetadataAfterWrite,
		contentsAfterWriteAndBeforeRead,
		contentsAndMetadataAfterWriteAndBeforeRead
	};

	struct VFDFlags
	{
		// If true, writes will always occur at the end of the file.
		bool append{false};

		// If true, reads and writes will fail if they can't immediately complete.
		bool nonBlocking{false};

		// The amount of synchronization implied for reads and writes.
		VFDSync syncLevel{VFDSync::none};
	};

	struct VFDInfo
	{
		FileType type;
		VFDFlags flags;
	};

	struct FileInfo
	{
		U64 deviceNumber;
		U64 fileNumber;

		FileType type;
		U32 numLinks;
		U64 numBytes;

		// Time values correspond to the "real-time" clock defined by Platform::Clock::realtime.
		Time lastAccessTime;
		Time lastWriteTime;
		Time creationTime;
	};

	struct DirEnt
	{
		U64 fileNumber;
		std::string name;
		FileType type;
	};

	struct IOReadBuffer
	{
		void* data;
		Uptr numBytes;
	};

	struct IOWriteBuffer
	{
		const void* data;
		Uptr numBytes;
	};

	// Error codes
	// clang-format off

	#define WAVM_ENUM_VFS_RESULTS(v) \
		v(success, "Success") \
		/* Asynchronous I/O statuses */ \
		v(ioPending, "IO pending") \
		/* Hardware errors */ \
		v(ioDeviceError, "IO device error") \
		/* Transient errors */ \
		v(interruptedBySignal, "Interrupted by signal") \
		v(interruptedByCancellation, "Interrupted by cancellation") \
		v(wouldBlock, "Operation on non-blocking file descriptor would block") \
		/* Invalid argument errors */ \
		v(inaccessibleBuffer, "A provided buffer is in memory that is not accessible") \
		v(invalidOffset, "Invalid offset") \
		/* Capability errors */ \
		v(notSeekable, "File descriptor is not seekable") \
		v(notPermitted, "Not permitted") \
		v(notAccessible, "Not accessible") \
		v(notSynchronizable,"File descriptor is not synchronizable") \
		/* Argument constraints */ \
		v(tooManyBufferBytes, "Too many bytes") \
		v(notEnoughBufferBytes, "Not enough bytes") \
		v(tooManyBuffers, "Too many buffers") \
		v(notEnoughBits, "Not enough bits") \
		v(exceededFileSizeLimit, "File is too large") \
		/* Resource exhaustion errors */ \
		v(outOfSystemFDs, "Out of system file descriptors") \
		v(outOfProcessFDs, "Out of process file descriptors") \
		v(outOfMemory, "Out of memory") \
		v(outOfQuota, "Out of quota") \
		v(outOfFreeSpace, "Out of free space") \
		v(outOfLinksToParentDir, "Out of links to parent directory") \
		/* Path errors */ \
		v(invalidNameCharacter, "Invalid filename character") \
		v(nameTooLong, "Filename is too long") \
		v(tooManyLinksInPath, "Path follows too many links") \
		/* File state errors */ \
		v(alreadyExists, "Already exists") \
		v(doesNotExist, "Doesn't exist") \
		v(isDirectory, "Is a directory") \
		v(isNotDirectory, "Isn't a directory") \
		v(isNotEmpty, "Directory isn't empty") \
		v(brokenPipe, "Pipe is broken") \
		v(missingDevice, "Device is missing") \
		v(busy, "Device or resource busy") \
		v(notSupported, "Operation not supported")

	enum class Result : I32
	{
		#define V(name, description) name,
		WAVM_ENUM_VFS_RESULTS(V)
		#undef V
	};

	// clang-format on

	struct DirEntStream
	{
		virtual ~DirEntStream() {}

		virtual void close() = 0;

		virtual bool getNext(DirEnt& outEntry) = 0;

		virtual void restart() = 0;
		virtual U64 tell() = 0;
		virtual bool seek(U64 offset) = 0;
	};

	struct VFD
	{
		// Closes the FD. Deletes the VFD regardless of whether an error code is returned.
		virtual Result close() = 0;

		virtual Result seek(I64 offset, SeekOrigin origin, U64* outAbsoluteOffset = nullptr) = 0;

		virtual Result readv(const IOReadBuffer* buffers,
							 Uptr numBuffers,
							 Uptr* outNumBytesRead = nullptr,
							 const U64* offset = nullptr)
			= 0;
		virtual Result writev(const IOWriteBuffer* buffers,
							  Uptr numBuffers,
							  Uptr* outNumBytesWritten = nullptr,
							  const U64* offset = nullptr)
			= 0;

		virtual Result sync(SyncType type) = 0;

		virtual Result getVFDInfo(VFDInfo& outInfo) = 0;
		virtual Result getFileInfo(FileInfo& outInfo) = 0;
		virtual Result setVFDFlags(const VFDFlags& flags) = 0;
		virtual Result setFileSize(U64 numBytes) = 0;
		virtual Result setFileTimes(bool setLastAccessTime,
									Time lastAccessTime,
									bool setLastWriteTime,
									Time lastWriteTime)
			= 0;

		virtual Result openDir(DirEntStream*& outStream) = 0;

		Result read(void* outData,
					Uptr numBytes,
					Uptr* outNumBytesRead = nullptr,
					U64* offset = nullptr)
		{
			IOReadBuffer buffer{outData, numBytes};
			return readv(&buffer, 1, outNumBytesRead, offset);
		}
		Result write(const void* data,
					 Uptr numBytes,
					 Uptr* outNumBytesWritten = nullptr,
					 U64* offset = nullptr)
		{
			IOWriteBuffer buffer{data, numBytes};
			return writev(&buffer, 1, outNumBytesWritten, offset);
		}

	protected:
		virtual ~VFD() {}
	};

	struct FileSystem
	{
		virtual ~FileSystem() {}

		virtual Result open(const std::string& path,
							FileAccessMode accessMode,
							FileCreateMode createMode,
							VFD*& outFD,
							const VFDFlags& flags = VFDFlags{})
			= 0;

		virtual Result getFileInfo(const std::string& path, FileInfo& outInfo) = 0;
		virtual Result setFileTimes(const std::string& path,
									bool setLastAccessTime,
									Time lastAccessTime,
									bool setLastWriteTime,
									Time lastWriteTime)
			= 0;

		virtual Result openDir(const std::string& path, DirEntStream*& outStream) = 0;

		virtual Result renameFile(const std::string& oldPath, const std::string& newPath) = 0;
		virtual Result unlinkFile(const std::string& path) = 0;
		virtual Result removeDir(const std::string& path) = 0;
		virtual Result createDir(const std::string& path) = 0;
	};

	WAVM_API const char* describeResult(Result result);
}}