NEOERR *hdf_write_file_atomic (HDF *hdf, const char *path) { NEOERR *err; FILE *fp; char tpath[PATH_BUF_SIZE]; static int count = 0; snprintf(tpath, sizeof(tpath), "%s.%5.5f.%d", path, ne_timef(), count++); fp = fopen(tpath, "w"); if (fp == NULL) return nerr_raise_errno (NERR_IO, "Unable to open %s for writing", tpath); err = hdf_dump_format (hdf, 0, fp); fclose (fp); if (err) { unlink(tpath); return nerr_pass(err); } if (rename(tpath, path) == -1) { unlink (tpath); return nerr_raise_errno (NERR_IO, "Unable to rename file %s to %s", tpath, path); } return STATUS_OK; }
bool mtc_msg(const char *func, const char *file, long line, int level, const char *format, ...) { //int dftlv = hdf_get_int_value(g_cfg, PRE_CONFIG".trace_level", TC_DEFAULT_LEVEL); if (level > m_dftlv) return false; if (g_fp == NULL) return false; va_list ap; char tm[25] = {0}; double usec = ne_timef(); time_t sec = (time_t)usec; //mutil_getdatetime(tm, sizeof(tm), "%Y-%m-%d %H:%M:%S", time(NULL)); struct tm *stm = localtime(&sec); strftime(tm, 25, "%Y-%m-%d %H:%M:%S", stm); tm[24] = '\0'; fprintf(g_fp, "[%s %f]", tm, usec); fprintf(g_fp, "[%s]", g_trace_level[level]); fprintf(g_fp, "[%s:%li %s] ", file, line, func); va_start(ap, (void*)format); vfprintf(g_fp, format, ap); va_end(ap); fprintf(g_fp, "\n"); trace_shift_file(); return true; }
int main(int argc, char *argv[]) { NEOERR *err; HDF *hdf; double tstart = 0; err = hdf_init(&hdf); if (err != STATUS_OK) { nerr_log_error(err); return -1; } tstart = ne_timef(); TestSort(hdf); ne_warn("sort took %5.5fs", ne_timef() - tstart); hdf_dump(hdf, NULL); hdf_destroy(&hdf); return 0; }
int main(int argc, char *argv[]) { NEOERR *err; HDF *hdf; int x; char name[256]; char value[256]; double tstart = 0; err = hdf_init(&hdf); DIE_NOT_OK(err); err = hdf_set_value (hdf, "Beware", "1"); DIE_NOT_OK(err); err = hdf_set_value (hdf, "Beware.The", "2"); DIE_NOT_OK(err); err = hdf_set_valuef (hdf, "Beware.The.%s=%d", "Ides", 3); DIE_NOT_OK(err); err = hdf_set_value (hdf, "Beware.Off", "4"); DIE_NOT_OK(err); err = hdf_set_value (hdf, "Beware.The.Ides.Of", "5"); DIE_NOT_OK(err); err = hdf_set_value (hdf, "Beware.The.Butter", "6"); DIE_NOT_OK(err); err = hdf_set_attr (hdf, "Beware.The.Butter", "Lang", "en"); DIE_NOT_OK(err); err = hdf_set_attr (hdf, "Beware.The.Butter", "Lang", "1"); DIE_NOT_OK(err); err = hdf_set_attr (hdf, "Beware.The.Butter", "Lang", NULL); DIE_NOT_OK(err); err = hdf_read_file (hdf, "test.hdf"); DIE_NOT_OK(err); hdf_dump(hdf, NULL); x = hdf_get_int_value (hdf, "Beware.The.Ides", 0); if (x != 3) { ne_warn("hdf_get_int_value returned %d, expected 3", x); return -1; } /* test symlinks */ { const char *v; err = hdf_set_value(hdf, "Destination.Foo", "bar"); DIE_NOT_OK(err); err = hdf_set_symlink(hdf, "Symlink.baz", "Destination.Foo"); DIE_NOT_OK(err); v = hdf_get_value(hdf, "Symlink.baz", "notfound"); if (strcmp(v, "bar")) { ne_warn("hdf_get_value through symlink returned %s, expected bar", v); return -1; } err = hdf_set_value(hdf, "Symlink.baz", "newvalue"); DIE_NOT_OK(err); v = hdf_get_value(hdf, "Symlink.baz", "notfound"); if (strcmp(v, "newvalue")) { ne_warn("hdf_get_value through symlink returned %s, expected newvalue", v); return -1; } err = hdf_set_value(hdf, "Symlink.baz.too", "newtoo"); DIE_NOT_OK(err); v = hdf_get_value(hdf, "Symlink.baz.too", "newtoo"); if (strcmp(v, "newtoo")) { ne_warn("hdf_get_value through symlink returned %s, expected newtoo", v); return -1; } v = hdf_get_value(hdf, "Destination.Foo.too", "newtoo"); if (strcmp(v, "newtoo")) { ne_warn("hdf_get_value through symlink returned %s, expected newtoo", v); return -1; } } for (x = 0; x < 10000; x++) { rand_name(name, sizeof(name)); neo_rand_word(value, sizeof(value)); /* ne_warn("Setting %s = %s", name, value); */ err = hdf_set_value (hdf, name, value); DIE_NOT_OK(err); } tstart = ne_timef(); hdf_sort_obj(hdf, sortByName); ne_warn("sort took %5.5fs", ne_timef() - tstart); hdf_dump(hdf, NULL); hdf_destroy(&hdf); return 0; }
NEOERR * rcfs_save (const char *path, const char *data, const char *user, const char *log) { NEOERR *err; HDF *meta = NULL, *vers; char fpath[_POSIX_PATH_MAX]; char buf[256]; int version = 0; int fd; int lock; int x, l, w; err = rcfs_lock (path, &lock); if (err) return nerr_pass (err); do { err = rcfs_meta_load (path, &meta); if (err && nerr_handle (&err, NERR_NOT_FOUND)) { /* new file! */ err = hdf_init (&meta); } if (err) return nerr_pass (err); for (vers = hdf_get_child (meta, "Versions"); vers; vers = hdf_obj_next (vers)) { x = atoi (hdf_obj_name (vers)); if (x > version) version = x; } /* new version */ version++; snprintf (fpath, sizeof (fpath), "%s,%d", path, version); fd = open (fpath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); if (fd == -1) { err = nerr_raise_errno (NERR_IO, "Unable to create file %s", fpath); break; } l = strlen(data); w = write (fd, data, l); if (w != l) { err = nerr_raise_errno (NERR_IO, "Unable to write file %s", fpath); close (fd); break; } close (fd); snprintf (buf, sizeof(buf), "Versions.%d.Log", version); err = hdf_set_value (meta, buf, log); if (err) break; snprintf (buf, sizeof(buf), "Versions.%d.User", version); err = hdf_set_value (meta, buf, user); if (err) break; snprintf (buf, sizeof(buf), "Versions.%d.Date", version); err = hdf_set_int_value (meta, buf, ne_timef()); if (err) break; err = _meta_save (path, meta); } while (0); rcfs_unlock (lock); hdf_destroy (&meta); return nerr_pass (err); }