1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
#include "ds_mount.h"
#include "jx.h"
#include "debug.h"
#include <stdlib.h>
#include <string.h>
struct ds_mount * ds_mounts_create( struct jx *jmounts )
{
struct jx_pair *p;
struct ds_mount *head = 0;
if (!jmounts) return NULL;
for(p=jmounts->u.pairs;p;p=p->next) {
const char *key = p->key->u.string_value;
struct jx *value = p->value;
struct ds_mount *m;
m = ds_mount_create(key,value);
m->next = head;
head = m;
}
return head;
}
dataswarm_flags_t dataswarm_flags_parse( const char *s )
{
if(!s) return 0;
int flags = 0;
while(*s) {
switch(*s) {
case 'r':
case 'R':
flags |= DS_FLAGS_READ;
break;
case 'w':
case 'W':
flags |= DS_FLAGS_WRITE;
break;
case 'a':
case 'A':
flags |= DS_FLAGS_APPEND;
break;
default:
debug(D_NOTICE|D_DATASWARM,"igoring invalid mount flag: %c\n",*s);
break;
}
s++;
}
return flags;
}
struct jx * dataswarm_flags_to_jx( dataswarm_flags_t flags )
{
static char str[4];
str[0] = 0;
if(flags&DS_FLAGS_READ) strcat(str,"R");
if(flags&DS_FLAGS_WRITE) strcat(str,"W");
if(flags&DS_FLAGS_APPEND) strcat(str,"A");
return jx_string(str);
}
struct ds_mount * ds_mount_create( const char *uuid, struct jx *jmount )
{
struct ds_mount *m = malloc(sizeof(*m));
memset(m,0,sizeof(*m));
m->uuid = strdup(uuid);
const char *type = jx_lookup_string(jmount,"type");
if(!strcmp(type,"path")) {
m->type = DS_MOUNT_PATH;
m->path = jx_lookup_string_dup(jmount,"path");
m->flags = dataswarm_flags_parse(jx_lookup_string(jmount,"flags"));
} else if(!strcmp(type,"fd")) {
m->type = DS_MOUNT_FD;
m->fd = jx_lookup_integer(jmount,"fd");
m->flags = dataswarm_flags_parse(jx_lookup_string(jmount,"flags"));
} else if(!strcmp(type,"stdin")) {
m->type = DS_MOUNT_FD;
m->fd = 0;
m->flags = DS_FLAGS_READ;
} else if(!strcmp(type,"stdout")) {
m->type = DS_MOUNT_FD;
m->fd = 1;
m->flags = DS_FLAGS_WRITE|DS_FLAGS_TRUNCATE;
} else if(!strcmp(type,"stderr")) {
m->type = DS_MOUNT_FD;
m->fd = 2;
m->flags = DS_FLAGS_WRITE|DS_FLAGS_TRUNCATE;
} else {
ds_mount_delete(m);
return 0;
}
return m;
}
struct jx * ds_mounts_to_jx( struct ds_mount *m )
{
struct jx *jmounts = jx_object(0);
while(m) {
struct jx *jm = ds_mount_to_jx(m);
jx_insert(jmounts,jx_string(m->uuid),jm);
m = m->next;
}
return jmounts;
}
struct jx * ds_mount_to_jx( struct ds_mount *m )
{
struct jx *j = jx_object(0);
if(m->type==DS_MOUNT_PATH) {
jx_insert_string(j,"type","path");
jx_insert_string(j,"path",m->path);
jx_insert(j,jx_string("flags"),dataswarm_flags_to_jx(m->flags));
} else if(m->type==DS_MOUNT_FD) {
jx_insert_string(j,"type","fd");
jx_insert_integer(j,"fd",m->fd);
jx_insert(j,jx_string("flags"),dataswarm_flags_to_jx(m->flags));
}
return j;
}
void ds_mount_delete( struct ds_mount *m )
{
if(!m) return;
ds_mount_delete(m->next);
free(m->path);
free(m);
}
|