disktidy: add a comment to the function
[maemo-rb.git] / apps / plugins / disktidy.c
blob141773aa17420904ad6d6b945167a9039626106f
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 David Dent
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "plugin.h"
22 #include "errno.h"
25 static int removed = 0; /* number of items removed */
27 /* function return values */
28 enum tidy_return
30 TIDY_RETURN_OK = 0,
31 TIDY_RETURN_ERROR = 1,
32 TIDY_RETURN_USB = 2,
33 TIDY_RETURN_ABORT = 3,
36 #define MAX_TYPES 64
37 struct tidy_type {
38 char filestring[64];
39 int pre;
40 int post;
41 bool directory;
42 bool remove;
43 } tidy_types[MAX_TYPES];
44 int tidy_type_count;
45 bool tidy_loaded_and_changed = false;
47 #define DEFAULT_FILES PLUGIN_APPS_DIR "/disktidy.config"
48 #define CUSTOM_FILES PLUGIN_APPS_DIR "/disktidy_custom.config"
50 void add_item(const char* name, int index)
52 char *a;
53 struct tidy_type *entry = tidy_types + index;
54 rb->strcpy(entry->filestring, name);
55 if (name[rb->strlen(name)-1] == '/')
57 entry->directory = true;
58 entry->filestring[rb->strlen(name)-1] = '\0';
60 else
61 entry->directory = false;
62 a = rb->strchr(name, '*');
63 if (a)
65 entry->pre = a - name;
66 entry->post = rb->strlen(a+1);
68 else
70 entry->pre = -1;
71 entry->post = -1;
75 static int find_file_string(const char *file, char *last_group)
77 char temp[MAX_PATH];
78 int i = 0, idx_last_group = -1;
79 bool folder = false;
80 rb->strcpy(temp, file);
81 if (temp[rb->strlen(temp)-1] == '/')
83 folder = true;
84 temp[rb->strlen(temp)-1] = '\0';
86 while (i<tidy_type_count)
88 if (!rb->strcmp(tidy_types[i].filestring, temp) &&
89 folder == tidy_types[i].directory)
90 return i;
91 else if (!rb->strcmp(tidy_types[i].filestring, last_group))
92 idx_last_group = i;
93 i++;
95 /* not found, so insert it into its group */
96 if (file[0] != '<' && idx_last_group != -1)
98 for (i=idx_last_group; i<tidy_type_count; i++)
100 if (tidy_types[i].filestring[0] == '<')
102 idx_last_group = i;
103 break;
106 /* shift items up one */
107 for (i=tidy_type_count;i>idx_last_group;i--)
109 rb->memcpy(&tidy_types[i], &tidy_types[i-1], sizeof(struct tidy_type));
111 tidy_type_count++;
112 add_item(file, idx_last_group+1);
113 return idx_last_group+1;
115 return i;
118 bool tidy_load_file(const char* file)
120 int fd = rb->open(file, O_RDONLY), i;
121 char buf[MAX_PATH], *str, *remove;
122 char last_group[MAX_PATH] = "";
123 bool new;
124 if (fd < 0)
125 return false;
126 while ((tidy_type_count < MAX_TYPES) &&
127 rb->read_line(fd, buf, MAX_PATH))
129 if (rb->settings_parseline(buf, &str, &remove))
131 i = find_file_string(str, last_group);
132 new = (i >= tidy_type_count);
133 if (!rb->strcmp(remove, "yes"))
134 tidy_types[i].remove = true;
135 else tidy_types[i].remove = false;
136 if (new)
138 i = tidy_type_count;
139 add_item(str, i);
140 tidy_type_count++;
142 if (str[0] == '<')
143 rb->strcpy(last_group, str);
146 rb->close(fd);
147 return true;
150 static bool match(struct tidy_type *tidy_type, char *string, int len)
152 char *pattern = tidy_type->filestring;
153 if (tidy_type->pre < 0)
155 /* no '*', just compare. */
156 return (rb->strcmp(pattern, string) == 0);
158 /* pattern is too long for the string. avoid 'ab*bc' matching 'abc'. */
159 if (len < tidy_type->pre + tidy_type->post)
160 return false;
161 /* pattern has '*', compare former part of '*' to the begining of
162 the string and compare next part of '*' to the end of string. */
163 return (rb->strncmp(pattern, string, tidy_type->pre) == 0 &&
164 rb->strcmp(pattern + tidy_type->pre + 1,
165 string + len - tidy_type->post) == 0);
168 bool tidy_remove_item(char *item, int attr)
170 int i;
171 int len;
172 bool ret = false;
173 len = rb->strlen(item);
174 for (i=0; ret == false && i < tidy_type_count; i++)
176 if (match(&tidy_types[i], item, len))
178 if (!tidy_types[i].remove)
179 return false;
180 if (attr&ATTR_DIRECTORY)
181 ret = tidy_types[i].directory;
182 else ret = true;
185 return ret;
188 void tidy_lcd_status(const char *name)
190 /* display status text */
191 rb->lcd_clear_display();
192 rb->lcd_puts(0, 0, "Working ...");
193 rb->lcd_puts(0, 1, name);
194 #ifdef HAVE_LCD_BITMAP
195 rb->lcd_putsf(0, 2, "Cleaned up %d items", removed);
196 #endif
197 rb->lcd_update();
200 int tidy_path_append_entry(char *path, struct dirent *entry, int *path_length)
202 int name_len = rb->strlen(entry->d_name);
203 /* for the special case of path="/" this is one bigger but it's not a problem */
204 int new_length = *path_length + name_len + 1;
206 /* check overflow (keep space for trailing zero) */
207 if(new_length >= MAX_PATH)
208 return 0;
210 /* special case for path <> "/" */
211 if(rb->strcmp(path, "/") != 0)
213 rb->strcat(path + *path_length, "/");
214 (*path_length)++;
216 /* strcat is unsafe but the previous check normally avoid any problem */
217 /* use path_length to optimise */
219 rb->strcat(path + *path_length, entry->d_name);
220 *path_length += name_len;
222 return 1;
225 void tidy_path_remove_entry(char *path, int old_path_length, int *path_length)
227 path[old_path_length] = '\0';
228 *path_length = old_path_length;
231 /* Removes the directory specified by 'path'. This includes recursively
232 removing all files and directories in that directory.
233 path is assumed to be array of size MAX_PATH.
235 enum tidy_return tidy_removedir(char *path, int *path_length)
237 /* delete directory */
238 struct dirent *entry;
239 enum tidy_return status = TIDY_RETURN_OK;
240 int button;
241 DIR *dir;
242 int old_path_length = *path_length;
244 /* display status text */
245 tidy_lcd_status(path);
247 rb->yield();
249 dir = rb->opendir(path);
250 if (dir)
252 while((status == TIDY_RETURN_OK) && ((entry = rb->readdir(dir)) != 0))
253 /* walk directory */
255 /* check for user input and usb connect */
256 button = rb->get_action(CONTEXT_STD, TIMEOUT_NOBLOCK);
257 if (button == ACTION_STD_CANCEL)
259 rb->closedir(dir);
260 return TIDY_RETURN_ABORT;
262 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
264 rb->closedir(dir);
265 return TIDY_RETURN_USB;
268 rb->yield();
270 /* get absolute path */
271 /* returns an error if path is too long */
272 if(!tidy_path_append_entry(path, entry, path_length))
273 /* silent error */
274 continue;
276 struct dirinfo info = rb->dir_get_info(dir, entry);
277 if (info.attribute & ATTR_DIRECTORY)
279 /* dir ignore "." and ".." */
280 if ((rb->strcmp(entry->d_name, ".") != 0) && \
281 (rb->strcmp(entry->d_name, "..") != 0))
283 status = tidy_removedir(path, path_length);
286 else
288 /* file */
289 removed++;
290 rb->remove(path);
293 /* restore path */
294 tidy_path_remove_entry(path, old_path_length, path_length);
296 rb->closedir(dir);
297 /* rmdir */
298 removed++;
299 rb->rmdir(path);
301 else
303 status = TIDY_RETURN_ERROR;
305 return status;
308 /* path is assumed to be array of size MAX_PATH */
309 enum tidy_return tidy_clean(char *path, int *path_length)
311 /* deletes junk files and dirs left by system */
312 struct dirent *entry;
313 enum tidy_return status = TIDY_RETURN_OK;
314 int button;
315 DIR *dir;
316 int old_path_length = *path_length;
318 /* display status text */
319 tidy_lcd_status(path);
321 rb->yield();
323 dir = rb->opendir(path);
324 if (dir)
326 while((status == TIDY_RETURN_OK) && ((entry = rb->readdir(dir)) != 0))
327 /* walk directory */
329 struct dirinfo info = rb->dir_get_info(dir, entry);
330 /* check for user input and usb connect */
331 button = rb->get_action(CONTEXT_STD, TIMEOUT_NOBLOCK);
332 if (button == ACTION_STD_CANCEL)
334 rb->closedir(dir);
335 return TIDY_RETURN_ABORT;
337 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
339 rb->closedir(dir);
340 return TIDY_RETURN_USB;
343 rb->yield();
345 if (info.attribute & ATTR_DIRECTORY)
347 /* directory ignore "." and ".." */
348 if ((rb->strcmp(entry->d_name, ".") != 0) && \
349 (rb->strcmp(entry->d_name, "..") != 0))
351 /* get absolute path */
352 /* returns an error if path is too long */
353 if(!tidy_path_append_entry(path, entry, path_length))
354 /* silent error */
355 continue;
357 if (tidy_remove_item(entry->d_name, info.attribute))
359 /* delete dir */
360 status = tidy_removedir(path, path_length);
362 else
364 /* dir not deleted so clean it */
365 status = tidy_clean(path, path_length);
368 /* restore path */
369 tidy_path_remove_entry(path, old_path_length, path_length);
372 else
374 /* file */
375 if (tidy_remove_item(entry->d_name, info.attribute))
377 /* get absolute path */
378 /* returns an error if path is too long */
379 if(!tidy_path_append_entry(path, entry, path_length))
380 /* silent error */
381 continue;
383 removed++; /* increment removed files counter */
384 /* delete file */
385 rb->remove(path);
387 /* restore path */
388 tidy_path_remove_entry(path, old_path_length, path_length);
392 rb->closedir(dir);
393 return status;
395 else
397 return TIDY_RETURN_ERROR;
401 enum tidy_return tidy_do(void)
403 /* clean disk and display num of items removed */
404 enum tidy_return status;
405 char path[MAX_PATH];
406 int path_length;
408 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
409 rb->cpu_boost(true);
410 #endif
412 rb->strcpy(path, "/");
413 path_length = rb->strlen(path);
414 status = tidy_clean(path, &path_length);
416 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
417 rb->cpu_boost(false);
418 #endif
420 if ((status == TIDY_RETURN_OK) || (status == TIDY_RETURN_ABORT))
422 rb->lcd_clear_display();
423 if (status == TIDY_RETURN_ABORT)
425 rb->splash(HZ, "User aborted");
426 rb->lcd_clear_display();
428 rb->splashf(HZ*2, "Cleaned up %d items", removed);
430 return status;
433 enum themable_icons get_icon(int item, void * data)
435 (void)data;
436 if (tidy_types[item].filestring[0] == '<') /* special type */
437 return Icon_Folder;
438 else if (tidy_types[item].remove)
439 return Icon_Cursor;
440 else
441 return Icon_NOICON;
444 static const char* get_name(int selected_item, void * data,
445 char * buffer, size_t buffer_len)
447 (void)data;
448 if (tidy_types[selected_item].directory)
450 rb->snprintf(buffer, buffer_len, "%s/",
451 tidy_types[selected_item].filestring);
452 return buffer;
454 return tidy_types[selected_item].filestring;
457 int list_action_callback(int action, struct gui_synclist *lists)
459 if (action == ACTION_STD_OK)
461 int selection = rb->gui_synclist_get_sel_pos(lists);
462 if (tidy_types[selection].filestring[0] == '<')
464 int i;
465 if (!rb->strcmp(tidy_types[selection].filestring, "< ALL >"))
467 for (i=0; i<tidy_type_count; i++)
469 if (tidy_types[i].filestring[0] != '<')
470 tidy_types[i].remove = true;
473 else if (!rb->strcmp(tidy_types[selection].filestring, "< NONE >"))
475 for (i=0; i<tidy_type_count; i++)
477 if (tidy_types[i].filestring[0] != '<')
478 tidy_types[i].remove = false;
481 else /* toggle all untill the next <> */
483 selection++;
484 while (selection < tidy_type_count &&
485 tidy_types[selection].filestring[0] != '<')
487 tidy_types[selection].remove = !tidy_types[selection].remove;
488 selection++;
492 else
493 tidy_types[selection].remove = !tidy_types[selection].remove;
494 tidy_loaded_and_changed = true;
495 return ACTION_REDRAW;
497 return action;
500 enum tidy_return tidy_lcd_menu(void)
502 int selection = 0;
503 enum tidy_return status = TIDY_RETURN_OK;
504 bool menu_quit = false;
506 MENUITEM_STRINGLIST(menu, "Disktidy Menu", NULL,
507 "Start Cleaning", "Files to Clean",
508 "Quit");
510 while (!menu_quit)
512 switch(rb->do_menu(&menu, &selection, NULL, false))
514 case 0:
515 menu_quit = true; /* start cleaning */
516 break;
518 case 1:
520 struct simplelist_info list;
521 rb->simplelist_info_init(&list, "Files to Clean",
522 tidy_type_count, NULL);
523 list.get_icon = get_icon;
524 list.get_name = get_name;
525 list.action_callback = list_action_callback;
526 rb->simplelist_show_list(&list);
528 break;
530 default:
531 status = TIDY_RETURN_ABORT; /* exit plugin */
532 menu_quit = true;
533 break;
536 return status;
539 /* Creates a file and writes information about what files to
540 delete and what to keep to it.
541 Returns true iff the file was successfully created.
543 static bool save_config(const char *file_name)
545 int fd, i;
546 bool result;
548 fd = rb->creat(file_name, 0666);
549 result = (fd >= 0);
551 if (result)
553 for (i=0; i<tidy_type_count; i++)
555 rb->fdprintf(fd, "%s%s: %s\n", tidy_types[i].filestring,
556 tidy_types[i].directory ? "/" : "",
557 tidy_types[i].remove ? "yes" : "no");
559 rb->close(fd);
561 else
563 DEBUGF("Could not create file %s\n", file_name);
566 return result;
569 /* this is the plugin entry point */
570 enum plugin_status plugin_start(const void* parameter)
572 enum tidy_return status;
573 (void)parameter;
575 tidy_type_count = 0;
576 tidy_load_file(DEFAULT_FILES);
577 tidy_load_file(CUSTOM_FILES);
578 if (tidy_type_count == 0)
580 rb->splash(3*HZ, "Missing disktidy.config file");
581 return PLUGIN_ERROR;
583 status = tidy_lcd_menu();
584 if (tidy_loaded_and_changed)
586 save_config(CUSTOM_FILES);
588 if (status == TIDY_RETURN_ABORT)
589 return PLUGIN_OK;
590 while (true)
592 status = tidy_do();
594 switch (status)
596 case TIDY_RETURN_OK:
597 return PLUGIN_OK;
598 case TIDY_RETURN_ERROR:
599 return PLUGIN_ERROR;
600 case TIDY_RETURN_USB:
601 return PLUGIN_USB_CONNECTED;
602 case TIDY_RETURN_ABORT:
603 return PLUGIN_OK;
607 if (rb->default_event_handler(rb->button_get(false)) == SYS_USB_CONNECTED)
608 return PLUGIN_USB_CONNECTED;
610 rb->yield();
612 return PLUGIN_OK;