Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: node fs module #84

Merged
merged 58 commits into from
Dec 5, 2022
Merged

feat: node fs module #84

merged 58 commits into from
Dec 5, 2022

Conversation

Puellaquae
Copy link
Contributor

@Puellaquae Puellaquae commented Sep 12, 2022

  • FileHandle
  • fsPromises.access
  • fsPromises.appendFile
  • fsPromises.chmod Unsupported
  • fsPromises.chown Unsupported
  • fsPromises.copyFile
  • fsPromises.cp
  • fsPromises.lchmod Unsupported
  • fsPromises.lchown Unsupported
  • fsPromises.lutimes
  • fsPromises.link
  • fsPromises.lstat
  • fsPromises.mkdir
  • fsPromises.mkdtemp
  • fsPromises.open
  • fsPromises.opendir
  • fsPromises.readdir
  • fsPromises.readFile
  • fsPromises.readlink
  • fsPromises.realpath
  • fsPromises.rename
  • fsPromises.rmdir
  • fsPromises.rm
  • fsPromises.stat
  • fsPromises.symlink
  • fsPromises.truncate
  • fsPromises.unlink
  • fsPromises.utimes
  • fsPromises.watch
  • fsPromises.writeFile
  • fsPromises.constants
  • fs.access
  • fs.accessSync
  • fs.appendFile
  • fs.appendFileSync
  • fs.chmod Unsupported
  • fs.chmodSync Unsupported
  • fs.chown Unsupported
  • fs.chownSync Unsupported
  • fs.close
  • fs.closeSync
  • fs.copyFile
  • fs.copyFileSync
  • fs.cp
  • fs.cpSync
  • fs.createReadStream
  • fs.createWriteStream
  • fs.exists
  • fs.existsSync
  • fs.fchmod Unsupported
  • fs.fchmodSync Unsupported
  • fs.fchown Unsupported
  • fs.fchownSync Unsupported
  • fs.fdatasync
  • fs.fdatasyncSync
  • fs.fstat
  • fs.fstatSync
  • fs.fsync
  • fs.fsyncSync
  • fs.ftruncate
  • fs.ftruncateSync
  • fs.futimes
  • fs.futimesSync
  • fs.lchmod Unsupported
  • fs.lchmodSync Unsupported
  • fs.lchown Unsupported
  • fs.lchownSync Unsupported
  • fs.link
  • fs.linkSync
  • fs.lstat
  • fs.lstatSync
  • fs.lutimes
  • fs.lutimesSync
  • fs.mkdir
  • fs.mkdirSync
  • fs.mkdtemp
  • fs.mkdtempSync
  • fs.open
  • fs.openSync
  • fs.opendir
  • fs.opendirSync
  • fs.read
  • fs.readSync
  • fs.readdir
  • fs.readdirSync
  • fs.readFile
  • fs.readFileSync
  • fs.readlink
  • fs.readlinkSync
  • fs.readv
  • fs.readvSync
  • fs.realpath
  • fs.realpathSync
  • fs.rename
  • fs.renameSync
  • fs.rmdir
  • fs.rmdirSync
  • fs.rm
  • fs.rmSync
  • fs.stat
  • fs.statSync
  • fs.symlink
  • fs.symlinkSync
  • fs.truncate
  • fs.truncateSync
  • fs.unlink
  • fs.unlinkSync
  • fs.unwatchFile Unsupported
  • fs.utimes
  • fs.utimesSync
  • fs.watch Unsupported
  • fs.watchFile Unsupported
  • fs.write
  • fs.writeSync
  • fs.writeFile
  • fs.writeFileSync
  • fs.writev
  • fs.writevSync
  • fs.Dir
  • fs.Dirent
  • fs.FSWatcher Unsupported
  • fs.StatWatcher Unsupported
  • fs.ReadStream
  • fs.Stats
  • fs.WriteStream
  • fs.constants

@Puellaquae Puellaquae marked this pull request as ready for review September 26, 2022 14:20
@juntao juntao requested a review from L-jasmine October 1, 2022 06:37
@juntao
Copy link
Member

juntao commented Oct 2, 2022

@Puellaquae Does nodejs have a CI test suite we can run to validate those API implementations? Thanks!

@Puellaquae
Copy link
Contributor Author

@Puellaquae Does nodejs have a CI test suite we can run to validate those API implementations? Thanks!

There seems no test suite for node lib api in nodejs, while deno has some test suite for its node api implementations.

@juntao
Copy link
Member

juntao commented Oct 4, 2022

Is it possible for us to run the deno tests then? Thanks.

@Puellaquae
Copy link
Contributor Author

It need some works before running.

@Puellaquae
Copy link
Contributor Author

@L-jasmine poll fd write will cause a infinite loop in ubuntu, while in WSL1 it won't happen.

fn main() -> Result<(), wasi::Errno> {
    unsafe {
        let fd = wasi::path_open(
            3,
            0,
            "test.txt",
            0,
            wasi::RIGHTS_FD_WRITE | wasi::RIGHTS_POLL_FD_READWRITE,
            0,
            wasi::FDFLAGS_NONBLOCK,
        )?;
        println!("{}", fd);
        let subs = vec![wasi::Subscription {
            userdata: 0,
            u: wasi::SubscriptionU {
                tag: 2,
                u: wasi::SubscriptionUU {
                    fd_write: wasi::SubscriptionFdReadwrite {
                        file_descriptor: fd as u32,
                    },
                },
            },
        }];
        let mut events = vec![wasi::Event {
            userdata: 0,
            error: wasi::ERRNO_SUCCESS,
            type_: wasi::EVENTTYPE_FD_WRITE,
            fd_readwrite: wasi::EventFdReadwrite {
                nbytes: 0,
                flags: 0,
            },
        }];
        let n = wasi::poll_oneoff(subs.as_ptr(), events.as_mut_ptr(), subs.len())?;
        println!("{}", n);
    };
    Ok(())
}

@hydai

poll fd read also has same problem.

@Puellaquae
Copy link
Contributor Author

path_readlink is not work well

fn main() -> Result<(), wasi::Errno> {
    unsafe {
        wasi::path_symlink("./test.txt", 3, "./test-link.txt")?;
        let stat = wasi::path_filestat_get(3, 0, "./test-link.txt")?;
        println!("{:?}", stat);
        let mut buf = [0;1024];
        let len = wasi::path_readlink(3, "./test-link.txt", buf.as_mut_ptr(), buf.len())?;
        println!("{:?}", String::from_utf8_lossy(&buf[0..len]));
    };
    Ok(())
}

It will return error: Error: Errno { code: 28, name: "INVAL", message: "Invalid argument." }
If the link file is under a folder like:

fn main() -> Result<(), wasi::Errno> {
    unsafe {
        wasi::path_symlink("./test.txt", 3, "./test/test-link.txt")?;
        let stat = wasi::path_filestat_get(3, 0, "./test/test-link.txt")?;
        println!("{:?}", stat);
        let mut buf = [0;1024];
        let len = wasi::path_readlink(3, "./test/test-link.txt", buf.as_mut_ptr(), buf.len())?;
        println!("{:?}", String::from_utf8_lossy(&buf[0..len]));
    };
    Ok(())
}

It will return error: Error: Errno { code: 44, name: "NOENT", message: "No such file or directory." }
@hydai

@hydai
Copy link
Member

hydai commented Oct 20, 2022

We are focusing on some high-priority issues. If you encounter any bugs, please raise issues on the WasmEdge project with the detailed reproduction steps and the environment information. Thanks.

@L-jasmine
Copy link
Collaborator

path_readlink is not work well

fn main() -> Result<(), wasi::Errno> {
    unsafe {
        wasi::path_symlink("./test.txt", 3, "./test-link.txt")?;
        let stat = wasi::path_filestat_get(3, 0, "./test-link.txt")?;
        println!("{:?}", stat);
        let mut buf = [0;1024];
        let len = wasi::path_readlink(3, "./test-link.txt", buf.as_mut_ptr(), buf.len())?;
        println!("{:?}", String::from_utf8_lossy(&buf[0..len]));
    };
    Ok(())
}

It will return error: Error: Errno { code: 28, name: "INVAL", message: "Invalid argument." } If the link file is under a folder like:

fn main() -> Result<(), wasi::Errno> {
    unsafe {
        wasi::path_symlink("./test.txt", 3, "./test/test-link.txt")?;
        let stat = wasi::path_filestat_get(3, 0, "./test/test-link.txt")?;
        println!("{:?}", stat);
        let mut buf = [0;1024];
        let len = wasi::path_readlink(3, "./test/test-link.txt", buf.as_mut_ptr(), buf.len())?;
        println!("{:?}", String::from_utf8_lossy(&buf[0..len]));
    };
    Ok(())
}

It will return error: Error: Errno { code: 44, name: "NOENT", message: "No such file or directory." } @hydai

I think symlink is not very important.

@L-jasmine
Copy link
Collaborator

@L-jasmine poll fd write will cause a infinite loop in ubuntu, while in WSL1 it won't happen.

fn main() -> Result<(), wasi::Errno> {
    unsafe {
        let fd = wasi::path_open(
            3,
            0,
            "test.txt",
            0,
            wasi::RIGHTS_FD_WRITE | wasi::RIGHTS_POLL_FD_READWRITE,
            0,
            wasi::FDFLAGS_NONBLOCK,
        )?;
        println!("{}", fd);
        let subs = vec![wasi::Subscription {
            userdata: 0,
            u: wasi::SubscriptionU {
                tag: 2,
                u: wasi::SubscriptionUU {
                    fd_write: wasi::SubscriptionFdReadwrite {
                        file_descriptor: fd as u32,
                    },
                },
            },
        }];
        let mut events = vec![wasi::Event {
            userdata: 0,
            error: wasi::ERRNO_SUCCESS,
            type_: wasi::EVENTTYPE_FD_WRITE,
            fd_readwrite: wasi::EventFdReadwrite {
                nbytes: 0,
                flags: 0,
            },
        }];
        let n = wasi::poll_oneoff(subs.as_ptr(), events.as_mut_ptr(), subs.len())?;
        println!("{}", n);
    };
    Ok(())
}

@hydai

poll fd read also has same problem.

in linux, epoll a normal file will get a Operation not permitted.
normal file read & write not need wait.
so, we can skip poll a file, just think it is ready to read or write.

…ame-type-check, rmdir-[recursive-[sync-warns, throws, warns]-[not-found, on-file], type-check], truncate-[clear-file-zero, fd, sync], unlink-type-check, write-[file, no-fd, stream, stream-throw-type-error, sync], writefile-with-fd
…rite-[negativeoffset, stream-encoding, sync-optional-params]
@L-jasmine
Copy link
Collaborator

@Puellaquae if you think this pr is time to merge, please @ me. Thinks.

…times, fs-writev, fs-writev-[promises, sync]
…params], fs-promises, fs-promisified, fs-read-[offset-null, optional-params, position-validation, promises-optional-params, type], fs-write-[buffer, optional-params]
@Puellaquae
Copy link
Contributor Author

@L-jasmine Most of fs api is implemented and passed nodejs's tests. ReadStream and WriteStream only has basic function. chmod, chown and watch is unsupported. And symlink on dir, readlink and realpath is not work well now.

@L-jasmine L-jasmine merged commit 2ac8eaa into second-state:main Dec 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants