-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.c
More file actions
394 lines (354 loc) · 8.03 KB
/
Copy pathcontrol.c
File metadata and controls
394 lines (354 loc) · 8.03 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#define _XOPEN_SOURCE 700
#define IXP_NO_P9_
#define IXP_P9_STRUCTS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <ixp.h>
#include "omm.h"
/// Length of string: "tcp!ip!port"
#define ADDR_MAX (64)
/// Length of string: "<objid>/meta"
#define PATH_MAX (128)
/// Length of string: "put <mrl>"
#define MRL_MAX (128)
/// Size of obj meta data buffer
#define META_MAX (1024)
/// Length of fav command
#define FAV_MAX (128)
// #define COL_SEP "|"
#define COL_SEP "│"
#define fatal(...) ixp_eprint("ixpc: fatal: " __VA_ARGS__); \
static IxpClient *serve, *render;
/// Set the IP address of the omm servers via environment variable
static char *omm_ip_envar = "OMM_ADDRESS";
static char *serve_ip_envar = "OMM_SERVE_ADDRESS";
static char *render_ip_envar = "OMM_RENDER_ADDRESS";
/// Default IP address of omm servers
static char *omm_ip = "127.0.0.1";
static char *serve_ip = NULL;
static char *render_ip = NULL;
static int serve_port = 2001;
static int render_port = 2002;
/// FIXME server and renderer addresses should be dynamic arrays
static char serve_addr[ADDR_MAX] = {0};
static char render_addr[ADDR_MAX] = {0};
enum {
MET_TYPE = 0,
MET_FMT,
MET_DUR,
MET_ORIG,
MET_ALBUM,
MET_TRACK,
MET_TITLE,
MET_PATH,
MET_CNT,
};
struct time {
int h, m, s, ms;
};
void msec2time(struct time *t, uint64_t ms)
{
t->s = ms / 1e3f;
t->ms = ms % 1000;
t->m = t->s / 60;
t->s = t->s % 60;
t->h = t->m / 60;
t->m = t->m % 60;
t->h = t->h % 60;
}
int write_sqry_cmdbuf(char *buf);
void
print_direntry(Stat *stat)
{
char path[PATH_MAX] = {0};
char *buf;
int count;
IxpCFid *fid;
/// Entry is a file, we're looking for dirs, only
if ((stat->mode & P9_DMDIR) == 0) return;
/// Stat data file
sprintf(path, "/%s/data", stat->name);
Stat *dstat = ixp_stat(serve, path);
if(dstat == NULL) {
fprintf(stderr, "failed to stat '%s', skipping ...\n", path);
return;
}
uint64_t fsize = dstat->length;
ixp_freestat(dstat);
/// Read meta file
sprintf(path, "/%s/meta", stat->name);
fid = ixp_open(serve, path, P9_OREAD);
if(fid == NULL) {
fprintf(stderr, "failed to open '%s', skipping ...\n", path);
return;
}
int meta_len = 0;
char meta[META_MAX] = {0};
buf = ixp_emalloc(fid->iounit);
while((count = ixp_read(fid, buf, fid->iounit)) > 0) {
memcpy(meta + meta_len, buf, count);
meta_len += count;
}
ixp_close(fid);
free(buf);
if(count == -1) {
fprintf(stderr, "failed to read from '%s': %s\n", path, ixp_errbuf());
return;
}
if(meta_len == 0) {
fprintf(stderr, "'%s' is empty\n", path);
return;
}
char *metargs[MET_CNT] = {0};
metargs[0] = meta;
char *ma = meta;
for (int m = 1; m < MET_CNT; ++m) {
ma = memchr(ma, LIST_SEP, meta_len - (ma - meta));
*(ma) = '\0';
ma++;
metargs[m] = ma;
}
struct time t = {0};
uint64_t ms = atol(metargs[MET_DUR]);
msec2time(&t, ms);
fprintf(stdout,
"%2s " COL_SEP " %4.1f MB " \
COL_SEP " %02d:%02d:%02d " COL_SEP \
" %16s " COL_SEP " %s\n",
stat->name, fsize / 1e6,
t.h, t.m, t.s,
metargs[MET_ORIG], metargs[MET_TITLE]);
}
static int
xls(int argc, char *argv[])
{
if (argc == 1) {
write_sqry_cmdbuf("");
} else if (argc == 2) {
write_sqry_cmdbuf(argv[1]);
} else if (argc > 2) {
fprintf(stderr, "usage: %s <search pattern>\n", argv[0]);
return 1;
}
IxpMsg m;
Stat *stat;
IxpCFid *fid;
char *file, *buf;
int count, i;
int ret = 1;
file = "/";
stat = ixp_stat(serve, file);
if(stat == NULL) {
fprintf(stderr, "failed to stat file '%s': %s\n", file, ixp_errbuf());
goto cleanup;
}
if((stat->mode & P9_DMDIR) == 0) {
fprintf(stderr, "root is a file, skipping ...");
goto cleanup;
}
ixp_freestat(stat);
fid = ixp_open(serve, file, P9_OREAD);
if(fid == NULL) {
fprintf(stderr, "failed to open dir '%s': %s\n", file, ixp_errbuf());
goto cleanup;
}
/// Read the root dir and stat each entry
stat = ixp_emalloc(sizeof(*stat));
buf = ixp_emalloc(fid->iounit);
while((count = ixp_read(fid, buf, fid->iounit)) > 0) {
m = ixp_message(buf, count, MsgUnpack);
while(m.pos < m.end) {
ixp_pstat(&m, stat);
print_direntry(stat);
}
}
ixp_close(fid);
free(buf);
if(count == -1) {
fprintf(stderr, "failed to read dir '%s': %s\n", file, ixp_errbuf());
goto cleanup;
}
ret = 0;
cleanup:
ixp_freestat(stat);
return ret;
}
void
write_buf(IxpCFid *fid, char *buf, int len)
{
/// FIXME ixp_write() seems to write even though count == 0,
/// ... also, we must write one more byte ...
len++;
int pos = 0, count = 0;
while (pos < len && (count = ixp_write(fid, buf + pos, len - pos)) > 0) {
pos += count;
}
}
int
write_rctl_cmdbuf(char *buf)
{
char *file = "/ctl";
IxpCFid *fid = ixp_open(render, file, P9_OWRITE);
if(fid == NULL) {
fprintf(stderr, "failed to open ommrender ctl file: %s\n", ixp_errbuf());
return 1;
}
write_buf(fid, buf, strlen(buf));
ixp_close(fid);
return 0;
}
int
write_sqry_cmdbuf(char *buf)
{
char *file = "/query";
IxpCFid *fid = ixp_open(serve, file, P9_OWRITE);
if(fid == NULL) {
fprintf(stderr, "failed to open ommserve query file: %s\n", ixp_errbuf());
return 1;
}
write_buf(fid, buf, strlen(buf));
ixp_close(fid);
return 0;
}
int
write_sctl_cmdbuf(char *buf)
{
char *file = "/ctl";
IxpCFid *fid = ixp_open(serve, file, P9_OWRITE);
if(fid == NULL) {
fprintf(stderr, "failed to open ommserve ctl file: %s\n", ixp_errbuf());
return 1;
}
write_buf(fid, buf, strlen(buf));
ixp_close(fid);
return 0;
}
static int
xnoparms(int argc, char *argv[])
{
if (argc >= 2) {
fprintf(stderr, "usage: %s\n", argv[0]);
return 1;
}
return write_rctl_cmdbuf(argv[0]);
}
static int
xput(int argc, char *argv[])
{
char *arg;
if (argc == 2) {
arg = argv[1];
}
else {
fprintf(stderr, "usage: %s <media id>\n", argv[0]);
return 1;
}
char buf[MRL_MAX] = {0};
sprintf(buf, "%s 9p://%s/%s/data", argv[0], serve_addr, arg);
return write_rctl_cmdbuf(buf);
}
static int
xseek(int argc, char *argv[])
{
char *arg;
if (argc == 2) {
arg = argv[1];
}
else {
fprintf(stderr, "usage: %s <position percentage>\n", argv[0]);
return 1;
}
char buf[MRL_MAX] = {0};
sprintf(buf, "%s %s", argv[0], arg);
return write_rctl_cmdbuf(buf);
}
static int
xvol(int argc, char *argv[])
{
char *arg;
if (argc == 2) {
arg = argv[1];
}
else {
fprintf(stderr, "usage: %s <volume percentage>\n", argv[0]);
return 1;
}
char buf[MRL_MAX] = {0};
sprintf(buf, "%s %s", argv[0], arg);
return write_rctl_cmdbuf(buf);
}
static int
xfav(int argc, char *argv[])
{
char buf[FAV_MAX] = {0};
if (argc == 4) {
sprintf(buf, "%s %s %s %s", argv[0], argv[1], argv[2], argv[3]);
} else if (argc == 3) {
sprintf(buf, "%s %s %s", argv[0], argv[1], argv[2]);
} else if (argc == 2) {
sprintf(buf, "%s %s", argv[0], argv[1]);
} else {
fprintf(stderr, "usage:\n %s add|del <favlist id> <media id>\n %s set <favlist id>\n",
argv[0], argv[0]);
return 1;
}
return write_sctl_cmdbuf(buf);
}
struct exectab {
char *cmd;
int (*fn)(int, char**);
} etab[] = {
{"ls", xls},
{"put", xput},
{"play", xnoparms},
{"stop", xnoparms},
{"pause", xnoparms},
{"fav", xfav},
{"seek", xseek},
{"vol", xvol},
{0, 0}
};
int
main(int argc, char *argv[]) {
char *cmd;
struct exectab *tab;
int ret = 1;
char *oe = getenv(omm_ip_envar);
if (oe) omm_ip = oe;
serve_ip = getenv(serve_ip_envar);
if (!serve_ip) serve_ip = omm_ip;
render_ip = getenv(render_ip_envar);
if (!render_ip) render_ip = omm_ip;
sprintf(serve_addr, "tcp!%s!%d", serve_ip, serve_port);
sprintf(render_addr, "tcp!%s!%d", render_ip, render_port);
serve = ixp_mount(serve_addr);
if(serve == NULL) {
fatal("ommserve not available, %s\n", ixp_errbuf());
}
render = ixp_mount(render_addr);
if(render == NULL) {
fprintf(stderr, "ommrender not available, %s\n", ixp_errbuf());
}
if (argc == 1) {
cmd = "ls";
} else {
cmd = argv[1];
}
for(tab = etab; tab->cmd; tab++) {
if(strcmp(cmd, tab->cmd) == 0) break;
}
if (tab->cmd == NULL) {
fprintf(stderr, "unknown command '%s'\n", cmd);
} else {
ret = tab->fn(argc - 1, argv + 1);
}
ret = 0;
ixp_unmount(serve);
if (render) {
ixp_unmount(render);
}
return ret;
}