summaryrefslogtreecommitdiff
path: root/firmware/common/dir_uncached.c
diff options
context:
space:
mode:
authorMichael Sevakis <jethead71@rockbox.org>2013-08-05 22:02:45 -0400
committerMichael Sevakis <jethead71@rockbox.org>2014-08-30 03:48:23 +0200
commit7d1a47cf13726c95ac46027156cc12dd9da5b855 (patch)
treeeb20d07656806479a8e1fea25887a490ea30d1d8 /firmware/common/dir_uncached.c
parent95a4c3afcd53a1f8b835dec33de51f9c304de4d9 (diff)
downloadrockbox-7d1a47cf13726c95ac46027156cc12dd9da5b855.tar.gz
rockbox-7d1a47cf13726c95ac46027156cc12dd9da5b855.zip
Rewrite filesystem code (WIP)
This patch redoes the filesystem code from the FAT driver up to the clipboard code in onplay.c. Not every aspect of this is finished therefore it is still "WIP". I don't wish to do too much at once (haha!). What is left to do is get dircache back in the sim and find an implementation for the dircache indicies in the tagcache and playlist code or do something else that has the same benefit. Leaving these out for now does not make anything unusable. All the basics are done. Phone app code should probably get vetted (and app path handling just plain rewritten as environment expansions); the SDL app and Android run well. Main things addressed: 1) Thread safety: There is none right now in the trunk code. Most of what currently works is luck when multiple threads are involved or multiple descriptors to the same file are open. 2) POSIX compliance: Many of the functions behave nothing like their counterparts on a host system. This leads to inconsistent code or very different behavior from native to hosted. One huge offender was rename(). Going point by point would fill a book. 3) Actual running RAM usage: Many targets will use less RAM and less stack space (some more RAM because I upped the number of cache buffers for large memory). There's very little memory lying fallow in rarely-used areas (see 'Key core changes' below). Also, all targets may open the same number of directory streams whereas before those with less than 8MB RAM were limited to 8, not 12 implying those targets will save slightly less. 4) Performance: The test_disk plugin shows markedly improved performance, particularly in the area of (uncached) directory scanning, due partly to more optimal directory reading and to a better sector cache algorithm. Uncached times tend to be better while there is a bit of a slowdown in dircache due to it being a bit heavier of an implementation. It's not noticeable by a human as far as I can say. Key core changes: 1) Files and directories share core code and data structures. 2) The filesystem code knows which descriptors refer to same file. This ensures that changes from one stream are appropriately reflected in every open descriptor for that file (fileobj_mgr.c). 3) File and directory cache buffers are borrowed from the main sector cache. This means that when they are not in use by a file, they are not wasted, but used for the cache. Most of the time, only a few of them are needed. It also means that adding more file and directory handles is less expensive. All one must do in ensure a large enough cache to borrow from. 4) Relative path components are supported and the namespace is unified. It does not support full relative paths to an implied current directory; what is does support is use of "." and "..". Adding the former would not be very difficult. The namespace is unified in the sense that volumes may be specified several times along with relative parts, e.g.: "/<0>/foo/../../<1>/bar" :<=> "/<1>/bar". 5) Stack usage is down due to sharing of data, static allocation and less duplication of strings on the stack. This requires more serialization than I would like but since the number of threads is limited to a low number, the tradoff in favor of the stack seems reasonable. 6) Separates and heirarchicalizes (sic) the SIM and APP filesystem code. SIM path and volume handling is just like the target. Some aspects of the APP file code get more straightforward (e.g. no path hashing is needed). Dircache: Deserves its own section. Dircache is new but pays homage to the old. The old one was not compatible and so it, since it got redone, does all the stuff it always should have done such as: 1) It may be update and used at any time during the build process. No longer has one to wait for it to finish building to do basic file management (create, remove, rename, etc.). 2) It does not need to be either fully scanned or completely disabled; it can be incomplete (i.e. overfilled, missing paths), still be of benefit and be correct. 3) Handles mounting and dismounting of individual volumes which means a full rebuild is not needed just because you pop a new SD card in the slot. Now, because it reuses its freed entry data, may rebuild only that volume. 4) Much more fundamental to the file code. When it is built, it is the keeper of the master file list whether enabled or not ("disabled" is just a state of the cache). Its must always to ready to be started and bind all streams opened prior to being enabled. 5) Maintains any short filenames in OEM format which means that it does not need to be rebuilt when changing the default codepage. Miscellaneous Compatibility: 1) Update any other code that would otherwise not work such as the hotswap mounting code in various card drivers. 2) File management: Clipboard needed updating because of the behavioral changes. Still needs a little more work on some finer points. 3) Remove now-obsolete functionality such as the mutex's "no preempt" flag (which was only for the prior FAT driver). 4) struct dirinfo uses time_t rather than raw FAT directory entry time fields. I plan to follow up on genericizing everything there (i.e. no FAT attributes). 5) unicode.c needed some redoing so that the file code does not try try to load codepages during a scan, which is actually a problem with the current code. The default codepage, if any is required, is now kept in RAM separarately (bufalloced) from codepages specified to iso_decode() (which must not be bufalloced because the conversion may be done by playback threads). Brings with it some additional reusable core code: 1) Revised file functions: Reusable code that does things such as safe path concatenation and parsing without buffer limitations or data duplication. Variants that copy or alter the input path may be based off these. To do: 1) Put dircache functionality back in the sim. Treating it internally as a different kind of file system seems the best approach at this time. 2) Restore use of dircache indexes in the playlist and database or something effectively the same. Since the cache doesn't have to be complete in order to be used, not getting a hit on the cache doesn't unambiguously say if the path exists or not. Change-Id: Ia30f3082a136253e3a0eae0784e3091d138915c8 Reviewed-on: http://gerrit.rockbox.org/566 Reviewed-by: Michael Sevakis <jethead71@rockbox.org> Tested: Michael Sevakis <jethead71@rockbox.org>
Diffstat (limited to 'firmware/common/dir_uncached.c')
-rw-r--r--firmware/common/dir_uncached.c312
1 files changed, 0 insertions, 312 deletions
diff --git a/firmware/common/dir_uncached.c b/firmware/common/dir_uncached.c
deleted file mode 100644
index b850a514e7..0000000000
--- a/firmware/common/dir_uncached.c
+++ /dev/null
@@ -1,312 +0,0 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id: dir.c 13741 2007-06-30 02:08:27Z jethead71 $
9 *
10 * Copyright (C) 2002 by Björn Stenberg
11 *
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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#include <stdio.h>
22#include <errno.h>
23#include <string.h>
24#include <stdbool.h>
25#include <stdlib.h>
26#include "fat.h"
27#include "dir.h"
28#include "debug.h"
29#include "filefuncs.h"
30
31#if (MEMORYSIZE > 8)
32#define MAX_OPEN_DIRS 12
33#else
34#define MAX_OPEN_DIRS 8
35#endif
36
37static DIR_UNCACHED opendirs[MAX_OPEN_DIRS];
38
39// release all dir handles on a given volume "by force", to avoid leaks
40int release_dirs(int volume)
41{
42 DIR_UNCACHED* pdir = opendirs;
43 int dd;
44 int closed = 0;
45 for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
46 {
47#ifdef HAVE_MULTIVOLUME
48 if (pdir->fatdir.file.volume == volume)
49#else
50 (void)volume;
51#endif
52 {
53 pdir->busy = false; /* mark as available, no further action */
54 closed++;
55 }
56 }
57 return closed; /* return how many we did */
58}
59
60DIR_UNCACHED* opendir_uncached(const char* name)
61{
62 char namecopy[MAX_PATH];
63 char* part;
64 char* end;
65 struct fat_direntry entry;
66 int dd;
67 DIR_UNCACHED* pdir = opendirs;
68#ifdef HAVE_MULTIVOLUME
69 int volume;
70#endif
71
72 if ( name[0] != '/' ) {
73 DEBUGF("Only absolute paths supported right now\n");
74 return NULL;
75 }
76
77 /* find a free dir descriptor */
78 for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
79 if ( !pdir->busy )
80 break;
81
82 if ( dd == MAX_OPEN_DIRS ) {
83 DEBUGF("Too many dirs open\n");
84 errno = EMFILE;
85 return NULL;
86 }
87
88 pdir->busy = true;
89
90#ifdef HAVE_MULTIVOLUME
91 /* try to extract a heading volume name, if present */
92 volume = strip_volume(name, namecopy);
93 pdir->volumecounter = 0;
94#else
95 strlcpy(namecopy, name, sizeof(namecopy)); /* just copy */
96#endif
97
98 if ( fat_opendir(IF_MV(volume,) &pdir->fatdir, 0, NULL) < 0 ) {
99 DEBUGF("Failed opening root dir\n");
100 pdir->busy = false;
101 return NULL;
102 }
103
104 for ( part = strtok_r(namecopy, "/", &end); part;
105 part = strtok_r(NULL, "/", &end)) {
106 /* scan dir for name */
107 while (1) {
108 if ((fat_getnext(&pdir->fatdir,&entry) < 0) ||
109 (!entry.name[0])) {
110 pdir->busy = false;
111 return NULL;
112 }
113 if ( (entry.attr & FAT_ATTR_DIRECTORY) &&
114 (!strcasecmp(part, entry.name)) ) {
115 /* In reality, the parent_dir parameter of fat_opendir seems
116 * useless because it's sole purpose it to have a way to
117 * update the file metadata, but here we are only reading
118 * a directory so there's no need for that kind of stuff.
119 * However, the rmdir_uncached function uses a ugly hack to
120 * avoid opening a directory twice when deleting it and thus
121 * needs those information. That's why we pass pdir->fatdir both
122 * as the parent directory and the resulting one (this is safe,
123 * in doubt, check fat_open(dir) code) which will allow this kind of
124 * (ugly) things */
125 if ( fat_opendir(IF_MV(volume,)
126 &pdir->fatdir,
127 entry.firstcluster,
128 &pdir->fatdir) < 0 ) {
129 DEBUGF("Failed opening dir '%s' (%ld)\n",
130 part, entry.firstcluster);
131 pdir->busy = false;
132 return NULL;
133 }
134#ifdef HAVE_MULTIVOLUME
135 pdir->volumecounter = -1; /* n.a. to subdirs */
136#endif
137 break;
138 }
139 }
140 }
141
142 return pdir;
143}
144
145int closedir_uncached(DIR_UNCACHED* dir)
146{
147 dir->busy=false;
148 return 0;
149}
150
151struct dirent_uncached* readdir_uncached(DIR_UNCACHED* dir)
152{
153 struct fat_direntry entry;
154 struct dirent_uncached* theent = &(dir->theent);
155
156 if (!dir->busy)
157 return NULL;
158
159#ifdef HAVE_MULTIVOLUME
160 /* Volumes (secondary file systems) get inserted into the root directory
161 of the first volume, since we have no separate top level. */
162 if (dir->volumecounter >= 0 /* on a root dir */
163 && dir->volumecounter < NUM_VOLUMES /* in range */
164 && dir->fatdir.file.volume == 0) /* at volume 0 */
165 { /* fake special directories, which don't really exist, but
166 will get redirected upon opendir_uncached() */
167 while (++dir->volumecounter < NUM_VOLUMES)
168 {
169 if (fat_ismounted(dir->volumecounter))
170 {
171 memset(theent, 0, sizeof(*theent));
172 theent->info.attribute = FAT_ATTR_DIRECTORY | FAT_ATTR_VOLUME;
173 snprintf(theent->d_name, sizeof(theent->d_name),
174 VOL_NAMES, dir->volumecounter);
175 return theent;
176 }
177 }
178 }
179#endif
180 /* normal directory entry fetching follows here */
181 if (fat_getnext(&(dir->fatdir),&entry) < 0)
182 return NULL;
183
184 if ( !entry.name[0] )
185 return NULL;
186
187 strlcpy(theent->d_name, entry.name, sizeof(theent->d_name));
188 theent->info.attribute = entry.attr;
189 theent->info.wrtdate = entry.wrtdate;
190 theent->info.wrttime = entry.wrttime;
191 theent->info.size = entry.filesize;
192 theent->startcluster = entry.firstcluster;
193
194 return theent;
195}
196
197int mkdir_uncached(const char *name)
198{
199 DIR_UNCACHED *dir;
200 char namecopy[MAX_PATH];
201 char* end;
202 char *basename;
203 char *parent;
204 struct dirent_uncached *entry;
205 int dd;
206 DIR_UNCACHED* pdir = opendirs;
207 struct fat_dir *newdir;
208 int rc;
209
210 if ( name[0] != '/' ) {
211 DEBUGF("mkdir: Only absolute paths supported right now\n");
212 return -1;
213 }
214 /* find a free dir descriptor */
215 for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
216 if ( !pdir->busy )
217 break;
218
219 if ( dd == MAX_OPEN_DIRS ) {
220 DEBUGF("Too many dirs open\n");
221 errno = EMFILE;
222 return -5;
223 }
224
225 pdir->busy = true;
226 newdir = &pdir->fatdir;
227
228 strlcpy(namecopy, name, sizeof(namecopy));
229
230 /* Split the base name and the path */
231 end = strrchr(namecopy, '/');
232 *end = 0;
233 basename = end+1;
234
235 if(namecopy == end) /* Root dir? */
236 parent = "/";
237 else
238 parent = namecopy;
239
240 DEBUGF("mkdir: parent: %s, name: %s\n", parent, basename);
241
242 dir = opendir_uncached(parent);
243
244 if(!dir) {
245 DEBUGF("mkdir: can't open parent dir\n");
246 pdir->busy = false;
247 return -2;
248 }
249
250 if(basename[0] == 0) {
251 DEBUGF("mkdir: Empty dir name\n");
252 pdir->busy = false;
253 errno = EINVAL;
254 return -3;
255 }
256
257 /* Now check if the name already exists */
258 while ((entry = readdir_uncached(dir))) {
259 if ( !strcasecmp(basename, entry->d_name) ) {
260 DEBUGF("mkdir error: file exists\n");
261 errno = EEXIST;
262 closedir_uncached(dir);
263 pdir->busy = false;
264 return - 4;
265 }
266 }
267
268 memset(newdir, 0, sizeof(struct fat_dir));
269
270 rc = fat_create_dir(basename, newdir, &(dir->fatdir));
271 closedir_uncached(dir);
272 pdir->busy = false;
273
274 return rc;
275}
276
277int rmdir_uncached(const char* name)
278{
279 int rc;
280 DIR_UNCACHED* dir;
281 struct dirent_uncached* entry;
282
283 dir = opendir_uncached(name);
284 if (!dir)
285 {
286 errno = ENOENT; /* open error */
287 return -1;
288 }
289
290 /* check if the directory is empty */
291 while ((entry = readdir_uncached(dir)))
292 {
293 if (strcmp(entry->d_name, ".") &&
294 strcmp(entry->d_name, ".."))
295 {
296 DEBUGF("rmdir error: not empty\n");
297 errno = ENOTEMPTY;
298 closedir_uncached(dir);
299 return -2;
300 }
301 }
302
303 rc = fat_remove(&(dir->fatdir.file));
304 if ( rc < 0 ) {
305 DEBUGF("Failed removing dir: %d\n", rc);
306 errno = EIO;
307 rc = rc * 10 - 3;
308 }
309
310 closedir_uncached(dir);
311 return rc;
312}