static void test_reopen(const char *path) { PMEMlogpool *log1 = pmemlog_create(path, PMEMLOG_MIN_POOL, S_IWUSR | S_IRUSR); if (!log1) FATAL("!create"); PMEMlogpool *log2 = pmemlog_open(path); if (log2) FATAL("pmemlog_open should not succeed"); if (errno != EWOULDBLOCK) FATAL("!pmemlog_open failed but for unexpected reason"); pmemlog_close(log1); log2 = pmemlog_open(path); if (!log2) FATAL("pmemlog_open should succeed after close"); pmemlog_close(log2); UNLINK(path); }
static void test_open_in_different_process(const char *path, int sleep) { pid_t pid = fork(); PMEMlogpool *log; if (pid < 0) FATAL("fork failed"); if (pid == 0) { /* child */ if (sleep) usleep(sleep); while (access(path, R_OK)) usleep(100 * 1000); log = pmemlog_open(path); if (log) FATAL("pmemlog_open after fork should not succeed"); if (errno != EWOULDBLOCK) FATAL("!pmemlog_open after fork failed but for " "unexpected reason"); exit(0); } log = pmemlog_create(path, PMEMLOG_MIN_POOL, S_IWUSR | S_IRUSR); if (!log) FATAL("!create"); int status; if (waitpid(pid, &status, 0) < 0) FATAL("!waitpid failed"); if (!WIFEXITED(status)) FATAL("child process failed"); pmemlog_close(log); UNLINK(path); }
int main(int argc, char *argv[]) { const char path[] = "/pmem-fs/myfile"; PMEMlogpool *plp; size_t nbyte; char *str; /* create the pmemlog pool or open it if it already exists */ plp = pmemlog_create(path, POOL_SIZE, 0666); if (plp == NULL) plp = pmemlog_open(path); if (plp == NULL) { perror(path); exit(1); } /* how many bytes does the log hold? */ nbyte = pmemlog_nbyte(plp); printf("log holds %zu bytes\n", nbyte); /* append to the log... */ str = "This is the first string appended\n"; if (pmemlog_append(plp, str, strlen(str)) < 0) { perror("pmemlog_append"); exit(1); } str = "This is the second string appended\n"; if (pmemlog_append(plp, str, strlen(str)) < 0) { perror("pmemlog_append"); exit(1); } /* print the log contents */ printf("log contains:\n"); pmemlog_walk(plp, 0, printit, NULL); pmemlog_close(plp); }
int main(int argc, char *argv[]) { int opt; int tflag = 0; PMEMlogpool *plp; while ((opt = getopt(argc, argv, "t")) != -1) switch (opt) { case 't': tflag = 1; break; default: fprintf(stderr, "usage: %s [-t] file\n", argv[0]); exit(1); } if (optind >= argc) { fprintf(stderr, "usage: %s [-t] file\n", argv[0]); exit(1); } const char *path = argv[optind]; if ((plp = pmemlog_open(path)) == NULL) { perror(path); exit(1); } /* the rest of the work happens in printlog() above */ pmemlog_walk(plp, 0, printlog, NULL); if (tflag) pmemlog_rewind(plp); pmemlog_close(plp); }
int main(int argc, char *argv[]) { PMEMlogpool *plp; struct logentry header; struct iovec *iovp; struct iovec *next_iovp; int iovcnt; if (argc < 3) { fprintf(stderr, "usage: %s filename lines...\n", argv[0]); exit(1); } const char *path = argv[1]; /* create the log in the given file, or open it if already created */ if ((plp = pmemlog_create(path, 0, S_IWUSR | S_IRUSR)) == NULL && (plp = pmemlog_open(path)) == NULL) { perror(path); exit(1); } /* fill in the header */ time(&header.timestamp); header.pid = getpid(); /* * Create an iov for pmemlog_appendv(). For each argument given, * allocate two entries (one for the string, one for the newline * appended to the string). Allocate 1 additional entry for the * header that gets prepended to the entry. */ iovcnt = (argc - 2) * 2 + 1; if ((iovp = malloc(sizeof (*iovp) * iovcnt)) == NULL) { perror("malloc"); exit(1); } next_iovp = iovp; /* put the header into iov first */ next_iovp->iov_base = &header; next_iovp->iov_len = sizeof (header); next_iovp++; /* * Now put each arg in, following it with the string "\n". * Calculate a total character count in header.len along the way. */ header.len = 0; for (int arg = 2; arg < argc; arg++) { /* add the string given */ next_iovp->iov_base = argv[arg]; next_iovp->iov_len = strlen(argv[arg]); header.len += next_iovp->iov_len; next_iovp++; /* add the newline */ next_iovp->iov_base = "\n"; next_iovp->iov_len = 1; header.len += 1; next_iovp++; } /* atomically add it all to the log */ if (pmemlog_appendv(plp, iovp, iovcnt) < 0) { perror("pmemlog_appendv"); exit(1); } pmemlog_close(plp); }
int main(int argc, char *argv[]) { if (argc < 2) { fprintf(stderr, "usage: %s [o,c] file [val...]", argv[0]); return 1; } PMEMlogpool *plp; if (strncmp(argv[1], "c", 1) == 0) { plp = pmemlog_create(argv[2], POOL_SIZE, S_IRUSR | S_IWUSR); } else if (strncmp(argv[1], "o", 1) == 0) { plp = pmemlog_open(argv[2]); } else { fprintf(stderr, "usage: %s [o,c] file [val...]", argv[0]); return 1; } if (plp == NULL) { perror("pmemlog_create/pmemlog_open"); return 1; } /* process the command line arguments */ for (int i = 3; i < argc; i++) { switch (*argv[i]) { case 'a': { printf("append: %s\n", argv[i] + 2); pmemlog_append(plp, argv[i] + 2, strlen(argv[i] + 2)); break; } case 'v': { printf("appendv: %s\n", argv[i] + 2); int count = count_iovec(argv[i] + 2); struct iovec *iov = malloc(count * sizeof (struct iovec)); fill_iovec(iov, argv[i] + 2); pmemlog_appendv(plp, iov, count); free(iov); break; } case 'r': { printf("rewind\n"); pmemlog_rewind(plp); break; } case 'w': { printf("walk\n"); pmemlog_walk(plp, 0, process_chunk, NULL); break; } case 'n': { printf("nbytes: %zu\n", pmemlog_nbyte(plp)); break; } case 't': { printf("offset: %ld\n", pmemlog_tell(plp)); break; } default: { fprintf(stderr, "unrecognized command %s\n", argv[i]); break; } }; } /* all done */ pmemlog_close(plp); return 0; }