MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / copydir

Function copydir

components/dfs/dfs_v2/src/dfs_file.c:2835–2901  ·  view source on GitHub ↗

* @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 */

Source from the content-addressed store, hash-verified

2833 * @param[in] dst Path to the destination directory to be created
2834 */
2835static 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 }

Callers 1

copyFunction · 0.70

Calls 12

dfs_file_initFunction · 0.85
rt_kprintfFunction · 0.85
dfs_file_deinitFunction · 0.85
rt_memsetFunction · 0.85
rt_freeFunction · 0.85
dfs_file_lstatFunction · 0.85
dfs_file_openFunction · 0.70
dfs_file_getdentsFunction · 0.70
dfs_normalize_pathFunction · 0.70
mkdirFunction · 0.70
copyfileFunction · 0.70
dfs_file_closeFunction · 0.70

Tested by

no test coverage detected