/* _box_attach(): attach a box to the free list. */ void _box_attach(u3a_box* box_u) { c3_assert(box_u->siz_w >= (1 + c3_wiseof(u3a_fbox))); c3_assert(0 != u3of(u3a_fbox, box_u)); #if 0 // For debugging, fill the box with beef. { c3_w* box_w = (void *)box_u; c3_w i_w; for ( i_w = c3_wiseof(u3a_box); (i_w + 1) < box_u->siz_w; i_w++ ) { box_w[i_w] = 0xdeadbeef; } } #endif u3R->all.fre_w += box_u->siz_w; { c3_w sel_w = _box_slot(box_u->siz_w); u3p(u3a_fbox) fre_p = u3of(u3a_fbox, box_u); u3p(u3a_fbox)* pfr_p = &u3R->all.fre_p[sel_w]; u3p(u3a_fbox) nex_p = *pfr_p; u3to(u3a_fbox, fre_p)->pre_p = 0; u3to(u3a_fbox, fre_p)->nex_p = nex_p; if ( u3to(u3a_fbox, fre_p)->nex_p ) { u3to(u3a_fbox, u3to(u3a_fbox, fre_p)->nex_p)->pre_p = fre_p; } (*pfr_p) = fre_p; } }
/* u2_cm_rind(): open and produce a new jump buffer. */ void* u2_cm_rind() { u2_ray kit_r = u2_rl_ralloc(u2_Wire, c3_wiseof(u2_loom_kite)); u2_kite_par_r(kit_r) = u2_wire_kit_r(u2_Wire); u2_wire_kit_r(u2_Wire) = kit_r; // Save the old stack and actions. // u2_kite_tax(kit_r) = u2k(u2_wire_tax(u2_Wire)); u2_kite_don(kit_r) = u2k(u2_wrac_at(u2_Wire, duz.don)); return u2_at_cord(u2_kite_buf_r(kit_r), c3_wiseof(jmp_buf)); }
u2_noun u2_cm_bail(c3_l how_l) { u2_ray kit_r = u2_wire_kit_r(u2_Wire); if ( u2_yes == u2_Flag_Abort ) { if ( c3__fail == how_l ) { c3_assert(0); } c3_assert(0); } u2_tx_sys_bit(u2_Wire, u2_yes); // fprintf(stderr, "bail\n"); // if ( _num == 0 ) { c3_assert(0); } else _num--; { u2_noun jaq; jmp_buf buf_f; // Reset the old stack trace, pulling off the local top. // jaq = u2_cm_wail(); // Reset the old action trace. { u2z(u2_wrac_at(u2_Wire, duz.don)); u2_wrac_at(u2_Wire, duz.don) = u2_kite_don(kit_r); } // Copy out the jump buffer; free the old kite. { memcpy((void *)buf_f, u2_at_cord(u2_kite_buf_r(kit_r), c3_wiseof(jmp_buf)), sizeof(jmp_buf)); u2_wire_kit_r(u2_Wire) = u2_kite_par_r(kit_r); u2_rl_rfree(u2_Wire, kit_r); } // Longjmp with the how-trace pair. XX: no workee with 64-bit nouns. // { _longjmp(buf_f, u2nc(how_l, jaq)); } } return 0; }
/* u2_cm_bowl(): bail out with preset report. */ u2_noun u2_cm_bowl(u2_noun how) { u2_ray kit_r = u2_wire_kit_r(u2_Wire); u2_tx_sys_bit(u2_Wire, u2_yes); { u2_noun jaq; jmp_buf buf_f; // Reset the old stack trace, pulling off the local top. // jaq = u2_cm_wail(); // Reset the old action trace. { u2z(u2_wrac_at(u2_Wire, duz.don)); u2_wrac_at(u2_Wire, duz.don) = u2_kite_don(kit_r); } // Copy out the jump buffer; free the old kite. { memcpy((void *)buf_f, u2_at_cord(u2_kite_buf_r(kit_r), c3_wiseof(jmp_buf)), sizeof(jmp_buf)); u2_wire_kit_r(u2_Wire) = u2_kite_par_r(kit_r); u2_rl_rfree(u2_Wire, kit_r); } // Longjmp with the how-trace pair. XX: no workee with 64-bit nouns. // { u2z(jaq); _longjmp(buf_f, how); } } return 0; }
/* _ca_willoc(): u3a_walloc() internals. */ static void* _ca_willoc(c3_w len_w, c3_w ald_w, c3_w alp_w) { c3_w siz_w = c3_max(u3a_minimum, u3a_boxed(len_w)); c3_w sel_w = _box_slot(siz_w); alp_w = (alp_w + c3_wiseof(u3a_box)) % ald_w; // XX: this logic is totally bizarre, but preserve it. // if ( (sel_w != 0) && (sel_w != u3a_fbox_no - 1) ) { sel_w += 1; } // fprintf(stderr, "walloc %d: *pfr_p %x\n", len_w, u3R->all.fre_p[sel_w]); while ( 1 ) { u3p(u3a_fbox) *pfr_p = &u3R->all.fre_p[sel_w]; while ( 1 ) { if ( 0 == *pfr_p ) { if ( sel_w < (u3a_fbox_no - 1) ) { sel_w += 1; break; } else { /* Nothing in top free list. Chip away at the hat. */ return u3a_boxto(_ca_box_make_hat(siz_w, ald_w, alp_w, 1)); } } else { c3_w pad_w = _me_align_pad(*pfr_p, ald_w, alp_w); if ( 1 == ald_w ) c3_assert(0 == pad_w); if ( (siz_w + pad_w) > u3to(u3a_fbox, *pfr_p)->box_u.siz_w ) { /* This free block is too small. Continue searching. */ pfr_p = &(u3to(u3a_fbox, *pfr_p)->nex_p); continue; } else { u3a_box* box_u = &(u3to(u3a_fbox, *pfr_p)->box_u); /* We have found a free block of adequate size. Remove it ** from the free list. */ siz_w += pad_w; { { c3_assert((0 == u3to(u3a_fbox, *pfr_p)->pre_p) || (u3to(u3a_fbox, u3to(u3a_fbox, *pfr_p)->pre_p)->nex_p == (*pfr_p))); c3_assert((0 == u3to(u3a_fbox, *pfr_p)->nex_p) || (u3to(u3a_fbox, u3to(u3a_fbox, *pfr_p)->nex_p)->pre_p == (*pfr_p))); } if ( 0 != u3to(u3a_fbox, *pfr_p)->nex_p ) { u3to(u3a_fbox, u3to(u3a_fbox, *pfr_p)->nex_p)->pre_p = u3to(u3a_fbox, *pfr_p)->pre_p; } *pfr_p = u3to(u3a_fbox, *pfr_p)->nex_p; } /* If we can chop off another block, do it. */ if ( (siz_w + u3a_minimum) <= box_u->siz_w ) { /* Split the block. */ c3_w* box_w = ((c3_w *)(void *)box_u); c3_w* end_w = box_w + siz_w; c3_w lef_w = (box_u->siz_w - siz_w); _box_attach(_box_make(end_w, lef_w, 0)); return u3a_boxto(_box_make(box_w, siz_w, 1)); } else { c3_assert(0 == box_u->use_w); box_u->use_w = 1; #ifdef U3_MEMORY_DEBUG box_u->cod_w = u3_Code; #endif return u3a_boxto(box_u); } } } } } }
/* u3a_wfree(): free storage. */ void u3a_wfree(void* tox_v) { u3a_box* box_u = u3a_botox(tox_v); c3_w* box_w = (c3_w *)(void *)box_u; u3t_on(mal_o); c3_assert(box_u->use_w != 0); box_u->use_w -= 1; if ( 0 != box_u->use_w ) { u3t_off(mal_o); return; } #if 0 /* Clear the contents of the block, for debugging. */ { c3_w i_w; for ( i_w = c3_wiseof(u3a_box); (i_w + 1) < box_u->siz_w; i_w++ ) { box_w[i_w] = 0xdeadbeef; } } #endif if ( c3y == u3a_is_north(u3R) ) { /* Try to coalesce with the block below. */ if ( box_w != u3a_into(u3R->rut_p) ) { c3_w laz_w = *(box_w - 1); u3a_box* pox_u = (u3a_box*)(void *)(box_w - laz_w); if ( 0 == pox_u->use_w ) { _box_detach(pox_u); _box_make(pox_u, (laz_w + box_u->siz_w), 0); box_u = pox_u; box_w = (c3_w*)(void *)pox_u; } } /* Try to coalesce with the block above, or the wilderness. */ if ( (box_w + box_u->siz_w) == u3a_into(u3R->hat_p) ) { u3R->hat_p = u3a_outa(box_w); } else { u3a_box* nox_u = (u3a_box*)(void *)(box_w + box_u->siz_w); if ( 0 == nox_u->use_w ) { _box_detach(nox_u); _box_make(box_u, (box_u->siz_w + nox_u->siz_w), 0); } _box_attach(box_u); } } else { /* Try to coalesce with the block above. */ if ( (box_w + box_u->siz_w) != u3a_into(u3R->rut_p) ) { u3a_box* nox_u = (u3a_box*)(void *)(box_w + box_u->siz_w); if ( 0 == nox_u->use_w ) { _box_detach(nox_u); _box_make(box_u, (box_u->siz_w + nox_u->siz_w), 0); } } /* Try to coalesce with the block below, or with the wilderness. */ if ( box_w == u3a_into(u3R->hat_p) ) { u3R->hat_p = u3a_outa(box_w + box_u->siz_w); } else { c3_w laz_w = *(box_w - 1); u3a_box* pox_u = (u3a_box*)(void *)(box_w - laz_w); if ( 0 == pox_u->use_w ) { _box_detach(pox_u); _box_make(pox_u, (laz_w + box_u->siz_w), 0); box_u = pox_u; } _box_attach(box_u); } } u3t_off(mal_o); }
/* _sist_rest(): restore from record, or exit. */ static void _sist_rest() { struct stat buf_b; c3_i fid_i; c3_c ful_c[2048]; c3_d old_d = u3A->ent_d; c3_d las_d = 0; u3_noun roe = u3_nul; u3_noun sev_l, key_l, sal_l; u3_noun ohh = c3n; if ( 0 != u3A->ent_d ) { u3_noun ent; c3_c* ent_c; ent = u3i_chubs(1, &u3A->ent_d); ent = u3dc("scot", c3__ud, ent); ent_c = u3r_string(ent); uL(fprintf(uH, "rest: checkpoint to event %s\n", ent_c)); free(ent_c); u3z(ent); } // Open the fscking file. Does it even exist? { c3_i pig_i = O_RDWR; #ifdef O_DSYNC pig_i |= O_DSYNC; #endif snprintf(ful_c, 2048, "%s/.urb/egz.hope", u3_Host.dir_c); if ( ((fid_i = open(ful_c, pig_i)) < 0) || (fstat(fid_i, &buf_b) < 0) ) { uL(fprintf(uH, "rest: can't open record (%s)\n", ful_c)); u3_lo_bail(); return; } #ifdef F_NOCACHE if ( -1 == fcntl(fid_i, F_NOCACHE, 1) ) { uL(fprintf(uH, "rest: can't uncache %s: %s\n", ful_c, strerror(errno))); u3_lo_bail(); return; } #endif u3Z->lug_u.fid_i = fid_i; u3Z->lug_u.len_d = ((buf_b.st_size + 3ULL) >> 2ULL); } // Check the fscking header. It's probably corrupt. { u3_uled led_u; if ( sizeof(led_u) != read(fid_i, &led_u, sizeof(led_u)) ) { uL(fprintf(uH, "record (%s) is corrupt (a)\n", ful_c)); u3_lo_bail(); } if ( u3r_mug('f') == led_u.mag_l ) { _sist_rest_nuu(&u3Z->lug_u, led_u, ful_c); fid_i = u3Z->lug_u.fid_i; } else if (u3r_mug('g') != led_u.mag_l ) { uL(fprintf(uH, "record (%s) is obsolete (or corrupt)\n", ful_c)); u3_lo_bail(); } if ( led_u.kno_w != 163 ) { // XX perhaps we should actually do something here // uL(fprintf(uH, "rest: (not) translating events (old %d, now %d)\n", led_u.kno_w, 163)); } sev_l = led_u.sev_l; sal_l = led_u.sal_l; key_l = led_u.key_l; { u3_noun old = u3dc("scot", c3__uv, sev_l); u3_noun nuu = u3dc("scot", c3__uv, u3A->sev_l); c3_c* old_c = u3r_string(old); c3_c* nuu_c = u3r_string(nuu); uL(fprintf(uH, "rest: old %s, new %s\n", old_c, nuu_c)); free(old_c); free(nuu_c); u3z(old); u3z(nuu); } c3_assert(sev_l != u3A->sev_l); // 1 in 2 billion, just retry } // Oh, and let's hope you didn't forget the fscking passcode. { if ( 0 != key_l ) { u3_noun pas = _sist_staf(key_l); u3_noun key; while ( 1 ) { pas = pas ? pas : _sist_cask(u3_Host.dir_c, c3n); key = _sist_fatt(sal_l, pas); if ( u3r_mug(key) != key_l ) { uL(fprintf(uH, "incorrect passcode\n")); u3z(key); pas = 0; } else { u3z(u3A->key); u3A->key = key; break; } } } } // Read in the fscking events. These are probably corrupt as well. { c3_d ent_d; c3_d end_d; u3_noun rup = c3n; end_d = u3Z->lug_u.len_d; ent_d = 0; if ( -1 == lseek64(fid_i, 4ULL * end_d, SEEK_SET) ) { fprintf(stderr, "end_d %" PRIu64 "\n", end_d); perror("lseek"); uL(fprintf(uH, "record (%s) is corrupt (c)\n", ful_c)); u3_lo_bail(); } while ( end_d != c3_wiseof(u3_uled) ) { c3_d tar_d = (end_d - (c3_d)c3_wiseof(u3_ular)); u3_ular lar_u; c3_w* img_w; u3_noun ron; // uL(fprintf(uH, "rest: reading event at %" PRIx64 "\n", end_d)); if ( -1 == lseek64(fid_i, 4ULL * tar_d, SEEK_SET) ) { uL(fprintf(uH, "record (%s) is corrupt (d)\n", ful_c)); u3_lo_bail(); } if ( sizeof(u3_ular) != read(fid_i, &lar_u, sizeof(u3_ular)) ) { uL(fprintf(uH, "record (%s) is corrupt (e)\n", ful_c)); u3_lo_bail(); } if ( lar_u.syn_w != u3r_mug((c3_w)tar_d) ) { if ( c3n == rup ) { uL(fprintf(uH, "corruption detected; attempting to fix\n")); rup = c3y; } uL(fprintf(uH, "lar:%x mug:%x\n", lar_u.syn_w, u3r_mug((c3_w)tar_d))); end_d--; u3Z->lug_u.len_d--; continue; } else if ( c3y == rup ) { uL(fprintf(uH, "matched at %x\n", lar_u.syn_w)); rup = c3n; } if ( lar_u.ent_d == 0 ) { ohh = c3y; } #if 0 uL(fprintf(uH, "log: read: at %d, %d: lar ent %" PRIu64 ", len %d, mug %x\n", (tar_w - lar_u.len_w), tar_w, lar_u.ent_d, lar_u.len_w, lar_u.mug_w)); #endif if ( end_d == u3Z->lug_u.len_d ) { ent_d = las_d = lar_u.ent_d; } else { if ( lar_u.ent_d != (ent_d - 1ULL) ) { uL(fprintf(uH, "record (%s) is corrupt (g)\n", ful_c)); uL(fprintf(uH, "lar_u.ent_d %" PRIx64 ", ent_d %" PRIx64 "\n", lar_u.ent_d, ent_d)); u3_lo_bail(); } ent_d -= 1ULL; } end_d = (tar_d - (c3_d)lar_u.len_w); if ( ent_d < old_d ) { // XX this could be a break if we didn't want to see the sequence // number of the first event. continue; } img_w = c3_malloc(4 * lar_u.len_w); if ( -1 == lseek64(fid_i, 4ULL * end_d, SEEK_SET) ) { uL(fprintf(uH, "record (%s) is corrupt (h)\n", ful_c)); u3_lo_bail(); } if ( (4 * lar_u.len_w) != read(fid_i, img_w, (4 * lar_u.len_w)) ) { uL(fprintf(uH, "record (%s) is corrupt (i)\n", ful_c)); u3_lo_bail(); } ron = u3i_words(lar_u.len_w, img_w); free(img_w); if ( lar_u.mug_w != u3r_mug_both(u3r_mug(ron), u3r_mug_both(u3r_mug(lar_u.tem_w), u3r_mug(lar_u.typ_w))) ) { uL(fprintf(uH, "record (%s) is corrupt (j)\n", ful_c)); u3_lo_bail(); } if ( c3__ov != lar_u.typ_w ) { u3z(ron); continue; } if ( u3A->key ) { u3_noun dep; dep = u3dc("de:crua", u3k(u3A->key), ron); if ( c3n == u3du(dep) ) { uL(fprintf(uH, "record (%s) is corrupt (k)\n", ful_c)); u3_lo_bail(); } else { ron = u3k(u3t(dep)); u3z(dep); } } roe = u3nc(u3ke_cue(ron), roe); } u3A->ent_d = c3_max(las_d + 1ULL, old_d); } if ( u3_nul == roe ) { // Nothing in the log that was not also in the checkpoint. // c3_assert(u3A->ent_d == old_d); if ( las_d + 1 != old_d ) { uL(fprintf(uH, "checkpoint and log disagree! las:%" PRIu64 " old:%" PRIu64 "\n", las_d + 1, old_d)); uL(fprintf(uH, "Some events appear to be missing from the log.\n" "Please contact the authorities, " "and do not delete your pier!\n")); u3_lo_bail(); } } else { u3_noun rou = roe; c3_w xno_w; // Execute the fscking things. This is pretty much certain to crash. // uL(fprintf(uH, "rest: replaying through event %" PRIu64 "\n", las_d)); fprintf(uH, "---------------- playback starting----------------\n"); xno_w = 0; while ( u3_nul != roe ) { u3_noun i_roe = u3h(roe); u3_noun t_roe = u3t(roe); u3_noun now = u3h(i_roe); u3_noun ovo = u3t(i_roe); u3v_time(u3k(now)); if ( (c3y == u3_Host.ops_u.vno) && ( (c3__veer == u3h(u3t(ovo)) || (c3__vega == u3h(u3t(ovo)))) ) ) { fprintf(stderr, "replay: skipped veer\n"); } else if ( c3y == u3_Host.ops_u.fog && u3_nul == t_roe ) { fprintf(stderr, "replay: -Xwtf, skipped last event\n"); } else { _sist_sing(u3k(ovo)); fputc('.', stderr); } // fprintf(stderr, "playback: sing: %d\n", xno_w)); roe = t_roe; xno_w++; if ( 0 == (xno_w % 1000) ) { uL(fprintf(uH, "{%d}\n", xno_w)); // u3_lo_grab("rest", rou, u3_none); } } u3z(rou); } uL(fprintf(stderr, "\n---------------- playback complete----------------\n")); #if 0 // If you see this error, your record is totally fscking broken! // Which probably serves you right. Please consult a consultant. { if ( u3_nul == u3A->own ) { uL(fprintf(uH, "record did not install a master!\n")); u3_lo_bail(); } u3A->our = u3k(u3h(u3A->own)); u3A->pod = u3dc("scot", 'p', u3k(u3A->our))); } // Now, who the fsck are you? No, really. { u3_noun who; c3_c* fil_c; c3_c* who_c; if ( (fil_c = strrchr(u3_Host.dir_c, '/')) ) { fil_c++; } else fil_c = u3_Host.dir_c; who = u3dc("scot", 'p', u3k(u3A->our))); who_c = u3r_string(who); u3z(who); if ( strncmp(fil_c, who_c + 1, strlen(fil_c)) ) { uL(fprintf(uH, "record master (%s) does not match filename!\n", who_c)); u3_lo_bail(); } free(who_c); } #endif // Increment sequence numbers. New logs start at 1. if ( c3y == ohh ) { uL(fprintf(uH, "rest: bumping ent_d, don't panic.\n")); u3_ular lar_u; c3_d end_d; c3_d tar_d; u3A->ent_d++; end_d = u3Z->lug_u.len_d; while ( end_d != c3_wiseof(u3_uled) ) { tar_d = end_d - c3_wiseof(u3_ular); if ( -1 == lseek64(fid_i, 4ULL * tar_d, SEEK_SET) ) { uL(fprintf(uH, "bumping sequence numbers failed (a)\n")); u3_lo_bail(); } if ( sizeof(lar_u) != read(fid_i, &lar_u, sizeof(lar_u)) ) { uL(fprintf(uH, "bumping sequence numbers failed (b)\n")); u3_lo_bail(); } lar_u.ent_d++; if ( -1 == lseek64(fid_i, 4ULL * tar_d, SEEK_SET) ) { uL(fprintf(uH, "bumping sequence numbers failed (c)\n")); u3_lo_bail(); } if ( sizeof(lar_u) != write(fid_i, &lar_u, sizeof(lar_u)) ) { uL(fprintf(uH, "bumping sequence numbers failed (d)\n")); u3_lo_bail(); } end_d = tar_d - lar_u.len_w; } } // Rewrite the header. Will probably corrupt the record. { u3_uled led_u; led_u.mag_l = u3r_mug('g'); led_u.sal_l = sal_l; led_u.sev_l = u3A->sev_l; led_u.key_l = u3A->key ? u3r_mug(u3A->key) : 0; led_u.kno_w = 163; // may need actual translation! led_u.tno_l = 1; if ( (-1 == lseek64(fid_i, 0, SEEK_SET)) || (sizeof(led_u) != write(fid_i, &led_u, sizeof(led_u))) ) { uL(fprintf(uH, "record (%s) failed to rewrite\n", ful_c)); u3_lo_bail(); } } // Hey, fscker! It worked. { u3_term_ef_boil(); } }
/* _sist_rest_nuu(): upgrade log from previous format. */ static void _sist_rest_nuu(u3_ulog* lug_u, u3_uled led_u, c3_c* old_c) { c3_c nuu_c[2048]; u3_noun roe = u3_nul; c3_i fid_i = lug_u->fid_i; c3_i fud_i; c3_i ret_i; c3_d end_d = lug_u->len_d; uL(fprintf(uH, "rest: converting log from prior format\n")); c3_assert(led_u.mag_l == u3r_mug('f')); if ( -1 == lseek64(fid_i, 4ULL * end_d, SEEK_SET) ) { uL(fprintf(uH, "rest_nuu failed (a)\n")); perror("lseek64"); u3_lo_bail(); } while ( end_d != c3_wiseof(u3_uled) ) { c3_d tar_d; u3_olar lar_u; c3_w* img_w; u3_noun ron; tar_d = (end_d - (c3_d)c3_wiseof(u3_olar)); if ( -1 == lseek64(fid_i, 4ULL * tar_d, SEEK_SET) ) { uL(fprintf(uH, "rest_nuu failed (b)\n")); perror("lseek64"); u3_lo_bail(); } if ( sizeof(u3_olar) != read(fid_i, &lar_u, sizeof(u3_olar)) ) { uL(fprintf(uH, "rest_nuu failed (c)\n")); perror("read"); u3_lo_bail(); } if ( lar_u.syn_w != u3r_mug((c3_w)tar_d) ) { uL(fprintf(uH, "rest_nuu failed (d)\n")); u3_lo_bail(); } img_w = c3_malloc(4 * lar_u.len_w); end_d = (tar_d - (c3_d)lar_u.len_w); if ( -1 == lseek64(fid_i, 4ULL * end_d, SEEK_SET) ) { uL(fprintf(uH, "rest_nuu failed (e)\n")); perror("lseek64"); u3_lo_bail(); } if ( (4 * lar_u.len_w) != read(fid_i, img_w, (4 * lar_u.len_w)) ) { uL(fprintf(uH, "rest_nuu failed (f)\n")); perror("read"); u3_lo_bail(); } ron = u3i_words(lar_u.len_w, img_w); free(img_w); if ( lar_u.mug_w != u3r_mug(ron) ) { uL(fprintf(uH, "rest_nuu failed (g)\n")); u3_lo_bail(); } roe = u3nc(ron, roe); } if ( 0 != close(fid_i) ) { uL(fprintf(uH, "rest: could not close\n")); perror("close"); u3_lo_bail(); } ret_i = snprintf(nuu_c, 2048, "%s/.urb/ham.hope", u3_Host.dir_c); c3_assert(ret_i < 2048); if ( (fud_i = open(nuu_c, O_CREAT | O_TRUNC | O_RDWR, 0600)) < 0 ) { uL(fprintf(uH, "rest: can't open record (%s)\n", nuu_c)); perror("open"); u3_lo_bail(); } led_u.mag_l = u3r_mug('g'); if ( (sizeof(led_u) != write(fud_i, &led_u, sizeof(led_u))) ) { uL(fprintf(uH, "rest: can't write header\n")); perror("write"); u3_lo_bail(); } { c3_d ent_d = 1; c3_assert(end_d == c3_wiseof(u3_uled)); while ( u3_nul != roe ) { u3_noun ovo = u3k(u3h(roe)); u3_noun nex = u3k(u3t(roe)); u3_ular lar_u; c3_w* img_w; c3_d tar_d; lar_u.len_w = u3r_met(5, ovo); tar_d = end_d + lar_u.len_w; lar_u.syn_w = u3r_mug(tar_d); lar_u.ent_d = ent_d; lar_u.tem_w = 0; lar_u.typ_w = c3__ov; lar_u.mug_w = u3r_mug_both(u3r_mug(ovo), u3r_mug_both(u3r_mug(0), u3r_mug(c3__ov))); img_w = c3_malloc(lar_u.len_w << 2); u3r_words(0, lar_u.len_w, img_w, ovo); u3z(ovo); if ( (lar_u.len_w << 2) != write(fud_i, img_w, lar_u.len_w << 2) ) { uL(fprintf(uH, "rest_nuu failed (h)\n")); perror("write"); u3_lo_bail(); } if ( sizeof(u3_ular) != write(fud_i, &lar_u, sizeof(u3_ular)) ) { uL(fprintf(uH, "rest_nuu failed (i)\n")); perror("write"); u3_lo_bail(); } ent_d++; end_d = tar_d + c3_wiseof(u3_ular); u3z(roe); roe = nex; } } if ( 0 != rename(nuu_c, old_c) ) { uL(fprintf(uH, "rest_nuu failed (k)\n")); perror("rename"); u3_lo_bail(); } if ( -1 == lseek64(fud_i, sizeof(u3_uled), SEEK_SET) ) { uL(fprintf(uH, "rest_nuu failed (l)\n")); perror("lseek64"); u3_lo_bail(); } lug_u->fid_i = fud_i; lug_u->len_d = end_d; }
/* _sist_zest(): create a new, empty record. */ static void _sist_zest() { struct stat buf_b; c3_i fid_i; c3_c ful_c[8193]; c3_l sal_l; // Create the ship directory. // _sist_home(); // Create the record file. { c3_i pig_i = O_CREAT | O_WRONLY | O_EXCL; #ifdef O_DSYNC pig_i |= O_DSYNC; #endif snprintf(ful_c, 2048, "%s/.urb/egz.hope", u3_Host.dir_c); if ( ((fid_i = open(ful_c, pig_i, 0600)) < 0) || (fstat(fid_i, &buf_b) < 0) ) { uL(fprintf(uH, "can't create record (%s)\n", ful_c)); u3_lo_bail(); } #ifdef F_NOCACHE if ( -1 == fcntl(fid_i, F_NOCACHE, 1) ) { uL(fprintf(uH, "zest: can't uncache %s: %s\n", ful_c, strerror(errno))); u3_lo_bail(); } #endif u3Z->lug_u.fid_i = fid_i; } // Generate a 31-bit salt. // { c3_w rad_w[8]; c3_rand(rad_w); sal_l = (0x7fffffff & rad_w[0]); } // Create and save a passcode. // { c3_w rad_w[8]; u3_noun pas; c3_rand(rad_w); pas = u3i_words(2, rad_w); u3A->key = _sist_fatt(sal_l, u3k(pas)); _sist_fast(pas, u3r_mug(u3A->key)); } // Write the header. { u3_uled led_u; led_u.mag_l = u3r_mug('g'); led_u.kno_w = 163; if ( 0 == u3A->key ) { led_u.key_l = 0; } else { led_u.key_l = u3r_mug(u3A->key); c3_assert(!(led_u.key_l >> 31)); } led_u.sal_l = sal_l; led_u.sev_l = u3A->sev_l; led_u.tno_l = 1; if ( sizeof(led_u) != write(fid_i, &led_u, sizeof(led_u)) ) { uL(fprintf(uH, "can't write record (%s)\n", ful_c)); u3_lo_bail(); } u3Z->lug_u.len_d = c3_wiseof(led_u); } // Work through the boot events. u3_raft_work(); }
/* u3_sist_pack(): write a blob to disk, transferring. */ c3_d u3_sist_pack(c3_w tem_w, c3_w typ_w, c3_w* bob_w, c3_w len_w) { u3_ulog* lug_u = &u3Z->lug_u; c3_d tar_d; u3_ular lar_u; tar_d = lug_u->len_d + len_w; lar_u.tem_w = tem_w; lar_u.typ_w = typ_w; lar_u.syn_w = u3r_mug((c3_w)tar_d); lar_u.mug_w = u3r_mug_both(u3r_mug_words(bob_w, len_w), u3r_mug_both(u3r_mug(lar_u.tem_w), u3r_mug(lar_u.typ_w))); lar_u.ent_d = u3A->ent_d; u3A->ent_d++; lar_u.len_w = len_w; if ( -1 == lseek64(lug_u->fid_i, 4ULL * tar_d, SEEK_SET) ) { perror("lseek"); uL(fprintf(uH, "sist_pack: seek failed\n")); c3_assert(0); } if ( sizeof(lar_u) != write(lug_u->fid_i, &lar_u, sizeof(lar_u)) ) { perror("write"); uL(fprintf(uH, "sist_pack: write failed\n")); c3_assert(0); } if ( -1 == lseek64(lug_u->fid_i, 4ULL * lug_u->len_d, SEEK_SET) ) { perror("lseek"); uL(fprintf(uH, "sist_pack: seek failed\n")); c3_assert(0); } #if 0 uL(fprintf(uH, "sist_pack: write %" PRIu64 ", %" PRIu64 ": lar ent %" PRIu64 ", len %d, mug %x\n", lug_u->len_d, tar_d, lar_u.ent_d, lar_u.len_w, lar_u.mug_w)); #endif if ( (4 * len_w) != write(lug_u->fid_i, bob_w, (4 * len_w)) ) { perror("write"); uL(fprintf(uH, "sist_pack: write failed\n")); c3_assert(0); } lug_u->len_d += (c3_d)(lar_u.len_w + c3_wiseof(lar_u)); free(bob_w); // Sync. Or, what goes by sync. { fsync(lug_u->fid_i); // fsync is almost useless, F_FULLFSYNC too slow #if defined(U3_OS_linux) fdatasync(lug_u->fid_i); #elif defined(U3_OS_osx) fcntl(lug_u->fid_i, F_FULLFSYNC); #elif defined(U3_OS_bsd) fsync(lug_u->fid_i); #else # error "port: datasync" #endif } return u3A->ent_d; }
/* u2_wr_init(): ** ** Install an empty wire within `hat_r` and `mat_r` in the loom, ** with memory model `hip`. ** ** Returns ray to wire, which always equalls the passed `mat_r`. */ u2_ray u2_wr_init(c3_m hip_m, u2_ray hat_r, u2_ray mat_r) { u2_ray wir_r; wir_r = u2_rl_init(hip_m, hat_r, mat_r); u2_rail_hat_r(wir_r) += (c3_wiseof(u2_loom_wire) - c3_wiseof(u2_loom_rail)); #if 1 u2_wire_bas_r(wir_r) = 0; #endif u2_wire_kit_r(wir_r) = 0; u2_cs_init(u2_wire_des_r(wir_r)); /* Trace stack, garbage. */ { u2_wire_tax(wir_r) = u2_nul; u2_wire_lan(wir_r) = u2_yes; } /* Permanent basket = 1/16 of address space. */ { u2_ray bas_r; bas_r = u2_rl_leap_part(wir_r, c3__sand, 1, 16, 0); u2_wire_bas_r(wir_r) = bas_r; #if 0 fprintf(stderr, "bas_r %d, hat %d, mat %d, cap %d, rut %d\n", bas_r >> LoomPageWords, u2_rail_hat_r(bas_r) >> LoomPageWords, u2_rail_mat_r(bas_r) >> LoomPageWords, u2_rail_cap_r(bas_r) >> LoomPageWords, u2_rail_rut_r(bas_r) >> LoomPageWords); fprintf(stderr, "wir_r %d, hat %d, mat %d, cap %d, rut %d\n", wir_r >> LoomPageWords, u2_rail_hat_r(wir_r) >> LoomPageWords, u2_rail_mat_r(wir_r) >> LoomPageWords, u2_rail_cap_r(wir_r) >> LoomPageWords, u2_rail_rut_r(wir_r) >> LoomPageWords); #endif // u2_ba_init(wir_r, 0); } #if 1 /* Host control. */ { u2_ho_push(); } #endif /* Basic performance tracking. */ { u2_wire_bex_r(wir_r) = u2_rl_ralloc(wir_r, c3_wiseof(u2_loom_benx)); u2_bx_boot(wir_r); } /* New performance tracking. */ { u2_wire_rac_r(wir_r) = u2_tx_init(wir_r); } /* Global namespace. */ { u2_wire_hev_r(wir_r) = u2_hv_init(wir_r); } /* OS kernel. */ { u2_wire_arv_r(wir_r) = u2_rl_ralloc(wir_r, c3_wiseof(u2_reck)); } return wir_r; }