Skip to content

Commit

Permalink
Merge branch 'main' into file-info-functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bcpeinhardt authored Aug 28, 2024
2 parents d44fa4f + 838ef4f commit 6b0736b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Add `FileInfo` and `file_info_type` to get the file type from a `FileInfo` without checking the file system again
- Add `file_info_permissions` and `file_info_permissions_octal` to get the currently set permissions of a file or directory.
- Improve performance of `get_files`
- Add `link_info` function to get `FileInfo` values without following symlinks

## v2.0.1 - 27 June 2024
- Internal refactoring and some added tests
Expand Down
15 changes: 15 additions & 0 deletions src/simplifile.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,25 @@ pub fn file_info_type(from file_info: FileInfo) -> FileType {
}

/// Get information about a file at a given path
///
/// When the given `filepath` points to a symlink, this function will follow
/// the symlink and return information about the target file.
///
/// See `link_info` if you want to get information about a symlink instead.
@external(erlang, "simplifile_erl", "file_info")
@external(javascript, "./simplifile_js.mjs", "fileInfo")
pub fn file_info(filepath: String) -> Result(FileInfo, FileError)

/// Get information about a file at a given path
///
/// When the given `filepath` is a symlink, this function will return
/// infromation about the symlink itself.
///
/// See `file_info` if you want to follow symlinks instead.
@external(erlang, "simplifile_erl", "link_info")
@external(javascript, "./simplifile_js.mjs", "linkInfo")
pub fn link_info(filepath: String) -> Result(FileInfo, FileError)

/// Read a files contents as a string
/// ## Example
/// ```gleam
Expand Down
4 changes: 4 additions & 0 deletions src/simplifile_erl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
delete/1,
delete_directory/1,
file_info/1,
link_info/1,
is_directory/1,
is_file/1,
is_symlink/1,
Expand Down Expand Up @@ -223,3 +224,6 @@ file_info_result(Result) ->

file_info(Filename) ->
file_info_result(file:read_file_info(Filename, [{time, posix}])).

link_info(Filename) ->
file_info_result(file:read_link_info(Filename, [{time, posix}])).
22 changes: 19 additions & 3 deletions src/simplifile_js.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,28 @@ export function currentDirectory() {
* @returns {Ok | GError}
*/
export function fileInfo(filepath) {
return gleamResult(() => new FileInfo(filepath));
return gleamResult(() => {
const stat = fs.statSync(path.normalize(filepath))
return new FileInfo(stat)
});
}

/**
* @param {string} filepath
* @returns {Ok | GError}
*/
export function linkInfo(filepath) {
return gleamResult(() => {
const stat = fs.lstatSync(path.normalize(filepath))
return new FileInfo(stat)
})
}

class FileInfo {
constructor(filepath) {
const stat = fs.statSync(path.normalize(filepath));
/**
* @param {fs.Stats} stat
*/
constructor(stat) {
this.size = stat.size;
this.mode = stat.mode;
this.nlinks = stat.nlink;
Expand Down
22 changes: 22 additions & 0 deletions test/simplifile_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ pub fn file_info_test() {
let assert Ok(_info) = file_info("./test.sh")
}


pub fn file_info_type_test() {
let filepath = "./tmp/file_info_type_test.txt"
let assert Ok(_) = write(to: filepath, contents: "")
Expand All @@ -525,6 +526,27 @@ pub fn file_info_type_test() {
// |> should.equal(Symlink)
}

pub fn link_info_test() {
let target_path = "./tmp/the_target"
let symlink_path = "./tmp/the_symlink"
let target_relative_to_symlink = "the_target"

let assert Ok(_) = write(to: target_path, contents: "Wibble")
let assert Ok(_) = create_symlink(target_relative_to_symlink, symlink_path)

let assert Ok(lstat) = link_info(symlink_path)
let assert Ok(stat) = file_info(symlink_path)

stat
|> should.not_equal(lstat)

stat.size
|> should.equal(6)

lstat.size
|> should.not_equal(6)
}

/// I visually inspected this info to make sure it matched on all targets.
/// TODO: Add a better test setup for validating file info functionality.
pub fn clear_directory_test() {
Expand Down

0 comments on commit 6b0736b

Please sign in to comment.