2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2014 GitHub, Inc.
|
2014-09-23 11:14:30 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:56:58 +00:00
|
|
|
#ifndef SHELL_COMMON_ASAR_ARCHIVE_H_
|
|
|
|
#define SHELL_COMMON_ASAR_ARCHIVE_H_
|
2014-09-23 11:14:30 +00:00
|
|
|
|
2016-07-04 06:08:55 +00:00
|
|
|
#include <memory>
|
2017-04-04 04:50:44 +00:00
|
|
|
#include <unordered_map>
|
2014-09-24 10:44:00 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2015-05-11 02:47:07 +00:00
|
|
|
#include "base/files/file.h"
|
2014-09-23 11:14:30 +00:00
|
|
|
#include "base/files/file_path.h"
|
2021-06-04 01:49:08 +00:00
|
|
|
#include "base/synchronization/lock.h"
|
2014-09-23 11:14:30 +00:00
|
|
|
|
|
|
|
namespace base {
|
|
|
|
class DictionaryValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace asar {
|
|
|
|
|
2014-09-25 08:56:50 +00:00
|
|
|
class ScopedTemporaryFile;
|
|
|
|
|
|
|
|
// This class represents an asar package, and provides methods to read
|
2021-06-04 01:49:08 +00:00
|
|
|
// information from it. It is thread-safe after |Init| has been called.
|
2014-09-25 12:38:12 +00:00
|
|
|
class Archive {
|
2014-09-23 11:14:30 +00:00
|
|
|
public:
|
|
|
|
struct FileInfo {
|
2015-11-27 14:06:37 +00:00
|
|
|
FileInfo() : unpacked(false), executable(false), size(0), offset(0) {}
|
2015-03-20 12:34:58 +00:00
|
|
|
bool unpacked;
|
2015-11-26 12:48:47 +00:00
|
|
|
bool executable;
|
2016-03-08 04:40:10 +00:00
|
|
|
uint32_t size;
|
2016-03-08 14:28:53 +00:00
|
|
|
uint64_t offset;
|
2014-09-23 11:14:30 +00:00
|
|
|
};
|
|
|
|
|
2014-09-24 04:02:33 +00:00
|
|
|
struct Stats : public FileInfo {
|
|
|
|
Stats() : is_file(true), is_directory(false), is_link(false) {}
|
|
|
|
bool is_file;
|
|
|
|
bool is_directory;
|
|
|
|
bool is_link;
|
|
|
|
};
|
|
|
|
|
2014-09-23 11:14:30 +00:00
|
|
|
explicit Archive(const base::FilePath& path);
|
2014-09-25 12:38:12 +00:00
|
|
|
virtual ~Archive();
|
2014-09-23 11:14:30 +00:00
|
|
|
|
|
|
|
// Read and parse the header.
|
|
|
|
bool Init();
|
|
|
|
|
|
|
|
// Get the info of a file.
|
2021-06-04 01:49:08 +00:00
|
|
|
bool GetFileInfo(const base::FilePath& path, FileInfo* info) const;
|
2014-09-23 11:14:30 +00:00
|
|
|
|
2014-09-24 04:02:33 +00:00
|
|
|
// Fs.stat(path).
|
2021-06-04 01:49:08 +00:00
|
|
|
bool Stat(const base::FilePath& path, Stats* stats) const;
|
2014-09-24 04:02:33 +00:00
|
|
|
|
2014-09-24 10:44:00 +00:00
|
|
|
// Fs.readdir(path).
|
2021-06-04 01:49:08 +00:00
|
|
|
bool Readdir(const base::FilePath& path,
|
|
|
|
std::vector<base::FilePath>* files) const;
|
2014-09-24 10:44:00 +00:00
|
|
|
|
2014-09-30 06:53:41 +00:00
|
|
|
// Fs.realpath(path).
|
2021-06-04 01:49:08 +00:00
|
|
|
bool Realpath(const base::FilePath& path, base::FilePath* realpath) const;
|
2014-09-30 06:53:41 +00:00
|
|
|
|
2014-09-25 08:56:50 +00:00
|
|
|
// Copy the file into a temporary file, and return the new path.
|
2015-03-20 12:34:58 +00:00
|
|
|
// For unpacked file, this method will return its real path.
|
2014-09-25 08:56:50 +00:00
|
|
|
bool CopyFileOut(const base::FilePath& path, base::FilePath* out);
|
|
|
|
|
2021-03-15 18:42:54 +00:00
|
|
|
// Returns the file's fd.
|
|
|
|
int GetFD() const;
|
|
|
|
|
2014-09-23 11:14:30 +00:00
|
|
|
base::FilePath path() const { return path_; }
|
|
|
|
|
|
|
|
private:
|
2021-06-04 01:49:08 +00:00
|
|
|
bool initialized_;
|
|
|
|
const base::FilePath path_;
|
2021-03-15 18:42:54 +00:00
|
|
|
base::File file_;
|
|
|
|
int fd_ = -1;
|
2018-05-21 22:18:38 +00:00
|
|
|
uint32_t header_size_ = 0;
|
2016-05-23 01:59:39 +00:00
|
|
|
std::unique_ptr<base::DictionaryValue> header_;
|
2014-09-23 11:14:30 +00:00
|
|
|
|
2014-09-25 08:56:50 +00:00
|
|
|
// Cached external temporary files.
|
2021-06-04 01:49:08 +00:00
|
|
|
base::Lock external_files_lock_;
|
2017-04-04 04:50:44 +00:00
|
|
|
std::unordered_map<base::FilePath::StringType,
|
2018-04-18 01:44:10 +00:00
|
|
|
std::unique_ptr<ScopedTemporaryFile>>
|
|
|
|
external_files_;
|
2014-09-25 08:56:50 +00:00
|
|
|
|
2014-09-23 11:14:30 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(Archive);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace asar
|
|
|
|
|
2019-06-19 20:56:58 +00:00
|
|
|
#endif // SHELL_COMMON_ASAR_ARCHIVE_H_
|