1 /* 2014, Copyright © Intel Coporation, license APACHE-2.0, see LICENSE file */
3 #include "common/utils/file_util.h"
7 #include <linux/limits.h>
11 #include <boost/algorithm/string/classification.hpp>
12 #include <boost/algorithm/string/split.hpp>
13 #include <boost/filesystem/operations.hpp>
14 #include <boost/filesystem/path.hpp>
15 #include <boost/system/error_code.hpp>
17 #include <manifest_parser/utils/logging.h>
23 #include "common/paths.h"
24 #include "common/utils/byte_size_literals.h"
26 namespace ba = boost::algorithm;
27 namespace bs = boost::system;
28 namespace bf = boost::filesystem;
32 unsigned kZipBufSize = 8_kB;
33 unsigned kZipMaxPath = PATH_MAX;
35 int64_t GetBlockSizeForPath(const bf::path& path_in_partition) {
37 if (stat(path_in_partition.string().c_str(), &stats)) {
38 LOG(ERROR) << "stat(" << path_in_partition.string()
39 << ") failed - error code: " << errno;
42 return stats.st_blksize;
45 int64_t RoundUpToBlockSizeOf(int64_t size, int64_t block_size) {
46 return ((size + block_size - 1) / block_size) * block_size;
49 class UnzFilePointer {
54 currentFileOpened_(false) { }
57 if (currentFileOpened_)
58 unzCloseCurrentFile(zipFile_);
63 bool Open(const char* zip_path) {
64 zipFile_ = static_cast<unzFile*>(unzOpen(zip_path));
72 if (unzOpenCurrentFile(zipFile_) != UNZ_OK)
74 currentFileOpened_ = true;
79 if (currentFileOpened_)
80 unzCloseCurrentFile(zipFile_);
81 currentFileOpened_ = false;
84 unzFile* Get() { return zipFile_; }
89 bool currentFileOpened_;
94 namespace common_installer {
96 FSFlag operator|(FSFlag a, FSFlag b) {
97 return static_cast<FSFlag>(static_cast<int>(a) | static_cast<int>(b));
100 bool SetOwnership(const bf::path& path, uid_t uid, gid_t gid) {
101 int fd = open(path.c_str(), O_RDONLY);
103 LOG(ERROR) << "Can't open directory : " << path;
106 int ret = fchown(fd, uid, gid);
109 LOG(ERROR) << "Failed to change owner of: " << path;
115 bool SetOwnershipAll(const bf::path& path, uid_t uid, gid_t gid) {
116 if (!SetOwnership(path, uid, gid))
118 if (!bf::is_directory(path))
120 for (bf::recursive_directory_iterator iter(path);
121 iter != bf::recursive_directory_iterator();
123 bf::path current(iter->path());
124 if (bf::is_symlink(symlink_status(current)))
126 if (!SetOwnership(current, uid, gid))
132 bool CreateDir(const bf::path& path) {
133 if (bf::exists(path))
136 boost::system::error_code error;
137 bf::create_directories(path, error);
140 LOG(ERROR) << "Failed to create directory: "
141 << boost::system::system_error(error).what();
147 bool SetDirPermissions(const boost::filesystem::path& path,
148 boost::filesystem::perms permissions) {
149 boost::system::error_code error;
150 bf::permissions(path, permissions, error);
153 LOG(ERROR) << "Failed to set permissions for directory: " << path
154 << boost::system::system_error(error).what();
160 bool SetDirOwnershipAndPermissions(const boost::filesystem::path& path,
161 boost::filesystem::perms permissions, uid_t uid,
163 if (!SetOwnership(path, uid, gid)) {
164 LOG(ERROR) << "Failed to change owner: " << path
165 << "(" << uid << ", " << gid << ")";
168 if (!SetDirPermissions(path, permissions)) {
169 LOG(ERROR) << "Failed to change permission: " << path
170 << std::oct << permissions;
177 bool CopyOwnershipAndPermissions(const boost::filesystem::path& src,
178 const boost::filesystem::path& dst) {
179 if (!bf::exists(src)) {
180 LOG(ERROR) << "Failed to copy ownership and permissions"
181 << " from " << src << " to " << dst;
184 bf::perms permissions = bf::status(src).permissions();
186 if (stat(src.c_str(), &stats) != 0)
188 if (!SetDirOwnershipAndPermissions(dst, permissions, stats.st_uid,
190 LOG(ERROR) << "Failed to copy ownership and permissions"
191 << " from " << src << " to " << dst;
197 bool CopyDir(const bf::path& src, const bf::path& dst,
198 FSFlag flags, bool skip_symlink) {
200 // Check whether the function call is valid
201 if (!bf::exists(src) || !bf::is_directory(src)) {
202 LOG(ERROR) << "Source directory " << src
203 << " does not exist or is not a directory.";
206 if (!bf::exists(dst)) {
207 // Create the destination directory
208 if (!CreateDir(dst)) {
209 LOG(ERROR) << "Unable to create destination directory" << dst;
212 if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS)
213 CopyOwnershipAndPermissions(src, dst);
215 if (!(flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE))) {
216 LOG(ERROR) << "Destination directory " << dst.string()
217 << " already exists.";
220 if (flags & (FS_MERGE_OVERWRITE | FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS))
221 CopyOwnershipAndPermissions(src, dst);
223 } catch (const bf::filesystem_error& error) {
224 LOG(ERROR) << "Failed to copy directory: " << error.what();
228 // Iterate through the source directory
229 for (bf::directory_iterator file(src);
230 file != bf::directory_iterator();
233 bf::path current(file->path());
234 bf::path target = dst / current.filename();
236 if (bf::is_symlink(symlink_status(current))) {
239 if ((flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE)) &&
242 bs::error_code error;
243 bf::copy_symlink(current, target, error);
245 LOG(ERROR) << "Failed to copy symlink: " << current << ", "
249 } else if (bf::is_directory(current)) {
250 // Found directory: Recursion
251 if (!CopyDir(current, target, flags, skip_symlink)) {
255 if ((flags & FS_MERGE_SKIP) && bf::exists(target))
257 bf::path destination = target;
259 if (flags & FS_COMMIT_COPY_FILE)
261 bf::unique_path(target.parent_path() / "%%%%-%%%%-%%%%-%%%%");
263 if (flags & FS_MERGE_OVERWRITE)
264 bf::copy_file(current, destination,
265 bf::copy_option::overwrite_if_exists);
267 bf::copy_file(current, destination);
269 if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS)
270 CopyOwnershipAndPermissions(current, destination);
272 if (flags & FS_COMMIT_COPY_FILE) {
273 if (flags & FS_MERGE_OVERWRITE)
275 bf::rename(destination, target);
278 } catch (const bf::filesystem_error& error) {
279 LOG(ERROR) << "Failed to copy directory: " << error.what();
286 bool CopyFile(const bf::path& src, const bf::path& dst) {
287 bs::error_code error;
289 bf::copy_file(src, dst, bf::copy_option::overwrite_if_exists, error);
291 LOG(WARNING) << "copy file " << src << " due to error [" << error << "]";
297 bool RestoreBackup(const bf::path& path) {
298 bf::path backup_path = GetBackupPathForPackagePath(path);
299 if (!bf::exists(backup_path) &&
300 !bf::is_symlink(bf::symlink_status(backup_path))) {
301 LOG(WARNING) << backup_path << " does not exist";
304 return MoveDir(backup_path, path);
307 bool MakeBackup(const bf::path& path) {
308 if (!bf::exists(path) && !bf::is_symlink(bf::symlink_status(path))) {
309 LOG(WARNING) << path << " does not exist";
312 bf::path backup_path = GetBackupPathForPackagePath(path);
313 return MoveDir(path, backup_path);
316 bool RemoveBackup(const bf::path& path) {
317 bf::path backup_path = GetBackupPathForPackagePath(path);
318 if (!bf::exists(backup_path) &&
319 !bf::is_symlink(bf::symlink_status(backup_path))) {
320 LOG(WARNING) << backup_path << " does not exist";
323 return RemoveAll(backup_path);
326 bool RemoveAll(const bf::path& path) {
327 if (!exists(path) && !bf::is_symlink(bf::symlink_status(path)))
330 bs::error_code error;
331 bf::remove_all(path, error);
334 LOG(ERROR) << "Cannot remove: " << path << ", " << error.message();
341 bool Remove(const bf::path& path) {
342 if (!exists(path) && !bf::is_symlink(bf::symlink_status(path)))
345 bs::error_code error;
346 bf::remove(path, error);
349 LOG(ERROR) << "Cannot remove: " << path << ", " << error.message();
355 bool MoveDir(const bf::path& src, const bf::path& dst, FSFlag flags) {
356 if (bf::exists(dst) &&
357 !(flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE))) {
358 LOG(ERROR) << "Destination directory does exist: " << dst;
362 bs::error_code error;
363 bf::rename(src, dst, error);
365 LOG(WARNING) << "Cannot move directory: " << src << ". Will copy/remove...";
366 if (!CopyDir(src, dst, flags, false)) {
367 LOG(ERROR) << "Cannot copy directory: " << src;
370 bf::remove_all(src, error);
372 LOG(ERROR) << "Cannot remove old directory when coping: " << src;
379 bool MoveFile(const bf::path& src, const bf::path& dst, bool force) {
380 if (!force && bf::exists(dst))
382 bs::error_code error;
383 bf::rename(src, dst, error);
385 LOG(WARNING) << "Cannot move file: " << src <<
386 ". Will copy/remove... with error [" << error << "]";
387 bf::copy_file(src, dst, bf::copy_option::overwrite_if_exists, error);
389 LOG(WARNING) << "Cannot copy file " << src <<
390 " due to error [" << error << "]";
393 bf::remove_all(src, error);
395 LOG(ERROR) << "Cannot remove old file when coping: " << src <<
396 "with error [" << error << "]";
402 bool BackupDir(const boost::filesystem::path& src,
403 const boost::filesystem::path& dst, const std::string& entry) {
404 if (!bf::exists(src / entry))
406 if (!MoveDir(src / entry, dst / entry,
408 FS_COMMIT_COPY_FILE |
409 FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS)) {
410 LOG(ERROR) << "Failed to backup file";
417 int64_t GetUnpackedPackageSize(const bf::path& path) {
419 int64_t block_size = GetBlockSizeForPath(path);
421 // if failed to stat path
422 if (block_size == -1)
425 unz_global_info info;
426 unz_file_info64 raw_file_info;
427 char raw_file_name_in_zip[kZipMaxPath];
429 unzFile* zip_file = static_cast<unzFile*>(unzOpen(path.string().c_str()));
430 if (zip_file == nullptr) {
431 LOG(ERROR) << "Failed to open the source dir: " << path.string();
435 if (unzGetGlobalInfo(zip_file, &info) != UNZ_OK) {
436 LOG(ERROR) << "Failed to read global info";
441 for (uLong i = 0; i < info.number_entry; i++) {
442 if (unzGetCurrentFileInfo64(zip_file, &raw_file_info, raw_file_name_in_zip,
443 sizeof(raw_file_name_in_zip), nullptr, 0, nullptr, 0) != UNZ_OK) {
444 LOG(ERROR) << "Failed to read file info";
448 size += RoundUpToBlockSizeOf(raw_file_info.uncompressed_size, block_size);
449 unzGoToNextFile(zip_file);
452 // FIXME: calculate space needed for directories
457 int64_t GetDirectorySize(const boost::filesystem::path& path) {
458 int64_t block_size = GetBlockSizeForPath(path);
460 // if failed to stat path
461 if (block_size == -1)
465 for (bf::recursive_directory_iterator iter(path);
466 iter != bf::recursive_directory_iterator(); ++iter) {
468 if (lstat(iter->path().c_str(), &buf) == -1) {
469 LOG(ERROR) << "lstat() failed for: " << iter->path();
472 size += RoundUpToBlockSizeOf(buf.st_size, block_size);
475 // FIXME: block size for external device may differ...
479 bool CheckFreeSpaceAtPath(int64_t required_size,
480 const boost::filesystem::path& target_location) {
481 bs::error_code error;
482 boost::filesystem::path root = target_location;
483 while (!bf::exists(root) && root != root.root_path())
484 root = root.parent_path();
486 if (!bf::exists(root)) {
487 LOG(ERROR) << "No mount point for path: " << target_location;
490 bf::space_info space_info = bf::space(root, error);
492 LOG(ERROR) << "Failed to get space_info: " << error.message();
496 return (space_info.free >= static_cast<uint64_t>(required_size));
499 boost::filesystem::path GenerateTmpDir(const bf::path &app_path) {
500 boost::filesystem::path install_tmp_dir;
501 boost::filesystem::path tmp_dir(app_path);
504 boost::filesystem::path model;
505 boost::filesystem::path unique_dir =
506 boost::filesystem::unique_path(model = "unpack-%%%%%%");
508 install_tmp_dir = tmp_dir /= unique_dir;
509 } while (boost::filesystem::exists(install_tmp_dir) &&
510 boost::filesystem::is_directory(install_tmp_dir));
512 return install_tmp_dir;
515 boost::filesystem::path GenerateTemporaryPath(
516 const boost::filesystem::path& path) {
517 bf::path pattern = path;
518 pattern += "-%%%%%%";
521 tmp_path = boost::filesystem::unique_path(pattern);
522 } while (boost::filesystem::exists(tmp_path));
526 bool ExtractToTmpDir(const char* zip_path,
527 const boost::filesystem::path& tmp_dir) {
528 return ExtractToTmpDir(zip_path, tmp_dir, "");
531 bool ExtractToTmpDir(const char* zip_path, const bf::path& tmp_dir,
532 const std::string& filter_prefix) {
533 unz_global_info info;
534 char read_buffer[kZipBufSize];
535 unz_file_info raw_file_info;
536 char raw_file_name_in_zip[kZipMaxPath];
538 current_path(tmp_dir);
540 UnzFilePointer zip_file;
541 if (!zip_file.Open(zip_path)) {
542 LOG(WARNING) << "Failed to open the source dir: " << zip_path;
546 if (unzGetGlobalInfo(zip_file.Get(), &info) != UNZ_OK) {
547 LOG(ERROR) << "Failed to read global info";
551 for (uLong i = 0; i < info.number_entry; i++) {
552 if (unzGetCurrentFileInfo(zip_file.Get(),
554 raw_file_name_in_zip,
555 sizeof(raw_file_name_in_zip),
560 LOG(ERROR) << "Failed to read file info";
564 if (raw_file_name_in_zip[0] == '\0')
567 // unpack if filter is empty or path is matched
568 if (filter_prefix.empty() ||
569 std::string(raw_file_name_in_zip).find(filter_prefix) == 0) {
570 bf::path filename_in_zip_path(raw_file_name_in_zip);
572 // prevent "directory climbing" attack
573 if (HasDirectoryClimbing(filename_in_zip_path)) {
574 LOG(ERROR) << "Relative path in widget in malformed";
578 if (!filename_in_zip_path.parent_path().empty()) {
579 if (!CreateDir(filename_in_zip_path.parent_path())) {
580 LOG(ERROR) << "Failed to create directory: "
581 << filename_in_zip_path.parent_path();
586 if (!zip_file.OpenCurrent()) {
587 LOG(ERROR) << "Failed to open file";
591 if (!is_directory(filename_in_zip_path)) {
592 FILE* out = fopen(raw_file_name_in_zip, "wbx");
594 LOG(ERROR) << "Failed to open destination ";
600 ret = unzReadCurrentFile(zip_file.Get(), read_buffer, kZipBufSize);
602 LOG(ERROR) << "Failed to read data: " << ret;
606 fwrite(read_buffer, sizeof(char), ret, out);
613 zip_file.CloseCurrent();
616 if ((i+1) < info.number_entry) {
617 if (unzGoToNextFile(zip_file.Get()) != UNZ_OK) {
618 LOG(ERROR) << "Failed to read next file";
627 bool CheckPathInZipArchive(const char* zip_archive_path,
628 const boost::filesystem::path& relative_zip_path,
631 UnzFilePointer zip_file;
632 if (!zip_file.Open(zip_archive_path)) {
633 LOG(ERROR) << "Failed to open the source dir: " << zip_archive_path;
637 unz_global_info info;
638 if (unzGetGlobalInfo(zip_file.Get(), &info) != UNZ_OK) {
639 LOG(ERROR) << "Failed to read global info";
643 char raw_file_name_in_zip[kZipMaxPath];
644 unz_file_info raw_file_info;
645 for (uLong i = 0; i < info.number_entry; i++) {
646 if (unzGetCurrentFileInfo(zip_file.Get(),
648 raw_file_name_in_zip,
649 sizeof(raw_file_name_in_zip),
654 LOG(ERROR) << "Failed to read file info";
658 if (raw_file_name_in_zip[0] == '\0')
661 if (relative_zip_path.string() == raw_file_name_in_zip) {
666 if ((i + 1) < info.number_entry) {
667 if (unzGoToNextFile(zip_file.Get()) != UNZ_OK) {
668 LOG(ERROR) << "Failed to read next file";
677 bool HasDirectoryClimbing(const boost::filesystem::path& path) {
678 std::vector<std::string> segments;
679 ba::split(segments, path.string(), ba::is_any_of("/\\"));
680 return std::any_of(segments.begin(), segments.end(),
681 [](const std::string& segment) {
682 return segment == "..";
686 boost::filesystem::path MakeRelativePath(const boost::filesystem::path& input,
687 const boost::filesystem::path& base) {
688 if (input.string().find(base.string()) == std::string::npos) {
689 LOG(ERROR) << base.string() << " is not base path for " << input.string();
692 return input.string().substr(base.string().length() + 1);
695 bool IsSubDir(const boost::filesystem::path& path,
696 const boost::filesystem::path& root) {
697 boost::filesystem::path p = path;
698 while (p != boost::filesystem::path()) {
699 if (bf::equivalent(p, root))
707 bf::path RelativePath(const bf::path& from,
708 const bf::path& to) {
709 bf::path::const_iterator itr_path = from.begin();
710 bf::path::const_iterator itr_relative_to = to.begin();
711 while (itr_path != from.end() && itr_relative_to != to.end() &&
712 *itr_path == *itr_relative_to) {
718 if (itr_relative_to != to.end()) {
720 while (itr_relative_to != to.end()) {
726 while (itr_path != from.end()) {
731 bs::error_code error;
732 bf::path resolved_path = bf::canonical(result, error);
734 LOG(ERROR) << "Failed to get canonical path";
738 if (from != resolved_path) {
739 LOG(ERROR) << "Failed to get right relative path :" << resolved_path;
746 } // namespace common_installer