No description
  • C 36.5%
  • Python 34.9%
  • Makefile 18.6%
  • Rust 7.9%
  • Shell 1.2%
  • Other 0.9%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Giuseppe Scrivano b465698c1d
Merge pull request #173 from giuseppe/fix-fuzzing
fuzzing: fix honggfuzz build by linking libzstd
2026-07-01 16:05:23 +02:00
.github/workflows src: replace YAJL with json-c for JSON parsing and generation 2026-05-08 13:06:53 +02:00
build-aux build: add make syntax-check 2017-07-10 17:30:31 +02:00
image-spec@26647a49f6 image-spec: update 2025-12-09 11:41:11 +01:00
runtime-spec@d64c1d945d runtime-spec: update 2025-12-11 09:44:44 +01:00
src json_common: disable slash escaping 2026-07-01 21:36:43 +09:00
tests fuzzing: fix honggfuzz build by linking libzstd 2026-07-01 15:11:47 +02:00
.gitignore gitignore: update 2025-12-15 13:44:40 +01:00
.gitmodules src: replace YAJL with json-c for JSON parsing and generation 2026-05-08 13:06:53 +02:00
autogen.sh src: replace YAJL with json-c for JSON parsing and generation 2026-05-08 13:06:53 +02:00
Cargo.lock switch default type fields to public 2021-04-10 14:31:47 +05:30
Cargo.toml Add rust port for libocispec 2021-04-06 23:43:58 +05:30
cfg.mk parser: allocate empty arrays 2022-08-25 12:42:52 +02:00
CODE-OF-CONDUCT.md Code of Conduct 2020-02-08 23:48:14 +01:00
configure.ac src: replace YAJL with json-c for JSON parsing and generation 2026-05-08 13:06:53 +02:00
COPYING COPYING: add bison-like exception 2021-03-15 15:55:56 +01:00
GNUmakefile build: add make syntax-check 2017-07-10 17:30:31 +02:00
maint.mk parser: allocate empty arrays 2022-08-25 12:42:52 +02:00
Makefile.am src: replace YAJL with json-c for JSON parsing and generation 2026-05-08 13:06:53 +02:00
ocispec.pc.in src: replace YAJL with json-c for JSON parsing and generation 2026-05-08 13:06:53 +02:00
README.md src: replace YAJL with json-c for JSON parsing and generation 2026-05-08 13:06:53 +02:00
rust-gen.js switch default type fields to public 2021-04-10 14:31:47 +05:30
SECURITY.md Add Security Policy 2020-05-11 14:17:21 -04:00

libocispec

Build Status

A library for easily parsing of OCI runtime and OCI image files from C, and generate json string from corresponding struct.

The parser is generated directly from the JSON schema in the source repository.

Installation

Expects json-c (>= 0.14) to be installed and linkable.

$ ./autogen.sh
$ ./configure
$ make
$ sudo make install

Parsing an OCI configuration file is easy as:

    #include <config.h>
    #include <runtime_spec_schema_config_schema.h>

    runtime_spec_schema_config_schema *container = runtime_spec_schema_config_schema_parse_file ("config.json", NULL, &err);

    if (container == NULL)
      exit (EXIT_FAILURE);

    /* Print the container hostname.  */
    if (container->hostname)
        printf ("The specified hostname is %s\n", container->hostname);

    for (size_t i; i < container->mounts_len; i++)
        printf ("Mounting to %s\n", container->mounts[i]->destination);

    printf ("Running as user ID and GID %d %d\n", container->process->user->uid, container->process->user->gid);

Generating an OCI configuration json string is also easy as:

    #include <config.h>
    #include <runtime_spec_schema_config_schema.h>

    runtime_spec_schema_config_schema container;
    char *json_buf = NULL;

    memset (&container, 0, sizeof (runtime_spec_schema_config_schema));

    container.oci_version = "2";
    container.hostname = "ubuntu";
    /* Add other configuration. */
    /* ... ... */

    json_buf = runtime_spec_schema_config_schema_generate_json (&container, NULL, &err);
    if (json_buf == NULL)
      exit (EXIT_FAILURE);

    printf ("The generated json string is:\n%s\n", json_buf);

Rust Bindings

libocispec supports rust bindings as well. You can use it directly by adding it as dependency to Cargo.toml or generate fresh types using make generate-rust

[dependencies]
libocispec = { git = "https://github.com/containers/libocispec" }

for Cargo version older than 0.51.0 specify branch explicitly

[dependencies]
libocispec = { git = "https://github.com/containers/libocispec", branch = "main" }

Example usage

extern crate libocispec;
use libocispec::runtime;
use libocispec::image;

fn main() {
    let runtime_spec = match runtime::Spec::load("path/to/spec") {
        Ok(spec) => spec,
        Err(e) => panic!("{}", e),
    }
    let image_spec = match image::ImageConfig::load("path/to/spec") {
        Ok(spec) => spec,
        Err(e) => panic!("{}", e),
    }
}