LensSerious 0.1
Lens-correction mathematics as data, not as a library of callbacks
Loading...
Searching...
No Matches
lensserious_import.c File Reference
#include "lensserious.h"
#include "lensserious_import.h"
#include "lensserious_db.h"
#include <lensfun.h>
#include <sqlite3.h>
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
+ Include dependency graph for lensserious_import.c:

Go to the source code of this file.

Data Structures

struct  name_ctx_t
 

Functions

static void die (const char *what, sqlite3 *db)
 
static void exec_or_die (sqlite3 *db, const char *sql)
 
static sqlite3_stmt * prep (sqlite3 *db, const char *sql)
 
static void step_reset (sqlite3 *db, sqlite3_stmt *st)
 
static void for_each_mlstr (const lfMLstr s, void(*fn)(const char *lang, const char *value, void *ud), void *ud)
 Walk an lfMLstr: the default string, then (lang, translation) pairs.
 
static void insert_name (const char *lang, const char *value, void *ud)
 
static int dist_model (enum lfDistortionModel m)
 
static int tca_model (enum lfTCAModel m)
 
static int lens_type (enum lfLensType t)
 
static long long mount_id_for (sqlite3 *db, sqlite3_stmt *sel, sqlite3_stmt *ins, const char *name)
 
static char * read_file (const char *path)
 
static int _load_directory (lfDatabase *ldb, const char *dirname)
 Load every XML file in a directory into ldb.
 
int ls_import_run (const char *schema_path, const char *out_path, const char *base_xml_dir, const char *xml_dir)
 Read a lensfun database and write it as a LensSerious SQLite file.
 

Variables

static const char * SCHEMA_PATH_HINT = "src/schema.sql"
 

Function Documentation

◆ _load_directory()

static int _load_directory ( lfDatabase * ldb,
const char * dirname )
static

Load every XML file in a directory into ldb.

Returns
non-zero on success.

lf_db_load_directory() does exactly this and is the obvious call, but it only arrived in liblensfun 0.3.3. Ubuntu 22.04 – which is what this project's consumers build their nightlies on – still ships 0.3.2, where the symbol does not exist and the link fails outright. So the directory walk is written out for that case.

The two loaders also report success in OPPOSITE conventions, which is worth being explicit about: lf_db_load() returns an lfError, where 0 means success, while lf_db_load_directory() and lf_db_load_file() return a cbool/lfError pair that do not agree with each other. Treating them alike made the directory path fail on a directory it had just read correctly – a path nothing exercised until a consumer started importing from fetched XML rather than from the installed database. This function returns one convention: non-zero for success.

Definition at line 264 of file lensserious_import.c.

Referenced by ls_import_run().

◆ die()

static void die ( const char * what,
sqlite3 * db )
static

Definition at line 67 of file lensserious_import.c.

Referenced by ls_import_run(), prep(), and step_reset().

◆ dist_model()

static int dist_model ( enum lfDistortionModel m)
static

Definition at line 174 of file lensserious_import.c.

References LS_DIST_NONE, LS_DIST_POLY3, LS_DIST_POLY5, and LS_DIST_PTLENS.

Referenced by ls_import_run().

◆ exec_or_die()

static void exec_or_die ( sqlite3 * db,
const char * sql )
static

Definition at line 74 of file lensserious_import.c.

Referenced by ls_import_run().

◆ for_each_mlstr()

static void for_each_mlstr ( const lfMLstr s,
void(* fn )(const char *lang, const char *value, void *ud),
void * ud )
static

Walk an lfMLstr: the default string, then (lang, translation) pairs.

Definition at line 102 of file lensserious_import.c.

Referenced by ls_import_run().

◆ insert_name()

static void insert_name ( const char * lang,
const char * value,
void * ud )
static

◆ lens_type()

◆ ls_import_run()

int ls_import_run ( const char * schema_path,
const char * out_path,
const char * base_xml_dir,
const char * xml_dir )

Read a lensfun database and write it as a LensSerious SQLite file.

Parameters
schema_paththe SQL schema to create the file from (src/schema.sql).
out_pathwhere to write. The file is written to a temporary and RENAMED into place, so a reader opening it with immutable=1 never sees a partial database and a killed run cannot leave one that looks complete. See lensserious_db.h.
base_xml_dira directory of lensfun XML to load FIRST, or NULL. Everything loaded afterwards can override a lens defined here, and a lens nobody else defines survives – lensfun's own "later definitions override earlier" rule, applied by lensfun.

This is what makes an update additive rather than a replacement. A consumer that ships a database passes its own calibrations here and the machine's profiles are merged over the top; without it, a machine with no system-wide lensfun yields a database holding only the few profiles its user wrote by hand, which is a catastrophic thing to install in place of fifteen hundred lenses.

Parameters
xml_dira directory of lensfun XML to read, or NULL.

NULL is the interesting value, and is what a consumer's updater wants: it calls lensfun's own lf_db_load(), which searches all four standard locations – the system database, the system updates directory, the user's updates directory, and the user's own ~/.local/share/lensfun. That last one is where hand-written profiles live, and the aggregation and override rules between the four are lensfun's, not this project's approximation of them. Passing a directory instead reads exactly that directory, which is what a reproducible package build wants, since a build machine has no user profiles.

Returns
0 on success, non-zero on failure (already reported on stderr).
Warning
Not thread-safe and not re-entrant: this is a batch conversion that exits the process on a database error, which is the right contract for the two command-line tools that call it and the wrong one for anything else.

Definition at line 291 of file lensserious_import.c.

References _load_directory(), die(), dist_model(), exec_or_die(), for_each_mlstr(), insert_name(), name_ctx_t::kind, lens_type(), LS_VIG_PA, mount_id_for(), prep(), read_file(), SCHEMA_PATH_HINT, step_reset(), and tca_model().

Referenced by main().

◆ mount_id_for()

static long long mount_id_for ( sqlite3 * db,
sqlite3_stmt * sel,
sqlite3_stmt * ins,
const char * name )
static

Definition at line 213 of file lensserious_import.c.

References step_reset().

Referenced by ls_import_run().

◆ prep()

static sqlite3_stmt * prep ( sqlite3 * db,
const char * sql )
static

Definition at line 85 of file lensserious_import.c.

References die().

Referenced by ls_import_run().

◆ read_file()

static char * read_file ( const char * path)
static

Definition at line 230 of file lensserious_import.c.

Referenced by ls_import_run().

◆ step_reset()

static void step_reset ( sqlite3 * db,
sqlite3_stmt * st )
static

Definition at line 92 of file lensserious_import.c.

References die().

Referenced by insert_name(), ls_import_run(), and mount_id_for().

◆ tca_model()

static int tca_model ( enum lfTCAModel m)
static

Definition at line 185 of file lensserious_import.c.

References LS_TCA_LINEAR, LS_TCA_NONE, and LS_TCA_POLY3.

Referenced by ls_import_run().

Variable Documentation

◆ SCHEMA_PATH_HINT

const char* SCHEMA_PATH_HINT = "src/schema.sql"
static

Definition at line 63 of file lensserious_import.c.

Referenced by ls_import_run().