Apophis
view release on metacpan or search on metacpan
# undef stat
# undef unlink
# undef rename
# undef open
# undef close
# undef read
# undef write
/* Windows mkdir takes only one arg */
# define apophis_mkdir(p, m) _mkdir(p)
/*
* Stat_t is struct w32_stat, the layout perl's own stat() fills in - but
* that stat() is the macro undef'd just above, so what is left is the CRT
* one, which writes a differently shaped struct. Pair the call and the
* type instead: _stat64 keeps st_size 64 bit, which the CRT's struct stat
* does not on Win64.
*/
# define apophis_stat_t struct _stat64
# define apophis_stat(p, b) _stat64((p), (b))
#else
# define apophis_mkdir(p, m) mkdir(p, m)
# define apophis_stat_t struct stat
# define apophis_stat(p, b) stat((p), (b))
#endif
/*
* The CRT rename() refuses a destination that already exists, so the second
* store of the same content with metadata could not replace its sidecar.
* MoveFileEx replaces, and stays atomic.
*/
static int
apophis_rename(const char *from, const char *to)
{
apophis_meta_read(pTHX_ const char *meta_path)
{
PerlIO *fh;
HV *meta;
apophis_stat_t st;
char *buf, *p, *end;
SSize_t nread;
/* A directory opens, and on the BSDs, Cygwin and the Hurd reads
* too, so it is refused by its mode rather than by the read. */
if (apophis_stat(meta_path, &st) != 0 || S_ISDIR(st.st_mode)) return NULL;
fh = PerlIO_open(meta_path, "r");
if (!fh) return NULL;
buf = (char *)malloc((size_t)st.st_size + 1);
/*
# uncoverable branch true
# uncoverable condition left count:1 */
if (!buf) { PerlIO_close(fh); return NULL; }
char path[APOPHIS_PATH_MAX];
int path_len;
apophis_stat_t st;
apophis_identify_content(uuid, ns, content, content_len);
horus_format_uuid(id_str, uuid, HORUS_FMT_STR);
store_dir = apophis_get_store_dir(aTHX_ hv, opts, &store_dir_len);
path_len = apophis_path(path, sizeof(path), store_dir, store_dir_len, id_str, HORUS_FMT_STR_LEN);
if (apophis_stat(path, &st) != 0) {
apophis_ensure_parent_dir(path);
apophis_atomic_write(aTHX_ path, content, content_len);
}
if (meta) {
char meta_path[APOPHIS_PATH_MAX];
apophis_build_meta_path(meta_path, sizeof(meta_path), path, path_len);
apophis_meta_write(aTHX_ meta_path, meta);
}
}
static bool
apophis_do_exists(pTHX_ const char *who, SV *self, SV *id, SV **args, I32 items)
{
const char *id_str;
STRLEN id_len;
char path[APOPHIS_PATH_MAX];
apophis_stat_t st;
apophis_id_path(aTHX_ who, apophis_self_hv(aTHX_ who, self), id, args, items, path, sizeof(path), &id_str, &id_len);
return apophis_stat(path, &st) == 0;
}
/* A new reference to the content, or &PL_sv_undef when there is none. */
static SV *
apophis_do_fetch(pTHX_ const char *who, SV *self, SV *id, SV **args, I32 items)
{
const char *id_str;
STRLEN id_len;
char path[APOPHIS_PATH_MAX];
apophis_stat_t st;
PerlIO *fh;
SV *content;
SSize_t nread;
apophis_id_path(aTHX_ who, apophis_self_hv(aTHX_ who, self), id, args, items, path, sizeof(path), &id_str, &id_len);
if (apophis_stat(path, &st) != 0)
return &PL_sv_undef;
if (S_ISDIR(st.st_mode))
croak("%s: read error on '%s': %s", who, path, strerror(EISDIR));
fh = PerlIO_open(path, "rb");
if (!fh)
croak("%s: cannot open '%s': %s", who, path, strerror(errno));
content = newSV((STRLEN)st.st_size + 1);
SvPOK_on(content);
content_sv = SvRV(*svp);
content = SvPV(content_sv, content_len);
apophis_identify_content(uuid, ns, content, content_len);
horus_format_uuid(id_str, uuid, HORUS_FMT_STR);
apophis_path(path, sizeof(path),
store_dir, store_dir_len,
id_str, HORUS_FMT_STR_LEN);
if (apophis_stat(path, &st) != 0) {
apophis_ensure_parent_dir(path);
apophis_atomic_write(aTHX_ path, content, content_len);
}
PUSHs(sv_2mortal(newSVpvn(id_str, HORUS_FMT_STR_LEN)));
}
# ------------------------------------------------------------------ #
# find_missing(\@ids, %opts) -> @missing_ids #
# ------------------------------------------------------------------ #
STRLEN id_len;
char path[APOPHIS_PATH_MAX];
apophis_stat_t st;
if (!svp || !SvOK(*svp)) continue;
id_str = SvPV(*svp, id_len);
apophis_path(path, sizeof(path),
store_dir, store_dir_len, id_str, id_len);
if (apophis_stat(path, &st) != 0) {
XPUSHs(sv_2mortal(newSVpvn(id_str, id_len)));
}
}
# ------------------------------------------------------------------ #
# meta($id, %opts) -> \%meta or undef #
# ------------------------------------------------------------------ #
SV *
meta(self, id, ...)
( run in 1.123 second using v1.01-cache-2.11-cpan-500dd33cbaf )