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

Filesystem queries #20

Open
2 of 7 tasks
deaddog opened this issue May 23, 2022 · 6 comments
Open
2 of 7 tasks

Filesystem queries #20

deaddog opened this issue May 23, 2022 · 6 comments

Comments

@deaddog
Copy link
Contributor

deaddog commented May 23, 2022

A lot of bash scripts rely on checking for the existence of files/dirs as pre-flight checks. E.g. something like:

# exit if file doesn't exist
[[ ! -f "/myfile" ]] && exit 1
# exit if dir doesn't exist
[[ ! -d "/mydir" ]] && exit 1

If would be beneficial to be able to make the same kind of queries in hush - maybe something like std.io.file_exists(...)
If it has interest I'll create a PR (and one for the docs as well)

  • Test file exists
  • Test dir exists
  • Read file
  • Write file
  • Create dir/path
  • Delete file
  • Delete path recursively
@gahag
Copy link
Collaborator

gahag commented May 24, 2022

Surely, a full featured io/fs library is a must. Your PR would be more than welcome! Please let me know if I can be of any help.

@deaddog
Copy link
Contributor Author

deaddog commented May 24, 2022

I was mainly focused on the features I'm missing from bash. I have implementations for std.io.file_exists(...) and std.io.dir_exists(...) but I'd be up for adding some additional functions.

Do you have some suggestions for what those could be?

For most other fs operations I'd probably just rely on existing commands and the flexibility they already offer through various arguments. They're not too weird to use either:

# read a file
let mydata = ${ cat "/myfile" }.stdout
# delete a file
{ rm "/myfile" }
# write a file
{ echo "$mydata" > "/myfile" }

On the other hand this marks my first bit of Rust - I'd be up for a challenge.

@gahag
Copy link
Collaborator

gahag commented May 24, 2022

Makes sense, I believe I would use the existing commands in most cases too. It would be nice to have the basic functionality in the standard library tho, just in case. Feel free to implement whichever methods you'd like. And by the way, congrats for your first bit of Rust 🦀

@deaddog
Copy link
Contributor Author

deaddog commented May 25, 2022

Fair point - some essential functions would be nice to have. I've listed the ones I could think of. Is there anything I've missed?

@gahag
Copy link
Collaborator

gahag commented May 25, 2022

For a 100% feature complete set, I'd consider Rust's own std::fs module:

  • canonicalize: Returns the canonical, absolute form of a path with all intermediate components normalized and symbolic links resolved.
  • copy: Copies the contents of one file to another. This function will also copy the permission bits of the original file to the destination file.
  • create_dir: Creates a new, empty directory at the provided path
  • create_dir_all: Recursively create a directory and all of its parent components if they are missing.
  • hard_link: Creates a new hard link on the filesystem.
  • metadata: Given a path, query the file system to get information about a file, directory, etc.
  • read: Read the entire contents of a file into a bytes vector.
  • read_dir: Returns an iterator over the entries within a directory.
  • read_link: Reads a symbolic link, returning the file that the link points to.
  • read_to_string: Read the entire contents of a file into a string.
  • remove_dir: Removes an empty directory.
  • remove_dir_all: Removes a directory at this path, after removing all its contents. Use carefully!
  • remove_file: Removes a file from the filesystem.
  • rename: Rename a file or directory to a new name, replacing the original file if to already exists.
  • set_permissions: Changes the permissions found on a file or a directory.
  • soft_linkDeprecated: Creates a new symbolic link on the filesystem.
  • symlink_metadata: Query the metadata about a file without following symlinks.
  • write: Write a slice as the entire contents of a file

@gahag
Copy link
Collaborator

gahag commented May 25, 2022

Some of these would be collapsed into a single function, such as create_dir and create_dir_all, also read and read_to_string.

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

No branches or pull requests

2 participants