2019-02-22 22:53:20 -05:00
|
|
|
#pragma once
|
2017-12-01 17:17:24 +08:00
|
|
|
|
|
|
|
#include <stdint.h>
|
2019-01-19 23:59:37 -05:00
|
|
|
#include <string>
|
2019-02-22 02:56:18 -05:00
|
|
|
#include <memory>
|
2019-02-22 22:53:20 -05:00
|
|
|
#include <map>
|
|
|
|
#include <string_view>
|
2017-12-01 17:17:24 +08:00
|
|
|
|
2020-03-09 01:50:30 -07:00
|
|
|
#include <stream.hpp>
|
2019-02-23 05:06:07 -05:00
|
|
|
|
|
|
|
struct cpio_newc_header;
|
2018-10-24 21:08:06 -04:00
|
|
|
|
2019-02-23 02:23:24 -05:00
|
|
|
struct cpio_entry_base {
|
2020-12-30 22:11:24 -08:00
|
|
|
uint32_t mode = 0;
|
|
|
|
uint32_t uid = 0;
|
|
|
|
uint32_t gid = 0;
|
|
|
|
uint32_t filesize = 0;
|
2019-02-22 02:56:18 -05:00
|
|
|
|
2020-12-30 22:11:24 -08:00
|
|
|
void *data = nullptr;
|
2017-12-01 17:17:24 +08:00
|
|
|
|
2020-12-30 22:11:24 -08:00
|
|
|
cpio_entry_base() : mode(0), uid(0), gid(0), filesize(0) {};
|
|
|
|
explicit cpio_entry_base(const cpio_newc_header *h);
|
|
|
|
virtual ~cpio_entry_base() = default;
|
2019-02-23 02:23:24 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
struct cpio_entry : public cpio_entry_base {
|
2020-12-30 22:11:24 -08:00
|
|
|
std::string filename;
|
2019-02-23 02:23:24 -05:00
|
|
|
|
2020-12-30 22:11:24 -08:00
|
|
|
cpio_entry() = default;
|
|
|
|
explicit cpio_entry(const char *name, uint32_t mode) : filename(name) {
|
|
|
|
this->mode = mode;
|
|
|
|
}
|
|
|
|
explicit cpio_entry(const cpio_newc_header *h) : cpio_entry_base(h) {}
|
2019-02-23 16:16:35 -05:00
|
|
|
|
2020-12-30 22:11:24 -08:00
|
|
|
~cpio_entry() override { free(data); };
|
2018-10-24 21:08:06 -04:00
|
|
|
};
|
|
|
|
|
2019-02-23 02:23:24 -05:00
|
|
|
typedef std::map<std::string_view, std::unique_ptr<cpio_entry_base>> entry_map;
|
2019-02-22 22:53:20 -05:00
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
class cpio {
|
|
|
|
public:
|
2020-12-30 22:11:24 -08:00
|
|
|
void dump(const char *file);
|
|
|
|
void rm(const char *name, bool r = false);
|
|
|
|
void extract();
|
|
|
|
bool extract(const char *name, const char *file);
|
|
|
|
bool exists(const char *name);
|
2019-02-22 22:53:20 -05:00
|
|
|
|
2019-02-23 02:23:24 -05:00
|
|
|
protected:
|
2020-12-30 22:11:24 -08:00
|
|
|
entry_map entries;
|
|
|
|
void rm(entry_map::iterator &it);
|
|
|
|
void dump(FILE *out);
|
2019-02-23 02:23:24 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class cpio_rw : public cpio {
|
|
|
|
public:
|
2020-12-30 22:11:24 -08:00
|
|
|
cpio_rw() = default;
|
|
|
|
explicit cpio_rw(const char *file);
|
|
|
|
void load_cpio(const char *file);
|
|
|
|
void add(mode_t mode, const char *name, const char *file);
|
|
|
|
void mkdir(mode_t mode, const char *name);
|
|
|
|
void ln(const char *target, const char *name);
|
|
|
|
bool mv(const char *from, const char *to);
|
2018-10-24 21:08:06 -04:00
|
|
|
|
|
|
|
protected:
|
2020-12-30 22:11:24 -08:00
|
|
|
void insert(cpio_entry *e);
|
|
|
|
void mv(entry_map::iterator &it, const char *to);
|
|
|
|
void load_cpio(const char *buf, size_t sz);
|
2019-02-22 22:53:20 -05:00
|
|
|
};
|
2019-02-23 02:23:24 -05:00
|
|
|
|
|
|
|
class cpio_mmap : public cpio {
|
|
|
|
public:
|
2020-12-30 22:11:24 -08:00
|
|
|
explicit cpio_mmap(const char *file);
|
|
|
|
~cpio_mmap();
|
2019-02-23 02:23:24 -05:00
|
|
|
private:
|
2020-12-30 22:11:24 -08:00
|
|
|
char *buf;
|
|
|
|
size_t sz;
|
2019-02-23 02:23:24 -05:00
|
|
|
};
|