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

Function copyfile

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

* @brief Copy file contents from source to destination * * This function copies the contents of a source file to a destination file. * It handles memory allocation, file operations, and error checking. * * @param[in] src Path to the source file to be copied * @param[in] dst Path to the destination file to be created/overwritten */

Source from the content-addressed store, hash-verified

2759 * @param[in] dst Path to the destination file to be created/overwritten
2760 */
2761static void copyfile(const char *src, const char *dst)
2762{
2763 int ret;
2764 struct dfs_file src_file, dst_file;
2765 rt_uint8_t *block_ptr;
2766 rt_int32_t read_bytes;
2767
2768 block_ptr = (rt_uint8_t *)rt_malloc(BUF_SZ);
2769 if (block_ptr == NULL)
2770 {
2771 rt_kprintf("out of memory\n");
2772 return;
2773 }
2774
2775 dfs_file_init(&src_file);
2776
2777 ret = dfs_file_open(&src_file, src, O_RDONLY, 0);
2778 if (ret < 0)
2779 {
2780 dfs_file_deinit(&src_file);
2781 rt_free(block_ptr);
2782 rt_kprintf("Read %s failed\n", src);
2783 return;
2784 }
2785
2786 dfs_file_init(&dst_file);
2787
2788 ret = dfs_file_open(&dst_file, dst, O_WRONLY | O_CREAT | O_TRUNC, 0);
2789 if (ret < 0)
2790 {
2791 dfs_file_deinit(&dst_file);
2792 dfs_file_close(&src_file);
2793 dfs_file_deinit(&src_file);
2794 rt_free(block_ptr);
2795 rt_kprintf("Write %s failed\n", dst);
2796 return;
2797 }
2798
2799 do
2800 {
2801 read_bytes = dfs_file_read(&src_file, block_ptr, BUF_SZ);
2802 if (read_bytes > 0)
2803 {
2804 int length;
2805
2806 length = dfs_file_write(&dst_file, block_ptr, read_bytes);
2807 if (length != read_bytes)
2808 {
2809 /* write failed. */
2810 rt_kprintf("Write file data failed, errno=%d\n", length);
2811 break;
2812 }
2813 }
2814 } while (read_bytes > 0);
2815
2816 dfs_file_close(&dst_file);
2817 dfs_file_deinit(&dst_file);
2818 dfs_file_close(&src_file);

Callers 2

copydirFunction · 0.70
copyFunction · 0.70

Calls 9

rt_mallocFunction · 0.85
rt_kprintfFunction · 0.85
dfs_file_initFunction · 0.85
dfs_file_deinitFunction · 0.85
rt_freeFunction · 0.85
dfs_file_openFunction · 0.70
dfs_file_closeFunction · 0.70
dfs_file_readFunction · 0.70
dfs_file_writeFunction · 0.70

Tested by

no test coverage detected