move realms under carts (realms per cart) and got carts realms and universes working

This commit is contained in:
Iris Lightshard 2021-07-18 22:58:08 -06:00
parent c524a9b65b
commit e469044b23
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
11 changed files with 124 additions and 46 deletions

2
.gitignore vendored
View file

@ -1,3 +1,3 @@
*.o
xrxs
realms/
carts/*/realms/

View file

1
carts/other/other.rom Normal file
View file

@ -0,0 +1 @@
this file is non empty!

View file

1
carts/test/test.rom Normal file
View file

@ -0,0 +1 @@
this file is nonempty~~

69
realm.c
View file

@ -3,35 +3,65 @@
#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 @@ Realm* parse_realm(char* name) {
return nil;
}
scpy(path, file, 64);
scpy(path, file, 128);
scat(file, "/universe");
self->universe = parse_universe(path);
return self;
@ -65,20 +95,25 @@ Realm* find_realm(UserInfo* table, char* name) {
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);
}
}

View file

@ -8,8 +8,8 @@ typedef struct Realm {
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);

View file

@ -29,11 +29,16 @@ Universe* parse_universe(char* realm_name) {
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 @@ Universe* parse_universe(char* realm_name) {
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");

View file

@ -14,7 +14,7 @@ typedef struct 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);

53
user.c
View file

@ -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 @@ int load_cart(UserInfo* table, char* uname, char* cart_name) {
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);

23
xrxs.c
View file

@ -106,8 +106,9 @@ void write_ctl(Req* r) {
}
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 @@ void write_ctl(Req* r) {
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 @@ end:
}
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 @@ void read_realms(Req* r) {
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;