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