move realms under carts (realms per cart) and got carts realms and universes working
Iris Lightshard nilix@nilfm.cc
PGP Signature
-----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEEkFh6dA+k/6CXFXU4O3+8IhROY5gFAmD1BmAACgkQO3+8IhRO Y5jt3Q/+MSJ5P4oBq58ou0nF8x9V35I6rfFqrur3WUqJxnAGwsRpUgKoJzYpgnWI Z6/BPGIQUAfcNDQkg68NSsPkJ/FPGTEt8VnpzZrb/FIoVcV+Bt4TQU0ZzjId25Lq xVS3tBK9QJd+e8CnUbiZk78Hi+HjG4U1A5BC2lnPZiJlN9tqRX1ujWBJH+5kTMQk wv+oJABWAQ0o7KeSwLUHPs3tRXBrsXuOv+I7X4H+51EZhSBeFs4QEo/QfA2Rtu6O RaGy2hZnG8tClnFeSjuisWxQSwF+w2LMGsE1IhKseYvpsH3vFD1wzQ51poqBjgW4 lYaQ57U/Hp58jtlBZ0Xnfsk0+9G0seeHN5oRVDiRU+XMGcN8usxb0djrW9MbQSso Hk274OT3o8HEc7PIs4JoyWIY6Ieqmd+hRccHlMzFGlM9BgZI+kKd1qnsVLModipz /n7AXHsCNSbvFvaAolGrtgLpy0Xynw4AXxzxo+A0Le0+zJzZgQSNDQloi8dFIthH FuVMmOfIChTSTZtd2gvpk65VUTBlaApuBPMzWjfFqosc2a5um6vCPRfN835g+xLB 6C/7eTKr8Qk1SfDxn4OJt6q4mwDdbs6KUuusWaqKccIuGJfJhDRm9RMICcn3gelw 2M5wUrSRtsbKZf7dQwvRS5QdPk1/+4+GqrqXco9TAFGUgj8H9tw= =cZMt -----END PGP SIGNATURE-----
11 files changed,
124 insertions(+),
46 deletions(-)
M
realm.c
→
realm.c
@@ -3,35 +3,65 @@ #include <libc.h>
#include <stdio.h> #include "util.h" #include "user.h" +#include "cart.h" #include "universe.h" #include "realm.h" -Realm* create_realm(char* name) { +Realm* create_realm(UserInfo* table, char* uname, char* name) { Realm* self; - char path[64]; - scat(path, "realms/"); + UserInfo* u = find_user(table, uname); + char cart[32]; + int max = 4; + char* n = name; + char path[128] = {0}; + + if (u == nil || u->cart == nil) + return 0; + scpy(u->cart->name, cart, 32); + + while (*n && *n != ' ') { + n++; + } + if (*n) { + *n = 0; + n++; + max = atoi(n); + } + + scat(path, "carts/"); + scat(path, cart); + scat(path, "/realms/"); + if (open(path, OREAD) < 0) + create(path, OREAD, DMDIR | 0755); + scat(path, name); - if (create(path, OREAD, 0755) < 0) { + fprintf(stderr, "trying to create realm: %s\n", path); + if (create(path, OREAD, DMDIR | 0755) < 0) { + fprintf(stderr, "failed to create realm backing store\n"); return nil; } else { self = malloc(sizeof(Realm)); scpy(name, self->name, 32); - self->max = 4; + self->max = max; self->password = 0; self->universe = create_universe(); + save_realm(cart, self); + fprintf(stderr, "created realm '%s'\n", name); return self; } } -Realm* parse_realm(char* name) { +Realm* parse_realm(char* cart, char* name) { Realm* self; FILE* f; - char path[64]; - char file[64]; + char path[128]; + char file[128]; char buf[256]; - scat(path, "realms/"); + scat(path, "carts/"); + scat(path, cart); + scat(path, "/realms/"); scat(path, name); - scpy(path, file, 64); + scpy(path, file, 128); scat(file, "/realm"); f = fopen(file, "r");@@ -47,7 +77,7 @@ } else {
return nil; } - scpy(path, file, 64); + scpy(path, file, 128); scat(file, "/universe"); self->universe = parse_universe(path); return self;@@ -65,20 +95,25 @@ }
return nil; } -void save_realm(Realm* self) { +void save_realm(char* cart, Realm* self) { FILE* f; - char path[64]; - char file[64]; - scat(path, "realms/"); + char path[128] = {0}; + char file[128] = {0}; + scat(path, "carts/"); + scat(path, cart); + scat(path, "/realms/"); scat(path, self->name); - scpy(path, file, 64); + scpy(path, file, 128); scat(file, "/realm"); f = fopen(file, "w"); if (f != nil) { fprintf(f, "%hu %llu", self->max, self->password); fclose(f); - save_universe(self->universe, self->name); + save_universe(cart, self->universe, self->name); + fprintf(stderr, "saved realm data"); + } else { + fprintf(stderr, "error saving realm: '%s'", file); } }
M
realm.h
→
realm.h
@@ -8,8 +8,8 @@ uvlong password;
Universe* universe; } Realm; -Realm* create_realm(char* name); -Realm* parse_realm(char* name); +Realm* create_realm(UserInfo* table, char* cart, char* name); +Realm* parse_realm(char* cart, char* name); Realm* find_realm(UserInfo* table, char* name); -void save_realm(Realm* self); +void save_realm(char* cart, Realm* self); void destroy_realm(Realm* self);
M
universe.c
→
universe.c
@@ -29,11 +29,16 @@
f = fopen(path, "r"); if (f != nil) { self = malloc(sizeof(Universe)); - while (fgets(buf, 256, f) != nil) { + while (fgets(buf, 256, f) != nil && !scmp(buf, 0)) { + fprintf(stderr, "read a line\n"); sscanf(buf, "%16s = %64s", name, value); + fprintf(stderr, "parsed a line\n"); a = malloc(sizeof(Atom)); + fprintf(stderr, "allocated an atom\n"); scpy(name, a->name, 16); + fprintf(stderr, "assigned name: %s\n", name); scpy(value, a->value, 64); + fprintf(stderr, "assigned value: %s\n", value); a->next = nil; set_atom(self, a); }@@ -43,13 +48,15 @@ }
return nil; } -void save_universe(Universe* self, char* realm_name) { +void save_universe(char* cart, Universe* self, char* realm_name) { char path[64] = {0}; FILE* f; Atom* a; int i; - scat(path, "realms/"); + scat(path, "carts/"); + scat(path, cart); + scat(path, "/realms/"); scat(path, realm_name); scat(path, "/universe");
M
universe.h
→
universe.h
@@ -14,7 +14,7 @@ } Universe;
Universe* create_universe(); Universe* parse_universe(char* realm_name); -void save_universe(Universe* self, char* realm_name); +void save_universe(char* cart, Universe* self, char* realm_name); void set_atom(Universe* self, Atom* atom); Atom* get_atom(Universe* self, char* name); void remove_atom(Universe* self, char* name);
M
user.c
→
user.c
@@ -1,5 +1,6 @@
#include <u.h> #include <libc.h> +#include <stdio.h> #include "util.h" #include "cart.h" #include "realm.h"@@ -30,37 +31,50 @@ u->cart = c;
return 1; } else { u->cart = create_cart(cart_name); - if (u->cart == nil) + if (u->cart == nil) { + fprintf(stderr, "failed creating cart\n"); return 0; - else + } else { return 1; + } } } int enter_realm(UserInfo* table, char* uname, char* realm_name) { Realm* r = find_realm(table, realm_name); UserInfo* u = find_user(table, uname); - int i, j; + int i, j = 0; - if (u == nil || u->cart == nil) + if (u == nil) { + fprintf(stderr, "no user\n"); + return 0; + } + if (u->cart == nil) { + fprintf(stderr, "no cart\n"); return 0; + } - for (i = j = 0; i < 64; i++) { + for (i = 0; i < 64; i++) { + fprintf(stderr, "iterating through users table: slot %d\n", i); if (table[i].realm != nil && scmp(table[i].realm->name, realm_name)) j++; } - if (j >= r->max) - return 0; if (r != nil) { + if (j < r->max) { + u->realm = r; + return 1; + } else { + return 0; + } + } else { + r = parse_realm(u->cart->name, realm_name); + if (r == nil) + return 0; + if (j <= r->max) + return 0; u->realm = r; return 1; - } else { - u->realm = parse_realm(realm_name); - if (u->realm == nil) - return 0; - else - return 1; } }@@ -68,14 +82,21 @@ int leave_realm(UserInfo* table, char* uname) {
UserInfo* u = find_user(table, uname); Realm* r; - if (u == nil) + if (u == nil) { + fprintf(stderr, "couldn't find user: %s\n", uname); return 0; + } r = u->realm; - if (r == nil) + if (r == nil) { + fprintf(stderr, "%s is not in a realm!\n", uname); + return 0; + } + + if (u->cart == nil) return 0; - save_realm(r); + save_realm(u->cart->name, r); u->realm = nil; if (find_realm(table, r->name) == nil) destroy_realm(r);
M
xrxs.c
→
xrxs.c
@@ -106,8 +106,9 @@ ccat(cmd, *c++);
} if (*c == ' ') c++; + scsw(c, '\n', 0); - fprintf(stderr, cmd); + fprintf(stderr, "%s(%s)\n", cmd, c); uvlong const cmd_hashv = hash(cmd, 0); switch (cmd_hashv) { case LOGIN:@@ -117,7 +118,7 @@ case LOAD:
load_cart(users_table, r->fid->uid, c); break; case CREATE: - create_realm(c); + create_realm(users_table, r->fid->uid, c); break; case PROTECT: protect(r->fid->uid, c);@@ -291,8 +292,10 @@ respond(r, nil);
} void read_realms(Req* r) { - String** realms = list_dir(REALMSPATH); - String** rr = realms; + UserInfo* user = find_user(users_table, r->fid->uid); + char realm_path[128] = {0}; + String** realms; + String** rr; Realm* realm; char data[4096] = {0}; int i, u, m, p;@@ -300,11 +303,21 @@ char ubuf[8] = {0};
char mbuf[8] = {0}; char pbuf[2] = {0}; + if (user->cart == nil) + respond(r, nil); + + scat(realm_path, "carts/"); + scat(realm_path, user->cart->name); + scat(realm_path, "/realms"); + + realms = list_dir(realm_path); + rr = realms; + while (*rr != nil) { scat(data, (*rr)->base); ccat(data, ' '); - realm = parse_realm((*rr)->base); + realm = parse_realm(user->cart->name, (*rr)->base); m = realm->max; p = realm->password ? 1 : 0;