Skip to content

Commit

Permalink
Merge pull request #321 from hsiangkao/turbooci-erofs-fix
Browse files Browse the repository at this point in the history
Fix EROFS support of TurboOCI-apply
  • Loading branch information
BigVan authored Apr 1, 2024
2 parents 8fbcd89 + 232febb commit 654b344
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
14 changes: 9 additions & 5 deletions src/overlaybd/tar/tarerofs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#define __stringify_1(x...) #x
#define __stringify(x...) __stringify_1(x)

#define LSMT_ALIGNMENT 512

int TarErofs::extract_all() {
ssize_t read;
struct stat st;
Expand All @@ -44,7 +46,7 @@ int TarErofs::extract_all() {
return -1;
}

if (fs_base_file->fstat(&st)>= 0 && st.st_blocks > 0) {
if (!first_layer) {
int fd = mkstemp(base_path);
if (fd < 0) {
LOG_ERROR("cannot generate a temporary file to dump overlaybd disk");
Expand All @@ -53,11 +55,12 @@ int TarErofs::extract_all() {
std::strcat(command_line, " --base ");
std::strcat(command_line, base_path);

if (fs_base_file->pread(&metasize, sizeof(metasize), 0) !=
sizeof(metasize)) {
// lsmt.pread should align to 512
if (fs_base_file->pread(&buf, LSMT_ALIGNMENT, 0) != LSMT_ALIGNMENT) {
LOG_ERROR("failed to read EROFS metadata size");
return -1;
}
metasize = *(uint64_t *)buf;

while (metasize) {
int count = std::min(sizeof(buf), metasize);
Expand Down Expand Up @@ -89,6 +92,9 @@ int TarErofs::extract_all() {
}
status = pclose(fp);

if (!first_layer)
unlink(base_path);

if (read < 0 || status) {
return -1;
}
Expand All @@ -110,10 +116,8 @@ int TarErofs::extract_all() {
read = -1;
break;
}
metasize += read;
}


/* write mapfile */
fp = fopen("upper.map", "r");
if (fp == NULL) {
Expand Down
5 changes: 3 additions & 2 deletions src/overlaybd/tar/tarerofs.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
class TarErofs {
public:
TarErofs(photon::fs::IFile *file, photon::fs::IFile *target, uint64_t fs_blocksize = 4096,
photon::fs::IFile *bf = nullptr, bool meta_only = true)
: file(file), fout(target), fs_base_file(bf), meta_only(meta_only) {}
photon::fs::IFile *bf = nullptr, bool meta_only = true, bool first_layer = true)
: file(file), fout(target), fs_base_file(bf), meta_only(meta_only), first_layer(first_layer) {}

int extract_all();

Expand All @@ -31,6 +31,7 @@ class TarErofs {
photon::fs::IFile *fout = nullptr; // target
photon::fs::IFile *fs_base_file = nullptr;
bool meta_only;
bool first_layer;
std::set<std::string> unpackedPaths;
std::list<std::pair<std::string, int>> dirs; // <path, utime>
};
8 changes: 7 additions & 1 deletion src/tools/turboOCI-apply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,14 @@ int main(int argc, char **argv) {
if (fstype == "erofs") {
photon::fs::IFile* base_file = raw ? nullptr : ((ImageFile *)imgfile)->get_base();

ImageConfigNS::ImageConfig cfg;
if (!cfg.ParseJSON(image_config_path)) {
fprintf(stderr, "failed to parse image config\n");
exit(-1);
}

auto tar =
new TarErofs(src_file, imgfile, 4096, base_file, true);
new TarErofs(src_file, imgfile, 4096, base_file, true, cfg.lowers().size() == 0);

if (tar->extract_all() < 0) {
fprintf(stderr, "failed to extract\n");
Expand Down

0 comments on commit 654b344

Please sign in to comment.