Chandra

 view release on metacpan or  search on metacpan

include/chandra/chandra_store.h  view on Meta::CPAN

    if (!fh) return;  /* File doesn't exist yet - start fresh */

    fd = fileno(fh);
    flock(fd, LOCK_SH);

    fseek(fh, 0, SEEK_END);
    size = ftell(fh);
    fseek(fh, 0, SEEK_SET);

    if (size <= 0) {
        flock(fd, LOCK_UN);
        fclose(fh);
        return;
    }

    Newx(buf, size + 1, char);
    fread(buf, 1, (size_t)size, fh);
    buf[size] = '\0';

    flock(fd, LOCK_UN);
    fclose(fh);

    json_sv = sv_2mortal(newSVpvn(buf, (STRLEN)size));
    Safefree(buf);

    new_data = chandra_store_decode(aTHX_ json_sv, path);
    if (new_data) {
        SV **old_svp = hv_fetchs(self_hv, "_data", 0);
        if (old_svp && SvROK(*old_svp)) {
            /* Replace _data with freshly decoded HV */
            (void)hv_stores(self_hv, "_data", newRV_noinc((SV *)new_data));
        } else {
            (void)hv_stores(self_hv, "_data", newRV_noinc((SV *)new_data));
        }
    }
    /* If new_data is NULL, we leave _data as-is (start fresh, already set in new) */
}

/* ============================================================================
 * Constructor helper - build default path from name
 * ============================================================================ */

static SV *
chandra_store_default_path(pTHX_ const char *name)
{
    const char *home = getenv("HOME");
    SV *path_sv;

#ifdef _WIN32
    if (!home || !*home) {
        home = getenv("USERPROFILE");
    }
    if (!home || !*home) {
        home = getenv("APPDATA");
    }
    if (!home || !*home) {
        home = "C:\\\\";
    }
#else
    if (!home || !*home) {
        /* Fall back to getpwuid */
        struct passwd *pw = getpwuid(getuid());
        home = pw ? pw->pw_dir : "/tmp";
    }
#endif

    path_sv = newSVpvf("%s/.chandra/%s/store.json", home, name);
    return path_sv;
}

#endif /* CHANDRA_XS_IMPLEMENTATION */
#endif /* CHANDRA_STORE_H */



( run in 1.318 second using v1.01-cache-2.11-cpan-995e09ba956 )