Skip to content

Commit 9d6325e

Browse files
committed
remove mode argument, some minor changes
1 parent f02c2e2 commit 9d6325e

File tree

6 files changed

+44
-61
lines changed

6 files changed

+44
-61
lines changed

doc/specs/stdlib_system.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -543,12 +543,12 @@ Experimental
543543

544544
### Description
545545

546-
It creates an empty directory.
546+
It creates an empty directory with default permissions.
547547
It is designed to work across multiple platforms. On Windows, paths with both forward `/` and backward `\` slashes are accepted.
548548

549549
### Syntax
550550

551-
`call [[stdlib_system(module):make_directory(subroutine)]] (path, mode, err)`
551+
`call [[stdlib_system(module):make_directory(subroutine)]] (path [,err])`
552552

553553
### Class
554554

@@ -558,13 +558,11 @@ Subroutine
558558

559559
`path`: Shall be a character string containing the path of the directory to create. It is an `intent(in)` argument.
560560

561-
`mode`: Shall be a scalar integer indicating the permission bits required (Not applicable to Windows). It is an `optional, intent(in)` argument.
562-
563-
`err`: Shall be of type `state_type`, for error handling. It is an `optional, intent(out)` argument.
561+
`err`(optional): Shall be of type `state_type`, for error handling. It is an `optional, intent(out)` argument.
564562

565563
### Return values
566564

567-
The `err` is set accordingly.
565+
`err` is an optional state return flag. On error if not requested, a `FS_ERROR` will trigger an error stop.
568566

569567
### Example
570568

@@ -597,11 +595,11 @@ Subroutine
597595

598596
`path`: Shall be a character string containing the path of the directory to create. It is an `intent(in)` argument.
599597

600-
`err`: Shall be of type `state_type`, for error handling. It is an `intent(out)` argument.
598+
`err`(optional): Shall be of type `state_type`, for error handling. It is an `intent(out)` argument.
601599

602600
### Return values
603601

604-
The `err` is set accordingly.
602+
`err` is an optional state return flag. On error if not requested, a `FS_ERROR` will trigger an error stop.
605603

606604
### Example
607605

example/system/example_make_directory.f90

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
! Illustrate the usage of make_directory
1+
! Illustrate the usage of `make_directory`
22
program example_make_directory
3-
use stdlib_system, only: make_directory, is_directory
3+
use stdlib_system, only: make_directory
44
use stdlib_error, only: state_type
55
implicit none
66

77
type(state_type) :: err
88

9-
call make_directory("test", err=err)
9+
call make_directory("temp_dir", err)
1010

1111
if (err%error()) then
1212
print *, err%print()

example/system/example_remove_directory.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
! Illustrate the usage of remove_directory
1+
! Illustrate the usage of `remove_directory`
22
program example_remove_directory
3-
use stdlib_system, only: make_directory, is_directory, remove_directory
3+
use stdlib_system, only: remove_directory
44
use stdlib_error, only: state_type
55
implicit none
66

src/stdlib_system.F90

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ module stdlib_system
22
use, intrinsic :: iso_c_binding, only : c_int, c_long, c_ptr, c_null_ptr, c_int64_t, c_size_t, &
33
c_f_pointer
44
use stdlib_kinds, only: int64, dp, c_bool, c_char
5-
use stdlib_strings, only: to_c_char
5+
use stdlib_strings, only: to_c_char, ends_with
66
use stdlib_string_type, only: string_type
7+
use stdlib_optval, only: optval
78
use stdlib_error, only: state_type, STDLIB_SUCCESS, STDLIB_FS_ERROR
89
implicit none
910
private
@@ -115,12 +116,12 @@ module stdlib_system
115116
!! ([Specification](../page/specs/stdlib_system.html#make_directory))
116117
!!
117118
!! ### Summary
118-
!! Creates an empty directory with particular permissions.
119+
!! Creates an empty directory with default permissions.
119120
!!
120121
!! ### Description
121122
!! This function makes an empty directory according to the path provided.
122-
!! Relative paths as well as on Windows paths involving either `/` or `\` are accepted
123-
!! appropriate error message is returned whenever any error occur.
123+
!! Relative paths as well as on Windows, paths involving either `/` or `\` are accepted.
124+
!! Appropriate error message is returned whenever any error occurs.
124125
!!
125126
public :: make_directory
126127

@@ -130,12 +131,12 @@ module stdlib_system
130131
!! ([Specification](../page/specs/stdlib_system.html#remove_directory))
131132
!!
132133
!! ### Summary
133-
!! Deletes an empty directory.
134+
!! Removes an empty directory.
134135
!!
135136
!! ### Description
136-
!! This function deletes an empty directory according to the path provided.
137+
!! This function Removes an empty directory according to the path provided.
137138
!! Relative paths as well as on Windows paths involving either `/` or `\` are accepted.
138-
!! appropriate error message is returned whenever any error occur.
139+
!! Appropriate error message is returned whenever any error occurs.
139140
!!
140141
public :: remove_directory
141142

@@ -879,6 +880,9 @@ end function stdlib_is_directory
879880

880881
end function is_directory
881882

883+
! A helper function to get the result of the C function `strerror`.
884+
! `strerror` is a function provided by `<string.h>`.
885+
! It returns a string describing the meaning of `errno` in the C header `<errno.h>`
882886
function c_get_strerror() result(str)
883887
character(len=:), allocatable :: str
884888

@@ -906,40 +910,27 @@ end function strerror
906910
end function c_get_strerror
907911

908912
!! makes an empty directory
909-
subroutine make_directory(path, mode, err)
913+
subroutine make_directory(path, err)
910914
character(len=*), intent(in) :: path
911-
integer, intent(in), optional :: mode
912915
type(state_type), optional, intent(out) :: err
913916

914917
integer :: code
915918
type(state_type) :: err0
916919

917-
918920
interface
919-
integer function stdlib_make_directory(cpath, cmode) bind(C, name='stdlib_make_directory')
921+
integer function stdlib_make_directory(cpath) bind(C, name='stdlib_make_directory')
920922
import c_char
921923
character(kind=c_char), intent(in) :: cpath(*)
922-
integer, intent(in) :: cmode
923924
end function stdlib_make_directory
924925
end interface
925926

926-
if (is_windows() .and. present(mode)) then
927-
! _mkdir() doesn't have a `mode` argument
928-
err0 = state_type(STDLIB_FS_ERROR, "mode argument not present for Windows")
927+
code = stdlib_make_directory(to_c_char(trim(path)))
928+
929+
if (code /= 0) then
930+
err0 = FS_ERROR_CODE(code, c_get_strerror())
929931
call err0%handle(err)
930-
return
931932
end if
932933

933-
code = stdlib_make_directory(to_c_char(trim(path)), mode)
934-
935-
select case (code)
936-
case (0)
937-
return
938-
case default
939-
! error
940-
err0 = state_type(STDLIB_FS_ERROR, "code:", to_string(code)//',', c_get_strerror())
941-
call err0%handle(err)
942-
end select
943934
end subroutine make_directory
944935

945936
!! Removes an empty directory
@@ -959,14 +950,11 @@ end function stdlib_remove_directory
959950

960951
code = stdlib_remove_directory(to_c_char(trim(path)))
961952

962-
select case (code)
963-
case (0)
964-
return
965-
case default
966-
! error
967-
err0 = state_type(STDLIB_FS_ERROR, "code:", to_string(code)//',', c_get_strerror())
968-
call err0%handle(err)
969-
end select
953+
if (code /= 0) then
954+
err0 = FS_ERROR_CODE(code, c_get_strerror())
955+
call err0%handle(err)
956+
end if
957+
970958
end subroutine remove_directory
971959

972960
!> Returns the file path of the null device for the current operating system.

src/stdlib_system.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,16 @@ char* stdlib_strerror(size_t* len){
1515
return err;
1616
}
1717

18-
int stdlib_make_directory(const char* path, mode_t mode){
18+
int stdlib_make_directory(const char* path){
1919
int code;
2020
#ifdef _WIN32
2121
code = _mkdir(path);
2222
#else
23-
code = mkdir(path, mode);
23+
// Default mode 0777
24+
code = mkdir(path, 0777);
2425
#endif /* ifdef _WIN32 */
2526

26-
if (!code){
27-
return 0;
28-
}
29-
30-
return errno;
27+
return (!code) ? 0 : errno;
3128
}
3229

3330
int stdlib_remove_directory(const char* path){
@@ -38,9 +35,5 @@ int stdlib_remove_directory(const char* path){
3835
code = rmdir(path);
3936
#endif /* ifdef _WIN32 */
4037

41-
if (!code){
42-
return 0;
43-
}
44-
45-
return errno;
38+
return (!code) ? 0 : errno;
4639
}

test/system/test_filesystem.f90

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module test_filesystem
22
use testdrive, only : new_unittest, unittest_type, error_type, check, skip_test
3-
use stdlib_system, only: is_directory, delete_file, FS_ERROR, FS_ERROR_CODE
3+
use stdlib_system, only: is_directory, delete_file, FS_ERROR, FS_ERROR_CODE, &
4+
make_directory, remove_directory
45
use stdlib_error, only: state_type, STDLIB_FS_ERROR
56

67
implicit none
@@ -207,11 +208,13 @@ subroutine test_make_directory_existing(error)
207208

208209
! Clean up: remove the empty directory
209210
call execute_command_line('rmdir ' // filename, exitstat=ios, cmdstat=iocmd, cmdmsg=msg)
211+
210212
if (allocated(error)) then
211213
! if previous error is allocated as well
212214
call check(error, ios==0 .and. iocmd==0, error%message // ' and cannot cleanup make_directory test: '//trim(msg))
213215
return
214216
end if
217+
215218
call check(error, ios==0 .and. iocmd==0, 'Cannot cleanup make_directory test: '//trim(msg))
216219
end subroutine test_make_directory_existing
217220

@@ -230,6 +233,7 @@ subroutine test_remove_directory(error)
230233

231234
call remove_directory(filename, err)
232235
call check(error, err%ok(), 'Could not remove directory: '//err%print())
236+
233237
if (allocated(error)) then
234238
! Clean up: remove the empty directory
235239
call execute_command_line('rmdir ' // filename, exitstat=ios, cmdstat=iocmd, cmdmsg=msg)
@@ -242,7 +246,7 @@ subroutine test_remove_directory_nonexistent(error)
242246
type(state_type) :: err
243247

244248
call remove_directory("random_name", err)
245-
call check(error, err%error(), 'Somehow removed a non-existent directory!: ')
249+
call check(error, err%error(), 'Somehow removed a non-existent directory')
246250
if (allocated(error)) return
247251
end subroutine test_remove_directory_nonexistent
248252

0 commit comments

Comments
 (0)