* @brief Recursively copy directory contents from source to destination * * This function recursively copies all files and subdirectories from the source * directory to the destination directory. It handles both files and directories * appropriately. * * @param[in] src Path to the source directory to be copied * @param[in] dst Path to the destination directory to be created */
| 2833 | * @param[in] dst Path to the destination directory to be created |
| 2834 | */ |
| 2835 | static void copydir(const char *src, const char *dst) |
| 2836 | { |
| 2837 | struct dirent dirent; |
| 2838 | struct stat stat; |
| 2839 | int length; |
| 2840 | struct dfs_file file; |
| 2841 | |
| 2842 | dfs_file_init(&file); |
| 2843 | |
| 2844 | if (dfs_file_open(&file, src, O_DIRECTORY, 0) < 0) |
| 2845 | { |
| 2846 | rt_kprintf("open %s failed\n", src); |
| 2847 | dfs_file_deinit(&file); |
| 2848 | return ; |
| 2849 | } |
| 2850 | |
| 2851 | do |
| 2852 | { |
| 2853 | rt_memset(&dirent, 0, sizeof(struct dirent)); |
| 2854 | |
| 2855 | length = dfs_file_getdents(&file, &dirent, sizeof(struct dirent)); |
| 2856 | if (length > 0) |
| 2857 | { |
| 2858 | char *src_entry_full = NULL; |
| 2859 | char *dst_entry_full = NULL; |
| 2860 | |
| 2861 | if (strcmp(dirent.d_name, "..") == 0 || strcmp(dirent.d_name, ".") == 0) |
| 2862 | continue; |
| 2863 | |
| 2864 | /* build full path for each file */ |
| 2865 | if ((src_entry_full = dfs_normalize_path(src, dirent.d_name)) == NULL) |
| 2866 | { |
| 2867 | rt_kprintf("out of memory!\n"); |
| 2868 | break; |
| 2869 | } |
| 2870 | if ((dst_entry_full = dfs_normalize_path(dst, dirent.d_name)) == NULL) |
| 2871 | { |
| 2872 | rt_kprintf("out of memory!\n"); |
| 2873 | rt_free(src_entry_full); |
| 2874 | break; |
| 2875 | } |
| 2876 | |
| 2877 | rt_memset(&stat, 0, sizeof(struct stat)); |
| 2878 | if (dfs_file_lstat(src_entry_full, &stat) != 0) |
| 2879 | { |
| 2880 | rt_kprintf("open file: %s failed\n", dirent.d_name); |
| 2881 | continue; |
| 2882 | } |
| 2883 | |
| 2884 | if (S_ISDIR(stat.st_mode)) |
| 2885 | { |
| 2886 | mkdir(dst_entry_full, 0); |
| 2887 | copydir(src_entry_full, dst_entry_full); |
| 2888 | } |
| 2889 | else |
| 2890 | { |
| 2891 | copyfile(src_entry_full, dst_entry_full); |
| 2892 | } |
no test coverage detected