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

Added file/link functions #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions posix.ipkg
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package posix

sourcedir = src
modules = System.Posix.Directory
, System.Posix.Files
, System.Posix.Time
, System.Posix.Signal
, System.Posix.Syslog
Expand All @@ -13,6 +14,7 @@ objs = dirent_accessors.c
, signal_wrappers.c
, syslog_wrappers.c
, getcwd_wrapper.c
, files_wrappers.c
, create_dir_wrappers.c

tests = System.Posix.Test.Syslog.test_syslog
Expand Down
29 changes: 29 additions & 0 deletions src/System/Posix/Files.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module System.Posix.Files

%include C "files_wrappers.c"

||| Checks if @file exists using access
export
fileExists : String -> IO Bool
fileExists path = map (/= 0) $ foreign FFI_C "idris_file_exists" (String -> IO Int) path

||| Creates a link from @old to @new
export
createLink : String -> String -> IO ()
createLink old new = foreign FFI_C "link" (String -> String -> IO Int) old new *> pure ()

||| Removes a link from @path
export
removeLink : String -> IO ()
removeLink path = foreign FFI_C "unlink" (String -> IO Int) path *> pure ()

||| Creates a symbolic named @path2 that contains @path1
export
createSymbolicLink : String -> String -> IO ()
createSymbolicLink path1 path2 = foreign FFI_C
"symlink" (String -> String -> IO Unit) path1 path2

||| Changes the name of @old to @new
export
rename : String -> String -> IO ()
rename old new = foreign FFI_C "rename" (String -> String -> IO Int) old new *> pure ()
6 changes: 6 additions & 0 deletions src/files_wrappers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <unistd.h>

int idris_file_exists(const char *path) {
// simply check for existence
return access(path, F_OK) == 0;
}