-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mksquashfs can read uid/gid/file type from fakeroot(1) database
With this patch I can easily pack chroots built in a fakeroot environment without running mksquashfs under fakeroot. Instead I can use `-fakerootdb .fakedata` to get ownership/permissions/device nodes right. It's possible to convert fakeroot database into mksquashfs' pseudo files definitions but using the fakeroot database directly is more efficient (fakeroot uses device/inode number to identify the object) and requires less code.
- Loading branch information
1 parent
57930cf
commit d132b9b
Showing
4 changed files
with
152 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <errno.h> | ||
#include "fakerootdb.h" | ||
|
||
#define FAKEROOT_ENTRY_FIELDS 7 | ||
#define FAKEROOT_ENTRY_FMT "dev=%lx,ino=%lu,mode=%o,uid=%u,gid=%u,nlink=%lu,rdev=%lu" | ||
|
||
static int compare_by_dev_ino(const void *px, const void *py) | ||
{ | ||
struct stat const* const x = px; | ||
struct stat const* const y = py; | ||
|
||
if (x->st_dev < y->st_dev) | ||
return -1; | ||
else if (x->st_dev > y->st_dev) | ||
return 1; | ||
else if (x->st_ino < y->st_ino) | ||
return -1; | ||
else if (x->st_ino > y->st_ino) | ||
return 1; | ||
else | ||
return 0; | ||
} | ||
|
||
int fakeroot_read_db(FILE *fakedata, struct fakerootdb *db) | ||
{ | ||
struct stat elt, *d; | ||
int n; | ||
if (!db) | ||
return -EINVAL; | ||
if (db->db) { | ||
free(db->db); | ||
db->db = NULL; | ||
db->count = 0; | ||
} | ||
while (!feof(fakedata)) { | ||
if (ferror(fakedata)) | ||
return -EIO; | ||
memset(&elt, 0, sizeof(elt)); | ||
n = fscanf(fakedata, | ||
FAKEROOT_ENTRY_FMT "\n", | ||
&elt.st_dev, | ||
&elt.st_ino, | ||
&elt.st_mode, | ||
&elt.st_uid, | ||
&elt.st_gid, | ||
&elt.st_nlink, | ||
&elt.st_rdev); | ||
if (n != FAKEROOT_ENTRY_FIELDS) | ||
return -EINVAL; | ||
|
||
/* skip uid = gid = 0 entries, unless they are device nodes. | ||
* fakeroot assumes uid = gid = 0 by default */ | ||
if (elt.st_uid == 0 && elt.st_gid == 0 && elt.st_rdev == 0) | ||
continue; | ||
|
||
d = realloc(db->db, (db->count + 1)*sizeof(elt)); | ||
if (!d) | ||
return -ENOMEM; | ||
memcpy(&d[db->count], &elt, sizeof(elt)); | ||
db->db = d; | ||
db->count += 1; | ||
} | ||
qsort(db->db, db->count, sizeof(elt), compare_by_dev_ino); | ||
return 0; | ||
} | ||
|
||
void fakeroot_override_stat(struct stat *st, const struct fakerootdb *db) | ||
{ | ||
struct stat key; | ||
struct stat const* o = NULL; | ||
if (!db|| !db->db || db->count == 0) | ||
return; | ||
memset(&key, 0, sizeof(key)); | ||
key.st_dev = st->st_dev; | ||
key.st_ino = st->st_ino; | ||
o = bsearch(&key, db->db, db->count, sizeof(key), compare_by_dev_ino); | ||
if (o) { | ||
st->st_mode = o->st_mode; | ||
st->st_uid = o->st_uid; | ||
st->st_gid = o->st_gid; | ||
st->st_rdev = o->st_rdev; | ||
} else { | ||
/* fakeroot sets uid=gid=0 if the object is not in the DB */ | ||
st->st_uid = 0; | ||
st->st_gid = 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef SQUASHFS_FAKEROOTDB_H | ||
#define SQUASHFS_FAKEROOTDB_H | ||
#include <stdio.h> | ||
#include <sys/types.h> | ||
#include <sys/stat.h> | ||
|
||
struct fakerootdb { | ||
struct stat *db; | ||
size_t count; | ||
}; | ||
|
||
int fakeroot_read_db(FILE *fakedata, struct fakerootdb *db); | ||
|
||
void fakeroot_override_stat(struct stat *st, const struct fakerootdb *fakerootdb); | ||
|
||
#endif /* SQUASHFS_FAKEROOTDB_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters