Coverage Report

Created: 2025-07-12 06:09

/src/clib/deps/copy/copy.c
Line
Count
Source (jump to first uncovered line)
1
#include <stdio.h>
2
#include "fs/fs.h"
3
#include "tinydir/tinydir.h"
4
#include "copy.h"
5
6
7
0
#define is_dot_file(file) 0 == strcmp(".", file.name) || 0 == strcmp("..", file.name)
8
0
#define check_err(x) if (0 != (err = x)) break;
9
10
11
int copy_file(char *from, char *to)
12
0
{
13
0
    char *content = fs_read(from);
14
0
    if (!content) {
15
0
        return -1;
16
0
    }
17
0
    fs_write(to, content);
18
0
    free(content);
19
20
0
    return 0;
21
0
}
22
23
static void check_dir(char *dir)
24
0
{
25
0
    if (0 != fs_exists(dir)) {
26
0
        fs_mkdir(dir, 0700);
27
0
    }
28
0
}
29
30
static int copy(tinydir_file file, char *target_dir)
31
0
{
32
0
    char target_path[strlen(target_dir) + strlen(file.name) + 2];
33
34
0
    sprintf(target_path, "%s/%s", target_dir, file.name);
35
36
0
    if (file.is_dir) {
37
0
        return copy_dir(file.path, target_path);
38
0
    }
39
40
0
    return copy_file(file.path, target_path);
41
0
}
42
43
int copy_dir(char *dir_path, char *target_dir)
44
0
{
45
0
    int err = 0;
46
0
    tinydir_dir dir;
47
0
    tinydir_file file;
48
0
    tinydir_open(&dir, dir_path);
49
0
    check_dir(target_dir);
50
51
0
    while (dir.has_next) {
52
0
        check_err(tinydir_readfile(&dir, &file));
53
54
0
        if (is_dot_file(file)) {
55
0
            goto next;
56
0
        }
57
0
        check_err(copy(file, target_dir));
58
59
0
        next:
60
0
        check_err(tinydir_next(&dir));
61
0
    }
62
0
    tinydir_close(&dir);
63
64
0
    return err;
65
0
}