int main(int argc, char **argv) { int nthreads; unsigned long i; pthread_t *threads; struct jfsck_result ckres; if (argc != 4) { help(); return 1; } mb = atoi(argv[1]); blocksize = atoi(argv[2]) * 1024; nthreads = atoi(argv[3]); towrite = mb * 1024 * 1024; threads = malloc(sizeof(pthread_t) * nthreads); if (threads == NULL) { perror("malloc()"); return 1; } fs = jopen(FILENAME, O_RDWR | O_CREAT | O_TRUNC, 0600, 0); if (fs == NULL) { perror("jopen()"); return 1; } jtruncate(fs, towrite * nthreads); for (i = 0; i < nthreads; i++) { pthread_create(threads + i, NULL, &worker, (void *) i); } for (i = 0; i < nthreads; i++) { pthread_join(*(threads + i), NULL); } jclose(fs); jfsck(FILENAME, NULL, &ckres, 0); if (ckres.total != 0) { fprintf(stderr, "There were %d errors during the test\n", ckres.total); fprintf(stderr, "jfsck() was used to fix them, but that "); fprintf(stderr, "shouldn't happen.\n"); return 1; } return 0; }
check_exe(char *name) { int file; Vlib ev; int ok; if ((file = jopen(name, 0)) == 0) { cant_find(name); return(0); } ok = verify_exe_head(file, &ev.eh); jclose(file); return(ok); }
int main(void) { int r; jfs_t *file; struct jtrans *trans; struct jfsck_result result; /* check the file is OK */ jfsck(FILENAME, NULL, &result, 0); /* and open it */ file = jopen(FILENAME, O_RDWR | O_CREAT | O_TRUNC, 0600, 0); if (file == NULL) { perror("jopen"); return 1; } /* write two "Hello world"s next to each other */ trans = jtrans_new(file, 0); jtrans_add_w(trans, TEXT, strlen(TEXT), 0); jtrans_add_w(trans, TEXT, strlen(TEXT), strlen(TEXT)); r = jtrans_commit(trans); if (r < 0) { perror("jtrans_commit"); return 1; } /* at this point the file has "Hello world!\nHello world!\n" */ /* now we rollback */ r = jtrans_rollback(trans); if (r < 0) { perror("jtrans_rollback"); return 1; } /* and now the file is empty! */ jtrans_free(trans); jclose(file); return 0; }
int main(int argc, char **argv) { int rv; jfs_t *fs; jtrans_t *ts; fs = jopen("test3", O_RDWR | O_CREAT, 0660, 0); if (fs == NULL) perror("jopen()"); ts = jtrans_new(fs, 0); if (ts == NULL) perror("jtrans_new()"); #define str1 "1ROLLBACKTEST1!\n" jtrans_add_w(ts, str1, strlen(str1), 0); #define str2 "2ROLLBACKTEST2!\n" jtrans_add_w(ts, str2, strlen(str2), strlen(str1)); #define str3 "3ROLLBACKTEST3!\n" jtrans_add_w(ts, str3, strlen(str3), strlen(str1) + strlen(str2)); rv = jtrans_commit(ts); if (rv < 0) perror("jtrans_commit()"); printf("commit ok: %d\n", rv); rv = jtrans_rollback(ts); if (rv < 0) perror("jtrans_rollback()"); printf("rollback ok: %d\n", rv); jtrans_free(ts); if (jclose(fs)) perror("jclose()"); return 0; }
/* fopen() wrapper */ struct jfs *jfopen(const char *path, const char *mode) { int flags; int pos_at_the_beginning; struct jfs *fs; if (strlen(mode) < 1) return NULL; if (mode[0] == 'r') { pos_at_the_beginning = 1; if (strlen(mode) > 1 && strchr(mode, '+')) flags = O_RDWR; else flags = O_RDONLY; } else if (mode[0] == 'a') { if (strlen(mode) > 1 && strchr(mode, '+')) pos_at_the_beginning = 1; else pos_at_the_beginning = 0; flags = O_RDWR | O_CREAT | O_APPEND; } else if (mode[0] == 'w') { pos_at_the_beginning = 1; flags = O_RDWR | O_CREAT | O_TRUNC; } else { return NULL; } fs = jopen(path, flags, 0666, 0); if (fs == NULL) return NULL; if (pos_at_the_beginning) lseek(fs->fd, 0, SEEK_SET); else lseek(fs->fd, 0, SEEK_END); return fs; }
void *load_exe(char *filename, Exe_head *eh) { long retval; char *alligned_buf; char *alloc_buf = NULL; long (*rfunc)(); unsigned long code_offset; unsigned long init_size; unsigned long bss_size; unsigned long total_size; void *v; unsigned long fixup_offset; UWORD fixup[2]; UWORD *segpt; UWORD code_seg; int f; unsigned i; int ok = 0; if ((f = jopen(filename, JREADONLY)) == 0) { cant_find(filename); return(NULL); } if (!verify_exe_head(f, eh)) goto OUT; code_offset = eh->head_size; code_offset *= 16; /* make it a paragraph */ init_size = eh->blocks; init_size *= 512; init_size += eh->mod512; if (eh->mod512 != 0) init_size -= 512; init_size -= code_offset; bss_size = eh->min_data; bss_size *= 16; total_size = init_size + bss_size; if ((alloc_buf = begmem((unsigned)total_size+16)) == NULL) goto OUT; code_seg = ptr_seg(alloc_buf) + 1; alligned_buf = make_ptr(0, code_seg); zero_structure(alligned_buf, (unsigned)total_size); jseek(f, code_offset, JSEEK_START); if (jread(f, alligned_buf, init_size) < init_size) { truncated(filename); goto OUT; } v = alligned_buf; eh->entry_point = v; if (eh->reloc_count > 0) { fixup_offset = eh->reloc_list; jseek(f, fixup_offset, JSEEK_START); for (i=0; i<eh->reloc_count; i++) { if (jread(f, fixup, sizeof(fixup)) != sizeof(fixup)) { truncated(filename); goto OUT; } segpt = make_ptr(fixup[0], code_seg + fixup[1]); segpt[0] += code_seg; } } ok = 1; OUT: if (!ok) { gentle_freemem(alloc_buf); alloc_buf = NULL; } jclose(f); return(alloc_buf); }