Magisk/native/jni/magiskboot/cpio.h

66 lines
1.4 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 <vector>
#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
2018-10-25 01:08:06 +00:00
struct cpio_newc_header {
char magic[6];
char ino[8];
char mode[8];
char uid[8];
char gid[8];
char nlink[8];
char mtime[8];
char filesize[8];
char devmajor[8];
char devminor[8];
char rdevmajor[8];
char rdevminor[8];
char namesize[8];
char check[8];
} __attribute__((packed));
struct cpio_entry {
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
/* Dynamic data */
2019-01-20 04:59:37 +00:00
std::string filename;
2018-10-26 21:02:07 +00:00
void *data = nullptr;
2017-12-01 09:17:24 +00:00
2019-01-20 04:59:37 +00:00
cpio_entry() = default;
explicit cpio_entry(const char *name) : filename(name) {}
2018-10-25 01:08:06 +00:00
cpio_entry(int fd, cpio_newc_header &header);
~cpio_entry();
};
2019-02-23 03:53:20 +00:00
typedef std::map<std::string_view, std::unique_ptr<cpio_entry>> entry_map;
2018-10-25 01:08:06 +00:00
class cpio {
public:
2019-01-20 04:59:37 +00:00
explicit cpio(const char *filename);
2018-10-25 01:08:06 +00:00
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);
void insert(cpio_entry *e);
void add(mode_t mode, const char *name, const char *file);
2018-10-25 01:08:06 +00:00
void makedir(mode_t mode, const char *name);
void ln(const char *target, const char *name);
bool mv(const char *from, const char *to);
protected:
2019-02-23 03:53:20 +00:00
entry_map entries;
2017-12-03 19:14:04 +00:00
2019-02-23 03:53:20 +00:00
void rm(entry_map::iterator &it);
void mv(entry_map::iterator &it, const char *to);
};