ladishd: Basic recent projects functionality
[ladish.git] / daemon / recent_store.c
blobd2a60a1512e49b8b29c2088db80f73bbf47b9130
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2010 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains implementation of the recent items store
9 **************************************************************************
11 * LADI Session Handler is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * LADI Session Handler is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 * or write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <unistd.h>
32 #include "recent_store.h"
33 #include "save.h"
35 struct ladish_recent_store
37 char * path;
38 unsigned int max_items;
39 char ** items;
42 static
43 void
44 ladish_recent_store_save(
45 struct ladish_recent_store * store_ptr)
47 unsigned int i;
48 int fd;
50 fd = open(store_ptr->path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
51 if (fd == -1)
53 log_error("open(%s) failed: %d (%s)", store_ptr->path, errno, strerror(errno));
54 return;
57 for (i = 0; i < store_ptr->max_items && store_ptr->items[i] != NULL; i++)
59 if (!ladish_write_string(fd, store_ptr->items[i]))
61 log_error("write to file '%s' failed", store_ptr->path);
62 break;
65 if (!ladish_write_string(fd, "\n"))
67 log_error("write to file '%s' failed", store_ptr->path);
68 break;
72 close(fd);
75 static
76 void
77 ladish_recent_store_load(
78 struct ladish_recent_store * store_ptr)
80 int fd;
81 struct stat st;
82 char * buffer;
83 size_t buffer_size;
84 ssize_t ret;
85 char * temp_ptr;
86 unsigned int i;
88 fd = open(store_ptr->path, O_RDONLY);
89 if (fd == -1)
91 log_error("open(%s) failed: %d (%s)", store_ptr->path, errno, strerror(errno));
92 goto exit;
95 if (fstat(fd, &st) != 0)
97 log_error("fstat(%s) failed: %d (%s)", store_ptr->path, errno, strerror(errno));
98 goto close;
101 buffer_size = (size_t)st.st_size;
102 buffer = malloc(buffer_size + 1);
103 if (buffer == NULL)
105 log_error("malloc() failed to allocate %zy byte buffer for reading the contents of the recent items file '%s'", buffer_size + 1, store_ptr->path);
106 goto close;
109 ret = read(fd, buffer, buffer_size);
110 if (ret == -1)
112 log_error("read(%s) failed: %d (%s)", store_ptr->path, errno, strerror(errno));
113 goto free;
116 if (ret < (size_t)buffer_size)
118 /* this could be handled better but we dont care because it is expected for the file to be small enough */
119 log_error("read(%s) returned less bytes than requested", store_ptr->path);
120 goto free;
123 buffer[buffer_size] = 0;
125 /* read lines and store them in the item array */
126 temp_ptr = strtok(buffer, "\n");
127 i = 0;
128 while (temp_ptr != NULL && i < store_ptr->max_items)
130 ASSERT(store_ptr->items[i] == NULL); /* filling an empty array */
132 store_ptr->items[i] = strdup(temp_ptr);
133 if (store_ptr->items[i] == NULL)
135 log_error("strdup(\"%s\") failed.", temp_ptr);
136 goto free;
139 i++;
140 temp_ptr = strtok(NULL, "\n");
143 free:
144 free(buffer);
145 close:
146 close(fd);
147 exit:
148 return;
151 bool
152 ladish_recent_store_create(
153 const char * file_path,
154 unsigned int max_items,
155 ladish_recent_store_handle * store_handle_ptr)
157 struct ladish_recent_store * store_ptr;
158 unsigned int i;
160 store_ptr = malloc(sizeof(struct ladish_recent_store));
161 if (store_ptr == NULL)
163 log_error("malloc() failed to allocate a ladish_recent_store struct");
164 goto fail;
167 store_ptr->path = strdup(file_path);
168 if (store_ptr->path == NULL)
170 log_error("strdup() failed for recent store path '%s'", file_path);
171 goto free_store;
174 store_ptr->items = malloc(sizeof(char *) * max_items);
175 if (store_ptr->items == NULL)
177 log_error("malloc() failed to array of %u pointers", max_items);
178 goto free_path;
181 for (i = 0; i < max_items; i++)
183 store_ptr->items[i] = NULL;
186 store_ptr->max_items = max_items;
188 /* try to load items from file */
189 ladish_recent_store_load(store_ptr);
191 *store_handle_ptr = (ladish_recent_store_handle)store_ptr;
193 return true;
195 free_path:
196 free(store_ptr->path);
197 free_store:
198 free(store_ptr);
199 fail:
200 return false;
203 #define store_ptr ((struct ladish_recent_store *)store_handle)
205 void
206 ladish_recent_store_destroy(
207 ladish_recent_store_handle store_handle)
209 free(store_ptr->items);
210 free(store_ptr->path);
211 free(store_ptr);
214 void
215 ladish_recent_store_use_item(
216 ladish_recent_store_handle store_handle,
217 const char * item_const)
219 char * item;
220 char * item_save;
221 unsigned int i;
223 for (i = 0; i < store_ptr->max_items && store_ptr->items[i] != NULL; i++)
225 if (strcmp(store_ptr->items[i], item_const) == 0)
226 { /* already known item */
228 /* if the item is already the most recent one, there is nothing to do */
229 if (i != 0)
231 item = store_ptr->items[i];
232 store_ptr->items[i] = NULL; /* mark end for reorder loop */
233 goto reorder;
236 return;
240 /* new item */
241 item = strdup(item_const);
242 if (item == NULL)
244 log_error("strdup() failed for recent item '%s'", item_const);
245 return;
248 reorder:
249 /* we have valid non-NULL item pointer before the iteration begins */
250 /* the item pointer is checked for NULL because that means either
251 end of used items or a mark created when already known item
252 was removed from the array */
253 for (i = 0; i < store_ptr->max_items && item != NULL; i++)
255 item_save = store_ptr->items[i];
256 store_ptr->items[i] = item;
257 item = item_save;
260 if (item != NULL)
261 { /* eventually free the oldest item */
262 free(item);
265 ladish_recent_store_save(store_ptr);
268 void
269 ladish_recent_store_iterate_items(
270 ladish_recent_store_handle store_handle,
271 void * callback_context,
272 bool (* callback)(void * callback_context, const char * item))
274 unsigned int i;
276 for (i = 0; i < store_ptr->max_items && store_ptr->items[i] != NULL; i++)
278 if (!callback(callback_context, store_ptr->items[i]))
280 break;
285 #undef store_ptr