Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/library_wasmfs_opfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,13 @@ mergeInto(LibraryManager.library, {
_wasmfs_opfs_get_size_access__deps: ['$wasmfsOPFSAccessHandles'],
_wasmfs_opfs_get_size_access: async function(ctx, accessID, sizePtr) {
let accessHandle = wasmfsOPFSAccessHandles.get(accessID);
// TODO: Error handling
let size = await accessHandle.getSize();
{{{ makeSetValue('sizePtr', 0, 'size', 'i32') }}};
let size;
try {
size = await accessHandle.getSize();
} catch {
size = -{{{ cDefine('EIO') }}};
}
{{{ makeSetValue('sizePtr', 0, 'size', 'i64') }}};
_emscripten_proxy_finish(ctx);
},

Expand All @@ -369,14 +373,21 @@ mergeInto(LibraryManager.library, {
_wasmfs_opfs_get_size_file__deps: ['$wasmfsOPFSFileHandles'],
_wasmfs_opfs_get_size_file: async function(ctx, fileID, sizePtr) {
let fileHandle = wasmfsOPFSFileHandles.get(fileID);
// TODO: Error handling
let size = (await fileHandle.getFile()).size;
{{{ makeSetValue('sizePtr', 0, 'size', 'i32') }}};
let size;
try {
size = (await fileHandle.getFile()).size;
} catch {
size = -{{{ cDefine('EIO') }}};
}
{{{ makeSetValue('sizePtr', 0, 'size', 'i64') }}};
_emscripten_proxy_finish(ctx);
},

_wasmfs_opfs_set_size_access__deps: ['$wasmfsOPFSAccessHandles'],
_wasmfs_opfs_set_size_access: async function(ctx, accessID, size, errPtr) {
_wasmfs_opfs_set_size_access: async function(ctx, accessID,
{{{ defineI64Param('size') }}},
errPtr) {
{{{ receiveI64ParamAsDouble('size') }}};
let accessHandle = wasmfsOPFSAccessHandles.get(accessID);
try {
await accessHandle.truncate(size);
Expand All @@ -388,7 +399,10 @@ mergeInto(LibraryManager.library, {
},

_wasmfs_opfs_set_size_file__deps: ['$wasmfsOPFSFileHandles'],
_wasmfs_opfs_set_size_file: async function(ctx, fileID, size, errPtr) {
_wasmfs_opfs_set_size_file: async function(ctx, fileID,
{{{ defineI64Param('size') }}},
errPtr) {
{{{ receiveI64ParamAsDouble('size') }}};
let fileHandle = wasmfsOPFSFileHandles.get(fileID);
try {
let writable = await fileHandle.createWritable({keepExistingData: true});
Expand Down
6 changes: 3 additions & 3 deletions system/lib/wasmfs/backends/node_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class NodeFile : public DataFile {
: DataFile(mode, backend), state(path) {}

private:
size_t getSize() override {
off_t getSize() override {
// TODO: This should really be using a 64-bit file size type.
uint32_t size;
if (state.isOpen()) {
Expand All @@ -151,10 +151,10 @@ class NodeFile : public DataFile {
return 0;
}
}
return size_t(size);
return off_t(size);
}

int setSize(size_t size) override {
int setSize(off_t size) override {
WASMFS_UNREACHABLE("TODO: implement NodeFile::setSize");
}

Expand Down
22 changes: 11 additions & 11 deletions system/lib/wasmfs/backends/opfs_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,22 @@ int _wasmfs_opfs_write_access(int access_id,
// Get the size via an AccessHandle.
void _wasmfs_opfs_get_size_access(em_proxying_ctx* ctx,
int access_id,
uint32_t* size);
off_t* size);

// TODO: return 64-byte off_t.
uint32_t _wasmfs_opfs_get_size_blob(int blob_id);

// Get the size of a file handle via a File Blob.
void _wasmfs_opfs_get_size_file(em_proxying_ctx* ctx,
int file_id,
uint32_t* size);
void _wasmfs_opfs_get_size_file(em_proxying_ctx* ctx, int file_id, off_t* size);

void _wasmfs_opfs_set_size_access(em_proxying_ctx* ctx,
int access_id,
uint32_t size,
off_t size,
int* err);

void _wasmfs_opfs_set_size_file(em_proxying_ctx* ctx,
int file_id,
uint32_t size,
off_t size,
int* err);

void _wasmfs_opfs_flush_access(em_proxying_ctx* ctx, int access_id, int* err);
Expand Down Expand Up @@ -222,9 +221,8 @@ class OPFSFile : public DataFile {
}

private:
size_t getSize() override {
// TODO: 64-bit sizes.
uint32_t size;
off_t getSize() override {
off_t size;
switch (state.getKind()) {
case OpenState::None:
proxy([&](auto ctx) {
Expand All @@ -242,10 +240,10 @@ class OPFSFile : public DataFile {
default:
WASMFS_UNREACHABLE("Unexpected open state");
}
return size_t(size);
return size;
}

int setSize(size_t size) override {
int setSize(off_t size) override {
int err = 0;
switch (state.getKind()) {
case OpenState::Access:
Expand All @@ -259,6 +257,8 @@ class OPFSFile : public DataFile {
// become invalidated and refreshing it while ensuring other in-flight
// operations on the same file do not observe the invalidated blob would
// be extremely complicated.
// TODO: Can we assume there are no other in-flight operations on this
// file and do something better here?
return -EIO;
case OpenState::None: {
proxy([&](auto ctx) {
Expand Down
6 changes: 3 additions & 3 deletions system/lib/wasmfs/backends/proxied_file_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ class ProxiedFile : public DataFile {

// Querying the size of the Proxied File returns the size of the underlying
// file given by the proxying mechanism.
size_t getSize() override {
size_t result;
off_t getSize() override {
off_t result;
proxy([&]() { result = baseFile->locked().getSize(); });
return result;
}

int setSize(size_t size) override {
int setSize(off_t size) override {
WASMFS_UNREACHABLE("TODO: ProxiedFS setSize");
}

Expand Down
15 changes: 8 additions & 7 deletions system/lib/wasmfs/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ class File : public std::enable_shared_from_this<File> {
// A mutex is needed for multiple accesses to the same file.
std::recursive_mutex mutex;

// May be called on files that have not been opened.
virtual size_t getSize() = 0;
// The the size in bytes of a file or return a negative error code. May be
// called on files that have not been opened.
virtual off_t getSize() = 0;

mode_t mode = 0; // User and group mode bits for access permission.

Expand Down Expand Up @@ -144,7 +145,7 @@ class DataFile : public File {
// Sets the size of the file to a specific size. If new space is allocated, it
// should be zero-initialized. May be called on files that have not been
// opened. Returns 0 on success or a negative error code.
virtual int setSize(size_t size) = 0;
virtual int setSize(off_t size) = 0;

// Sync the file data to the underlying persistent storage, if any. Returns 0
// on success or a negative error code.
Expand Down Expand Up @@ -254,7 +255,7 @@ class Directory : public File {
protected:
// 4096 bytes is the size of a block in ext4.
// This value was also copied from the JS file system.
size_t getSize() override { return 4096; }
off_t getSize() override { return 4096; }
};

class Symlink : public File {
Expand All @@ -270,7 +271,7 @@ class Symlink : public File {
virtual std::string getTarget() const = 0;

protected:
size_t getSize() override { return getTarget().size(); }
off_t getSize() override { return getTarget().size(); }
};

class File::Handle {
Expand All @@ -286,7 +287,7 @@ class File::Handle {
Handle(std::shared_ptr<File> file) : file(file), lock(file->mutex) {}
Handle(std::shared_ptr<File> file, std::defer_lock_t)
: file(file), lock(file->mutex, std::defer_lock) {}
size_t getSize() { return file->getSize(); }
off_t getSize() { return file->getSize(); }
mode_t getMode() { return file->mode; }
void setMode(mode_t mode) {
// The type bits can never be changed (whether something is a file or a
Expand Down Expand Up @@ -325,7 +326,7 @@ class DataFile::Handle : public File::Handle {
return getFile()->write(buf, len, offset);
}

[[nodiscard]] int setSize(size_t size) { return getFile()->setSize(size); }
[[nodiscard]] int setSize(off_t size) { return getFile()->setSize(size); }

// TODO: Design a proper API for flushing files.
[[nodiscard]] int flush() { return getFile()->flush(); }
Expand Down
4 changes: 2 additions & 2 deletions system/lib/wasmfs/js_impl_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ class JSImplFile : public DataFile {

int flush() override { return 0; }

size_t getSize() override {
off_t getSize() override {
return _wasmfs_jsimpl_get_size(getBackendIndex(), getFileIndex());
}

int setSize(size_t size) override {
int setSize(off_t size) override {
WASMFS_UNREACHABLE("TODO: JSImpl setSize");
}

Expand Down
4 changes: 2 additions & 2 deletions system/lib/wasmfs/memory_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class MemoryFile : public DataFile {
ssize_t write(const uint8_t* buf, size_t len, off_t offset) override;
ssize_t read(uint8_t* buf, size_t len, off_t offset) override;
int flush() override { return 0; }
size_t getSize() override { return buffer.size(); }
int setSize(size_t size) override {
off_t getSize() override { return buffer.size(); }
int setSize(off_t size) override {
buffer.resize(size);
return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions system/lib/wasmfs/pipe_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ class PipeFile : public DataFile {

int flush() override { return 0; }

size_t getSize() override { return data->size(); }
off_t getSize() override { return data->size(); }

// TODO: Should this return an error?
int setSize(size_t size) override { return 0; }
int setSize(off_t size) override { return 0; }

public:
// PipeFiles do not have or need a backend. Pass NullBackend to the parent for
Expand Down
8 changes: 4 additions & 4 deletions system/lib/wasmfs/proxied_async_js_impl_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void _wasmfs_jsimpl_async_read(em_proxying_ctx* ctx,
void _wasmfs_jsimpl_async_get_size(em_proxying_ctx* ctx,
js_index_t backend,
js_index_t index,
size_t* result);
off_t* result);
}

namespace wasmfs {
Expand Down Expand Up @@ -108,16 +108,16 @@ class ProxiedAsyncJSImplFile : public DataFile {

int flush() override { return 0; }

size_t getSize() override {
size_t result;
off_t getSize() override {
off_t result;
proxy([&](auto ctx) {
_wasmfs_jsimpl_async_get_size(
ctx.ctx, getBackendIndex(), getFileIndex(), &result);
});
return result;
}

int setSize(size_t size) override {
int setSize(off_t size) override {
WASMFS_UNREACHABLE("TODO: ProxiedAsyncJSImplFile setSize");
}

Expand Down
16 changes: 8 additions & 8 deletions system/lib/wasmfs/special_files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class NullFile : public DataFile {
ssize_t read(uint8_t* buf, size_t len, off_t offset) override { return 0; }

int flush() override { return 0; }
size_t getSize() override { return 0; }
int setSize(size_t size) override { return -EPERM; }
off_t getSize() override { return 0; }
int setSize(off_t size) override { return -EPERM; }

public:
NullFile() : DataFile(S_IRUGO | S_IWUGO, NullBackend, S_IFCHR) {}
Expand All @@ -50,8 +50,8 @@ class StdinFile : public DataFile {
};

int flush() override { return 0; }
size_t getSize() override { return 0; }
int setSize(size_t size) override { return -EPERM; }
off_t getSize() override { return 0; }
int setSize(off_t size) override { return -EPERM; }

public:
StdinFile() : DataFile(S_IRUGO, NullBackend, S_IFCHR) { seekable = false; }
Expand All @@ -78,8 +78,8 @@ class WritingStdFile : public DataFile {
return 0;
}

size_t getSize() override { return 0; }
int setSize(size_t size) override { return -EPERM; }
off_t getSize() override { return 0; }
int setSize(off_t size) override { return -EPERM; }

ssize_t writeToJS(const uint8_t* buf,
size_t len,
Expand Down Expand Up @@ -152,8 +152,8 @@ class RandomFile : public DataFile {
};

int flush() override { return 0; }
size_t getSize() override { return 0; }
int setSize(size_t size) override { return -EPERM; }
off_t getSize() override { return 0; }
int setSize(off_t size) override { return -EPERM; }

public:
RandomFile() : DataFile(S_IRUGO, NullBackend, S_IFCHR) { seekable = false; }
Expand Down
Loading