Advertisement
Guest User

Untitled

a guest
Jul 9th, 2012
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.71 KB | None | 0 0
  1. /***************************************************************************
  2.  *             __________               __   ___.
  3.  *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
  4.  *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
  5.  *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
  6.  *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
  7.  *                     \/            \/     \/    \/            \/
  8.  *
  9.  * Copyright (C) 2012 by Jonathan Gordon
  10.  *
  11.  * This program is free software; you can redistribute it and/or
  12.  * modify it under the terms of the GNU General Public License
  13.  * as published by the Free Software Foundation; either version 2
  14.  * of the License, or (at your option) any later version.
  15.  *
  16.  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  17.  * KIND, either express or implied.
  18.  *
  19.  ****************************************************************************/
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <stdbool.h>
  23. #include "file.h"
  24. #include "fat.h"
  25. #include "dir.h"
  26. #include "debug.h"
  27. #include "dircache.h"
  28. #include "filefuncs.h"
  29. #include "system.h"
  30. #include "filescanner.h"
  31.  
  32. static struct event_queue filescanner_queue SHAREDBSS_ATTR;
  33. static long filescanner_stack[(DEFAULT_STACK_SIZE + 0x4000)/sizeof(long)];
  34. static const char filescanner_thread_name[] = "filescanner";
  35.  
  36. static long kill_scan;
  37. static bool in_scan;
  38.  
  39. enum {
  40.     Q_STOP_SCAN = 0,
  41.     Q_START_SCAN,
  42. };
  43.  
  44. /*
  45.  * User stacks.
  46.  *
  47.  * Some users of this scanner will require a stack to keep track of some
  48.  * recursive state. (i.e whether the user cares about the current subtree.)
  49.  * Instead of requireing each user to manage their own stack implementation,
  50.  * users can call filescanner_request_stack_var(size) which will allocate
  51.  * size bytes in each recursion level for that users use.
  52.  */
  53. #define MAX_STACK_VARS 8
  54. struct user_stack_var {
  55.     short offset;
  56.     short size;
  57. };
  58. static struct user_stack_var user_stack_vars[MAX_STACK_VARS];
  59. static int user_stack_var_count;
  60. static int user_stack_var_size;
  61. static void *current_user_stack;
  62.  
  63. /*
  64.  * returns -1 on error, or a handle to pass to filescanner_get_stack_var()
  65.  * to get the pointer to the data.
  66.  */
  67. int filescanner_request_stack_var(size_t size)
  68. {
  69.     size_t new_size = ALIGN_UP(size, sizeof(int));
  70.     if (user_stack_var_count >= MAX_STACK_VARS)
  71.         return -1;
  72.     else if (in_scan)
  73.         return -2;
  74.     user_stack_vars[user_stack_var_count].offset = user_stack_var_size;
  75.     user_stack_vars[user_stack_var_count].size = size;
  76.     user_stack_var_size += new_size;
  77.  
  78.     user_stack_var_count++;
  79.     return user_stack_var_count - 1;
  80. }
  81.  
  82. void *filescanner_get_stack_var(int handle, size_t *size)
  83. {
  84.     if (handle < 0 || >= user_stack_var_count)
  85.         return NULL;
  86.  
  87.     if (size)
  88.         *size = user_stack_vars[handle].size
  89.     return current_user_stack + user_stack_vars[handle].offset;
  90. }
  91.  
  92. static void filescanner_scan_dir(char *path)
  93. {
  94.     DIR *dir;
  95.     int len = strlen(path);
  96.     struct file_scan_dir_data dir_data;
  97.     char user_stack[user_stack_var_size];
  98.  
  99.     dir = opendir(path);
  100.     if (!dir)
  101.     {
  102.         DEBUGF("Couldn't open Directory %s\n", path);
  103.         return;
  104.     }
  105.     dir_data.dir = dir;
  106.     dir_data.path = path;
  107.     current_user_stack = &user_stack[0];
  108.     send_event(FILE_SCANNER_ENTER_DIRECTORY, &dir_data);
  109.  
  110.     while (!kill_scan)
  111.     {
  112.         struct dirent *entry = readdir(dir);
  113.         if (entry == NULL)
  114.             break;
  115.  
  116.         if (!strcmp((char *)entry->d_name, ".") ||
  117.             !strcmp((char *)entry->d_name, ".."))
  118.             continue;
  119.  
  120.         struct dirinfo info = dir_get_info(dir, entry);
  121.  
  122.         /* Not sure if yield() is necessary as the ata thread should
  123.          * cause this thread to block/yield anyway. Defintly need to
  124.          * yield on sim though.
  125.          */
  126.         yield();
  127.         /* don't add an extra / for curpath == / */
  128.         if (len <= 1) len = 0;
  129.         snprintf(&path[len], MAX_PATH - len, "/%s", entry->d_name);
  130.        
  131.         if (info.attribute & ATTR_DIRECTORY)
  132.             filescanner_scan_dir(path);
  133.         else
  134.         {
  135.             struct file_scan_file_data data;
  136.             data.filename = path;
  137.             data.info = &info;
  138.             data.dir = dir;
  139.             send_event(FILE_SCANNER_FILE, &data);
  140.         }
  141.     }
  142.  
  143.     closedir(dir);
  144.     path[len] = '\0';
  145.     send_event(FILE_SCANNER_EXIT_DIRECTORY, path);
  146. }
  147.  
  148. static void filescanner_thread(void)
  149. {
  150.     struct queue_event ev;
  151.     char buffer[MAX_PATH];
  152.  
  153.     while (1)
  154.     {
  155.         queue_wait_w_tmo(&filescanner_queue, &ev, HZ);
  156.        
  157.         switch (ev.id)
  158.         {
  159.             case Q_START_SCAN:
  160.                 in_scan = true;
  161.                 kill_scan = 0;
  162.                 cpu_boost(true);
  163.                 send_event(FILE_SCANNER_START, (void*)ev.data);
  164.  
  165.                 strcpy(buffer, "/");
  166.                 filescanner_scan_dir(buffer);
  167.  
  168.                 send_event(FILE_SCANNER_FINISH, &kill_scan);
  169.                 cpu_boost(false);
  170.                 in_scan = false;
  171.                 break;
  172.  
  173.             case Q_STOP_SCAN:
  174.                 kill_scan = 1;
  175.                 break;
  176.         }
  177.     }
  178. }
  179.  
  180.  
  181. void filescanner_init(void)
  182. {
  183.     user_stack_var_count = 0;
  184.     in_scan = false;
  185.     create_thread(filescanner_thread, filescanner_stack,
  186.                   sizeof(filescanner_stack), 0, filescanner_thread_name
  187.                   IF_PRIO(, PRIORITY_BACKGROUND)
  188.                   IF_COP(, CPU));
  189.  
  190. }
  191.  
  192. void filescanner_scan(long reinit)
  193. {
  194.     queue_post(&filescanner_queue, Q_START_SCAN, reinit);
  195. }
  196.  
  197. void filescanner_abort(void)
  198. {
  199.     queue_post(&filescanner_queue, Q_STOP_SCAN, 0);
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement