2018-10-25 01:08:06 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2019-01-20 04:59:37 +00:00
|
|
|
#include <algorithm>
|
2019-02-23 07:42:26 +00:00
|
|
|
|
2020-03-09 08:50:30 +00:00
|
|
|
#include <utils.hpp>
|
2021-08-13 11:53:11 +00:00
|
|
|
|
|
|
|
#include "cpio.hpp"
|
2018-10-25 01:08:06 +00:00
|
|
|
|
2019-01-20 04:59:37 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2019-02-23 10:06:07 +00:00
|
|
|
struct cpio_newc_header {
|
2020-12-31 06:11:24 +00:00
|
|
|
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];
|
2019-02-23 10:06:07 +00:00
|
|
|
} __attribute__((packed));
|
|
|
|
|
2019-02-23 07:23:24 +00:00
|
|
|
static uint32_t x8u(const char *hex) {
|
2020-12-31 06:11:24 +00:00
|
|
|
uint32_t val, inpos = 8, outpos;
|
|
|
|
char pattern[6];
|
2018-10-25 01:08:06 +00:00
|
|
|
|
2020-12-31 06:11:24 +00:00
|
|
|
while (*hex == '0') {
|
|
|
|
hex++;
|
|
|
|
if (!--inpos) return 0;
|
|
|
|
}
|
|
|
|
// Because scanf gratuitously treats %*X differently than printf does.
|
|
|
|
sprintf(pattern, "%%%dx%%n", inpos);
|
|
|
|
sscanf(hex, pattern, &val, &outpos);
|
|
|
|
if (inpos != outpos)
|
|
|
|
LOGE("bad cpio header\n");
|
2018-10-25 01:08:06 +00:00
|
|
|
|
2020-12-31 06:11:24 +00:00
|
|
|
return val;
|
2018-10-25 01:08:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 11:53:11 +00:00
|
|
|
cpio_entry::cpio_entry(uint32_t mode) : mode(mode), uid(0), gid(0), filesize(0), data(nullptr) {}
|
|
|
|
|
|
|
|
cpio_entry::cpio_entry(const cpio_newc_header *h) :
|
|
|
|
mode(x8u(h->mode)), uid(x8u(h->uid)), gid(x8u(h->gid)), filesize(x8u(h->filesize)), data(nullptr)
|
|
|
|
{}
|
2019-02-23 07:23:24 +00:00
|
|
|
|
2018-10-25 01:08:06 +00:00
|
|
|
void cpio::dump(const char *file) {
|
2020-12-31 06:11:24 +00:00
|
|
|
fprintf(stderr, "Dump cpio: [%s]\n", file);
|
|
|
|
dump(xfopen(file, "we"));
|
2018-10-25 01:08:06 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 03:53:20 +00:00
|
|
|
void cpio::rm(entry_map::iterator &it) {
|
2020-12-31 06:11:24 +00:00
|
|
|
fprintf(stderr, "Remove [%s]\n", it->first.data());
|
|
|
|
entries.erase(it);
|
2018-10-25 01:08:06 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 03:53:20 +00:00
|
|
|
void cpio::rm(const char *name, bool r) {
|
2020-12-31 06:11:24 +00:00
|
|
|
size_t len = strlen(name);
|
|
|
|
for (auto it = entries.begin(); it != entries.end();) {
|
|
|
|
if (it->first.compare(0, len, name) == 0 &&
|
|
|
|
((r && it->first[len] == '/') || it->first[len] == '\0')) {
|
|
|
|
auto tmp = it;
|
|
|
|
++it;
|
|
|
|
rm(tmp);
|
|
|
|
if (!r) return;
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
2019-02-22 07:56:18 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 11:53:11 +00:00
|
|
|
void cpio::extract_entry(const entry_map::value_type &e, const char *file) {
|
2020-12-31 06:11:24 +00:00
|
|
|
fprintf(stderr, "Extract [%s] to [%s]\n", e.first.data(), file);
|
|
|
|
unlink(file);
|
|
|
|
rmdir(file);
|
|
|
|
if (S_ISDIR(e.second->mode)) {
|
2021-08-13 11:53:11 +00:00
|
|
|
::mkdir(file, e.second->mode & 0777);
|
2020-12-31 06:11:24 +00:00
|
|
|
} else if (S_ISREG(e.second->mode)) {
|
|
|
|
int fd = creat(file, e.second->mode & 0777);
|
|
|
|
xwrite(fd, e.second->data, e.second->filesize);
|
|
|
|
fchown(fd, e.second->uid, e.second->gid);
|
|
|
|
close(fd);
|
|
|
|
} else if (S_ISLNK(e.second->mode)) {
|
|
|
|
auto target = strndup((char *) e.second->data, e.second->filesize);
|
|
|
|
symlink(target, file);
|
|
|
|
free(target);
|
|
|
|
}
|
2018-10-25 01:08:06 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 03:53:20 +00:00
|
|
|
void cpio::extract() {
|
2020-12-31 06:11:24 +00:00
|
|
|
for (auto &e : entries)
|
|
|
|
extract_entry(e, e.first.data());
|
2019-02-23 03:53:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cpio::extract(const char *name, const char *file) {
|
2020-12-31 06:11:24 +00:00
|
|
|
auto it = entries.find(name);
|
|
|
|
if (it != entries.end()) {
|
|
|
|
extract_entry(*it, file);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "Cannot find the file entry [%s]\n", name);
|
|
|
|
return false;
|
2018-10-25 01:08:06 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 03:53:20 +00:00
|
|
|
bool cpio::exists(const char *name) {
|
2020-12-31 06:11:24 +00:00
|
|
|
return entries.count(name) != 0;
|
2018-10-25 01:08:06 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 22:43:31 +00:00
|
|
|
#define do_out(buf, len) pos += fwrite(buf, 1, len, out);
|
2021-11-30 03:56:37 +00:00
|
|
|
#define out_align() do_out(zeros, align_padding(pos, 4))
|
2019-11-21 11:08:02 +00:00
|
|
|
void cpio::dump(FILE *out) {
|
2020-12-31 06:11:24 +00:00
|
|
|
size_t pos = 0;
|
|
|
|
unsigned inode = 300000;
|
|
|
|
char header[111];
|
|
|
|
char zeros[4] = {0};
|
|
|
|
for (auto &e : entries) {
|
|
|
|
sprintf(header, "070701%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
|
|
|
|
inode++, // e->ino
|
|
|
|
e.second->mode,
|
|
|
|
e.second->uid,
|
|
|
|
e.second->gid,
|
|
|
|
1, // e->nlink
|
|
|
|
0, // e->mtime
|
|
|
|
e.second->filesize,
|
|
|
|
0, // e->devmajor
|
|
|
|
0, // e->devminor
|
|
|
|
0, // e->rdevmajor
|
|
|
|
0, // e->rdevminor
|
|
|
|
(uint32_t) e.first.size() + 1,
|
|
|
|
0 // e->check
|
|
|
|
);
|
|
|
|
do_out(header, 110);
|
|
|
|
do_out(e.first.data(), e.first.size() + 1);
|
|
|
|
out_align();
|
|
|
|
if (e.second->filesize) {
|
|
|
|
do_out(e.second->data, e.second->filesize);
|
|
|
|
out_align();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Write trailer
|
|
|
|
sprintf(header, "070701%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
|
|
|
|
inode++, 0755, 0, 0, 1, 0, 0, 0, 0, 0, 0, 11, 0);
|
|
|
|
do_out(header, 110);
|
|
|
|
do_out("TRAILER!!!\0", 11);
|
|
|
|
out_align();
|
|
|
|
fclose(out);
|
2019-02-23 10:06:07 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 11:53:11 +00:00
|
|
|
void cpio::load_cpio(const char *file) {
|
2020-12-31 06:11:24 +00:00
|
|
|
fprintf(stderr, "Loading cpio: [%s]\n", file);
|
2021-11-30 09:50:55 +00:00
|
|
|
auto m = mmap_data(file);
|
|
|
|
load_cpio(reinterpret_cast<char *>(m.buf), m.sz);
|
2019-02-23 07:23:24 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 11:53:11 +00:00
|
|
|
void cpio::insert(string_view name, cpio_entry *e) {
|
|
|
|
auto it = entries.find(name);
|
|
|
|
if (it != entries.end()) {
|
|
|
|
it->second.reset(e);
|
2020-12-31 06:11:24 +00:00
|
|
|
} else {
|
2021-08-13 11:53:11 +00:00
|
|
|
entries.emplace(name, e);
|
2020-12-31 06:11:24 +00:00
|
|
|
}
|
2018-10-25 01:08:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 11:53:11 +00:00
|
|
|
void cpio::add(mode_t mode, const char *name, const char *file) {
|
2021-11-30 09:50:55 +00:00
|
|
|
auto m = mmap_data(file);
|
2021-08-13 11:53:11 +00:00
|
|
|
auto e = new cpio_entry(S_IFREG | mode);
|
2021-11-30 09:50:55 +00:00
|
|
|
e->filesize = m.sz;
|
|
|
|
e->data = xmalloc(m.sz);
|
|
|
|
memcpy(e->data, m.buf, m.sz);
|
2021-08-13 11:53:11 +00:00
|
|
|
insert(name, e);
|
2020-12-31 06:11:24 +00:00
|
|
|
fprintf(stderr, "Add entry [%s] (%04o)\n", name, mode);
|
2018-10-25 01:08:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 11:53:11 +00:00
|
|
|
void cpio::mkdir(mode_t mode, const char *name) {
|
|
|
|
insert(name, new cpio_entry(S_IFDIR | mode));
|
2020-12-31 06:11:24 +00:00
|
|
|
fprintf(stderr, "Create directory [%s] (%04o)\n", name, mode);
|
2018-10-25 01:08:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 11:53:11 +00:00
|
|
|
void cpio::ln(const char *target, const char *name) {
|
|
|
|
auto e = new cpio_entry(S_IFLNK);
|
2020-12-31 06:11:24 +00:00
|
|
|
e->filesize = strlen(target);
|
|
|
|
e->data = strdup(target);
|
2021-08-13 11:53:11 +00:00
|
|
|
insert(name, e);
|
2020-12-31 06:11:24 +00:00
|
|
|
fprintf(stderr, "Create symlink [%s] -> [%s]\n", name, target);
|
2018-11-07 07:10:38 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 11:53:11 +00:00
|
|
|
void cpio::mv(entry_map::iterator &it, const char *name) {
|
|
|
|
fprintf(stderr, "Move [%s] -> [%s]\n", it->first.data(), name);
|
|
|
|
auto e = it->second.release();
|
|
|
|
entries.erase(it);
|
|
|
|
insert(name, e);
|
2018-10-25 01:08:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 11:53:11 +00:00
|
|
|
bool cpio::mv(const char *from, const char *to) {
|
2020-12-31 06:11:24 +00:00
|
|
|
auto it = entries.find(from);
|
|
|
|
if (it != entries.end()) {
|
|
|
|
mv(it, to);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "Cannot find entry %s\n", from);
|
|
|
|
return false;
|
2018-10-25 01:08:06 +00:00
|
|
|
}
|
2019-02-23 07:23:24 +00:00
|
|
|
|
2021-11-30 03:56:37 +00:00
|
|
|
#define pos_align(p) p = align_to(p, 4)
|
2019-02-23 21:16:35 +00:00
|
|
|
|
2021-08-13 11:53:11 +00:00
|
|
|
void cpio::load_cpio(const char *buf, size_t sz) {
|
2020-12-31 06:11:24 +00:00
|
|
|
size_t pos = 0;
|
|
|
|
while (pos < sz) {
|
2021-08-13 11:53:11 +00:00
|
|
|
auto header = reinterpret_cast<const cpio_newc_header *>(buf + pos);
|
|
|
|
pos += sizeof(cpio_newc_header);
|
|
|
|
string_view name(buf + pos);
|
2020-12-31 06:11:24 +00:00
|
|
|
pos += x8u(header->namesize);
|
|
|
|
pos_align(pos);
|
2021-08-13 11:53:11 +00:00
|
|
|
if (name == "." || name == "..")
|
2020-12-31 06:11:24 +00:00
|
|
|
continue;
|
2021-08-13 11:53:11 +00:00
|
|
|
if (name == "TRAILER!!!")
|
2020-12-31 06:11:24 +00:00
|
|
|
break;
|
2021-08-13 11:53:11 +00:00
|
|
|
auto entry = new cpio_entry(header);
|
2020-12-31 06:11:24 +00:00
|
|
|
entry->data = xmalloc(entry->filesize);
|
|
|
|
memcpy(entry->data, buf + pos, entry->filesize);
|
|
|
|
pos += entry->filesize;
|
2021-08-13 11:53:11 +00:00
|
|
|
insert(name, entry);
|
2020-12-31 06:11:24 +00:00
|
|
|
pos_align(pos);
|
|
|
|
}
|
2019-02-23 21:16:35 +00:00
|
|
|
}
|