electron/shell/common/asar/archive.h

86 lines
2.2 KiB
C
Raw Normal View History

// Copyright (c) 2014 GitHub, Inc.
// 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_
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>
#include "base/files/file.h"
#include "base/files/file_path.h"
2020-08-04 18:48:04 +00:00
#include "base/files/memory_mapped_file.h"
namespace base {
class DictionaryValue;
}
namespace asar {
class ScopedTemporaryFile;
// This class represents an asar package, and provides methods to read
// information from it.
class Archive {
public:
struct FileInfo {
FileInfo() : unpacked(false), executable(false), size(0), offset(0) {}
bool unpacked;
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-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;
};
explicit Archive(const base::FilePath& path);
virtual ~Archive();
// Read and parse the header.
bool Init();
// Get the info of a file.
bool GetFileInfo(const base::FilePath& path, FileInfo* info);
2014-09-24 04:02:33 +00:00
// Fs.stat(path).
bool Stat(const base::FilePath& path, Stats* stats);
2014-09-24 10:44:00 +00:00
// Fs.readdir(path).
bool Readdir(const base::FilePath& path, std::vector<base::FilePath>* files);
2014-09-30 06:53:41 +00:00
// Fs.realpath(path).
bool Realpath(const base::FilePath& path, base::FilePath* realpath);
// Copy the file into a temporary file, and return the new path.
// For unpacked file, this method will return its real path.
bool CopyFileOut(const base::FilePath& path, base::FilePath* out);
2020-08-04 18:48:04 +00:00
base::MemoryMappedFile* file() { return &file_; }
base::FilePath path() const { return path_; }
base::DictionaryValue* header() const { return header_.get(); }
private:
base::FilePath path_;
2020-08-04 18:48:04 +00:00
base::MemoryMappedFile file_;
uint32_t header_size_ = 0;
2016-05-23 01:59:39 +00:00
std::unique_ptr<base::DictionaryValue> header_;
// Cached external temporary files.
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_;
DISALLOW_COPY_AND_ASSIGN(Archive);
};
} // namespace asar
2019-06-19 20:56:58 +00:00
#endif // SHELL_COMMON_ASAR_ARCHIVE_H_