Skip to content

Commit

Permalink
add link_info
Browse files Browse the repository at this point in the history
  • Loading branch information
joshi-monster committed Aug 26, 2024
1 parent 5193b3e commit e4f613c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
15 changes: 15 additions & 0 deletions src/simplifile.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,25 @@ pub type FileInfo {
}

/// 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: 19 additions & 3 deletions test/simplifile_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import simplifile.{
FilePermissions, NotUtf8, Read, Unknown, Write, append, append_bits,
copy_directory, copy_file, create_directory, create_directory_all, create_file,
create_symlink, delete, delete_all, file_info, file_permissions_to_octal,
get_files, is_directory, is_file, is_symlink, read, read_bits, read_directory,
rename_directory, rename_file, set_permissions, set_permissions_octal, write,
write_bits,
get_files, is_directory, is_file, is_symlink, link_info, read, read_bits,
read_directory, rename_directory, rename_file, set_permissions,
set_permissions_octal, write, write_bits,
}

pub fn main() {
Expand Down Expand Up @@ -473,6 +473,22 @@ pub fn file_info_test() {
let assert Ok(_info) = file_info("./test.sh")
}

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)

let assert False = stat == lstat
let assert True = stat.size == 6
let assert False = lstat.size == 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 e4f613c

Please sign in to comment.