├── .gitignore ├── .gitattributes ├── .editorconfig ├── post-example-build.cmake ├── tests ├── test_dir_exists_null.c ├── test_file_size_null.c ├── test_file_size_not_found.c ├── test_join_paths_nulls.c ├── test_read_text_file_null.c ├── test_read_binary_file_null.c ├── test_dir_exists_not_found.c ├── test_file_extension_empty.c ├── test_file_extension_null.c ├── test_mkdir_null.c ├── test_mkdirs_null.c ├── test_read_text_file_not_found.c ├── test_file_exists_not_found.c ├── test_path_exists_not_found.c ├── test_read_binary_file_not_found.c ├── test_file_extension_ends_with_dot.c ├── test_mkdirs_parent_null.c ├── test_write_text_file_null_file.c ├── test_write_binary_file_null_file.c ├── test_file_extension_dot_in_directory.c ├── test_file_extension_ends_with_unix_separator.c ├── test_file_extension_ends_with_windows_separator.c ├── test_chmod_recursive_null.c ├── test_copy_file_null_source.c ├── test_join_paths_empty.c ├── test_file_extension_only_file_without_extension.c ├── test_chmod_recursive_not_exists.c ├── test_join_paths_first_is_empty.c ├── test_join_paths_first_is_null.c ├── test_join_paths_second_is_empty.c ├── test_join_paths_second_is_null.c ├── test_file_extension_only_extension.c ├── test_file_extension_only_file_with_extension.c ├── test_dir_exists_dir.c ├── test_join_paths_missing_separator.c ├── test_file_exists_dir.c ├── test_join_paths_redundent_separator.c ├── test_path_exists_dir.c ├── test.h ├── test_dir_exists_file.c ├── test_join_paths_path1_ends_with_separator.c ├── test_file_exists_file.c ├── test_join_paths_path2_starts_with_separator.c ├── test_path_exists_file.c ├── test_join_paths_path1_ends_with_separator_windows_style.c ├── test_join_paths_redundent_separator_windows_style.c ├── test_join_paths_path2_starts_with_separator_windows_style.c ├── test_mkdir_parent_not_exists.c ├── test_write_text_file_null_text.c ├── test_file_extension_file_without_extension.c ├── test_read_binary_file_with_options_null_file.c ├── test_read_text_file_with_options_null_file.c ├── test_read_text_file_with_options_file_not_found.c ├── test_write_binary_file_null_content.c ├── test_copy_file_not_exists.c ├── test_file_size_exists.c ├── test_read_binary_file_with_options_file_not_found.c ├── test_copy_file_null_target.c ├── test_create_empty_file.c ├── test_write_text_file_empty.c ├── test_file_extension_file_with_extension.c ├── test_write_binary_file_empty.c ├── test_file_extension_only_extension_with_parent_directory.c ├── test_mkdir_dir.c ├── test_write_text_file_content.c ├── test_mkdirs_dir.c ├── test_write_binary_file_content.c ├── test_mkdirs_parent_dir.c ├── test_mkdirs_parent_file.c ├── test_move_file_null_source.c ├── test_chmod_recursive_exists.c ├── test_copy_file_exists.c ├── test_write_text_file_overwrite.c ├── test_write_binary_file_overwrite.c ├── test_read_text_file_with_options_max_size_same.c ├── test_read_text_file_with_options_max_size_smaller.c ├── test_read_text_file_with_options_max_size_bigger.c ├── test_move_file_exists.c ├── test_read_binary_file_with_options_max_size_same.c ├── test_read_binary_file_with_options_max_size_smaller.c ├── test_read_text_file_with_options_max_size_smaller_tail.c ├── test_read_binary_file_with_options_max_size_bigger.c ├── test_read_text_file_with_options_max_size_bigger_tail.c ├── test_read_binary_file_with_options_max_size_smaller_tail.c ├── test_create_empty_file_parent_dir_not_exists.c ├── test_read_binary_file_with_options_max_size_bigger_tail.c ├── test_read_binary_file_with_0.c ├── test_append_text_file_content.c ├── test_write_text_file_dir_not_exists.c ├── test_move_file_not_exists.c ├── test_chmod_recursive_dir.c ├── test_write_binary_file_dir_not_exists.c ├── test_write_text_file_long_content.c ├── test_append_binary_file_content.c ├── test_write_binary_file_long_content.c ├── test_move_file_null_target.c ├── test_move_file_exists_force_copy.c ├── test.c ├── test_write_text_file_stress.c └── test_write_binary_file_stress.c ├── .github ├── ISSUE_TEMPLATE │ ├── feature_request.md │ └── bug_report.md ├── CONTRIBUTING.md └── workflows │ ├── ci.yml │ └── codeql-analysis.yml ├── examples └── example.c ├── README.md ├── CHANGELOG.md ├── CMakeLists.txt ├── uncrustify.cfg ├── include └── fsio.h ├── LICENSE └── src └── fsio.c /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | temp/ 3 | **/*.log 4 | /core 5 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text eol=lf 2 | *.ico binary 3 | *.woff binary 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | end_of_line = lf 7 | insert_final_newline = true 8 | trim_trailing_whitespace = true 9 | indent_style = space 10 | indent_size = 2 11 | 12 | -------------------------------------------------------------------------------- /post-example-build.cmake: -------------------------------------------------------------------------------- 1 | 2 | include("./cmake-modules/src/utils.cmake") 3 | 4 | utils_embed_example_source_in_readme( 5 | EXAMPLE_FILE "../examples/example.c" 6 | DOCUMENT_FILE "../README.md" 7 | SOURCE_TYPE "c" 8 | ) 9 | 10 | -------------------------------------------------------------------------------- /tests/test_dir_exists_null.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | 4 | 5 | void test_impl() 6 | { 7 | bool exists = fsio_dir_exists(NULL); 8 | 9 | assert_true(!exists); 10 | } 11 | 12 | 13 | int main() 14 | { 15 | test_run(test_impl); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /tests/test_file_size_null.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | 4 | 5 | void test_impl() 6 | { 7 | long size = fsio_file_size(NULL); 8 | 9 | assert_num_equal(size, -1); 10 | } 11 | 12 | 13 | int main() 14 | { 15 | test_run(test_impl); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /tests/test_file_size_not_found.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | 4 | 5 | void test_impl() 6 | { 7 | long size = fsio_file_size("./bad.txt"); 8 | 9 | assert_num_equal(size, -1); 10 | } 11 | 12 | 13 | int main() 14 | { 15 | test_run(test_impl); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /tests/test_join_paths_nulls.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | 4 | 5 | void test_impl() 6 | { 7 | char *path = fsio_join_paths(NULL, NULL); 8 | 9 | assert_true(path == NULL); 10 | } 11 | 12 | 13 | int main() 14 | { 15 | test_run(test_impl); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /tests/test_read_text_file_null.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | 4 | 5 | void test_impl() 6 | { 7 | char *text = fsio_read_text_file(NULL); 8 | 9 | assert_true(text == NULL); 10 | } 11 | 12 | 13 | int main() 14 | { 15 | test_run(test_impl); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /tests/test_read_binary_file_null.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | 4 | 5 | void test_impl() 6 | { 7 | char *text = fsio_read_binary_file(NULL); 8 | 9 | assert_true(text == NULL); 10 | } 11 | 12 | 13 | int main() 14 | { 15 | test_run(test_impl); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /tests/test_dir_exists_not_found.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | 4 | 5 | void test_impl() 6 | { 7 | bool exists = fsio_dir_exists("./dir_exists_not_found"); 8 | 9 | assert_true(!exists); 10 | } 11 | 12 | 13 | int main() 14 | { 15 | test_run(test_impl); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /tests/test_file_extension_empty.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | 4 | 5 | void test_impl() 6 | { 7 | char *extension = fsio_file_extension(""); 8 | 9 | assert_true(extension == NULL); 10 | } 11 | 12 | 13 | int main() 14 | { 15 | test_run(test_impl); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /tests/test_file_extension_null.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | 4 | 5 | void test_impl() 6 | { 7 | char *extension = fsio_file_extension(NULL); 8 | 9 | assert_true(extension == NULL); 10 | } 11 | 12 | 13 | int main() 14 | { 15 | test_run(test_impl); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /tests/test_mkdir_null.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | bool exists = fsio_mkdir(NULL, 0777); 9 | 10 | assert_true(!exists); 11 | } 12 | 13 | 14 | int main() 15 | { 16 | test_run(test_impl); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /tests/test_mkdirs_null.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | bool exists = fsio_mkdirs(NULL, 0777); 9 | 10 | assert_true(!exists); 11 | } 12 | 13 | 14 | int main() 15 | { 16 | test_run(test_impl); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /tests/test_read_text_file_not_found.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | 4 | 5 | void test_impl() 6 | { 7 | char *text = fsio_read_text_file("./bad.txt"); 8 | 9 | assert_true(text == NULL); 10 | } 11 | 12 | 13 | int main() 14 | { 15 | test_run(test_impl); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /tests/test_file_exists_not_found.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | 4 | 5 | void test_impl() 6 | { 7 | bool exists = fsio_dir_exists("./file_exists_not_found"); 8 | 9 | assert_true(!exists); 10 | } 11 | 12 | 13 | int main() 14 | { 15 | test_run(test_impl); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /tests/test_path_exists_not_found.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | 4 | 5 | void test_impl() 6 | { 7 | bool exists = fsio_dir_exists("./path_exists_not_found"); 8 | 9 | assert_true(!exists); 10 | } 11 | 12 | 13 | int main() 14 | { 15 | test_run(test_impl); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /tests/test_read_binary_file_not_found.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | 4 | 5 | void test_impl() 6 | { 7 | char *text = fsio_read_binary_file("./bad.bin"); 8 | 9 | assert_true(text == NULL); 10 | } 11 | 12 | 13 | int main() 14 | { 15 | test_run(test_impl); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /tests/test_file_extension_ends_with_dot.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | 4 | 5 | void test_impl() 6 | { 7 | char *extension = fsio_file_extension("/test/."); 8 | 9 | assert_true(extension == NULL); 10 | } 11 | 12 | 13 | int main() 14 | { 15 | test_run(test_impl); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /tests/test_mkdirs_parent_null.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | bool exists = fsio_mkdirs_parent(NULL, 0777); 9 | 10 | assert_true(!exists); 11 | } 12 | 13 | 14 | int main() 15 | { 16 | test_run(test_impl); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /tests/test_write_text_file_null_file.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | bool done = fsio_write_text_file(NULL, "test"); 9 | 10 | assert_true(!done); 11 | } 12 | 13 | 14 | int main() 15 | { 16 | test_run(test_impl); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /tests/test_write_binary_file_null_file.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | bool done = fsio_write_binary_file(NULL, "test", 4); 9 | 10 | assert_true(!done); 11 | } 12 | 13 | 14 | int main() 15 | { 16 | test_run(test_impl); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /tests/test_file_extension_dot_in_directory.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | 4 | 5 | void test_impl() 6 | { 7 | char *extension = fsio_file_extension("/test/test.temp/test"); 8 | 9 | assert_true(extension == NULL); 10 | } 11 | 12 | 13 | int main() 14 | { 15 | test_run(test_impl); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /tests/test_file_extension_ends_with_unix_separator.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | 4 | 5 | void test_impl() 6 | { 7 | char *extension = fsio_file_extension("/test/test/"); 8 | 9 | assert_true(extension == NULL); 10 | } 11 | 12 | 13 | int main() 14 | { 15 | test_run(test_impl); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /tests/test_file_extension_ends_with_windows_separator.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | 4 | 5 | void test_impl() 6 | { 7 | char *extension = fsio_file_extension("/test/test\\"); 8 | 9 | assert_true(extension == NULL); 10 | } 11 | 12 | 13 | int main() 14 | { 15 | test_run(test_impl); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /tests/test_chmod_recursive_null.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | bool done = fsio_chmod_recursive(NULL, S_IRWXU | S_IRWXG | S_IRWXO); 9 | 10 | assert_true(!done); 11 | } 12 | 13 | 14 | int main() 15 | { 16 | test_run(test_impl); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /tests/test_copy_file_null_source.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | 6 | 7 | void test_impl() 8 | { 9 | bool done = fsio_copy_file(NULL, "./test.txt"); 10 | 11 | assert_true(!done); 12 | } 13 | 14 | 15 | int main() 16 | { 17 | test_run(test_impl); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /tests/test_join_paths_empty.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *path = fsio_join_paths("", ""); 9 | 10 | assert_string_equal(path, ""); 11 | 12 | free(path); 13 | } 14 | 15 | 16 | int main() 17 | { 18 | test_run(test_impl); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /tests/test_file_extension_only_file_without_extension.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *extension = fsio_file_extension("temp"); 9 | 10 | assert_true(extension == NULL); 11 | } 12 | 13 | 14 | int main() 15 | { 16 | test_run(test_impl); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /tests/test_chmod_recursive_not_exists.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | 4 | 5 | void test_impl() 6 | { 7 | bool done = fsio_chmod_recursive("./test_chmod_recursive_not_exists_dir", S_IRWXU | S_IRWXG | S_IRWXO); 8 | 9 | assert_true(!done); 10 | } 11 | 12 | 13 | int main() 14 | { 15 | test_run(test_impl); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /tests/test_join_paths_first_is_empty.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *path = fsio_join_paths("", "file.txt"); 9 | 10 | assert_string_equal(path, "file.txt"); 11 | 12 | free(path); 13 | } 14 | 15 | 16 | int main() 17 | { 18 | test_run(test_impl); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /tests/test_join_paths_first_is_null.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *path = fsio_join_paths(NULL, "file.txt"); 9 | 10 | assert_string_equal(path, "file.txt"); 11 | 12 | free(path); 13 | } 14 | 15 | 16 | int main() 17 | { 18 | test_run(test_impl); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /tests/test_join_paths_second_is_empty.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *path = fsio_join_paths("file.txt", ""); 9 | 10 | assert_string_equal(path, "file.txt"); 11 | 12 | free(path); 13 | } 14 | 15 | 16 | int main() 17 | { 18 | test_run(test_impl); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /tests/test_join_paths_second_is_null.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *path = fsio_join_paths("file.txt", NULL); 9 | 10 | assert_string_equal(path, "file.txt"); 11 | 12 | free(path); 13 | } 14 | 15 | 16 | int main() 17 | { 18 | test_run(test_impl); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /tests/test_file_extension_only_extension.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *extension = fsio_file_extension(".test"); 9 | 10 | assert_string_equal(extension, ".test"); 11 | free(extension); 12 | } 13 | 14 | 15 | int main() 16 | { 17 | test_run(test_impl); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /tests/test_file_extension_only_file_with_extension.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *extension = fsio_file_extension("temp.test"); 9 | 10 | assert_string_equal(extension, ".test"); 11 | free(extension); 12 | } 13 | 14 | 15 | int main() 16 | { 17 | test_run(test_impl); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /tests/test_dir_exists_dir.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *file = "./dir_exists_dir"; 9 | 10 | fsio_mkdir(file, 0777); 11 | bool exists = fsio_dir_exists(file); 12 | rmdir(file); 13 | 14 | assert_true(exists); 15 | } 16 | 17 | 18 | int main() 19 | { 20 | test_run(test_impl); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /tests/test_join_paths_missing_separator.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *path = fsio_join_paths("/dir1/dir2", "dir3/file.txt"); 9 | 10 | assert_string_equal(path, "/dir1/dir2/dir3/file.txt"); 11 | 12 | free(path); 13 | } 14 | 15 | 16 | int main() 17 | { 18 | test_run(test_impl); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /tests/test_file_exists_dir.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *file = "./file_exists_dir"; 9 | 10 | fsio_mkdir(file, 0777); 11 | bool exists = fsio_file_exists(file); 12 | rmdir(file); 13 | 14 | assert_true(!exists); 15 | } 16 | 17 | 18 | int main() 19 | { 20 | test_run(test_impl); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /tests/test_join_paths_redundent_separator.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *path = fsio_join_paths("/dir1/dir2/", "/dir3/file.txt"); 9 | 10 | assert_string_equal(path, "/dir1/dir2/dir3/file.txt"); 11 | 12 | free(path); 13 | } 14 | 15 | 16 | int main() 17 | { 18 | test_run(test_impl); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /tests/test_path_exists_dir.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *file = "./path_exists_dir"; 9 | 10 | fsio_mkdir(file, 0777); 11 | bool exists = fsio_file_exists(file); 12 | rmdir(file); 13 | 14 | assert_true(!exists); 15 | } 16 | 17 | 18 | int main() 19 | { 20 | test_run(test_impl); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /tests/test.h: -------------------------------------------------------------------------------- 1 | #ifndef __TEST_H__ 2 | #define __TEST_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | void test_run(void (*fn)()); 9 | void test_fail(); 10 | void assert_true(bool); 11 | 12 | void assert_string_equal(char *, char *); 13 | void assert_num_equal(long, long); 14 | 15 | void assert_mode_equal(mode_t, mode_t); 16 | 17 | #endif 18 | 19 | -------------------------------------------------------------------------------- /tests/test_dir_exists_file.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *file = "./dir_exists_file"; 9 | 10 | fsio_create_empty_file(file); 11 | bool exists = fsio_dir_exists(file); 12 | remove(file); 13 | 14 | assert_true(!exists); 15 | } 16 | 17 | 18 | int main() 19 | { 20 | test_run(test_impl); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /tests/test_join_paths_path1_ends_with_separator.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *path = fsio_join_paths("/dir1/dir2/", "dir3/file.txt"); 9 | 10 | assert_string_equal(path, "/dir1/dir2/dir3/file.txt"); 11 | 12 | free(path); 13 | } 14 | 15 | 16 | int main() 17 | { 18 | test_run(test_impl); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /tests/test_file_exists_file.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *file = "./file_exists_file.txt"; 9 | 10 | fsio_create_empty_file(file); 11 | bool exists = fsio_file_exists(file); 12 | remove(file); 13 | 14 | assert_true(exists); 15 | } 16 | 17 | 18 | int main() 19 | { 20 | test_run(test_impl); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /tests/test_join_paths_path2_starts_with_separator.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *path = fsio_join_paths("/dir1/dir2", "/dir3/file.txt"); 9 | 10 | assert_string_equal(path, "/dir1/dir2/dir3/file.txt"); 11 | 12 | free(path); 13 | } 14 | 15 | 16 | int main() 17 | { 18 | test_run(test_impl); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /tests/test_path_exists_file.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *file = "./path_exists_file.txt"; 9 | 10 | fsio_create_empty_file(file); 11 | bool exists = fsio_file_exists(file); 12 | remove(file); 13 | 14 | assert_true(exists); 15 | } 16 | 17 | 18 | int main() 19 | { 20 | test_run(test_impl); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /tests/test_join_paths_path1_ends_with_separator_windows_style.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *path = fsio_join_paths("/dir1/dir2\\", "dir3/file.txt"); 9 | 10 | assert_string_equal(path, "/dir1/dir2\\dir3/file.txt"); 11 | 12 | free(path); 13 | } 14 | 15 | 16 | int main() 17 | { 18 | test_run(test_impl); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /tests/test_join_paths_redundent_separator_windows_style.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *path = fsio_join_paths("/dir1/dir2\\", "\\dir3/file.txt"); 9 | 10 | assert_string_equal(path, "/dir1/dir2\\dir3/file.txt"); 11 | 12 | free(path); 13 | } 14 | 15 | 16 | int main() 17 | { 18 | test_run(test_impl); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /tests/test_join_paths_path2_starts_with_separator_windows_style.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *path = fsio_join_paths("/dir1/dir2", "\\dir3/file.txt"); 9 | 10 | assert_string_equal(path, "/dir1/dir2\\dir3/file.txt"); 11 | 12 | free(path); 13 | } 14 | 15 | 16 | int main() 17 | { 18 | test_run(test_impl); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /tests/test_mkdir_parent_not_exists.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *dir = "./mkdir_dir_parent_not_exists/1/2/3/"; 9 | 10 | bool exists = fsio_mkdir(dir, 0777); 11 | 12 | assert_true(!exists); 13 | exists = fsio_dir_exists(dir); 14 | assert_true(!exists); 15 | } 16 | 17 | 18 | int main() 19 | { 20 | test_run(test_impl); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /tests/test_write_text_file_null_text.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *file = "./write_text_file_null_text.txt"; 9 | bool done = fsio_write_text_file(file, NULL); 10 | char *text = fsio_read_text_file(file); 11 | 12 | assert_true(!done); 13 | assert_true(text == NULL); 14 | } 15 | 16 | 17 | int main() 18 | { 19 | test_run(test_impl); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /tests/test_file_extension_file_without_extension.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *extension = fsio_file_extension("dir.dir/temp"); 9 | 10 | assert_true(extension == NULL); 11 | 12 | extension = fsio_file_extension("dir.dir\\temp"); 13 | 14 | assert_true(extension == NULL); 15 | } 16 | 17 | 18 | int main() 19 | { 20 | test_run(test_impl); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /tests/test_read_binary_file_with_options_null_file.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | 4 | 5 | void test_impl() 6 | { 7 | struct FsIOReadFileOptions options; 8 | 9 | options.max_read_limit = 0; 10 | options.tail = false; 11 | 12 | char *text = fsio_read_binary_file_with_options(NULL, options); 13 | 14 | assert_true(text == NULL); 15 | } 16 | 17 | 18 | int main() 19 | { 20 | test_run(test_impl); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /tests/test_read_text_file_with_options_null_file.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | 4 | 5 | void test_impl() 6 | { 7 | struct FsIOReadFileOptions options; 8 | 9 | options.max_read_limit = 0; 10 | options.tail = false; 11 | 12 | char *text = fsio_read_text_file_with_options(NULL, options); 13 | 14 | assert_true(text == NULL); 15 | } 16 | 17 | 18 | int main() 19 | { 20 | test_run(test_impl); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /tests/test_read_text_file_with_options_file_not_found.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | 4 | 5 | void test_impl() 6 | { 7 | struct FsIOReadFileOptions options; 8 | 9 | options.max_read_limit = 0; 10 | options.tail = false; 11 | 12 | char *text = fsio_read_text_file_with_options("./bad.txt", options); 13 | 14 | assert_true(text == NULL); 15 | } 16 | 17 | 18 | int main() 19 | { 20 | test_run(test_impl); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /tests/test_write_binary_file_null_content.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *file = "./write_binary_file_null_text.bin"; 9 | bool done = fsio_write_binary_file(file, NULL, 0); 10 | char *text = fsio_read_binary_file(file); 11 | 12 | assert_true(!done); 13 | assert_true(text == NULL); 14 | } 15 | 16 | 17 | int main() 18 | { 19 | test_run(test_impl); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /tests/test_copy_file_not_exists.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | 4 | 5 | void test_impl() 6 | { 7 | char *file1 = "./copy_file_not_exists1.txt"; 8 | char *file2 = "./copy_file_not_exists2.txt"; 9 | 10 | bool done = fsio_copy_file(file1, file2); 11 | 12 | assert_true(!done); 13 | 14 | bool exists = fsio_path_exists(file2); 15 | assert_true(!exists); 16 | } 17 | 18 | 19 | int main() 20 | { 21 | test_run(test_impl); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /tests/test_file_size_exists.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *file = "./file_size_exists.txt"; 9 | bool done = fsio_write_text_file(file, "123456789"); 10 | 11 | assert_true(done); 12 | 13 | long size = fsio_file_size(file); 14 | 15 | remove(file); 16 | 17 | assert_num_equal(size, 9); 18 | } 19 | 20 | 21 | int main() 22 | { 23 | test_run(test_impl); 24 | } 25 | 26 | -------------------------------------------------------------------------------- /tests/test_read_binary_file_with_options_file_not_found.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | 4 | 5 | void test_impl() 6 | { 7 | struct FsIOReadFileOptions options; 8 | 9 | options.max_read_limit = 0; 10 | options.tail = false; 11 | 12 | char *text = fsio_read_binary_file_with_options("./bad.bin", options); 13 | 14 | assert_true(text == NULL); 15 | } 16 | 17 | 18 | int main() 19 | { 20 | test_run(test_impl); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature Request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: sagiegurari 7 | 8 | --- 9 | 10 | ### Feature Description 11 | 12 | 13 | ### Describe The Solution You'd Like 14 | 15 | 16 | ### Code Sample 17 | 18 | ```c 19 | // paste code here 20 | ``` 21 | -------------------------------------------------------------------------------- /tests/test_copy_file_null_target.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | 6 | 7 | void test_impl() 8 | { 9 | char *file1 = "./copy_file_null_target.txt"; 10 | bool done = fsio_write_text_file(file1, "some\ncontent"); 11 | 12 | assert_true(done); 13 | 14 | done = fsio_copy_file(file1, NULL); 15 | assert_true(!done); 16 | 17 | remove(file1); 18 | } 19 | 20 | 21 | int main() 22 | { 23 | test_run(test_impl); 24 | } 25 | 26 | -------------------------------------------------------------------------------- /tests/test_create_empty_file.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | 6 | 7 | void test_impl() 8 | { 9 | char *file = "./create_empty_file.txt"; 10 | bool done = fsio_create_empty_file(file); 11 | char *text = fsio_read_text_file(file); 12 | 13 | remove(file); 14 | 15 | assert_true(done); 16 | assert_string_equal(text, ""); 17 | free(text); 18 | } 19 | 20 | 21 | int main() 22 | { 23 | test_run(test_impl); 24 | } 25 | 26 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug Report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: sagiegurari 7 | 8 | --- 9 | 10 | ### Describe The Bug 11 | 12 | 13 | ### To Reproduce 14 | 15 | 16 | ### Error Stack 17 | 18 | ```console 19 | The error stack trace 20 | ``` 21 | 22 | ### Code Sample 23 | 24 | ```c 25 | // paste code here 26 | ``` 27 | -------------------------------------------------------------------------------- /tests/test_write_text_file_empty.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | 6 | 7 | void test_impl() 8 | { 9 | char *file = "./write_text_file_empty.txt"; 10 | bool done = fsio_write_text_file(file, ""); 11 | char *text = fsio_read_text_file(file); 12 | 13 | remove(file); 14 | 15 | assert_true(done); 16 | assert_string_equal(text, ""); 17 | free(text); 18 | } 19 | 20 | 21 | int main() 22 | { 23 | test_run(test_impl); 24 | } 25 | 26 | -------------------------------------------------------------------------------- /tests/test_file_extension_file_with_extension.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *extension = fsio_file_extension("dir/temp.test"); 9 | 10 | assert_string_equal(extension, ".test"); 11 | free(extension); 12 | 13 | extension = fsio_file_extension("dir\\temp.test"); 14 | 15 | assert_string_equal(extension, ".test"); 16 | free(extension); 17 | } 18 | 19 | 20 | int main() 21 | { 22 | test_run(test_impl); 23 | } 24 | 25 | -------------------------------------------------------------------------------- /tests/test_write_binary_file_empty.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | 6 | 7 | void test_impl() 8 | { 9 | char *file = "./write_binary_file_empty.bin"; 10 | bool done = fsio_write_binary_file(file, "", 0); 11 | char *text = fsio_read_binary_file(file); 12 | 13 | remove(file); 14 | 15 | assert_true(done); 16 | assert_string_equal(text, ""); 17 | free(text); 18 | } 19 | 20 | 21 | int main() 22 | { 23 | test_run(test_impl); 24 | } 25 | 26 | -------------------------------------------------------------------------------- /tests/test_file_extension_only_extension_with_parent_directory.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *extension = fsio_file_extension("dir/.test"); 9 | 10 | assert_string_equal(extension, ".test"); 11 | free(extension); 12 | 13 | extension = fsio_file_extension("dir\\.test"); 14 | 15 | assert_string_equal(extension, ".test"); 16 | free(extension); 17 | } 18 | 19 | 20 | int main() 21 | { 22 | test_run(test_impl); 23 | } 24 | 25 | -------------------------------------------------------------------------------- /tests/test_mkdir_dir.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *dir = "./mkdir_dir"; 9 | 10 | bool exists = fsio_mkdir(dir, 0777); 11 | 12 | assert_true(exists); 13 | exists = fsio_dir_exists(dir); 14 | assert_true(exists); 15 | 16 | bool done = fsio_remove(dir); 17 | assert_true(done); 18 | exists = fsio_dir_exists(dir); 19 | assert_true(!exists); 20 | } 21 | 22 | 23 | int main() 24 | { 25 | test_run(test_impl); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /tests/test_write_text_file_content.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | 6 | 7 | void test_impl() 8 | { 9 | char *file = "./write_text_file_content.txt"; 10 | bool done = fsio_write_text_file(file, "some\ncontent"); 11 | char *text = fsio_read_text_file(file); 12 | 13 | remove(file); 14 | 15 | assert_true(done); 16 | assert_string_equal(text, "some\ncontent"); 17 | free(text); 18 | } 19 | 20 | 21 | int main() 22 | { 23 | test_run(test_impl); 24 | } 25 | 26 | -------------------------------------------------------------------------------- /tests/test_mkdirs_dir.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | char *dir = "./mkdirs_dir/1/2/3/4/5/"; 9 | 10 | bool exists = fsio_mkdirs(dir, 0777); 11 | 12 | assert_true(exists); 13 | exists = fsio_dir_exists(dir); 14 | assert_true(exists); 15 | 16 | bool done = fsio_remove(dir); 17 | assert_true(done); 18 | exists = fsio_dir_exists(dir); 19 | assert_true(!exists); 20 | 21 | fsio_remove("./mkdirs_dir"); 22 | } 23 | 24 | 25 | int main() 26 | { 27 | test_run(test_impl); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /tests/test_write_binary_file_content.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | void test_impl() 9 | { 10 | char *file = "./write_binary_file_content.bin"; 11 | char *content = "some\ncontent"; 12 | bool done = fsio_write_binary_file(file, content, strlen(content)); 13 | char *text = fsio_read_binary_file(file); 14 | 15 | remove(file); 16 | 17 | assert_true(done); 18 | assert_string_equal(text, "some\ncontent"); 19 | free(text); 20 | } 21 | 22 | 23 | int main() 24 | { 25 | test_run(test_impl); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /tests/test_mkdirs_parent_dir.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | bool exists = fsio_mkdirs_parent("./mkdirs_parent_dir/1/2/3/", 0777); 9 | 10 | assert_true(exists); 11 | 12 | bool is_directory = fsio_dir_exists("./mkdirs_parent_dir/1/2/3/"); 13 | assert_true(!is_directory); 14 | is_directory = fsio_dir_exists("./mkdirs_parent_dir/1/2/"); 15 | assert_true(is_directory); 16 | 17 | bool deleted = fsio_remove("./mkdirs_parent_dir"); 18 | assert_true(deleted); 19 | } 20 | 21 | 22 | int main() 23 | { 24 | test_run(test_impl); 25 | } 26 | 27 | -------------------------------------------------------------------------------- /tests/test_mkdirs_parent_file.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | 5 | 6 | void test_impl() 7 | { 8 | bool exists = fsio_mkdirs_parent("./mkdirs_parent_file/1/2/3/file.txt", 0777); 9 | 10 | assert_true(exists); 11 | 12 | bool is_directory = fsio_dir_exists("./mkdirs_parent_file/1/2/3/"); 13 | assert_true(is_directory); 14 | 15 | bool is_file = fsio_file_exists("./mkdirs_parent_file/1/2/3/file.txt"); 16 | assert_true(!is_file); 17 | 18 | bool deleted = fsio_remove("./mkdirs_parent_file"); 19 | assert_true(deleted); 20 | } 21 | 22 | 23 | int main() 24 | { 25 | test_run(test_impl); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /tests/test_move_file_null_source.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | 6 | 7 | void test_impl() 8 | { 9 | bool done = fsio_move_file(NULL, "./test.txt"); 10 | 11 | assert_true(!done); 12 | 13 | struct FsIOMoveFileOptions options; 14 | 15 | options.force_copy = false; 16 | options.write_retries = 0; 17 | options.retry_interval_seconds = 0; 18 | 19 | enum FsIOError result = fsio_move_file_with_options(NULL, "./test.txt", options); 20 | 21 | assert_num_equal(result, FSIO_ERROR_INVALID_INPUT); 22 | } 23 | 24 | 25 | int main() 26 | { 27 | test_run(test_impl); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /tests/test_chmod_recursive_exists.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | 4 | 5 | void test_impl() 6 | { 7 | char *file = "./_test_chmod_empty_file.txt"; 8 | bool done = fsio_create_empty_file(file); 9 | 10 | assert_true(done); 11 | 12 | done = fsio_chmod_recursive("./_test_chmod_empty_file.txt", FSIO_MODE_ALL); 13 | 14 | assert_true(done); 15 | 16 | struct stat info; 17 | stat("./_test_chmod_empty_file.txt", &info); 18 | mode_t mode = info.st_mode & 0777; 19 | assert_mode_equal(mode, FSIO_MODE_ALL); 20 | 21 | fsio_remove("./_test_chmod_empty_file.txt"); 22 | } 23 | 24 | 25 | int main() 26 | { 27 | test_run(test_impl); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /tests/test_copy_file_exists.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | 6 | 7 | void test_impl() 8 | { 9 | char *file1 = "./copy_file_exists1.txt"; 10 | char *file2 = "./copy_file_exists2.txt"; 11 | bool done = fsio_write_text_file(file1, "some\ncontent"); 12 | 13 | assert_true(done); 14 | 15 | done = fsio_copy_file(file1, file2); 16 | assert_true(done); 17 | 18 | char *text = fsio_read_text_file(file2); 19 | 20 | remove(file1); 21 | remove(file2); 22 | 23 | assert_string_equal(text, "some\ncontent"); 24 | free(text); 25 | } 26 | 27 | 28 | int main() 29 | { 30 | test_run(test_impl); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /tests/test_write_text_file_overwrite.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | 6 | 7 | void test_impl() 8 | { 9 | char *file = "./write_text_file_overwrite.txt"; 10 | bool done = fsio_write_text_file(file, "1"); 11 | char *text = fsio_read_text_file(file); 12 | 13 | assert_true(done); 14 | assert_string_equal(text, "1"); 15 | free(text); 16 | 17 | done = fsio_write_text_file(file, "2"); 18 | text = fsio_read_text_file(file); 19 | 20 | remove(file); 21 | 22 | assert_true(done); 23 | assert_string_equal(text, "2"); 24 | free(text); 25 | } 26 | 27 | 28 | int main() 29 | { 30 | test_run(test_impl); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /tests/test_write_binary_file_overwrite.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | 6 | 7 | void test_impl() 8 | { 9 | char *file = "./write_binary_file_overwrite.bin"; 10 | bool done = fsio_write_binary_file(file, "1", 1); 11 | char *text = fsio_read_binary_file(file); 12 | 13 | assert_true(done); 14 | assert_string_equal(text, "1"); 15 | free(text); 16 | 17 | done = fsio_write_binary_file(file, "2", 1); 18 | text = fsio_read_binary_file(file); 19 | 20 | remove(file); 21 | 22 | assert_true(done); 23 | assert_string_equal(text, "2"); 24 | free(text); 25 | } 26 | 27 | 28 | int main() 29 | { 30 | test_run(test_impl); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /tests/test_read_text_file_with_options_max_size_same.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | 6 | 7 | void test_impl() 8 | { 9 | char *file = "./read_text_file_with_options_max_size_same.txt"; 10 | bool done = fsio_write_text_file(file, "123456789"); 11 | 12 | assert_true(done); 13 | 14 | struct FsIOReadFileOptions options; 15 | 16 | options.max_read_limit = 9; 17 | options.tail = false; 18 | 19 | char *text = fsio_read_text_file_with_options(file, options); 20 | 21 | remove(file); 22 | 23 | assert_string_equal(text, "123456789"); 24 | free(text); 25 | } 26 | 27 | 28 | int main() 29 | { 30 | test_run(test_impl); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /tests/test_read_text_file_with_options_max_size_smaller.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | 6 | 7 | void test_impl() 8 | { 9 | char *file = "./read_text_file_with_options_max_size_smaller.txt"; 10 | bool done = fsio_write_text_file(file, "123456789"); 11 | 12 | assert_true(done); 13 | 14 | struct FsIOReadFileOptions options; 15 | 16 | options.max_read_limit = 3; 17 | options.tail = false; 18 | 19 | char *text = fsio_read_text_file_with_options(file, options); 20 | 21 | remove(file); 22 | 23 | assert_string_equal(text, "123"); 24 | free(text); 25 | } 26 | 27 | 28 | int main() 29 | { 30 | test_run(test_impl); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /tests/test_read_text_file_with_options_max_size_bigger.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | 6 | 7 | void test_impl() 8 | { 9 | char *file = "./read_text_file_with_options_max_size_bigger.txt"; 10 | bool done = fsio_write_text_file(file, "123456789"); 11 | 12 | assert_true(done); 13 | 14 | struct FsIOReadFileOptions options; 15 | 16 | options.max_read_limit = 100; 17 | options.tail = false; 18 | 19 | char *text = fsio_read_text_file_with_options(file, options); 20 | 21 | remove(file); 22 | 23 | assert_string_equal(text, "123456789"); 24 | free(text); 25 | } 26 | 27 | 28 | int main() 29 | { 30 | test_run(test_impl); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /tests/test_move_file_exists.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | 6 | 7 | void test_impl() 8 | { 9 | char *file1 = "./move_file_exists1.txt"; 10 | char *file2 = "./move_file_exists2.txt"; 11 | bool done = fsio_write_text_file(file1, "some\ncontent"); 12 | 13 | assert_true(done); 14 | 15 | done = fsio_move_file(file1, file2); 16 | assert_true(done); 17 | 18 | char *text = fsio_read_text_file(file2); 19 | 20 | remove(file2); 21 | 22 | bool exists = fsio_path_exists(file1); 23 | assert_true(!exists); 24 | assert_string_equal(text, "some\ncontent"); 25 | free(text); 26 | } 27 | 28 | 29 | int main() 30 | { 31 | test_run(test_impl); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /tests/test_read_binary_file_with_options_max_size_same.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | 6 | 7 | void test_impl() 8 | { 9 | char *file = "./read_binary_file_with_options_max_size_same.bin"; 10 | bool done = fsio_write_binary_file(file, "123456789", 9); 11 | 12 | assert_true(done); 13 | 14 | struct FsIOReadFileOptions options; 15 | 16 | options.max_read_limit = 9; 17 | options.tail = false; 18 | 19 | char *text = fsio_read_binary_file_with_options(file, options); 20 | 21 | remove(file); 22 | 23 | assert_string_equal(text, "123456789"); 24 | free(text); 25 | } 26 | 27 | 28 | int main() 29 | { 30 | test_run(test_impl); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /tests/test_read_binary_file_with_options_max_size_smaller.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | 6 | 7 | void test_impl() 8 | { 9 | char *file = "./read_binary_file_with_options_max_size_smaller.bin"; 10 | bool done = fsio_write_binary_file(file, "123456789", 9); 11 | 12 | assert_true(done); 13 | 14 | struct FsIOReadFileOptions options; 15 | 16 | options.max_read_limit = 3; 17 | options.tail = false; 18 | 19 | char *text = fsio_read_binary_file_with_options(file, options); 20 | 21 | remove(file); 22 | 23 | assert_string_equal(text, "123"); 24 | free(text); 25 | } 26 | 27 | 28 | int main() 29 | { 30 | test_run(test_impl); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /tests/test_read_text_file_with_options_max_size_smaller_tail.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | 6 | 7 | void test_impl() 8 | { 9 | char *file = "./read_text_file_with_options_max_size_smaller_tail.txt"; 10 | bool done = fsio_write_text_file(file, "123456789"); 11 | 12 | assert_true(done); 13 | 14 | struct FsIOReadFileOptions options; 15 | 16 | options.max_read_limit = 3; 17 | options.tail = true; 18 | 19 | char *text = fsio_read_text_file_with_options(file, options); 20 | 21 | remove(file); 22 | 23 | assert_string_equal(text, "789"); 24 | free(text); 25 | } 26 | 27 | 28 | int main() 29 | { 30 | test_run(test_impl); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /tests/test_read_binary_file_with_options_max_size_bigger.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | 6 | 7 | void test_impl() 8 | { 9 | char *file = "./read_binary_file_with_options_max_size_bigger.bin"; 10 | bool done = fsio_write_binary_file(file, "123456789", 9); 11 | 12 | assert_true(done); 13 | 14 | struct FsIOReadFileOptions options; 15 | 16 | options.max_read_limit = 100; 17 | options.tail = false; 18 | 19 | char *text = fsio_read_binary_file_with_options(file, options); 20 | 21 | remove(file); 22 | 23 | assert_string_equal(text, "123456789"); 24 | free(text); 25 | } 26 | 27 | 28 | int main() 29 | { 30 | test_run(test_impl); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /tests/test_read_text_file_with_options_max_size_bigger_tail.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | 6 | 7 | void test_impl() 8 | { 9 | char *file = "./read_text_file_with_options_max_size_bigger_tail.txt"; 10 | bool done = fsio_write_text_file(file, "123456789"); 11 | 12 | assert_true(done); 13 | 14 | struct FsIOReadFileOptions options; 15 | 16 | options.max_read_limit = 100; 17 | options.tail = true; 18 | 19 | char *text = fsio_read_text_file_with_options(file, options); 20 | 21 | remove(file); 22 | 23 | assert_string_equal(text, "123456789"); 24 | free(text); 25 | } 26 | 27 | 28 | int main() 29 | { 30 | test_run(test_impl); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /tests/test_read_binary_file_with_options_max_size_smaller_tail.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | 6 | 7 | void test_impl() 8 | { 9 | char *file = "./read_binary_file_with_options_max_size_smaller_tail.bin"; 10 | bool done = fsio_write_binary_file(file, "123456789", 9); 11 | 12 | assert_true(done); 13 | 14 | struct FsIOReadFileOptions options; 15 | 16 | options.max_read_limit = 3; 17 | options.tail = true; 18 | 19 | char *text = fsio_read_binary_file_with_options(file, options); 20 | 21 | remove(file); 22 | 23 | assert_string_equal(text, "789"); 24 | free(text); 25 | } 26 | 27 | 28 | int main() 29 | { 30 | test_run(test_impl); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /tests/test_create_empty_file_parent_dir_not_exists.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | 6 | 7 | void test_impl() 8 | { 9 | char *file = "./create_empty_file_parent_dir_not_exists/dir1/dir2/file.txt"; 10 | bool done = fsio_create_empty_file(file); 11 | char *text = fsio_read_text_file(file); 12 | 13 | assert_true(done); 14 | assert_string_equal(text, ""); 15 | free(text); 16 | 17 | done = fsio_remove("./create_empty_file_parent_dir_not_exists"); 18 | assert_true(done); 19 | 20 | bool exists = fsio_dir_exists("./create_empty_file_parent_dir_not_exists"); 21 | assert_true(!exists); 22 | } 23 | 24 | 25 | int main() 26 | { 27 | test_run(test_impl); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /tests/test_read_binary_file_with_options_max_size_bigger_tail.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | 6 | 7 | void test_impl() 8 | { 9 | char *file = "./read_binary_file_with_options_max_size_bigger_tail.bin"; 10 | bool done = fsio_write_binary_file(file, "123456789", 9); 11 | 12 | assert_true(done); 13 | 14 | struct FsIOReadFileOptions options; 15 | 16 | options.max_read_limit = 100; 17 | options.tail = true; 18 | 19 | char *text = fsio_read_binary_file_with_options(file, options); 20 | 21 | remove(file); 22 | 23 | assert_string_equal(text, "123456789"); 24 | free(text); 25 | } 26 | 27 | 28 | int main() 29 | { 30 | test_run(test_impl); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /tests/test_read_binary_file_with_0.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | 6 | 7 | void test_impl() 8 | { 9 | char content[500]; 10 | 11 | for (int index = 0; index < 500; index++) 12 | { 13 | content[index] = index - 100; 14 | } 15 | 16 | char *filename = "./read_write_binary_with_0.bin"; 17 | fsio_write_binary_file(filename, content, 500); 18 | char *text = fsio_read_binary_file(filename); 19 | 20 | for (size_t index = 0; index < 500; index++) 21 | { 22 | assert_true(content[index] == text[index]); 23 | } 24 | assert_true(strlen(text) < 500); 25 | 26 | free(text); 27 | 28 | fsio_remove(filename); 29 | } 30 | 31 | 32 | int main() 33 | { 34 | test_run(test_impl); 35 | } 36 | 37 | -------------------------------------------------------------------------------- /tests/test_append_text_file_content.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | 6 | 7 | void test_impl() 8 | { 9 | char *file = "./_test_append/write_append_file_content.txt"; 10 | bool done = fsio_write_text_file(file, "some\ncontent\n"); 11 | char *text = fsio_read_text_file(file); 12 | 13 | assert_true(done); 14 | assert_string_equal(text, "some\ncontent\n"); 15 | free(text); 16 | 17 | done = fsio_append_text_file(file, "more content"); 18 | text = fsio_read_text_file(file); 19 | 20 | remove("./_test_append"); 21 | 22 | assert_true(done); 23 | assert_string_equal(text, "some\ncontent\nmore content"); 24 | free(text); 25 | } 26 | 27 | 28 | int main() 29 | { 30 | test_run(test_impl); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /tests/test_write_text_file_dir_not_exists.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | void test_impl() 9 | { 10 | char *file = "./write_text_file_dir_not_exists/dir1/dir2/dir3/write_text_file_dir_not_exists.txt"; 11 | bool done = fsio_write_text_file(file, "some\ncontent"); 12 | char *text = fsio_read_text_file(file); 13 | 14 | assert_true(done); 15 | assert_string_equal(text, "some\ncontent"); 16 | free(text); 17 | 18 | done = fsio_remove("./write_text_file_dir_not_exists"); 19 | assert_true(done); 20 | 21 | bool exists = fsio_dir_exists("./write_text_file_dir_not_exists"); 22 | assert_true(!exists); 23 | } 24 | 25 | 26 | int main() 27 | { 28 | test_run(test_impl); 29 | } 30 | 31 | -------------------------------------------------------------------------------- /tests/test_move_file_not_exists.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | 4 | 5 | void test_impl() 6 | { 7 | char *file1 = "./move_file_not_exists1.txt"; 8 | char *file2 = "./move_file_not_exists2.txt"; 9 | 10 | bool done = fsio_move_file(file1, file2); 11 | 12 | assert_true(!done); 13 | 14 | bool exists = fsio_path_exists(file2); 15 | assert_true(!exists); 16 | 17 | struct FsIOMoveFileOptions options; 18 | options.force_copy = false; 19 | options.write_retries = 0; 20 | options.retry_interval_seconds = 0; 21 | 22 | enum FsIOError result = fsio_move_file_with_options(file1, file2, options); 23 | 24 | assert_num_equal(result, FSIO_ERROR_PATH_NOT_FOUND); 25 | } 26 | 27 | 28 | int main() 29 | { 30 | test_run(test_impl); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /tests/test_chmod_recursive_dir.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | 4 | 5 | void test_impl() 6 | { 7 | char *dir = "./_test_chmod_recursive_dir/1/2/3/4/5/"; 8 | 9 | bool exists = fsio_mkdirs(dir, S_IRWXU); 10 | 11 | assert_true(exists); 12 | exists = fsio_dir_exists(dir); 13 | assert_true(exists); 14 | 15 | bool done = fsio_chmod_recursive("./_test_chmod_recursive_dir", FSIO_MODE_ALL); 16 | assert_true(done); 17 | 18 | struct stat info; 19 | stat("./_test_chmod_recursive_dir", &info); 20 | mode_t mode = info.st_mode & 0777; 21 | assert_mode_equal(mode, FSIO_MODE_ALL); 22 | stat(dir, &info); 23 | mode = info.st_mode & 0777; 24 | assert_mode_equal(mode, FSIO_MODE_ALL); 25 | 26 | fsio_remove("./_test_chmod_recursive_dir"); 27 | } 28 | 29 | 30 | int main() 31 | { 32 | test_run(test_impl); 33 | } 34 | 35 | -------------------------------------------------------------------------------- /tests/test_write_binary_file_dir_not_exists.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | void test_impl() 10 | { 11 | char *file = "./write_binary_file_dir_not_exists/dir1/dir2/dir3/write_binary_file_dir_not_exists.bin"; 12 | char *content = "some\ncontent"; 13 | bool done = fsio_write_binary_file(file, content, strlen(content)); 14 | char *text = fsio_read_binary_file(file); 15 | 16 | assert_true(done); 17 | assert_string_equal(text, "some\ncontent"); 18 | free(text); 19 | 20 | done = fsio_remove("./write_binary_file_dir_not_exists"); 21 | assert_true(done); 22 | 23 | bool exists = fsio_dir_exists("./write_binary_file_dir_not_exists"); 24 | assert_true(!exists); 25 | } 26 | 27 | 28 | int main() 29 | { 30 | test_run(test_impl); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /tests/test_write_text_file_long_content.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "stringbuffer.h" 3 | #include "test.h" 4 | #include 5 | #include 6 | 7 | 8 | void test_impl() 9 | { 10 | struct StringBuffer *buffer = stringbuffer_new(); 11 | 12 | for (unsigned int index = 0; index < 1000; index++) 13 | { 14 | stringbuffer_append_string(buffer, "another line of content.\n"); 15 | } 16 | char *content = stringbuffer_to_string(buffer); 17 | stringbuffer_release(buffer); 18 | 19 | char *file = "./write_text_file_long_content.txt"; 20 | bool done = fsio_write_text_file(file, content); 21 | char *text = fsio_read_text_file(file); 22 | 23 | remove(file); 24 | 25 | assert_true(done); 26 | assert_string_equal(text, content); 27 | 28 | free(text); 29 | free(content); 30 | } 31 | 32 | 33 | int main() 34 | { 35 | test_run(test_impl); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /tests/test_append_binary_file_content.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | void test_impl() 9 | { 10 | char *file = "./_test_append/write_append_file_content.bin"; 11 | char *content = "some\ncontent\n"; 12 | bool done = fsio_write_binary_file(file, content, strlen(content)); 13 | char *text = fsio_read_binary_file(file); 14 | 15 | assert_true(done); 16 | assert_string_equal(text, "some\ncontent\n"); 17 | free(text); 18 | 19 | content = "more content"; 20 | done = fsio_append_binary_file(file, content, strlen(content)); 21 | text = fsio_read_binary_file(file); 22 | 23 | remove("./_test_append"); 24 | 25 | assert_true(done); 26 | assert_string_equal(text, "some\ncontent\nmore content"); 27 | free(text); 28 | } 29 | 30 | 31 | int main() 32 | { 33 | test_run(test_impl); 34 | } 35 | 36 | -------------------------------------------------------------------------------- /tests/test_write_binary_file_long_content.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "stringbuffer.h" 3 | #include "test.h" 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | void test_impl() 10 | { 11 | struct StringBuffer *buffer = stringbuffer_new(); 12 | 13 | for (unsigned int index = 0; index < 1000; index++) 14 | { 15 | stringbuffer_append_string(buffer, "another line of content.\n"); 16 | } 17 | char *content = stringbuffer_to_string(buffer); 18 | stringbuffer_release(buffer); 19 | 20 | char *file = "./write_binary_file_long_content.bin"; 21 | bool done = fsio_write_binary_file(file, content, strlen(content)); 22 | char *text = fsio_read_binary_file(file); 23 | 24 | remove(file); 25 | 26 | assert_true(done); 27 | assert_string_equal(text, content); 28 | 29 | free(text); 30 | free(content); 31 | } 32 | 33 | 34 | int main() 35 | { 36 | test_run(test_impl); 37 | } 38 | 39 | -------------------------------------------------------------------------------- /tests/test_move_file_null_target.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | 6 | 7 | void test_impl() 8 | { 9 | char *file1 = "./move_file_exists_null_target.txt"; 10 | bool done = fsio_write_text_file(file1, "some\ncontent"); 11 | 12 | assert_true(done); 13 | 14 | done = fsio_move_file(file1, NULL); 15 | assert_true(!done); 16 | 17 | char *text = fsio_read_text_file(file1); 18 | assert_string_equal(text, "some\ncontent"); 19 | free(text); 20 | 21 | struct FsIOMoveFileOptions options; 22 | options.force_copy = false; 23 | options.write_retries = 0; 24 | options.retry_interval_seconds = 0; 25 | 26 | enum FsIOError result = fsio_move_file_with_options(file1, NULL, options); 27 | 28 | assert_num_equal(result, FSIO_ERROR_INVALID_INPUT); 29 | 30 | remove(file1); 31 | } 32 | 33 | 34 | int main() 35 | { 36 | test_run(test_impl); 37 | } 38 | 39 | -------------------------------------------------------------------------------- /examples/example.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include 3 | #include 4 | 5 | 6 | int main() 7 | { 8 | // simple examples of writing/appending/reading text files 9 | char *file = "./somedir/myfile.txt"; 10 | bool done = fsio_write_text_file(file, "some text\n"); 11 | 12 | printf("Text file written: %d\n", done); 13 | done = fsio_append_text_file(file, "more text\n"); 14 | printf("Text file appended: %d\n", done); 15 | char *text = fsio_read_text_file(file); 16 | printf("Read text:\n%s\n", text); 17 | 18 | // creating a full directory path 19 | done = fsio_mkdirs("./somedir/dir1/dir2/dir3", FSIO_MODE_ALL); 20 | printf("Created directories: %d\n", done); 21 | 22 | // chmod to all permissions recursively 23 | done = fsio_chmod_recursive("./somedir/", FSIO_MODE_ALL); 24 | printf("Chmod done: %d\n", done); 25 | 26 | // recursive delete or files and directories 27 | done = fsio_remove("./somedir"); 28 | printf("Deleted somedir and all content.\n"); 29 | } 30 | -------------------------------------------------------------------------------- /tests/test_move_file_exists_force_copy.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "test.h" 3 | #include 4 | #include 5 | 6 | 7 | void test_impl() 8 | { 9 | char *file1 = "./move_file_exists_force_copy1.txt"; 10 | char *file2 = "./move_file_exists_force_copy2.txt"; 11 | bool done = fsio_write_text_file(file1, "some\ncontent"); 12 | 13 | assert_true(done); 14 | 15 | struct FsIOMoveFileOptions options; 16 | options.force_copy = true; 17 | options.write_retries = 0; 18 | options.retry_interval_seconds = 0; 19 | 20 | enum FsIOError result = fsio_move_file_with_options(file1, file2, options); 21 | assert_num_equal(result, FSIO_ERROR_NONE); 22 | 23 | char *text = fsio_read_text_file(file2); 24 | 25 | remove(file2); 26 | 27 | bool exists = fsio_path_exists(file1); 28 | assert_true(!exists); 29 | assert_string_equal(text, "some\ncontent"); 30 | free(text); 31 | } 32 | 33 | 34 | int main() 35 | { 36 | test_run(test_impl); 37 | } 38 | 39 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contribution Guidelines 2 | 3 | ## Issues 4 | 5 | Found a bug? Got a question? Want some enhancement?
6 | First place to go is the repository issues section, and I'll try to help as much as possible. 7 | 8 | ## Pull Requests 9 | 10 | Fixed a bug or just want to provided additional functionality?
11 | Simply fork this repository, implement your changes and create a pull request.
12 | Few guidelines regarding pull requests: 13 | 14 | * This repository is integrated with github actions for continuous integration.
15 | 16 | Your pull request build must pass (the build will run automatically).
17 | You can run the following command locally to ensure the build will pass: 18 | 19 | ```sh 20 | mkdir ./target 21 | cd ./target 22 | cmake .. 23 | make 24 | ctest -C Release --output-on-failure 25 | ``` 26 | 27 | * There are many automatic unit tests as part of the library which provide full coverage of the functionality.
Any fix/enhancement must come with a set of tests to ensure it's working well. 28 | -------------------------------------------------------------------------------- /tests/test.c: -------------------------------------------------------------------------------- 1 | #include "test.h" 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | void test_run(void (*fn)(void)) 8 | { 9 | printf("Test ... "); 10 | fn(); 11 | printf("Done\n"); 12 | } 13 | 14 | 15 | void test_fail() 16 | { 17 | printf(" Error\n"); 18 | exit(1); 19 | } 20 | 21 | 22 | void assert_true(bool value) 23 | { 24 | if (!value) 25 | { 26 | test_fail(); 27 | } 28 | } 29 | 30 | 31 | void assert_string_equal(char *value1, char *value2) 32 | { 33 | if (strcmp(value1, value2) != 0) 34 | { 35 | printf("Assert Failed, value: %s not equals to value: %s", value1, value2); 36 | test_fail(); 37 | } 38 | } 39 | 40 | 41 | void assert_num_equal(long value1, long value2) 42 | { 43 | if (value1 != value2) 44 | { 45 | printf("Assert Failed, value: %ld not equals to value: %ld", value1, value2); 46 | test_fail(); 47 | } 48 | } 49 | 50 | 51 | void assert_mode_equal(mode_t value1, mode_t value2) 52 | { 53 | if (value1 != value2) 54 | { 55 | printf("Assert Failed, value: %3o not equals to value: %3o", value1 & 0777, value2 & 0777); 56 | test_fail(); 57 | } 58 | } 59 | 60 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: [push, pull_request] 3 | env: 4 | BUILD_TYPE: Release 5 | jobs: 6 | ci: 7 | name: CI 8 | runs-on: ${{ matrix.os }} 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | os: [ubuntu-latest, macOS-latest] 13 | steps: 14 | - name: Checkout 15 | uses: actions/checkout@v2 16 | - name: Setup Env 17 | run: cmake -E make_directory target 18 | - name: Configure 19 | shell: bash 20 | working-directory: target 21 | run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE 22 | - name: Build 23 | shell: bash 24 | working-directory: target 25 | run: cmake --build . --config $BUILD_TYPE 26 | - name: Test 27 | shell: bash 28 | working-directory: target 29 | run: ctest -C $BUILD_TYPE --output-on-failure 30 | - name: Memory Leak Test 31 | if: matrix.os == 'ubuntu-latest' 32 | shell: bash 33 | working-directory: target 34 | run: | 35 | sudo apt update 36 | sudo apt install -y valgrind --fix-missing 37 | for testfile in ./bin/test_*; do echo "Testing ${testfile}" && valgrind --leak-check=full --show-leak-kinds=definite,indirect,possible --error-exitcode=1 "${testfile}"; done 38 | 39 | -------------------------------------------------------------------------------- /tests/test_write_text_file_stress.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "stringbuffer.h" 3 | #include "test.h" 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | void test_impl() 10 | { 11 | struct StringBuffer *buffer = stringbuffer_new(); 12 | 13 | for (unsigned int index = 0; index < 1000; index++) 14 | { 15 | stringbuffer_append_string(buffer, "another line of content.\n"); 16 | } 17 | char *content = stringbuffer_to_string(buffer); 18 | stringbuffer_append_string(buffer, content); 19 | char *double_content = stringbuffer_to_string(buffer); 20 | stringbuffer_release(buffer); 21 | 22 | char *file = "./stress1/stress2/write_text_file_stress.txt"; 23 | 24 | for (unsigned int index = 0; index < 50; index++) 25 | { 26 | bool done = fsio_write_text_file(file, content); 27 | char *text = fsio_read_text_file(file); 28 | 29 | assert_true(done); 30 | assert_string_equal(text, content); 31 | free(text); 32 | 33 | done = fsio_append_text_file(file, content); 34 | text = fsio_read_text_file(file); 35 | 36 | remove(file); 37 | rmdir("./stress1/stress2"); 38 | rmdir("./stress1"); 39 | 40 | assert_true(done); 41 | assert_string_equal(text, double_content); 42 | free(text); 43 | } 44 | 45 | free(double_content); 46 | free(content); 47 | } 48 | 49 | 50 | int main() 51 | { 52 | test_run(test_impl); 53 | } 54 | 55 | -------------------------------------------------------------------------------- /tests/test_write_binary_file_stress.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "stringbuffer.h" 3 | #include "test.h" 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | 10 | void test_impl() 11 | { 12 | struct StringBuffer *buffer = stringbuffer_new(); 13 | 14 | for (unsigned int index = 0; index < 1000; index++) 15 | { 16 | stringbuffer_append_string(buffer, "another line of content.\n"); 17 | } 18 | char *content = stringbuffer_to_string(buffer); 19 | stringbuffer_append_string(buffer, content); 20 | char *double_content = stringbuffer_to_string(buffer); 21 | stringbuffer_release(buffer); 22 | 23 | char *file = "./stress1/stress2/write_binary_file_stress.bin"; 24 | 25 | for (unsigned int index = 0; index < 50; index++) 26 | { 27 | bool done = fsio_write_binary_file(file, content, strlen(content)); 28 | char *text = fsio_read_binary_file(file); 29 | 30 | assert_true(done); 31 | assert_string_equal(text, content); 32 | free(text); 33 | 34 | done = fsio_append_binary_file(file, content, strlen(content)); 35 | text = fsio_read_binary_file(file); 36 | 37 | remove(file); 38 | rmdir("./stress1/stress2"); 39 | rmdir("./stress1"); 40 | 41 | assert_true(done); 42 | assert_string_equal(text, double_content); 43 | free(text); 44 | } 45 | 46 | free(double_content); 47 | free(content); 48 | } 49 | 50 | 51 | int main() 52 | { 53 | test_run(test_impl); 54 | } 55 | 56 | -------------------------------------------------------------------------------- /.github/workflows/codeql-analysis.yml: -------------------------------------------------------------------------------- 1 | name: "CodeQL" 2 | 3 | on: 4 | push: 5 | pull_request: 6 | schedule: 7 | - cron: '0 23 * * 5' 8 | 9 | jobs: 10 | analyse: 11 | name: Analyse 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - name: Checkout repository 16 | uses: actions/checkout@v2 17 | with: 18 | # We must fetch at least the immediate parents so that if this is 19 | # a pull request then we can checkout the head. 20 | fetch-depth: 2 21 | 22 | # If this run was triggered by a pull request event, then checkout 23 | # the head of the pull request instead of the merge commit. 24 | - run: git checkout HEAD^2 25 | if: ${{ github.event_name == 'pull_request' }} 26 | 27 | # Initializes the CodeQL tools for scanning. 28 | - name: Initialize CodeQL 29 | uses: github/codeql-action/init@v1 30 | with: 31 | languages: cpp 32 | # Override language selection by uncommenting this and choosing your languages 33 | # with: 34 | # languages: go, javascript, csharp, python, cpp, java 35 | 36 | # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). 37 | # If this step fails, then you should remove it and run the build manually (see below) 38 | - name: Autobuild 39 | uses: github/codeql-action/autobuild@v1 40 | 41 | # ℹ️ Command-line programs to run using the OS shell. 42 | # 📚 https://git.io/JvXDl 43 | 44 | # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines 45 | # and modify them (or add more) to build your code if your project 46 | # uses a compiled language 47 | 48 | #- run: | 49 | # make bootstrap 50 | # make release 51 | 52 | - name: Perform CodeQL Analysis 53 | uses: github/codeql-action/analyze@v1 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fsio 2 | 3 | [![CI](https://github.com/sagiegurari/c_fsio/workflows/CI/badge.svg?branch=master)](https://github.com/sagiegurari/c_fsio/actions) 4 | [![Release](https://img.shields.io/github/v/release/sagiegurari/c_fsio)](https://github.com/sagiegurari/c_fsio/releases) 5 | [![license](https://img.shields.io/github/license/sagiegurari/c_fsio)](https://github.com/sagiegurari/c_fsio/blob/master/LICENSE) 6 | 7 | > File System functions and utilities. 8 | 9 | * [Overview](#overview) 10 | * [Usage](#usage) 11 | * [Contributing](.github/CONTRIBUTING.md) 12 | * [Release History](CHANGELOG.md) 13 | * [License](#license) 14 | 15 | 16 | ## Overview 17 | This library provides a set of utility functions to work with the file system.
18 | This includes writing/append text files with automatic directory creation, creating full directory path and more. 19 | 20 | 21 | ## Usage 22 | 23 | 24 | ```c 25 | #include "fsio.h" 26 | #include 27 | #include 28 | 29 | 30 | int main() 31 | { 32 | // simple examples of writing/appending/reading text files 33 | char *file = "./somedir/myfile.txt"; 34 | bool done = fsio_write_text_file(file, "some text\n"); 35 | 36 | printf("Text file written: %d\n", done); 37 | done = fsio_append_text_file(file, "more text\n"); 38 | printf("Text file appended: %d\n", done); 39 | char *text = fsio_read_text_file(file); 40 | printf("Read text:\n%s\n", text); 41 | 42 | // creating a full directory path 43 | done = fsio_mkdirs("./somedir/dir1/dir2/dir3", FSIO_MODE_ALL); 44 | printf("Created directories: %d\n", done); 45 | 46 | // chmod to all permissions recursively 47 | done = fsio_chmod_recursive("./somedir/", FSIO_MODE_ALL); 48 | printf("Chmod done: %d\n", done); 49 | 50 | // recursive delete or files and directories 51 | done = fsio_remove("./somedir"); 52 | printf("Deleted somedir and all content.\n"); 53 | } 54 | ``` 55 | 56 | 57 | ## Contributing 58 | See [contributing guide](.github/CONTRIBUTING.md) 59 | 60 | 61 | ## Release History 62 | 63 | See [Changelog](CHANGELOG.md) 64 | 65 | 66 | ## License 67 | Developed by Sagie Gur-Ari and licensed under the Apache 2 open source license. 68 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## CHANGELOG 2 | 3 | ### v0.5.4 4 | 5 | * Maintenance: Update string_buffer dependency 6 | * Updated header include guard macro name 7 | 8 | ### v0.5.3 (2022-01-30) 9 | 10 | * Fixed minor allocation issue in fsio_join_paths 11 | * Added static to internal functions 12 | * Adding lint checks as part of build 13 | 14 | ### v0.5.2 (2021-10-23) 15 | 16 | * Enhancement: improve copy file performance. 17 | 18 | ### v0.5.1 (2021-09-26) 19 | 20 | * Enhancement: New fsio_file_extension function. 21 | 22 | ### v0.5.0 (2021-09-24) 23 | 24 | * Enhancement: Updated binary file operations. 25 | 26 | ### v0.4.0 (2021-09-24) 27 | 28 | * Enhancement: New binary file operations. 29 | 30 | ### v0.3.0 (2021-08-09) 31 | 32 | * Enhancement: fsio_move_file_with_options will return the error number enum instead of struct. 33 | 34 | ### v0.2.0 (2021-08-09) 35 | 36 | * Enhancement: fsio_move_file_with_options will return the error number struct instead of true/false. 37 | 38 | ### v0.1.7 (2021-07-31) 39 | 40 | * New fsio_join_paths function. 41 | * New fsio_file_size function. 42 | * New fsio_read_text_file_with_options function. 43 | * Improved input validation for more stable API. 44 | * Added cargo-make makefile for simpler sharing of optional development build instructions. 45 | 46 | ### v0.1.6 (2020-12-27) 47 | 48 | * New fsio_move_file function. 49 | * New fsio_move_file_with_options function. 50 | * New fsio_copy_file_with_options function. 51 | 52 | ### v0.1.5 (2020-12-16) 53 | 54 | * New fsio_chmod_recursive function. 55 | * New fsio_recursive_operation function which enables to recursively invoke an operation on all files/directories. 56 | * New FSIO_MODE_ALL global constant which defines an all permissions (777) mode_t value. 57 | 58 | ### v0.1.4 (2020-12-09) 59 | 60 | * Fixed root path mkdirs invocation. 61 | * Fixed existing directory error detection. 62 | 63 | ### v0.1.3 (2020-11-27) 64 | 65 | * New fsio_remove function. 66 | * New fsio_copy_file functions. 67 | * New fsio_path_exists function. 68 | * New fsio_file_exists function. 69 | * New fsio_create_empty_file function. 70 | * New fsio_mkdirs_parent function. 71 | * Free all memory in tests 72 | * Test memory leaks in CI 73 | 74 | ### v0.1.2 (2020-11-12) 75 | 76 | * Flush file before closing after write operation. 77 | 78 | ### v0.1.1 (2020-11-07) 79 | 80 | * Internal functions are now namespaced with _fsio prefix. 81 | * c_string_buffer upgraded to 0.1.4 82 | * Improved build setup (no need to list test names). 83 | 84 | ### v0.1.0 (2020-09-25) 85 | 86 | * Initial release 87 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | cmake_minimum_required(VERSION 3.7) 3 | 4 | project(fsio C) 5 | 6 | # include shared utilities 7 | if(NOT EXISTS "target/cmake-modules/src/utils.cmake") 8 | execute_process(COMMAND git clone https://github.com/sagiegurari/cmake-modules.git) 9 | endif() 10 | include("target/cmake-modules/src/utils.cmake") 11 | 12 | set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 13 | 14 | set(CMAKE_BUILD_TYPE Release) 15 | if(NOT WIN32) 16 | set(X_CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror -Wall -Wextra -Wcast-align -Wunused -Wshadow -Wpedantic") 17 | endif() 18 | 19 | set(X_CMAKE_PROJECT_ROOT_DIR ${CMAKE_BINARY_DIR}/..) 20 | 21 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) 22 | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) 23 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) 24 | 25 | macro(add_external_lib) 26 | utils_add_external_github_lib( 27 | REPO_USERNAME sagiegurari 28 | REPO_NAME c_${ARGV0} 29 | TAG_NAME ${ARGV1} 30 | LIBRARY_NAME ${ARGV0} 31 | LIBRARY_PARENT_DIRECTORY target 32 | ) 33 | endmacro(add_external_lib) 34 | add_external_lib("string_buffer") 35 | 36 | include_directories(include "${STRING_BUFFER_INCLUDE}") 37 | 38 | # define all sources 39 | file(GLOB SOURCES "src/*.c") 40 | file(GLOB HEADER_SOURCES "include/*.h") 41 | file(GLOB TEST_SOURCES "tests/*") 42 | file(GLOB COMMON_TEST_SOURCES "tests/test.*") 43 | file(GLOB EXAMPLE_SOURCES "examples/*.c") 44 | 45 | # lint code 46 | utils_cppcheck(INCLUDE_DIRECTORY "./include/" SOURCES "./src/*.c" WORKING_DIRECTORY "${X_CMAKE_PROJECT_ROOT_DIR}") 47 | 48 | # format code 49 | utils_uncrustify( 50 | CONFIG_FILE "${X_CMAKE_PROJECT_ROOT_DIR}/uncrustify.cfg" 51 | SOURCES ${SOURCES} ${HEADER_SOURCES} ${TEST_SOURCES} ${EXAMPLE_SOURCES} 52 | ) 53 | 54 | # create static library 55 | add_library(${CMAKE_PROJECT_NAME} STATIC ${SOURCES} ${STRING_BUFFER_SOURCES}) 56 | if(NOT WIN32) 57 | set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES COMPILE_FLAGS "${X_CMAKE_C_FLAGS} -Wconversion") 58 | endif() 59 | 60 | # example 61 | add_executable(example examples/example.c) 62 | target_link_libraries(example ${CMAKE_PROJECT_NAME}) 63 | set_target_properties(example PROPERTIES COMPILE_FLAGS "${X_CMAKE_C_FLAGS}") 64 | 65 | # tests 66 | include(CTest) 67 | 68 | utils_setup_test_lib( 69 | SOURCES "${COMMON_TEST_SOURCES}" 70 | COMPILATION_FLAGS "${X_CMAKE_C_FLAGS}" 71 | ) 72 | utils_setup_c_all_tests( 73 | COMPILATION_FLAGS "${X_CMAKE_C_FLAGS}" 74 | BINARY_DIRECTORY "${CMAKE_BINARY_DIR}/bin" 75 | LIBRARIES "Test" 76 | ) 77 | 78 | if("$ENV{X_CMAKE_DOC_STEPS}" STREQUAL "true") 79 | # post build steps 80 | add_custom_command( 81 | TARGET example 82 | POST_BUILD 83 | COMMENT "Post Build Steps" 84 | COMMAND ${CMAKE_COMMAND} -P "../post-example-build.cmake" 85 | ) 86 | endif() 87 | 88 | -------------------------------------------------------------------------------- /uncrustify.cfg: -------------------------------------------------------------------------------- 1 | output_tab_size = 2 2 | tok_split_gte = true 3 | indent_columns = 2 4 | indent_with_tabs = 0 5 | indent_class = true 6 | indent_member = 2 7 | indent_bool_paren = true 8 | indent_first_bool_expr = true 9 | sp_arith = force 10 | sp_assign = force 11 | sp_assign_default = force 12 | sp_bool = force 13 | sp_compare = force 14 | sp_inside_paren = remove 15 | sp_paren_paren = remove 16 | sp_before_ptr_star = force 17 | sp_between_ptr_star = remove 18 | sp_after_ptr_star = remove 19 | sp_before_byref = force 20 | sp_after_byref = remove 21 | sp_before_angle = remove 22 | sp_inside_angle = remove 23 | sp_after_angle = force 24 | sp_angle_paren = remove 25 | sp_angle_paren_empty = remove 26 | sp_angle_word = force 27 | sp_before_sparen = force 28 | sp_inside_sparen = remove 29 | sp_after_sparen = force 30 | sp_before_semi_for = remove 31 | sp_before_semi_for_empty = force 32 | sp_before_squares = remove 33 | sp_inside_square = remove 34 | sp_after_comma = force 35 | sp_before_ellipsis = remove 36 | sp_after_cast = remove 37 | sp_sizeof_paren = remove 38 | sp_inside_braces_enum = force 39 | sp_inside_braces_struct = force 40 | sp_inside_braces = force 41 | sp_func_proto_paren = remove 42 | sp_func_def_paren = remove 43 | sp_inside_fparen = remove 44 | sp_after_tparen_close = remove 45 | sp_func_call_paren = remove 46 | sp_return_paren = remove 47 | sp_attribute_paren = remove 48 | sp_defined_paren = force 49 | sp_brace_typedef = force 50 | sp_before_dc = remove 51 | sp_after_dc = remove 52 | sp_enum_assign = force 53 | align_func_params = true 54 | align_var_def_span = 2 55 | align_var_def_star_style = 1 56 | align_var_def_amp_style = 1 57 | align_var_def_colon = true 58 | align_var_def_inline = true 59 | align_assign_span = 1 60 | align_enum_equ_span = 4 61 | align_var_class_span = 2 62 | align_var_struct_span = 3 63 | align_struct_init_span = 3 64 | align_typedef_gap = 3 65 | align_typedef_span = 5 66 | align_typedef_star_style = 1 67 | align_right_cmt_span = 3 68 | align_nl_cont = true 69 | align_pp_define_gap = 4 70 | align_pp_define_span = 3 71 | align_asm_colon = true 72 | align_enum_equ_thresh = 8 73 | nl_assign_leave_one_liners = true 74 | nl_class_leave_one_liners = true 75 | nl_enum_leave_one_liners = true 76 | nl_getset_leave_one_liners = true 77 | nl_assign_brace = add 78 | nl_func_var_def_blk = 1 79 | nl_fcall_brace = add 80 | nl_enum_brace = force 81 | nl_struct_brace = add 82 | nl_union_brace = add 83 | nl_if_brace = add 84 | nl_brace_else = add 85 | nl_elseif_brace = add 86 | nl_else_brace = add 87 | nl_finally_brace = add 88 | nl_try_brace = add 89 | nl_for_brace = add 90 | nl_catch_brace = add 91 | nl_while_brace = add 92 | nl_do_brace = add 93 | nl_brace_while = remove 94 | nl_switch_brace = add 95 | nl_before_case = true 96 | nl_after_case = true 97 | nl_case_colon_brace = force 98 | nl_namespace_brace = force 99 | nl_constr_init_args = force 100 | nl_func_type_name = remove 101 | nl_func_paren = remove 102 | nl_func_def_paren = remove 103 | nl_func_decl_start = remove 104 | nl_func_def_start = remove 105 | nl_func_decl_start_single = remove 106 | nl_func_def_start_single = remove 107 | nl_func_decl_args = remove 108 | nl_func_decl_end = remove 109 | nl_func_def_end = remove 110 | nl_func_decl_end_single = remove 111 | nl_func_def_end_single = remove 112 | nl_fdef_brace = force 113 | nl_return_expr = remove 114 | nl_after_semicolon = true 115 | nl_after_brace_open = true 116 | nl_after_vbrace_open = true 117 | nl_after_brace_close = true 118 | nl_squeeze_ifdef = true 119 | nl_constr_colon = force 120 | pos_constr_comma = lead_force 121 | pos_constr_colon = lead_break 122 | pos_bool = lead 123 | pos_enum_comma = trail_force 124 | nl_max = 3 125 | nl_after_func_proto = 1 126 | nl_after_func_proto_group = 2 127 | nl_before_func_body_def = 3 128 | nl_after_access_spec = 1 129 | eat_blanks_after_open_brace = true 130 | eat_blanks_before_close_brace = true 131 | nl_after_return = true 132 | mod_full_brace_do = add 133 | mod_full_brace_for = add 134 | mod_full_brace_if = add 135 | mod_full_brace_while = add 136 | mod_paren_on_return = add 137 | mod_remove_extra_semicolon = true 138 | mod_add_long_function_closebrace_comment = 40 139 | mod_add_long_namespace_closebrace_comment = 5 140 | mod_add_long_switch_closebrace_comment = 40 141 | mod_remove_empty_return = true 142 | cmt_star_cont = true 143 | pp_indent = remove 144 | pp_space = remove 145 | mod_sort_include = true 146 | -------------------------------------------------------------------------------- /include/fsio.h: -------------------------------------------------------------------------------- 1 | #ifndef FSIO_H 2 | #define FSIO_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | enum FsIOError 9 | { 10 | // not an error 11 | FSIO_ERROR_NONE = 0, 12 | // error should be checked via errno 13 | FSIO_ERROR_SEE_ERRNO = 1, 14 | // invalid input such as NULL or invalid path 15 | FSIO_ERROR_INVALID_INPUT = 2, 16 | // path to file/dir not found 17 | FSIO_ERROR_PATH_NOT_FOUND = 3, 18 | // general error in copy 19 | FSIO_ERROR_COPY_FAILED = 4, 20 | }; 21 | 22 | struct FsIORecursiveCallbackInfo 23 | { 24 | void *context; 25 | char *path; 26 | bool is_file; 27 | }; 28 | 29 | struct FsIOReadFileOptions 30 | { 31 | // the limit to read. 0 or negative value for no limit. 32 | long max_read_limit; 33 | // if limit is set, if true, read from end, else read from start. 34 | bool tail; 35 | }; 36 | 37 | struct FsIOCopyFileOptions 38 | { 39 | // amount of additional retries in case of write error 40 | unsigned int write_retries; 41 | unsigned int retry_interval_seconds; 42 | }; 43 | 44 | struct FsIOMoveFileOptions 45 | { 46 | // True to force move via file copy instead of rename 47 | bool force_copy; 48 | // amount of additional retries in case of write error 49 | unsigned int write_retries; 50 | unsigned int retry_interval_seconds; 51 | }; 52 | 53 | /** 54 | * A 777 permission mode defined as S_IRWXU | S_IRWXG | S_IRWXO 55 | */ 56 | extern const mode_t FSIO_MODE_ALL; 57 | 58 | /** 59 | * Returns the file size. 60 | * If no input provided, file is not found or path does not point to a file, 61 | * -1 will be returned. 62 | */ 63 | long fsio_file_size(char * /* file */); 64 | 65 | /** 66 | * Writes the provided text into the file, deleting any previous content. 67 | * In case of any error or invalid input, this function will return false. 68 | * If parent directories do not exist, they will be created. 69 | */ 70 | bool fsio_write_text_file(char * /* file */, char * /* text */); 71 | 72 | /** 73 | * Writes the provided text into the file, appending if file already exists. 74 | * In case of any error or invalid input, this function will return false. 75 | * If parent directories do not exist, they will be created. 76 | */ 77 | bool fsio_append_text_file(char * /* file */, char * /* text */); 78 | 79 | /** 80 | * Reads and returns the text from the provided file. 81 | * In case of any error or invalid input, this function will return NULL. 82 | */ 83 | char *fsio_read_text_file(char * /* file */); 84 | 85 | /** 86 | * Reads and returns the text from the provided file. 87 | * In case of any error or invalid input, this function will return NULL. 88 | */ 89 | char *fsio_read_text_file_with_options(char * /* file */, struct FsIOReadFileOptions); 90 | 91 | /** 92 | * Writes the provided content into the file, deleting any previous content. 93 | * In case of any error or invalid input, this function will return false. 94 | * If parent directories do not exist, they will be created. 95 | */ 96 | bool fsio_write_binary_file(char * /* file */, char * /* binary */, size_t /* length */); 97 | 98 | /** 99 | * Writes the provided binary into the file, appending if file already exists. 100 | * In case of any error or invalid input, this function will return false. 101 | * If parent directories do not exist, they will be created. 102 | */ 103 | bool fsio_append_binary_file(char * /* file */, char * /* binary */, size_t /* length */); 104 | 105 | /** 106 | * Reads and returns the binary from the provided file. 107 | * In case of any error or invalid input, this function will return NULL. 108 | */ 109 | char *fsio_read_binary_file(char * /* file */); 110 | 111 | /** 112 | * Reads and returns the binary from the provided file. 113 | * In case of any error or invalid input, this function will return NULL. 114 | */ 115 | char *fsio_read_binary_file_with_options(char * /* file */, struct FsIOReadFileOptions); 116 | 117 | /** 118 | * Creates an empty file for the provided path. 119 | * If a file exists in that path, it will be truncated. 120 | */ 121 | bool fsio_create_empty_file(char * /* file */); 122 | 123 | /** 124 | * Copies the source file content to the target file, deleting any previous 125 | * content in the target file. 126 | * In case of an error, this function will return false. 127 | */ 128 | bool fsio_copy_file(char * /* source */, char * /* target */); 129 | 130 | /** 131 | * Copies the source file content to the target file, deleting any previous 132 | * content in the target file. 133 | * In case of an error, this function will return false. 134 | */ 135 | bool fsio_copy_file_with_options(char * /* source */, char * /* target */, struct FsIOCopyFileOptions); 136 | 137 | /** 138 | * Moves the source file content to the target file, deleting any previous 139 | * content in the target file. 140 | * In case of an error, this function will return false. 141 | * In case of file move via copy (in case of different file systems) the 142 | * new file will have current user owner and group and default permissions. 143 | */ 144 | bool fsio_move_file(char * /* source */, char * /* target */); 145 | 146 | /** 147 | * Moves the source file content to the target file, deleting any previous 148 | * content in the target file. 149 | * In case of an error, this function will return the error code, otherwise it will return 0. 150 | * In case of file move via copy (in case of different file systems) the 151 | * new file will have current user owner and group and default permissions. 152 | */ 153 | enum FsIOError fsio_move_file_with_options(char * /* source */, char * /* target */, struct FsIOMoveFileOptions); 154 | 155 | /** 156 | * Will return the file extension (the content after the last . of the last path element). 157 | * If no extension is detected, NULL will be returned. 158 | * The returned string must be freed once no longer needed. 159 | */ 160 | char *fsio_file_extension(char *); 161 | 162 | /** 163 | * Will join the provided paths and return a new allocated string with 164 | * the result. 165 | * If needed, a unix style separator will be added. 166 | */ 167 | char *fsio_join_paths(char *, char *); 168 | 169 | /** 170 | * Returns true if the provided path exists. 171 | */ 172 | bool fsio_path_exists(char * /* path */); 173 | 174 | /** 175 | * Returns true if the provided path exists and is a regular file. 176 | */ 177 | bool fsio_file_exists(char * /* path */); 178 | 179 | /** 180 | * Returns true if the provided path exists and is a directory. 181 | */ 182 | bool fsio_dir_exists(char * /* path */); 183 | 184 | /** 185 | * Creates a new directory. 186 | * This function will return true if the directory was created or 187 | * if it already exists. 188 | */ 189 | bool fsio_mkdir(char * /* directory */, mode_t /* mode */); 190 | 191 | /** 192 | * Creates a new directory, including any needed parent directory. 193 | * This function will return true if the directory was created or 194 | * if it already exists. 195 | */ 196 | bool fsio_mkdirs(char * /* directory */, mode_t /* mode */); 197 | 198 | /** 199 | * Creates a new directory for the parent path of the provided file path. 200 | * This function will return true if the directory was created or 201 | * if it already exists. 202 | */ 203 | bool fsio_mkdirs_parent(char * /* path */, mode_t /* mode */); 204 | 205 | /** 206 | * Removes file or directory for the provided path. 207 | * If the path points to an non empty directory, it will delete its content as well. 208 | */ 209 | bool fsio_remove(char * /* path */); 210 | 211 | /** 212 | * Runs change mode for all files and directories from the provided path. 213 | */ 214 | bool fsio_chmod_recursive(char * /* path */, mode_t /* mode */); 215 | 216 | /** 217 | * Runs recursively on the provided path and for each file/directory it will call the provided 218 | * callback with the info struct. 219 | * The callback will return true (to continue) or false (to stop and exit the flow). 220 | * The info struct should not be modified by the callback. 221 | */ 222 | bool fsio_recursive_operation(char * /* path */, bool (*callback)(struct FsIORecursiveCallbackInfo), void * /* context */); 223 | 224 | #endif 225 | 226 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /src/fsio.c: -------------------------------------------------------------------------------- 1 | #include "fsio.h" 2 | #include "stringbuffer.h" 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define FSIO_READ_BUFFER_SIZE 1024 12 | 13 | const mode_t FSIO_MODE_ALL = S_IRWXU | S_IRWXG | S_IRWXO; 14 | 15 | static bool _fsio_load_stat(char *, struct stat *); 16 | static bool _fsio_write_file(char *, char *, char *, bool, size_t); 17 | static char *_fsio_read_file_with_options(char *, char *, struct FsIOReadFileOptions); 18 | static bool _fsio_remove_callback(struct FsIORecursiveCallbackInfo); 19 | static bool _fsio_chmod_recursive_callback(struct FsIORecursiveCallbackInfo); 20 | static bool _fsio_recursive_operation(char *, bool (*callback)(struct FsIORecursiveCallbackInfo), void *, struct StringBuffer *); 21 | 22 | 23 | long fsio_file_size(char *file) 24 | { 25 | if (!fsio_file_exists(file)) 26 | { 27 | return(-1); 28 | } 29 | 30 | FILE *fp = fopen(file, "rb"); 31 | if (fp == NULL) 32 | { 33 | return(-1); 34 | } 35 | 36 | long current_position = ftell(fp); 37 | 38 | fseek(fp, 0L, SEEK_END); 39 | long size = ftell(fp); 40 | 41 | // set back to original position 42 | fseek(fp, current_position, SEEK_SET); 43 | 44 | fclose(fp); 45 | 46 | return(size); 47 | } 48 | 49 | 50 | bool fsio_write_text_file(char *file, char *text) 51 | { 52 | return(_fsio_write_file(file, text, "w", true, 0)); 53 | } 54 | 55 | 56 | bool fsio_append_text_file(char *file, char *text) 57 | { 58 | return(_fsio_write_file(file, text, "a", true, 0)); 59 | } 60 | 61 | 62 | char *fsio_read_text_file(char *file) 63 | { 64 | struct FsIOReadFileOptions options; 65 | 66 | options.max_read_limit = 0; 67 | options.tail = false; 68 | 69 | return(fsio_read_text_file_with_options(file, options)); 70 | } 71 | 72 | 73 | char *fsio_read_text_file_with_options(char *file, struct FsIOReadFileOptions options) 74 | { 75 | return(_fsio_read_file_with_options(file, "r", options)); 76 | } 77 | 78 | 79 | bool fsio_write_binary_file(char *file, char *content, size_t length) 80 | { 81 | return(_fsio_write_file(file, content, "wb", false, length)); 82 | } 83 | 84 | 85 | bool fsio_append_binary_file(char *file, char *content, size_t length) 86 | { 87 | return(_fsio_write_file(file, content, "ab", false, length)); 88 | } 89 | 90 | 91 | char *fsio_read_binary_file(char *file) 92 | { 93 | struct FsIOReadFileOptions options; 94 | 95 | options.max_read_limit = 0; 96 | options.tail = false; 97 | 98 | return(fsio_read_binary_file_with_options(file, options)); 99 | } 100 | 101 | 102 | char *fsio_read_binary_file_with_options(char *file, struct FsIOReadFileOptions options) 103 | { 104 | return(_fsio_read_file_with_options(file, "rb", options)); 105 | } 106 | 107 | 108 | bool fsio_create_empty_file(char *file) 109 | { 110 | return(fsio_write_binary_file(file, "", 0)); 111 | } 112 | 113 | 114 | bool fsio_copy_file(char *source, char *target) 115 | { 116 | struct FsIOCopyFileOptions options; 117 | 118 | options.write_retries = 0; 119 | options.retry_interval_seconds = 0; 120 | 121 | return(fsio_copy_file_with_options(source, target, options)); 122 | } 123 | 124 | 125 | bool fsio_copy_file_with_options(char *source, char *target, struct FsIOCopyFileOptions options) 126 | { 127 | if (source == NULL || target == NULL) 128 | { 129 | return(false); 130 | } 131 | 132 | long file_size = fsio_file_size(source); 133 | if (!file_size) 134 | { 135 | return(fsio_create_empty_file(target)); 136 | } 137 | 138 | FILE *source_fp = fopen(source, "r"); 139 | if (source_fp == NULL) 140 | { 141 | return(false); 142 | } 143 | 144 | FILE *target_fp = fopen(target, "w"); 145 | if (target_fp == NULL) 146 | { 147 | fclose(source_fp); 148 | return(false); 149 | } 150 | 151 | bool delete_file = false; 152 | long left_to_read = file_size; 153 | char io_buffer[FSIO_READ_BUFFER_SIZE] = { 0 }; 154 | do 155 | { 156 | if (feof(source_fp)) 157 | { 158 | break; 159 | } 160 | 161 | long to_read = FSIO_READ_BUFFER_SIZE; 162 | if (to_read > left_to_read) 163 | { 164 | to_read = left_to_read; 165 | } 166 | 167 | size_t read = fread(io_buffer, 1, (size_t)to_read, source_fp); 168 | if (!read) 169 | { 170 | delete_file = true; 171 | break; 172 | } 173 | else 174 | { 175 | left_to_read = left_to_read - (long)read; 176 | } 177 | 178 | size_t written = fwrite(io_buffer, 1, read, target_fp); 179 | if (written < read) 180 | { 181 | delete_file = true; 182 | break; 183 | } 184 | } while (left_to_read > 0); 185 | 186 | fclose(source_fp); 187 | fclose(target_fp); 188 | 189 | if (delete_file) 190 | { 191 | remove(target); 192 | 193 | if (options.write_retries > 0) 194 | { 195 | if (options.retry_interval_seconds) 196 | { 197 | sleep(options.retry_interval_seconds); 198 | } 199 | options.write_retries = options.write_retries - 1; 200 | 201 | return(fsio_copy_file_with_options(source, target, options)); 202 | } 203 | 204 | return(false); 205 | } 206 | 207 | return(true); 208 | } /* fsio_copy_file_with_options */ 209 | 210 | 211 | bool fsio_move_file(char *source, char *target) 212 | { 213 | struct FsIOMoveFileOptions options; 214 | 215 | options.force_copy = false; 216 | options.write_retries = 0; 217 | options.retry_interval_seconds = 0; 218 | 219 | enum FsIOError error = fsio_move_file_with_options(source, target, options); 220 | 221 | return(error == FSIO_ERROR_NONE); 222 | } 223 | 224 | 225 | enum FsIOError fsio_move_file_with_options(char *source, char *target, struct FsIOMoveFileOptions options) 226 | { 227 | if (source == NULL || target == NULL) 228 | { 229 | return(FSIO_ERROR_INVALID_INPUT); 230 | } 231 | if (!fsio_file_exists(source)) 232 | { 233 | return(FSIO_ERROR_PATH_NOT_FOUND); 234 | } 235 | 236 | if (!options.force_copy) 237 | { 238 | if (!rename(source, target)) 239 | { 240 | return(FSIO_ERROR_NONE); 241 | } 242 | 243 | if (errno != EXDEV) 244 | { 245 | return(FSIO_ERROR_SEE_ERRNO); 246 | } 247 | } 248 | 249 | struct FsIOCopyFileOptions copy_options; 250 | copy_options.write_retries = options.write_retries; 251 | copy_options.retry_interval_seconds = options.retry_interval_seconds; 252 | bool copy_done = fsio_copy_file_with_options(source, target, copy_options); 253 | if (copy_done) 254 | { 255 | fsio_remove(source); 256 | } 257 | 258 | if (!copy_done) 259 | { 260 | return(FSIO_ERROR_COPY_FAILED); 261 | } 262 | 263 | return(FSIO_ERROR_NONE); 264 | } /* fsio_move_file_with_options */ 265 | 266 | 267 | char *fsio_file_extension(char *path) 268 | { 269 | if (path == NULL) 270 | { 271 | return(NULL); 272 | } 273 | 274 | size_t length = strlen(path); 275 | if (!length) 276 | { 277 | return(NULL); 278 | } 279 | 280 | size_t extension_index = 0; 281 | bool found = false; 282 | for (size_t index = length - 1; ; index--) 283 | { 284 | char character = path[index]; 285 | if (character == '/' || character == '\\') 286 | { 287 | return(NULL); 288 | } 289 | 290 | if (character == '.') 291 | { 292 | found = true; 293 | extension_index = index; 294 | break; 295 | } 296 | 297 | if (!index) 298 | { 299 | break; 300 | } 301 | } 302 | 303 | if (!found) 304 | { 305 | return(NULL); 306 | } 307 | 308 | size_t extension_length = length - extension_index; 309 | if (extension_length <= 1) 310 | { 311 | return(NULL); 312 | } 313 | 314 | char *extension = malloc(sizeof(char) * (extension_length + 1)); 315 | for (size_t index = 0; index < extension_length; index++) 316 | { 317 | extension[index] = path[extension_index + index]; 318 | } 319 | extension[extension_length] = '\0'; 320 | 321 | return(extension); 322 | } /* fsio_get_file_extension */ 323 | 324 | 325 | char *fsio_join_paths(char *path1, char *path2) 326 | { 327 | if (path1 == NULL) 328 | { 329 | if (path2 == NULL) 330 | { 331 | return(NULL); 332 | } 333 | 334 | return(strdup(path2)); 335 | } 336 | if (path2 == NULL) 337 | { 338 | return(strdup(path1)); 339 | } 340 | 341 | size_t len1 = strlen(path1); 342 | if (!len1) 343 | { 344 | return(strdup(path2)); 345 | } 346 | size_t len2 = strlen(path2); 347 | if (!len2) 348 | { 349 | return(strdup(path1)); 350 | } 351 | 352 | bool path1_ends_with_separator = path1[len1 - 1] == '/' || path1[len1 - 1] == '\\'; 353 | bool path2_starts_with_separator = path2[0] == '/' || path2[0] == '\\'; 354 | bool need_to_add_separator = !path1_ends_with_separator && !path2_starts_with_separator; 355 | bool need_to_remove_separator = path1_ends_with_separator && path2_starts_with_separator; 356 | 357 | size_t concat_len = len1 + len2; 358 | if (need_to_add_separator) 359 | { 360 | concat_len = concat_len + 1; 361 | } 362 | else if (need_to_remove_separator) 363 | { 364 | concat_len = concat_len - 1; 365 | } 366 | 367 | char *concat_path = malloc(sizeof(char) * (concat_len + 1)); 368 | 369 | for (size_t index = 0; index < len1; index++) 370 | { 371 | concat_path[index] = path1[index]; 372 | } 373 | size_t offset = len1; 374 | if (need_to_add_separator) 375 | { 376 | concat_path[len1] = '/'; 377 | offset = offset + 1; 378 | } 379 | else if (need_to_remove_separator) 380 | { 381 | offset = offset - 1; 382 | } 383 | for (size_t index = 0; index < len2; index++) 384 | { 385 | concat_path[offset + index] = path2[index]; 386 | } 387 | 388 | concat_path[concat_len] = 0; 389 | 390 | return(concat_path); 391 | } /* fsio_join_paths */ 392 | 393 | 394 | bool fsio_path_exists(char *path) 395 | { 396 | struct stat info; 397 | 398 | return(_fsio_load_stat(path, &info)); 399 | } 400 | 401 | 402 | bool fsio_file_exists(char *path) 403 | { 404 | struct stat info; 405 | 406 | if (!_fsio_load_stat(path, &info)) 407 | { 408 | return(false); 409 | } 410 | 411 | return(S_ISREG(info.st_mode)); 412 | } 413 | 414 | 415 | bool fsio_dir_exists(char *path) 416 | { 417 | struct stat info; 418 | 419 | if (!_fsio_load_stat(path, &info)) 420 | { 421 | return(false); 422 | } 423 | 424 | return(S_ISDIR(info.st_mode)); 425 | } 426 | 427 | 428 | bool fsio_mkdir(char *directory, mode_t mode) 429 | { 430 | if (directory == NULL) 431 | { 432 | return(false); 433 | } 434 | 435 | if (fsio_dir_exists(directory)) 436 | { 437 | return(true); 438 | } 439 | 440 | int result = mkdir(directory, mode); 441 | 442 | if (result == 0 || errno == EEXIST) 443 | { 444 | return(true); 445 | } 446 | 447 | return(false); 448 | } 449 | 450 | 451 | bool fsio_mkdirs(char *directory, mode_t mode) 452 | { 453 | if (directory == NULL) 454 | { 455 | return(false); 456 | } 457 | 458 | if (fsio_mkdir(directory, mode)) 459 | { 460 | return(true); 461 | } 462 | 463 | char *directory_mutable = strdup(directory); 464 | 465 | for (char *path = directory_mutable; *path != 0; path++) 466 | { 467 | if (*path == '/') 468 | { 469 | *path = '\0'; 470 | 471 | if (strlen(directory_mutable)) 472 | { 473 | if (!fsio_mkdir(directory_mutable, mode)) 474 | { 475 | free(directory_mutable); 476 | return(false); 477 | } 478 | } 479 | 480 | *path = '/'; 481 | } 482 | } 483 | 484 | free(directory_mutable); 485 | 486 | return(fsio_mkdir(directory, mode)); 487 | } 488 | 489 | 490 | bool fsio_mkdirs_parent(char *path, mode_t mode) 491 | { 492 | if (path == NULL) 493 | { 494 | return(false); 495 | } 496 | 497 | char *path_clone = strdup(path); 498 | char *directory = dirname(path_clone); 499 | 500 | if (directory == NULL) 501 | { 502 | free(path_clone); 503 | return(false); 504 | } 505 | 506 | bool done = fsio_mkdirs(directory, mode); 507 | free(path_clone); 508 | 509 | return(done); 510 | } 511 | 512 | 513 | bool fsio_remove(char *path) 514 | { 515 | if (path == NULL) 516 | { 517 | return(true); 518 | } 519 | 520 | return(fsio_recursive_operation(path, _fsio_remove_callback, NULL)); 521 | } 522 | 523 | 524 | bool fsio_chmod_recursive(char *path, mode_t mode) 525 | { 526 | mode_t mode_ptr[1]; 527 | 528 | mode_ptr[0] = mode; 529 | 530 | return(fsio_recursive_operation(path, _fsio_chmod_recursive_callback, mode_ptr)); 531 | } 532 | 533 | 534 | bool fsio_recursive_operation(char *path, bool (*callback)(struct FsIORecursiveCallbackInfo), void *context) 535 | { 536 | if (path == NULL) 537 | { 538 | return(false); 539 | } 540 | 541 | struct StringBuffer *buffer = stringbuffer_new(); 542 | bool done = _fsio_recursive_operation(path, callback, context, buffer); 543 | stringbuffer_release(buffer); 544 | 545 | return(done); 546 | } 547 | 548 | 549 | static bool _fsio_load_stat(char *path, struct stat *info) 550 | { 551 | if (path == NULL) 552 | { 553 | return(false); 554 | } 555 | 556 | if (stat(path, info) != 0) 557 | { 558 | return(false); 559 | } 560 | 561 | return(true); 562 | } 563 | 564 | 565 | static bool _fsio_write_file(char *file, char *content, char *mode, bool is_text, size_t length) 566 | { 567 | if (file == NULL || content == NULL) 568 | { 569 | return(false); 570 | } 571 | 572 | if (is_text) 573 | { 574 | length = strlen(content); 575 | } 576 | 577 | bool directory_created = fsio_mkdirs_parent(file, FSIO_MODE_ALL); 578 | if (!directory_created) 579 | { 580 | return(false); 581 | } 582 | 583 | FILE *fp = fopen(file, mode); 584 | if (fp == NULL) 585 | { 586 | return(false); 587 | } 588 | 589 | size_t written = fwrite(content, 1, length, fp); 590 | if (written < length) 591 | { 592 | fclose(fp); 593 | 594 | // prevent partially written file to be 595 | remove(file); 596 | 597 | return(false); 598 | } 599 | 600 | fflush(fp); 601 | fclose(fp); 602 | 603 | return(true); 604 | } 605 | 606 | 607 | static char *_fsio_read_file_with_options(char *file, char *mode, struct FsIOReadFileOptions options) 608 | { 609 | long file_size = fsio_file_size(file); 610 | 611 | if (file_size < 0) 612 | { 613 | return(NULL); 614 | } 615 | if (!file_size) 616 | { 617 | return(strdup("")); 618 | } 619 | 620 | FILE *fp = fopen(file, mode); 621 | if (fp == NULL) 622 | { 623 | return(NULL); 624 | } 625 | 626 | long left_to_read = file_size; 627 | if (options.max_read_limit > 0 && left_to_read > options.max_read_limit) 628 | { 629 | left_to_read = options.max_read_limit; 630 | 631 | if (options.tail) 632 | { 633 | fseek(fp, (-1) * left_to_read, SEEK_END); 634 | } 635 | } 636 | 637 | struct StringBuffer *buffer = stringbuffer_new(); 638 | char io_buffer[FSIO_READ_BUFFER_SIZE] = { 0 }; 639 | do 640 | { 641 | if (feof(fp)) 642 | { 643 | break; 644 | } 645 | 646 | long to_read = FSIO_READ_BUFFER_SIZE; 647 | if (to_read > left_to_read) 648 | { 649 | to_read = left_to_read; 650 | } 651 | 652 | size_t read = fread(io_buffer, 1, (size_t)to_read, fp); 653 | if (!read) 654 | { 655 | break; 656 | } 657 | else 658 | { 659 | left_to_read = left_to_read - (long)read; 660 | } 661 | 662 | stringbuffer_append_binary(buffer, io_buffer, 0, read); 663 | } while (left_to_read > 0); 664 | 665 | fclose(fp); 666 | 667 | char *text = stringbuffer_to_string(buffer); 668 | 669 | stringbuffer_release(buffer); 670 | 671 | return(text); 672 | } /* _fsio_read_file_with_options */ 673 | 674 | 675 | static bool _fsio_remove_callback(struct FsIORecursiveCallbackInfo info) 676 | { 677 | return(remove(info.path) == 0); 678 | } 679 | 680 | 681 | static bool _fsio_chmod_recursive_callback(struct FsIORecursiveCallbackInfo info) 682 | { 683 | mode_t *mode = (mode_t *)info.context; 684 | 685 | return(chmod(info.path, mode[0]) == 0); 686 | } 687 | 688 | 689 | static bool _fsio_recursive_operation(char *path, bool (*callback)(struct FsIORecursiveCallbackInfo), void *context, struct StringBuffer *buffer) 690 | { 691 | struct FsIORecursiveCallbackInfo info; 692 | 693 | info.context = context; 694 | info.path = path; 695 | info.is_file = true; 696 | 697 | if (fsio_file_exists(path)) 698 | { 699 | return(callback(info)); 700 | } 701 | 702 | if (fsio_dir_exists(path)) 703 | { 704 | DIR *directory = opendir(path); 705 | if (directory == NULL) 706 | { 707 | return(false); 708 | } 709 | 710 | struct dirent *entry; 711 | while ((entry = readdir(directory))) 712 | { 713 | if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) 714 | { 715 | // skip special directories 716 | continue; 717 | } 718 | 719 | stringbuffer_append_string(buffer, path); 720 | stringbuffer_append(buffer, '/'); 721 | stringbuffer_append_string(buffer, entry->d_name); 722 | 723 | char *entry_path = stringbuffer_to_string(buffer); 724 | stringbuffer_clear(buffer); 725 | 726 | bool done = false; 727 | if (fsio_dir_exists(entry_path)) 728 | { 729 | done = _fsio_recursive_operation(entry_path, callback, context, buffer); 730 | } 731 | else 732 | { 733 | info.path = entry_path; 734 | info.is_file = true; 735 | done = callback(info); 736 | } 737 | free(entry_path); 738 | 739 | if (!done) 740 | { 741 | closedir(directory); 742 | return(false); 743 | } 744 | } 745 | 746 | closedir(directory); 747 | 748 | info.path = path; 749 | info.is_file = false; 750 | return(callback(info)); 751 | } 752 | 753 | return(false); 754 | } /* _fsio_recursive_operation */ 755 | 756 | --------------------------------------------------------------------------------