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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
#include "data.h"
#include "player.h"
#include "list.h"
#include "log.h"
#include "ref.h"
#include "track.h"
#include "tag.h"
#include <fts.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdbool.h>
#include <string.h>
const char *datadir;
struct list tracks;
struct list tags;
struct list tags_sel;
void
data_load(void)
{
struct dirent *ent;
struct tag *tag;
struct stat st;
char *path;
DIR *dir;
list_init(&tracks);
list_init(&tags);
list_init(&tags_sel);
datadir = getenv("TMUS_DATA");
if (!datadir) ERROR("TMUS_DATA not set!\n");
dir = opendir(datadir);
if (!dir) ERROR("Failed to access dir: %s\n", datadir);
while ((ent = readdir(dir))) {
if (!strcmp(ent->d_name, "."))
continue;
if (!strcmp(ent->d_name, ".."))
continue;
path = aprintf("%s/%s", datadir, ent->d_name);
OOM_CHECK(path);
if (!stat(path, &st) && S_ISDIR(st.st_mode)) {
tag = tag_alloc(datadir, ent->d_name);
OOM_CHECK(tag);
tracks_load(tag);
list_push_back(&tags, LINK(tag));
}
free(path);
}
closedir(dir);
list_sort(&tracks, track_fid_compare);
}
void
data_save(void)
{
struct link *iter;
struct tag *tag;
for (LIST_ITER(&tags, iter)) {
tag = UPCAST(iter, struct tag);
tracks_save(tag);
}
}
void
data_free(void)
{
struct link *track_link;
struct link *tag_link;
struct tag *tag;
log_info("MAIN: data_free()\n");
refs_free(&tracks);
refs_free(&tags_sel);
while (!list_empty(&tags)) {
tag_link = list_pop_front(&tags);
tag = UPCAST(tag_link, struct tag);
while (!list_empty(&tag->tracks)) {
track_link = list_pop_front(&tag->tracks);
track_free(UPCAST(track_link, struct ref)->data);
ref_free(UPCAST(track_link, struct ref));
}
tag_free(tag);
}
}
int
get_fid(const char *path)
{
struct stat st;
return stat(path, &st) ? -1 : st.st_ino;
}
int
track_fid_compare(struct link *a, struct link *b)
{
struct track *ta, *tb;
ta = UPCAST(a, struct ref)->data;
tb = UPCAST(b, struct ref)->data;
return ta->fid - tb->fid;
}
void
index_update(struct tag *tag)
{
struct dirent *ent;
struct stat st;
char *path;
FILE *file;
DIR *dir;
int fid;
path = aprintf("%s/index", tag->fpath);
OOM_CHECK(path);
dir = opendir(tag->fpath);
if (!dir) ERROR("Failed to access dir: %s\n", tag->fpath);
file = fopen(path, "w+");
if (!file) ERROR("Failed to create file: %s\n", path);
free(path);
while ((ent = readdir(dir))) {
if (!strcmp(ent->d_name, "."))
continue;
if (!strcmp(ent->d_name, ".."))
continue;
if (!strcmp(ent->d_name, "index"))
continue;
/* skip files without extension */
if (!strchr(ent->d_name + 1, '.'))
continue;
path = aprintf("%s/%s", tag->fpath, ent->d_name);
OOM_CHECK(path);
fid = get_fid(path);
free(path);
fprintf(file, "%i:%s\n", fid, ent->d_name);
}
closedir(dir);
fclose(file);
}
bool
tracks_update(struct tag *tag)
{
struct dirent *ent;
struct stat st;
struct link *link;
struct ref *ref;
struct track *track;
char *path;
DIR *dir;
int fid;
dir = opendir(tag->fpath);
if (!dir) return false;
while (!list_empty(&tag->tracks)) {
link = list_pop_front(&tag->tracks);
ref = UPCAST(link, struct ref);
track = ref->data;
ref_free(ref);
for (LIST_ITER(&tracks, link)) {
ref = UPCAST(link, struct ref);
if (ref->data == track) {
link = link_pop(link);
ref_free(ref);
break;
}
}
if (player.track == track)
player.track = NULL;
track_free(track);
}
while ((ent = readdir(dir))) {
if (!strcmp(ent->d_name, "."))
continue;
if (!strcmp(ent->d_name, ".."))
continue;
if (!strcmp(ent->d_name, "index"))
continue;
/* skip files without extension */
if (!strchr(ent->d_name + 1, '.'))
continue;
path = aprintf("%s/%s", tag->fpath, ent->d_name);
OOM_CHECK(path);
fid = get_fid(path);
track = track_alloc(tag->fpath, ent->d_name, fid);
OOM_CHECK(track);
ref = ref_alloc(tag);
OOM_CHECK(ref);
list_push_back(&track->tags, LINK(ref));
ref = ref_alloc(track);
OOM_CHECK(ref);
list_push_back(&tag->tracks, LINK(ref));
ref = ref_alloc(track);
OOM_CHECK(ref);
list_push_back(&tracks, LINK(ref));
free(path);
}
list_sort(&tracks, track_fid_compare);
closedir(dir);
return true;
}
void
tracks_load(struct tag *tag)
{
char linebuf[1024];
struct link *link;
struct track *track;
struct track *track2;
struct ref *ref;
char *index_path;
char *track_name, *sep;
int track_fid;
FILE *file;
index_path = aprintf("%s/index", tag->fpath);
OOM_CHECK(index_path);
file = fopen(index_path, "r");
if (file == NULL) {
index_update(tag); /* create index */
file = fopen(index_path, "r");
ASSERT(file != NULL);
}
while (fgets(linebuf, sizeof(linebuf), file)) {
sep = strchr(linebuf, '\n');
if (sep) *sep = '\0';
sep = strchr(linebuf, ':');
if (!sep) ERROR("Syntax error in index file: %s\n", index_path);
*sep = '\0';
track_fid = atoi(linebuf);
track_name = sep + 1;
track = track_alloc(tag->fpath, track_name, track_fid);
ref = ref_alloc(tag);
OOM_CHECK(ref);
list_push_back(&track->tags, LINK(ref));
ref = ref_alloc(track);
OOM_CHECK(ref);
list_push_back(&tag->tracks, LINK(ref));
ref = ref_alloc(track);
OOM_CHECK(ref);
list_push_back(&tracks, LINK(ref));
}
fclose(file);
free(index_path);
}
void
tracks_save(struct tag *tag)
{
struct track *track;
struct link *link;
char *index_path;
FILE *file;
/* write playlist back to index file */
index_path = aprintf("%s/index", tag->fpath);
OOM_CHECK(index_path);
file = fopen(index_path, "w+");
if (!file) {
fprintf(stderr, "Failed to write to index file: %s\n",
index_path);
free(index_path);
return;
}
for (LIST_ITER(&tag->tracks, link)) {
track = UPCAST(link, struct ref)->data;
fprintf(file, "%i:%s\n", track->fid, track->name);
}
fclose(file);
free(index_path);
}
bool
make_dir(const char *path)
{
return mkdir(path, S_IRWXU | S_IRWXG) == 0;
}
bool
rm_dir(const char *path, bool recursive)
{
char *files[] = { (char *) path, NULL };
FTSENT *ent;
FTS *fts;
int flags;
if (recursive) {
flags = FTS_NOCHDIR | FTS_PHYSICAL | FTS_XDEV;
fts = fts_open(files, flags, NULL);
if (!fts) return false;
while ((ent = fts_read(fts))) {
switch (ent->fts_info) {
case FTS_NS:
case FTS_DNR:
case FTS_ERR:
fts_close(fts);
return false;
case FTS_D:
break;
default:
if (remove(ent->fts_accpath) < 0) {
fts_close(fts);
return false;
}
break;
}
}
fts_close(fts);
} else {
if (rmdir(path) != 0)
return false;
}
return true;
}
bool
rm_file(const char *path)
{
return unlink(path) == 0;
}
bool
copy_file(const char *src, const char *dst)
{
FILE *in, *out;
char buf[4096];
int len, nread;
bool ok;
ok = false;
in = out = NULL;
in = fopen(src, "r");
if (in == NULL) goto cleanup;
out = fopen(dst, "w+");
if (out == NULL) goto cleanup;
while ((nread = fread(buf, 1, sizeof(buf), in)) > 0) {
fwrite(buf, 1, nread, out);
}
if (nread < 0)
goto cleanup;
ok = true;
cleanup:
if (in) fclose(in);
if (out) fclose(out);
return ok;
}
bool
move_file(const char *src, const char *dst)
{
return rename(src, dst) == 0;
}
struct tag *
tag_find(const char *query)
{
struct link *iter;
struct tag *tag;
for (LIST_ITER(&tags, iter)) {
tag = UPCAST(iter, struct tag);
if (!strcmp(tag->name, query)) {
return tag;
}
}
return NULL;
}
|