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

add link_info #36

Merged
merged 3 commits into from
Aug 28, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Unreleased
- 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 @@ -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
27 changes: 24 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,27 @@ 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)

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
Loading