-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfs.c
More file actions
151 lines (132 loc) · 2.24 KB
/
Copy pathfs.c
File metadata and controls
151 lines (132 loc) · 2.24 KB
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
#include <u.h>
#include <libc.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>
#include <libString.h>
#include "reddit_client.h"
typedef struct SubFid SubFid;
struct SubFid
{
char* src;
char* data;
};
static void xattach(Req*);
static void xopen(Req*);
static char* xwalk1(Fid*, char*, Qid*);
static char* xclone(Fid*, Fid*);
static void xread(Req*);
static void xdestroyfid(Fid*);
static char* readsub(SubFid*);
char Eperm[] = "permission denied";
char Esubdir[] = "subdirectory";
Srv xsrv;
void
xinit(void)
{
xsrv.attach = xattach;
xsrv.open = xopen;
xsrv.walk1 = xwalk1;
xsrv.clone = xclone;
xsrv.read = xread;
xsrv.destroyfid = xdestroyfid;
}
static void
xattach(Req *r)
{
SubFid *sf;
Qid q;
q.type = QTDIR;
q.vers = 0;
q.path = 1;
r->ofcall.qid = q;
r->fid->qid = q;
sf = emalloc9p(sizeof(SubFid));
r->fid->aux = sf;
respond(r, nil);
}
static char*
xwalk1(Fid *fid, char *name, Qid *qid)
{
SubFid *sf;
Qid q;
if(fid->qid.path != 1)
return Esubdir;
sf = fid->aux;
sf->src = estrdup9p(name);
q.type = QTFILE;
q.vers = 0;
q.path = 2;
*qid = q;
fid->qid = q;
return nil;
}
static char*
xclone(Fid *oldfid, Fid *newfid)
{
SubFid *sf, *nsf;
sf = oldfid->aux;
if(sf == nil)
return nil;
nsf = emalloc9p(sizeof(SubFid));
if(sf->src != nil)
nsf->src = estrdup9p(sf->src);
if(sf->data != nil)
nsf->data = estrdup9p(sf->data);
newfid->aux = nsf;
return nil;
}
static void
xopen(Req *r)
{
char *err;
if(r->ifcall.mode != OREAD){
respond(r, Eperm);
return;
}
r->ofcall.qid = r->fid->qid;
err = readsub(r->fid->aux);
respond(r, err);
}
static void
xread(Req *r)
{
SubFid *sf;
sf = r->fid->aux;
readstr(r, sf->data);
respond(r, nil);
}
static void
xdestroyfid(Fid* fid)
{
SubFid *sf;
sf = fid->aux;
if(sf == nil)
return;
free(sf->src);
free(sf->data);
free(sf);
}
static char*
readsub(SubFid *sf)
{
String *str;
Post **posts;
Post *post;
Error error;
char buf[1024];
posts = getposts(sf->src, &error);
if(posts == nil)
return estrdup9p(error.message);
str = s_newalloc(1024);
posts_foreach(post, posts) {
snprint(buf, 1024, "%ld - %s\n %s\n",
post->score,
post->title,
post->url);
s_append(str, buf);
}
sf->data = estrdup9p(s_to_c(str));
s_free(str);
return nil;
}