Versioning for queue files and a heuristic for reconstructing origin
[disorder] / server / server-queue.c
1 /*
2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2008 Richard Kettlewell
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "disorder-server.h"
19
20 /* the head of the queue is played next, so normally we add to the tail */
21 struct queue_entry qhead = {
22 .next = &qhead,
23 .prev = &qhead
24 };
25
26 /* the head of the recent list is the oldest thing, the tail the most recently
27 * played */
28 struct queue_entry phead = {
29 .next = &phead,
30 .prev = &phead
31 };
32
33 long pcount;
34
35 void queue_fix_sofar(struct queue_entry *q) {
36 long sofar;
37
38 /* Fake up SOFAR field for currently-playing tracks that don't have it filled
39 * in by the speaker process. XXX this horrible bodge should go away when we
40 * have a more general implementation of pausing as that field will always
41 * have to be right for the playing track. */
42 if((q->state == playing_started
43 || q->state == playing_paused)
44 && q->type & DISORDER_PLAYER_PAUSES
45 && (q->type & DISORDER_PLAYER_TYPEMASK) != DISORDER_PLAYER_RAW) {
46 if(q->lastpaused) {
47 if(q->uptopause == -1) /* Don't know how far thru. */
48 sofar = -1;
49 else if(q->lastresumed) /* Has been paused and resumed. */
50 sofar = q->uptopause + time(0) - q->lastresumed;
51 else /* Currently paused. */
52 sofar = q->uptopause;
53 } else /* Never been paused. */
54 sofar = time(0) - q->played;
55 q->sofar = sofar;
56 }
57 }
58
59 static void queue_read_error(const char *msg,
60 void *u) {
61 fatal(0, "error parsing queue %s: %s", (const char *)u, msg);
62 }
63
64 static void queue_do_read(struct queue_entry *head, const char *path) {
65 char *buffer;
66 FILE *fp;
67 struct queue_entry *q;
68 int version = 0;
69
70 if(!(fp = fopen(path, "r"))) {
71 if(errno == ENOENT)
72 return; /* no queue */
73 fatal(errno, "error opening %s", path);
74 }
75 head->next = head->prev = head;
76 while(!inputline(path, fp, &buffer, '\n')) {
77 if(buffer[0] == '#') {
78 /* Version indicator */
79 version = atoi(buffer + 1);
80 continue;
81 }
82 q = xmalloc(sizeof *q);
83 queue_unmarshall(q, buffer, queue_read_error, (void *)path);
84 if(version < 1) {
85 /* Fix up origin field as best we can; will be wrong in some cases but
86 * hopefully not too horribly so. */
87 q->origin = q->submitter ? origin_picked : origin_random;
88 if(q->state == playing_isscratch)
89 q->origin = origin_scratch;
90 }
91 if(head == &qhead
92 && (!q->track
93 || !q->when))
94 fatal(0, "incomplete queue entry in %s", path);
95 queue_insert_entry(head->prev, q);
96 }
97 if(ferror(fp)) fatal(errno, "error reading %s", path);
98 fclose(fp);
99 }
100
101 void queue_read(void) {
102 queue_do_read(&qhead, config_get_file("queue"));
103 }
104
105 void recent_read(void) {
106 struct queue_entry *q;
107
108 queue_do_read(&phead, config_get_file("recent"));
109 /* reset pcount after loading */
110 pcount = 0;
111 q = phead.next;
112 while(q != &phead) {
113 ++pcount;
114 q = q->next;
115 }
116 }
117
118 static void queue_do_write(const struct queue_entry *head, const char *path) {
119 char *tmp;
120 FILE *fp;
121 struct queue_entry *q;
122
123 byte_xasprintf(&tmp, "%s.new", path);
124 if(!(fp = fopen(tmp, "w"))) fatal(errno, "error opening %s", tmp);
125 /* Save version indicator */
126 if(fprintf(fp, "#1\n") < 0)
127 fatal(errno, "error writing %s", tmp);
128 for(q = head->next; q != head; q = q->next)
129 if(fprintf(fp, "%s\n", queue_marshall(q)) < 0)
130 fatal(errno, "error writing %s", tmp);
131 if(fclose(fp) < 0) fatal(errno, "error closing %s", tmp);
132 if(rename(tmp, path) < 0) fatal(errno, "error replacing %s", path);
133 }
134
135 void queue_write(void) {
136 queue_do_write(&qhead, config_get_file("queue"));
137 }
138
139 void recent_write(void) {
140 queue_do_write(&phead, config_get_file("recent"));
141 }
142
143 struct queue_entry *queue_find(const char *key) {
144 struct queue_entry *q;
145
146 for(q = qhead.next;
147 q != &qhead && strcmp(q->track, key) && strcmp(q->id, key);
148 q = q->next)
149 ;
150 return q != &qhead ? q : 0;
151 }
152
153 /*
154 Local Variables:
155 c-basic-offset:2
156 comment-column:40
157 fill-column:79
158 indent-tabs-mode:nil
159 End:
160 */