4
4
#include < dirent.h>
5
5
#include < sys/stat.h>
6
6
#include < string.h>
7
- #include < unistd .h>
7
+ #include < ftw .h>
8
8
#include " hvm.cu"
9
9
10
10
// Readback: λ-Encoded Ctr
@@ -838,12 +838,12 @@ Port io_dl_close(GNet* gnet, Book* book, Port argm) {
838
838
return gnet_inject_ok (gnet, new_port (ERA, 0 ));
839
839
}
840
840
841
- // Deletes a single file or an empty directory at the specified path.
841
+ // Removes a single file or an empty directory at the specified path.
842
842
// Returns Ok(None) if successful, or Err(reason) if an error occurs.
843
843
// This function attempts to remove both files and empty directories without
844
844
// first checking the type of the path.
845
845
// Returns: Result<*, IOError<i24>>
846
- Port io_delete_file (GNet* gnet, Port argm) {
846
+ Port io_remove (GNet* gnet, Port argm) {
847
847
Str s = gnet_readback_str (gnet, argm);
848
848
849
849
int result = remove (s.buf );
@@ -856,80 +856,35 @@ Port io_delete_file(GNet* gnet, Port argm) {
856
856
}
857
857
}
858
858
859
- int delete_directory_recursive (const char * path) {
860
- DIR* d = opendir (path);
861
- size_t path_len = strlen (path);
862
- int r = -1 ;
863
-
864
- if (d) {
865
- struct dirent *p;
866
- r = 0 ;
867
-
868
- while (!r && (p = readdir (d))) {
869
- int r2 = -1 ;
870
- char * buf;
871
- size_t len;
872
-
873
- if (!strcmp (p->d_name , " ." ) || !strcmp (p->d_name , " .." )) {
874
- continue ;
875
- }
876
-
877
- len = path_len + strlen (p->d_name ) + 2 ;
878
- buf = (char *) malloc (len);
879
-
880
- if (buf) {
881
- struct stat statbuf;
882
- snprintf (buf, len, " %s/%s" , path, p->d_name );
883
-
884
- if (!stat (buf, &statbuf)) {
885
- if (S_ISDIR (statbuf.st_mode )) {
886
- r2 = delete_directory_recursive (buf);
887
- } else {
888
- r2 = remove (buf);
889
- }
890
- }
891
-
892
- free (buf);
893
- }
894
-
895
- r = r2;
896
- }
859
+ int remove_all_aux (const char * path, const struct stat * stat, int flags, struct FTW * ftw) {
860
+ return remove (path);
861
+ }
897
862
898
- closedir (d);
863
+ int remove_all (const char * path) {
864
+ struct stat st;
865
+ if (stat (path, &st) != 0 ) {
866
+ return remove (path);
899
867
}
900
-
901
- if (!r) {
902
- r = rmdir (path);
868
+ if (S_ISDIR (st.st_mode )) {
869
+ return nftw (path, remove_all_aux, 32 , FTW_DEPTH | FTW_PHYS);
870
+ } else {
871
+ return remove (path);
903
872
}
904
-
905
- return r;
906
873
}
907
874
908
- // Deletes a directory at the specified path. If recursive is True,
875
+ // Removes any file or directory recursively at the specified path.
909
876
// it will delete the directory and all its contents.
910
877
// Returns Ok(None) if successful, or Err(reason) if an error occurs.
911
878
// Note: For non-recursive deletion of an empty directory,
912
879
// this function behaves the same as delete_file(path).
913
880
// Returns: Result<*, IOError<i24>>
914
- Port io_delete_directory (GNet* gnet, Port argm) {
915
- Tup tup = gnet_readback_tup (gnet, argm, 2 );
916
- if (tup.elem_len != 2 ) {
917
- fprintf (stderr, " io_delete_directory: expected tuple\n " );
918
-
919
- return gnet_inject_io_err_type (gnet);
920
- }
921
-
922
- Str path = gnet_readback_str (gnet, tup.elem_buf [0 ]);
923
- u32 rec = get_u24 (get_val (tup.elem_buf [1 ]));
924
- int res;
925
- if (rec) {
926
- res = delete_directory_recursive (path.buf );
927
- } else {
928
- res = rmdir (path.buf );
929
- }
881
+ Port io_remove_all (GNet* gnet, Port argm) {
882
+ Str path = gnet_readback_str (gnet, argm);
883
+
884
+ int res = remove_all (path.buf );
930
885
free (path.buf );
931
886
932
- if (res == 0 ) {
887
+ if (0 == res ) {
933
888
return gnet_inject_ok (gnet, new_port (ERA, 0 ));
934
889
} else {
935
890
return gnet_inject_io_err_inner (gnet, new_port (NUM, new_i24 (errno)));
@@ -943,10 +898,10 @@ Port io_mkdir(GNet* gnet, Port argm) {
943
898
Str name = gnet_readback_str (gnet, argm);
944
899
945
900
const mode_t mode = 0777 ;
946
- int result = mkdir (name.buf , mode);
901
+ int status = mkdir (name.buf , mode);
947
902
free (name.buf );
948
903
949
- if (result ) {
904
+ if (status ) {
950
905
return gnet_inject_io_err_inner (gnet, new_port (NUM, new_i24 (errno)));
951
906
} else {
952
907
return gnet_inject_ok (gnet, new_port (ERA, 0 ));
@@ -965,8 +920,8 @@ void book_init(Book* book) {
965
920
book->ffns_buf [book->ffns_len ++] = (FFn){" DL_OPEN" , io_dl_open};
966
921
book->ffns_buf [book->ffns_len ++] = (FFn){" DL_CALL" , io_dl_call};
967
922
book->ffns_buf [book->ffns_len ++] = (FFn){" DL_CLOSE" , io_dl_open};
968
- book->ffns_buf [book->ffns_len ++] = (FFn){" DELETE_FILE " , io_delete_file };
969
- book->ffns_buf [book->ffns_len ++] = (FFn){" DELETE_DIRECTORY " , io_delete_directory };
923
+ book->ffns_buf [book->ffns_len ++] = (FFn){" RM " , io_remove };
924
+ book->ffns_buf [book->ffns_len ++] = (FFn){" RM_ALL " , io_remove_all };
970
925
book->ffns_buf [book->ffns_len ++] = (FFn){" MKDIR" , io_mkdir};
971
926
972
927
cudaMemcpyToSymbol (BOOK, book, sizeof (Book));
0 commit comments