Skip to content

Commit

Permalink
Switch to gdbm from generic ndbm
Browse files Browse the repository at this point in the history
  • Loading branch information
tbodt committed Jan 5, 2018
1 parent a2bf5a8 commit d738dc4
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
build
build/
build-gcc/
xcuserdata/
tags
Expand Down
48 changes: 31 additions & 17 deletions fs/fake.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
#include <limits.h>
#ifndef GDBM_NDBM
#include <ndbm.h>
#else
#include <gdbm-ndbm.h>
#endif
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <gdbm.h>

#include "debug.h"
#include "kernel/errno.h"
Expand All @@ -20,16 +16,27 @@ struct ish_stat {
dword_t rdev;
};

static DBM *get_db(struct mount *mount) {
DBM *db = mount->data;
static void gdbm_fatal(const char *thingy) {
println("fatal gdbm error: %s", thingy);
abort();
}

static noreturn void gdbm_err(GDBM_FILE db) {
println("gdbm error: %s", gdbm_db_strerror(db));
abort();
}

static GDBM_FILE get_db(struct mount *mount) {
GDBM_FILE db = mount->data;
if (db == NULL) {
char db_path[PATH_MAX];
strcpy(db_path, mount->source);
char *basename = strrchr(db_path, '/') + 1;
assert(strcmp(basename, "data") == 0);
strncpy(basename, "meta", 4);
db = dbm_open(db_path, O_RDWR, 0666);
assert(db != NULL);
strncpy(basename, "meta.db", 7);
db = gdbm_open(db_path, 0, GDBM_WRITER, 0, gdbm_fatal);
if (db == NULL)
gdbm_err(db);
mount->data = db;
}
return db;
Expand All @@ -46,19 +53,26 @@ static datum build_key(char *keydata, const char *path, const char *type) {
static datum read_meta(struct mount *mount, const char *path, const char *type) {
char keydata[MAX_PATH+strlen(type)+1];
datum key = build_key(keydata, path, type);
return dbm_fetch(get_db(mount), key);
GDBM_FILE db = get_db(mount);
datum value = gdbm_fetch(db, key);
if (value.dptr == NULL && gdbm_last_errno(db) != GDBM_ITEM_NOT_FOUND)
gdbm_err(db);
return value;
}

static void write_meta(struct mount *mount, const char *path, const char *type, datum data) {
char keydata[MAX_PATH+strlen(type)+1];
datum key = build_key(keydata, path, type);
dbm_store(get_db(mount), key, data, DBM_REPLACE);
if (gdbm_store(get_db(mount), key, data, GDBM_REPLACE) == -1)
gdbm_err(get_db(mount));
}

static int delete_meta(struct mount *mount, const char *path, const char *type) {
static void delete_meta(struct mount *mount, const char *path, const char *type) {
char keydata[MAX_PATH+strlen(type)+1];
datum key = build_key(keydata, path, type);
return !dbm_delete(get_db(mount), key);
GDBM_FILE db = get_db(mount);
if (gdbm_delete(db, key) == -1 && gdbm_last_errno(db) != GDBM_ITEM_NOT_FOUND)
gdbm_err(db);
}

static int read_stat(struct mount *mount, const char *path, struct ish_stat *stat) {
Expand All @@ -77,8 +91,8 @@ static void write_stat(struct mount *mount, const char *path, struct ish_stat *s
write_meta(mount, path, "meta", data);
}

static int delete_stat(struct mount *mount, const char *path) {
return delete_meta(mount, path, "meta");
static void delete_stat(struct mount *mount, const char *path) {
delete_meta(mount, path, "meta");
}

static struct fd *fakefs_open(struct mount *mount, const char *path, int flags, int mode) {
Expand Down Expand Up @@ -235,7 +249,7 @@ static int fakefs_mount(struct mount *mount) {

static int fakefs_umount(struct mount *mount) {
if (mount->data)
dbm_close(mount->data);
gdbm_close(mount->data);
/* return realfs.umount(mount); */
return 0;
}
Expand Down
19 changes: 4 additions & 15 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,7 @@ cc = meson.get_compiler('c')
threads = dependency('threads')
librt = cc.find_library('rt', required: false)

# this is annoying af
if host_machine.system() == 'darwin'
# no need to link to anything, berkeley db and its ndbm shim is part of the C library
dbm = []
elif host_machine.system() == 'linux'
# we're going to use gdbm
dbm = [cc.find_library('gdbm'), cc.find_library('gdbm_compat')]
if not cc.has_header('ndbm.h') and cc.has_header('gdbm-ndbm.h')
# slight problem: on debian the header is called gdbm-ndbm.h for some reason
add_project_arguments('-DGDBM_NDBM=1', language: 'c')
endif
endif

gdbm = subproject('gdbm').get_variable('gdbm')
softfloat = subproject('softfloat').get_variable('softfloat_dep')

subdir('vdso') # ish depends on the vdso
Expand Down Expand Up @@ -93,15 +81,16 @@ src = [

libish = library('ish', src,
include_directories: includes,
dependencies: [librt, threads, softfloat, dbm])
dependencies: [librt, threads, softfloat, gdbm])
ish = declare_dependency(
link_with: libish,
dependencies: [librt, threads, softfloat, gdbm],
include_directories: includes)

# ptraceomatic et al
subdir('tools')

executable('ish', ['main.c'], dependencies: [ish, softfloat, threads, dbm])
executable('ish', ['main.c'], dependencies: ish)

gdb_scripts = ['ish-gdb.gdb']
foreach script : gdb_scripts
Expand Down
1 change: 1 addition & 0 deletions subprojects/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
softfloat
gdbm-1.14.1
10 changes: 10 additions & 0 deletions subprojects/gdbm.wrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[wrap-file]
directory = gdbm-1.14.1

source_url = ftp://ftp.gnu.org/gnu/gdbm/gdbm-1.14.1.tar.gz
source_filename = gdbm-1.14.1.tar.gz
source_hash = cdceff00ffe014495bed3aed71c7910aa88bf29379f795abc0f46d4ee5f8bc5f

patch_url = https://wrapdb.mesonbuild.com/v1/projects/gdbm/1.14.1/1/get_zip
patch_filename = gdbm-1.14.1-1-wrap.zip
patch_hash = fdc942bd3101c35dfaabaab018d251e597bfc677a303c4a29fb1d09084e5d433
4 changes: 2 additions & 2 deletions tools/fakefsify.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import struct
import urllib.request
import tarfile
import dbm
import dbm.gnu

def extract_archive(archive, db):
for member in archive.getmembers():
Expand Down Expand Up @@ -59,5 +59,5 @@ def extract_archive(archive, db):

with open(archive_path, 'rb') as archive:
with tarfile.open(fileobj=archive) as archive:
with dbm.ndbm.open(str(fs/'meta'), 'c') as db:
with dbm.gnu.open(str(fs/'meta.db'), 'c') as db:
extract_archive(archive, db)
3 changes: 1 addition & 2 deletions tools/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ if build_machine.system() == 'linux'
'ptutil.c',
cified_vdso,
]
executable('ptraceomatic', ['ptraceomatic.c', 'undefined-flags.c', transplant_src],
dependencies: [ish, softfloat, threads, dbm])
executable('ptraceomatic', ['ptraceomatic.c', 'undefined-flags.c', transplant_src], dependencies: [ish])
configure_file(input: 'ptraceomatic-gdb.gdb', output: '@PLAINNAME@', configuration: configuration_data())

# tools for messing with vdsos
Expand Down

0 comments on commit d738dc4

Please sign in to comment.