aboutsummaryrefslogtreecommitdiff
path: root/src/files.c
blob: 988b32976fa2e387c2c4c7e5be3c6d7511b927a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

const char *get_filename_ext(const char *filename) {
  const char *dot = strrchr(filename, '.');
  if (!dot || dot == filename) return "";
  return dot + 1;
}

static int write_all(int fd, const void *buf, size_t size) {
  while (size) {
    ssize_t written = write(fd, buf, size);
    if (written < 0) {
      return -1;
    }

    size -= written;
    buf = (const char *)buf + written;
  }

  return 0;
}

static int copy_file_content(int src_fd, int dst_fd) {
  char buf[4096];
  ssize_t readed;
  while ((readed = read(src_fd, buf, sizeof(buf))) > 0) {
    if (write_all(dst_fd, buf, readed) < 0) {
      perror("write_all");
      return -1;
    }
  }

  if (readed < 0) {
    perror("read");
    return -1;
  }

  return 0;
}

static int copy_at(int src_dir_fd, const char *src_path, int dst_dir_fd, const char *dst_path);

static int copy_dir_content(int src_fd, int dst_fd) {
  int ret = -1;
  // duplicate the descriptor to prevent closedir from closing original one
  src_fd = dup(src_fd);
  if (src_fd < 0) {
     perror("dup");
     goto cleanup0;
  }

  DIR *src_dir = fdopendir(src_fd);
  if (!src_dir) {
    perror("fdopendir");
    close(src_fd);
    goto cleanup0;
  }

  for (struct dirent *ent; (errno = 0, ent = readdir(src_dir));) {
    if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
      continue;
    }

    if (copy_at(src_fd, ent->d_name, dst_fd, ent->d_name) < 0) {
      goto cleanup1;
    }
  }

  if (errno != 0) {
    perror("readdir");
    goto cleanup1;
  }

  ret = 0;

cleanup1:
  closedir(src_dir);
cleanup0:
  return ret;
}

#define ERROR_NOT_A_SYMLINK (-2)

static int copy_symlink(int src_dir_fd, const char *src_path, int dst_dir_fd, const char *dst_path) {
  char symlink_target[PATH_MAX];
  ssize_t symlink_target_len = readlinkat(src_dir_fd, src_path, symlink_target, sizeof(symlink_target) - 1);
  if (symlink_target_len >= 0) {
    symlink_target[symlink_target_len] = '\0';

    if (symlinkat(symlink_target, dst_dir_fd, dst_path) < 0) {
      perror("symlinkat");
      return -1;
    }

    return 0;
  } else if (errno == EINVAL) {
    return ERROR_NOT_A_SYMLINK;
  } else {
    perror("readlinkat");
    return -1;
  }
}

static int copy_at(int src_dir_fd, const char *src_path, int dst_dir_fd, const char *dst_path) {
  int ret = copy_symlink(src_dir_fd, src_path, dst_dir_fd, dst_path);
  if (ret != ERROR_NOT_A_SYMLINK) {
    goto cleanup0;
  }

  ret = -1;
  int src_fd = openat(src_dir_fd, src_path, O_NOFOLLOW | O_RDONLY);
  if (src_fd < 0) {
    perror("openat");
    goto cleanup0;
  }

  struct stat stat;
  if (fstat(src_fd, &stat) < 0) {
    perror("fstat");
    goto cleanup1;
  }

  int dst_oflag = O_NOFOLLOW;
  if (S_ISDIR(stat.st_mode)) {
    if (mkdirat(dst_dir_fd, dst_path, stat.st_mode) < 0) {
      perror("mkdirat");
      goto cleanup1;
    }

    dst_oflag |= O_RDONLY | O_DIRECTORY;
  } else {
    dst_oflag |= O_WRONLY | O_CREAT | O_EXCL;
  }

  int dst_fd = openat(dst_dir_fd, dst_path, dst_oflag, stat.st_mode);
  if (dst_fd < 0) {
    perror("openat");
    goto cleanup1;
  }

  switch (stat.st_mode & S_IFMT) {
    case S_IFDIR:
      ret = copy_dir_content(src_fd, dst_fd);
      break;
    case S_IFREG:
      ret = copy_file_content(src_fd, dst_fd);
      break;
    default:
      fprintf(stderr, "copy_at: unsupported file inode type\n");
  }

cleanup2:
  close(dst_fd);
cleanup1:
  close(src_fd);
cleanup0:
  return ret;
}

void cpdir(const char *src_path, const char *dst_path) {
  copy_at(AT_FDCWD, src_path, AT_FDCWD, dst_path);
}