return false;
}
-int copy_content(const char *src, const char *dst, const char **exclude)
+static bool copy_file(const char *newsrc, const char *newdst, int mode)
+{
+ const int src_file = open(newsrc, O_RDONLY);
+ if (src_file == -1) {
+ SLOGE("Cannot open file %s: (%d) %m", newsrc, errno);
+ return false;
+ }
+
+ const int dst_file = open(newdst, O_CREAT | O_WRONLY, mode);
+ if (dst_file == -1) {
+ close(src_file);
+ SLOGE("Cannot open file %s: (%d) %m", newdst, errno);
+ return false;
+ }
+
+ bool ret = true;
+ struct stat fileinfo = {0};
+ if (fstat(src_file, &fileinfo) == -1) {
+ SLOGE("Get file %s status error: (%d) %m", newsrc, errno);
+ ret = false;
+ goto exit;
+ }
+
+ if (sendfile(dst_file, src_file, NULL, fileinfo.st_size) == -1) {
+ SLOGE("Copy file content (%s -> %s) error: (%d) %m", newsrc, newdst, errno);
+ ret = false;
+ goto exit;
+ }
+
+exit:
+ close(src_file);
+ close(dst_file);
+ return ret;
+}
+
+static int copy_content(const char *src, const char *dst, const char **exclude)
{
int result = -1;
DIR *dir;
return -1;
}
- int src_file = -1, dst_file = -1;
while ((entry = readdir(dir)) != NULL) {
if (is_name_excluded(entry->d_name, exclude) ||
strncmp(entry->d_name, ".", 2) == 0 ||
goto exit;
}
} else if (entry->d_type == DT_REG) {
- src_file = open(newsrc, O_RDONLY);
- dst_file = open(newdst, O_CREAT | O_WRONLY, mode);
- if (src_file == -1 || dst_file == -1) {
- if (src_file != -1)
- SLOGE("Cannot open file %s: (%d) %m", newdst, errno);
- if (dst_file != -1)
- SLOGE("Cannot open file %s: (%d) %m", newsrc, errno);
+ if (!copy_file(newsrc, newdst, mode))
goto exit;
- }
- struct stat fileinfo = {0};
- if (fstat(src_file, &fileinfo) == -1) {
- SLOGE("Get file %s status error: (%d) %m", newsrc, errno);
- goto exit;
- }
-
- if (sendfile(dst_file, src_file, NULL, fileinfo.st_size) == -1) {
- SLOGE("Copy file content (%s -> %s) error: (%d) %m", newsrc, newdst, errno);
- goto exit;
- }
- close(src_file);
- src_file = -1;
- close(dst_file);
- dst_file = -1;
}
}
result = 0;
exit:
- if (src_file != -1)
- close(src_file);
- if (dst_file != -1)
- close(dst_file);
closedir(dir);
return result;
}