* @brief Copy files or directories from source to destination * * This function handles copying operations between files and directories with * various combinations of source and destination types. It supports: * - File to file copy * - File to directory copy (copies into directory with original filename) * - Directory to directory copy (recursive) * - Directory to new directory creation an
| 2937 | * @param[in] dst Path to the destination file/directory |
| 2938 | */ |
| 2939 | void copy(const char *src, const char *dst) |
| 2940 | { |
| 2941 | #define FLAG_SRC_TYPE 0x03 |
| 2942 | #define FLAG_SRC_IS_DIR 0x01 |
| 2943 | #define FLAG_SRC_IS_FILE 0x02 |
| 2944 | #define FLAG_SRC_NON_EXSIT 0x00 |
| 2945 | |
| 2946 | #define FLAG_DST_TYPE 0x0C |
| 2947 | #define FLAG_DST_IS_DIR 0x04 |
| 2948 | #define FLAG_DST_IS_FILE 0x08 |
| 2949 | #define FLAG_DST_NON_EXSIT 0x00 |
| 2950 | |
| 2951 | struct stat stat; |
| 2952 | uint32_t flag = 0; |
| 2953 | |
| 2954 | /* check the staus of src and dst */ |
| 2955 | if (dfs_file_lstat(src, &stat) < 0) |
| 2956 | { |
| 2957 | rt_kprintf("copy failed, bad %s\n", src); |
| 2958 | return; |
| 2959 | } |
| 2960 | if (S_ISDIR(stat.st_mode)) |
| 2961 | flag |= FLAG_SRC_IS_DIR; |
| 2962 | else |
| 2963 | flag |= FLAG_SRC_IS_FILE; |
| 2964 | |
| 2965 | if (dfs_file_stat(dst, &stat) < 0) |
| 2966 | { |
| 2967 | flag |= FLAG_DST_NON_EXSIT; |
| 2968 | } |
| 2969 | else |
| 2970 | { |
| 2971 | if (S_ISDIR(stat.st_mode)) |
| 2972 | flag |= FLAG_DST_IS_DIR; |
| 2973 | else |
| 2974 | flag |= FLAG_DST_IS_FILE; |
| 2975 | } |
| 2976 | |
| 2977 | /* 2. check status */ |
| 2978 | if ((flag & FLAG_SRC_IS_DIR) && (flag & FLAG_DST_IS_FILE)) |
| 2979 | { |
| 2980 | rt_kprintf("cp faild, cp dir to file is not permitted!\n"); |
| 2981 | return ; |
| 2982 | } |
| 2983 | |
| 2984 | /* 3. do copy */ |
| 2985 | if (flag & FLAG_SRC_IS_FILE) |
| 2986 | { |
| 2987 | if (flag & FLAG_DST_IS_DIR) |
| 2988 | { |
| 2989 | char *fdst; |
| 2990 | fdst = dfs_normalize_path(dst, _get_path_lastname(src)); |
| 2991 | if (fdst == NULL) |
| 2992 | { |
| 2993 | rt_kprintf("out of memory\n"); |
| 2994 | return; |
| 2995 | } |
| 2996 | copyfile(src, fdst); |
nothing calls this directly
no test coverage detected