From 1f6ed0847a60ee55a263229e1d400a2474c5543f Mon Sep 17 00:00:00 2001 From: Magd <{69316030}+{Magd74NA}@users.noreply.github.com> Date: Thu, 26 Jun 2025 23:15:23 -0400 Subject: [PATCH 1/7] corrected typo --- src/simplifile_erl.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/simplifile_erl.erl b/src/simplifile_erl.erl index 810c2f3..60dcdbb 100644 --- a/src/simplifile_erl.erl +++ b/src/simplifile_erl.erl @@ -33,7 +33,7 @@ -include_lib("kernel/include/file.hrl"). -%% A macro for checking whether the error returned is one of the atoms for a posixe error. +%% A macro for checking whether the error returned is one of the atoms for a posix error. -define(is_posix_error(Error), Error =:= eacces orelse Error =:= eagain From bc95634cba9a5ec24b5a3371b67fc5174c945c07 Mon Sep 17 00:00:00 2001 From: Magd <{69316030}+{Magd74NA}@users.noreply.github.com> Date: Thu, 26 Jun 2025 23:24:16 -0400 Subject: [PATCH 2/7] Added a hrl file to represent the file info tuple used in simplifile_erl based on the file.hrl from the erlang standard library --- src/simplifile_file_info.hrl | 39 ++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/simplifile_file_info.hrl diff --git a/src/simplifile_file_info.hrl b/src/simplifile_file_info.hrl new file mode 100644 index 0000000..4148107 --- /dev/null +++ b/src/simplifile_file_info.hrl @@ -0,0 +1,39 @@ +-ifndef(SIMPLIFILE_FILE_HRL_). + +-define(SIMPLIFILE_FILE_HRL_, 1). + +%%-------------------------------------------------------------------------- + +-record(simplifile_file_info, + {size :: non_neg_integer() | undefined, % Size of file in bytes. + mode :: non_neg_integer() | undefined, + % File permissions. On Windows, + % the owner permissions will be + % duplicated for group and user. + links :: non_neg_integer() | undefined, + inode :: non_neg_integer() | undefined, % Inode number for file. + uid :: non_neg_integer() | undefined, % User id for owner. + gid :: non_neg_integer() | undefined, % Group id for owner. + major_device :: non_neg_integer() | undefined, + % Identifies the file system (Unix), + % or the drive number (A: = 0, B: = 1) + % (Windows). + %% The following are Unix specific. + %% They are set to zero on other operating systems. + atime :: file:date_time() | non_neg_integer() | undefined, + % The local time the file was last read: + % {{Year, Mon, Day}, {Hour, Min, Sec}}. + % atime, ctime, mtime may also be unix epochs() + mtime :: file:date_time() | non_neg_integer() | undefined, + % The local time the file was last written. + ctime :: + file:date_time() | + non_neg_integer() | + undefined}). % The interpretation of this time field + % is dependent on operating system. + % On Unix it is the last time the file + % or the inode was changed. On Windows, + % it is the creation time. + +%%-------------------------------------------------------------------------- +-endif. From cba5d0c06a6ed5a9174eda67b3cf41eea6fad315 Mon Sep 17 00:00:00 2001 From: Magd <{69316030}+{Magd74NA}@users.noreply.github.com> Date: Thu, 26 Jun 2025 23:32:12 -0400 Subject: [PATCH 3/7] reimplemented the file_info_result function to be a bit more maintainable and idiomatic (I think?) using the file.hrl and simplifile_file_info.hrl records. --- src/simplifile_erl.erl | 51 ++++++++++++++---------------------- src/simplifile_file_info.hrl | 6 +++++ 2 files changed, 25 insertions(+), 32 deletions(-) diff --git a/src/simplifile_erl.erl b/src/simplifile_erl.erl index 60dcdbb..9de0826 100644 --- a/src/simplifile_erl.erl +++ b/src/simplifile_erl.erl @@ -32,6 +32,7 @@ ]). -include_lib("kernel/include/file.hrl"). +-include_lib("simplifile_file_info.hrl"). %% A macro for checking whether the error returned is one of the atoms for a posix error. -define(is_posix_error(Error), @@ -203,38 +204,24 @@ is_symlink(Path) -> posix_result({error, Reason}) end. -file_info_result(Result) -> - case Result of - {ok, - {file_info, - Size, - _Type, - _Access, - Atime, - Mtime, - Ctime, - Mode, - Links, - MajorDevice, - _MinorDevice, - Inode, - Uid, - Gid}} -> - {ok, - {file_info, - Size, - Mode, - Links, - Inode, - Uid, - Gid, - MajorDevice, - Atime, - Mtime, - Ctime}}; - {error, Reason} when ?is_posix_error(Reason) -> - Result - end. + file_info_result(Result) -> + case Result of + {ok, FileInfo} when is_record(FileInfo, file_info) -> + {ok, #simplifile_file_info{ + size = FileInfo#file_info.size, + mode = FileInfo#file_info.mode, + links = FileInfo#file_info.links, + inode = FileInfo#file_info.inode, + uid = FileInfo#file_info.uid, + gid = FileInfo#file_info.gid, + major_device = FileInfo#file_info.major_device, + atime = FileInfo#file_info.atime, + mtime = FileInfo#file_info.mtime, + ctime = FileInfo#file_info.ctime + }}; + {error, Reason} when ?is_posix_error(Reason) -> + {error, Reason} + end. file_info(Filename) -> file_info_result(file:read_file_info(Filename, [{time, posix}])). diff --git a/src/simplifile_file_info.hrl b/src/simplifile_file_info.hrl index 4148107..9a9438b 100644 --- a/src/simplifile_file_info.hrl +++ b/src/simplifile_file_info.hrl @@ -1,3 +1,9 @@ +%%% -------------------------------------------------- +%%% @author Magd Aref +%%% @doc Erlang header file defining simplifile's file_info +%%% @end +%%% -------------------------------------------------- + -ifndef(SIMPLIFILE_FILE_HRL_). -define(SIMPLIFILE_FILE_HRL_, 1). From 767245a149625a0a4db71196f426054cf9d44e14 Mon Sep 17 00:00:00 2001 From: Magd <{69316030}+{Magd74NA}@users.noreply.github.com> Date: Thu, 26 Jun 2025 23:35:26 -0400 Subject: [PATCH 4/7] Wrote a new helper function to simplify filt type checking functions taking advantage of the new record I wrote to make code more concise and idiomatic (I think?) --- src/simplifile_erl.erl | 45 ++++++++++-------------------------------- 1 file changed, 10 insertions(+), 35 deletions(-) diff --git a/src/simplifile_erl.erl b/src/simplifile_erl.erl index 9de0826..c63203a 100644 --- a/src/simplifile_erl.erl +++ b/src/simplifile_erl.erl @@ -159,50 +159,25 @@ rename_file(Source, Destination) -> set_permissions_octal(Filename, Permissions) -> posix_result(file:change_mode(Filename, Permissions)). -is_directory(Path) -> - case file:read_file_info(Path) of - {ok, FileInfo} -> - case FileInfo#file_info.type of - directory -> - {ok, true}; - _ -> - {ok, false} - end; +% Helper function to check file type +check_file_type(Path, TypeToCheck, ReadFunction) -> + case ReadFunction(Path) of + {ok, FileInfo} when is_record(FileInfo, file_info) -> + {ok, FileInfo#file_info.type =:= TypeToCheck}; {error, enoent} -> {ok, false}; {error, Reason} -> posix_result({error, Reason}) end. +is_directory(Path) -> + check_file_type(Path, directory, fun file:read_file_info/1). + is_file(Path) -> - case file:read_file_info(Path) of - {ok, FileInfo} -> - case FileInfo#file_info.type of - regular -> - {ok, true}; - _ -> - {ok, false} - end; - {error, enoent} -> - {ok, false}; - {error, Reason} -> - posix_result({error, Reason}) - end. + check_file_type(Path, regular, fun file:read_file_info/1). is_symlink(Path) -> - case file:read_link_info(Path) of - {ok, FileInfo} -> - case FileInfo#file_info.type of - symlink -> - {ok, true}; - _ -> - {ok, false} - end; - {error, enoent} -> - {ok, false}; - {error, Reason} -> - posix_result({error, Reason}) - end. + check_file_type(Path, symlink, fun file:read_link_info/1). file_info_result(Result) -> case Result of From 3c54bef5a9fa2369e768d323ee7fcb8d475c9d85 Mon Sep 17 00:00:00 2001 From: Magd <{69316030}+{Magd74NA}@users.noreply.github.com> Date: Fri, 27 Jun 2025 13:19:39 -0400 Subject: [PATCH 5/7] Revert "Wrote a new helper function to simplify filt type checking functions taking advantage of the new record I wrote to make code more concise and idiomatic (I think?)" This reverts commit 767245a149625a0a4db71196f426054cf9d44e14. --- src/simplifile_erl.erl | 45 ++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/src/simplifile_erl.erl b/src/simplifile_erl.erl index c63203a..9de0826 100644 --- a/src/simplifile_erl.erl +++ b/src/simplifile_erl.erl @@ -159,25 +159,50 @@ rename_file(Source, Destination) -> set_permissions_octal(Filename, Permissions) -> posix_result(file:change_mode(Filename, Permissions)). -% Helper function to check file type -check_file_type(Path, TypeToCheck, ReadFunction) -> - case ReadFunction(Path) of - {ok, FileInfo} when is_record(FileInfo, file_info) -> - {ok, FileInfo#file_info.type =:= TypeToCheck}; +is_directory(Path) -> + case file:read_file_info(Path) of + {ok, FileInfo} -> + case FileInfo#file_info.type of + directory -> + {ok, true}; + _ -> + {ok, false} + end; {error, enoent} -> {ok, false}; {error, Reason} -> posix_result({error, Reason}) end. -is_directory(Path) -> - check_file_type(Path, directory, fun file:read_file_info/1). - is_file(Path) -> - check_file_type(Path, regular, fun file:read_file_info/1). + case file:read_file_info(Path) of + {ok, FileInfo} -> + case FileInfo#file_info.type of + regular -> + {ok, true}; + _ -> + {ok, false} + end; + {error, enoent} -> + {ok, false}; + {error, Reason} -> + posix_result({error, Reason}) + end. is_symlink(Path) -> - check_file_type(Path, symlink, fun file:read_link_info/1). + case file:read_link_info(Path) of + {ok, FileInfo} -> + case FileInfo#file_info.type of + symlink -> + {ok, true}; + _ -> + {ok, false} + end; + {error, enoent} -> + {ok, false}; + {error, Reason} -> + posix_result({error, Reason}) + end. file_info_result(Result) -> case Result of From 04ddb49d9091ae71e33d3d232e99cd6ca42a1d8b Mon Sep 17 00:00:00 2001 From: Magd <{69316030}+{Magd74NA}@users.noreply.github.com> Date: Fri, 27 Jun 2025 13:36:46 -0400 Subject: [PATCH 6/7] used record syntax without defining a new erlang record --- src/simplifile_FileInfo.hrl | 12 ++++++++++ src/simplifile_erl.erl | 25 ++++++++++---------- src/simplifile_file_info.hrl | 45 ------------------------------------ 3 files changed, 25 insertions(+), 57 deletions(-) create mode 100644 src/simplifile_FileInfo.hrl delete mode 100644 src/simplifile_file_info.hrl diff --git a/src/simplifile_FileInfo.hrl b/src/simplifile_FileInfo.hrl new file mode 100644 index 0000000..ca795bd --- /dev/null +++ b/src/simplifile_FileInfo.hrl @@ -0,0 +1,12 @@ +-record(file_info, { + size :: integer(), + mode :: integer(), + nlinks :: integer(), + inode :: integer(), + user_id :: integer(), + group_id :: integer(), + dev :: integer(), + atime_seconds :: integer(), + mtime_seconds :: integer(), + ctime_seconds :: integer() +}). diff --git a/src/simplifile_erl.erl b/src/simplifile_erl.erl index 9de0826..874130b 100644 --- a/src/simplifile_erl.erl +++ b/src/simplifile_erl.erl @@ -32,7 +32,6 @@ ]). -include_lib("kernel/include/file.hrl"). --include_lib("simplifile_file_info.hrl"). %% A macro for checking whether the error returned is one of the atoms for a posix error. -define(is_posix_error(Error), @@ -204,20 +203,22 @@ is_symlink(Path) -> posix_result({error, Reason}) end. + %% For information on the file_info record refer to + %% https://www.erlang.org/doc/apps/kernel/file.html#t:file_info/0 file_info_result(Result) -> case Result of {ok, FileInfo} when is_record(FileInfo, file_info) -> - {ok, #simplifile_file_info{ - size = FileInfo#file_info.size, - mode = FileInfo#file_info.mode, - links = FileInfo#file_info.links, - inode = FileInfo#file_info.inode, - uid = FileInfo#file_info.uid, - gid = FileInfo#file_info.gid, - major_device = FileInfo#file_info.major_device, - atime = FileInfo#file_info.atime, - mtime = FileInfo#file_info.mtime, - ctime = FileInfo#file_info.ctime + {ok, {file_info, + FileInfo#file_info.size, + FileInfo#file_info.mode, + FileInfo#file_info.links, + FileInfo#file_info.inode, + FileInfo#file_info.uid, + FileInfo#file_info.gid, + FileInfo#file_info.major_device, + FileInfo#file_info.atime, + FileInfo#file_info.mtime, + FileInfo#file_info.ctime }}; {error, Reason} when ?is_posix_error(Reason) -> {error, Reason} diff --git a/src/simplifile_file_info.hrl b/src/simplifile_file_info.hrl deleted file mode 100644 index 9a9438b..0000000 --- a/src/simplifile_file_info.hrl +++ /dev/null @@ -1,45 +0,0 @@ -%%% -------------------------------------------------- -%%% @author Magd Aref -%%% @doc Erlang header file defining simplifile's file_info -%%% @end -%%% -------------------------------------------------- - --ifndef(SIMPLIFILE_FILE_HRL_). - --define(SIMPLIFILE_FILE_HRL_, 1). - -%%-------------------------------------------------------------------------- - --record(simplifile_file_info, - {size :: non_neg_integer() | undefined, % Size of file in bytes. - mode :: non_neg_integer() | undefined, - % File permissions. On Windows, - % the owner permissions will be - % duplicated for group and user. - links :: non_neg_integer() | undefined, - inode :: non_neg_integer() | undefined, % Inode number for file. - uid :: non_neg_integer() | undefined, % User id for owner. - gid :: non_neg_integer() | undefined, % Group id for owner. - major_device :: non_neg_integer() | undefined, - % Identifies the file system (Unix), - % or the drive number (A: = 0, B: = 1) - % (Windows). - %% The following are Unix specific. - %% They are set to zero on other operating systems. - atime :: file:date_time() | non_neg_integer() | undefined, - % The local time the file was last read: - % {{Year, Mon, Day}, {Hour, Min, Sec}}. - % atime, ctime, mtime may also be unix epochs() - mtime :: file:date_time() | non_neg_integer() | undefined, - % The local time the file was last written. - ctime :: - file:date_time() | - non_neg_integer() | - undefined}). % The interpretation of this time field - % is dependent on operating system. - % On Unix it is the last time the file - % or the inode was changed. On Windows, - % it is the creation time. - -%%-------------------------------------------------------------------------- --endif. From 2daeed4828e220915b3ba9a51fdadd38ac962f58 Mon Sep 17 00:00:00 2001 From: Magd <{69316030}+{Magd74NA}@users.noreply.github.com> Date: Fri, 27 Jun 2025 13:37:35 -0400 Subject: [PATCH 7/7] fixed an oopsie added a file I didn't mean to --- src/simplifile_FileInfo.hrl | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 src/simplifile_FileInfo.hrl diff --git a/src/simplifile_FileInfo.hrl b/src/simplifile_FileInfo.hrl deleted file mode 100644 index ca795bd..0000000 --- a/src/simplifile_FileInfo.hrl +++ /dev/null @@ -1,12 +0,0 @@ --record(file_info, { - size :: integer(), - mode :: integer(), - nlinks :: integer(), - inode :: integer(), - user_id :: integer(), - group_id :: integer(), - dev :: integer(), - atime_seconds :: integer(), - mtime_seconds :: integer(), - ctime_seconds :: integer() -}).