#include <common/utils/byte_size_literals.h>
#include <manifest_parser/utils/logging.h>
+
#include <sys/stat.h>
+#include <unistd.h>
#include <algorithm>
#include <cstdio>
bool StepEncryptResources::EncryptFile(const bf::path &src) {
struct stat info;
memset(&info, 0, sizeof(info));
- if (stat(src.c_str(), &info) != 0) {
+ int fd;
+
+ FILE *input = OpenFile(src, "rb");
+ if (!input) {
+ LOG(ERROR) << "Cannot open file for encryption: " << src;
+ return false;
+ }
+
+ fd = fileno(input);
+ if (fstat(fd, &info) != 0) {
LOG(ERROR) << "Could not access file " << src;
+ fclose(input);
return false;
}
+
const std::size_t fileSize = info.st_size;
if (0 == fileSize) {
LOG(ERROR) << src << " size is 0, so encryption will be skipped";
+ fclose(input);
return true;
}
- FILE *input = OpenFile(src, "rb");
- if (!input) {
- LOG(ERROR) << "Cannot open file for encryption: " << src;
- return false;
- }
-
bf::path encFile(src.string() + ".enc");
FILE *output = OpenFile(encFile, "wb");
if (!output) {
} while (!std::feof(input));
fclose(output);
- fclose(input);
LOG(DEBUG) << "File encrypted successfully";
- if (0 != unlink(src.c_str()))
+ if (0 != unlink(src.c_str())) {
+ fclose(input);
return false;
+ }
+ fclose(input);
LOG(DEBUG) << "Rename encrypted file";
if (0 != std::rename(encFile.c_str(), src.c_str()))