Magisk/native/jni/utils/include/cpio.h

78 lines
1.7 KiB
C
Raw Normal View History

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