SLOGE("Cannot open directory %s: (%d) %m", src, errno);
return -1;
}
+
+ int src_file = -1, dst_file = -1;
while ((entry = readdir(dir)) != NULL) {
bool ignore = false;
goto exit;
}
} else if (entry->d_type == DT_REG) {
- int src_file = open(newsrc, O_RDONLY);
- int dst_file = open(newdst, O_CREAT | O_WRONLY, mode);
+ 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) {
+ if (src_file != -1)
SLOGE("Cannot open file %s: (%d) %m", newdst, errno);
- close(src_file);
- }
- if (dst_file != -1) {
+ if (dst_file != -1)
SLOGE("Cannot open file %s: (%d) %m", newsrc, errno);
- close(dst_file);
- }
goto exit;
}
struct stat fileinfo = {0};
if (fstat(src_file, &fileinfo) == -1) {
SLOGE("Get file %s status error: (%d) %m", newsrc, errno);
- close(src_file);
- close(dst_file);
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);
- close(src_file);
- close(dst_file);
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;
}