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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <vadefs.h>
#include "smount.h"
#include "dirent.h"
#define isseparator(c) (c == '/' || c == '\\')
static smtPath* smtnewpath(int type, const char* path, smtPath* next)
{
smtPath* temp = (smtPath*)malloc(sizeof(smtPath));
int len = strlen(path);
temp->path = (char*)malloc(len + 1);
memcpy(temp->path, path, len);
temp->path[len] = '\0';
// trim trailing separator
while (isseparator(temp->path[--len]))
temp->path[len] = '\0';
temp->next = next;
temp->type = type;
return temp;
}
static const char* mkstr(const char* str)
{
int len = strlen(str);
char* temp = (char*)malloc(strlen(str));
memcpy(temp, str, len);
return temp;
}
/**
* Create a new shared contex.
*/
smtShared* smtnewshared()
{
smtShared* shared = (smtShared*)malloc(sizeof(smtShared));
shared->mount = 0;
return shared;
}
/**
* Concatenate strings together.
*/
char* concat(const char *str, ...) {
va_list args;
const char *s;
// Get len
int len = strlen(str);
va_start(args, str);
while ((s = va_arg(args, char*))) {
len += strlen(s);
}
va_end(args);
// Build string
char *res = (char*)malloc(len + 1);
if (!res) return NULL;
strcpy(res, str);
va_start(args, str);
while ((s = va_arg(args, char*))) {
strcat(res, s);
}
va_end(args);
return res;
}
static int isdir(const char* path) {
struct stat s;
int res = stat(path, &s);
return S_ISDIR(s.st_mode);
}
int smtmount(smtShared* S, const char *path)
{
if (!isdir(path))
return SMT_INVALIDMOUNT;
S->mount = smtnewpath(PATH_DIR, path, 0);
return SMT_SUCCESS;
}
void smtunmount(smtShared* S)
{
smtPath* mount = S->mount;
while (mount)
{
free(mount->path);
mount = mount->next;
}
}
int smtexists(smtShared* S, const char *path)
{
char* r = concat(S->mount->path, "/", path, 0);
if (!r) return SMT_NOSUCHDIR;
struct stat s;
int res = stat(r, &s);
free(r);
if (res == 0)
return SMT_SUCCESS;
return SMT_NOSUCHDIR;
}
static struct stat smtgetstat(smtShared* S, const char*path)
{
char* r = concat(S->mount->path, "/", path, 0);
struct stat s = {0};
if (!r) return s;
int res = stat(r, &s);
free(r);
return s;
}
int smtisdir(smtShared* S, const char *path)
{
struct stat s = smtgetstat(S, path);
return S_ISDIR(s.st_mode);
}
int smtisreg(smtShared* S, const char *path)
{
struct stat s = smtgetstat(S, path);
return S_ISREG(s.st_mode);
}
const char* smterrstr(int e)
{
switch (e)
{
case SMT_INVALIDMOUNT: return "invalid mount directory";
default: return "unknown error";
}
}
void *smtread(smtShared* S, const char *path, unsigned int *size)
{
if (size == NULL) return NULL;
if (!smtisreg(S, path)) return NULL;
char *r = concat(S->mount->path, "/", path, NULL);
if (!r)
return NULL;
FILE *fp = fopen(r, "rb");
free(r);
if (!fp)
return 0;
/* Get file size */
fseek(fp, 0, SEEK_END);
*size = ftell(fp);
/* Load file */
fseek(fp, 0, SEEK_SET);
char *res = (char*)malloc(*size + 1);
if (!res) return NULL;
res[*size] = '\0';
if (fread(res, 1, *size, fp) != *size)
{
free(res);
fclose(fp);
return NULL;
}
fclose(fp);
return res;
}
char* smtfullpath(smtShared* S, const char* path)
{
return concat(S->mount->path, "/", path, 0);
}
int smtsize(smtShared* S, const char *path)
{
struct stat s = smtgetstat(S, path);
return s.st_size;
}
void smtdelete(smtShared* S, const char *path)
{
char* name = smtfullpath(S, path);
remove(name);
free(name);
}
int smtwrite(smtShared* S, const char *path, const void *data, int size)
{
char* name = smtfullpath(S, path);
if (!name) return SMT_NOSUCHDIR;
FILE *fp = fopen(name, "wb");
free(name);
if (!fp) return SMT_UNABLEOPEN;
int res = fwrite(data, size, 1, fp);
fclose(fp);
return (res == 1) ? SMT_SUCCESS : SMT_CANTWRITE;
}
|