summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--firmware/common/dircache.c70
-rw-r--r--firmware/include/dircache.h1
2 files changed, 31 insertions, 40 deletions
diff --git a/firmware/common/dircache.c b/firmware/common/dircache.c
index c3360adee6..69e3f16ec8 100644
--- a/firmware/common/dircache.c
+++ b/firmware/common/dircache.c
@@ -125,7 +125,6 @@ static struct dircache_entry* allocate_entry(void)
125 next_entry = (struct dircache_entry *)(((long)next_entry & ~0x03) + 0x04); 125 next_entry = (struct dircache_entry *)(((long)next_entry & ~0x03) + 0x04);
126 } 126 }
127#endif 127#endif
128 next_entry->name_len = 0;
129 next_entry->d_name = NULL; 128 next_entry->d_name = NULL;
130 next_entry->up = NULL; 129 next_entry->up = NULL;
131 next_entry->down = NULL; 130 next_entry->down = NULL;
@@ -232,16 +231,16 @@ static int sab_process_dir(unsigned long startcluster, struct dircache_entry *ce
232 !strcmp("..", sab.direntry->name)) 231 !strcmp("..", sab.direntry->name))
233 continue; 232 continue;
234 233
235 ce->name_len = strlen(sab.direntry->name) + 1;
236 ce->d_name = ((char *)dircache_root + dircache_size); 234 ce->d_name = ((char *)dircache_root + dircache_size);
237 ce->startcluster = sab.direntry->firstcluster; 235 ce->startcluster = sab.direntry->firstcluster;
238 ce->info.size = sab.direntry->filesize; 236 ce->info.size = sab.direntry->filesize;
239 ce->info.attribute = sab.direntry->attr; 237 ce->info.attribute = sab.direntry->attr;
240 ce->info.wrtdate = sab.direntry->wrtdate; 238 ce->info.wrtdate = sab.direntry->wrtdate;
241 ce->info.wrttime = sab.direntry->wrttime; 239 ce->info.wrttime = sab.direntry->wrttime;
242 memcpy(ce->d_name, sab.direntry->name, ce->name_len); 240
243 241 strcpy(ce->d_name, sab.direntry->name);
244 dircache_size += ce->name_len; 242 dircache_size += strlen(ce->d_name) + 1;
243
245 entry_count++; 244 entry_count++;
246 245
247 if(ce->info.attribute & FAT_ATTR_DIRECTORY) 246 if(ce->info.attribute & FAT_ATTR_DIRECTORY)
@@ -263,7 +262,6 @@ static int sab_process_dir(unsigned long startcluster, struct dircache_entry *ce
263 262
264 /* add "." and ".." */ 263 /* add "." and ".." */
265 ce->d_name = "."; 264 ce->d_name = ".";
266 ce->name_len = 2;
267 ce->info.attribute = FAT_ATTR_DIRECTORY; 265 ce->info.attribute = FAT_ATTR_DIRECTORY;
268 ce->startcluster = startcluster; 266 ce->startcluster = startcluster;
269 ce->info.size = 0; 267 ce->info.size = 0;
@@ -272,7 +270,6 @@ static int sab_process_dir(unsigned long startcluster, struct dircache_entry *ce
272 ce = dircache_gen_next(ce); 270 ce = dircache_gen_next(ce);
273 271
274 ce->d_name = ".."; 272 ce->d_name = "..";
275 ce->name_len = 3;
276 ce->info.attribute = FAT_ATTR_DIRECTORY; 273 ce->info.attribute = FAT_ATTR_DIRECTORY;
277 ce->startcluster = (first_ce->up ? first_ce->up->startcluster : 0); 274 ce->startcluster = (first_ce->up ? first_ce->up->startcluster : 0);
278 ce->info.size = 0; 275 ce->info.size = 0;
@@ -283,7 +280,8 @@ static int sab_process_dir(unsigned long startcluster, struct dircache_entry *ce
283 280
284 while(rc >= 0 && ce) 281 while(rc >= 0 && ce)
285 { 282 {
286 if(ce->name_len != 0 && ce->down != NULL && strcmp(ce->d_name, ".") && strcmp(ce->d_name, "..")) 283 if(ce->d_name != NULL && ce->down != NULL && strcmp(ce->d_name, ".")
284 && strcmp(ce->d_name, ".."))
287 rc = sab_process_dir(ce->startcluster, ce->down); 285 rc = sab_process_dir(ce->startcluster, ce->down);
288 286
289 ce = ce->next; 287 ce = ce->next;
@@ -303,9 +301,8 @@ static int dircache_scan_and_build(IF_MV2(int volume,) struct dircache_entry *ce
303 if (volume > 0) 301 if (volume > 0)
304 { 302 {
305 ce->d_name = ((char *)dircache_root+dircache_size); 303 ce->d_name = ((char *)dircache_root+dircache_size);
306 snprintf(ce->d_name, VOL_ENUM_POS + 3, VOL_NAMES, volume); 304 size_t len = snprintf(ce->d_name, VOL_ENUM_POS + 3, VOL_NAMES, volume)+1;
307 ce->name_len = VOL_ENUM_POS + 3; 305 dircache_size += len;
308 dircache_size += ce->name_len;
309 ce->info.attribute = FAT_ATTR_DIRECTORY | FAT_ATTR_VOLUME; 306 ce->info.attribute = FAT_ATTR_DIRECTORY | FAT_ATTR_VOLUME;
310 ce->info.size = 0; 307 ce->info.size = 0;
311 append_position = dircache_gen_next(ce); 308 append_position = dircache_gen_next(ce);
@@ -342,12 +339,12 @@ static int sab_process_dir(struct dircache_entry *ce)
342 !strcmp("..", entry->d_name)) 339 !strcmp("..", entry->d_name))
343 continue; 340 continue;
344 341
345 ce->name_len = strlen(entry->d_name) + 1;
346 ce->d_name = ((char *)dircache_root + dircache_size); 342 ce->d_name = ((char *)dircache_root + dircache_size);
347 ce->info = entry->info; 343 ce->info = entry->info;
348 memcpy(ce->d_name, entry->d_name, ce->name_len); 344
349 345 strcpy(ce->d_name, entry->d_name);
350 dircache_size += ce->name_len; 346 dircache_size += strlen(entry->d_name) + 1;
347
351 entry_count++; 348 entry_count++;
352 349
353 if(entry->info.attribute & ATTR_DIRECTORY) 350 if(entry->info.attribute & ATTR_DIRECTORY)
@@ -391,7 +388,6 @@ static int sab_process_dir(struct dircache_entry *ce)
391 388
392 /* add "." and ".." */ 389 /* add "." and ".." */
393 ce->d_name = "."; 390 ce->d_name = ".";
394 ce->name_len = 2;
395 ce->info.attribute = ATTR_DIRECTORY; 391 ce->info.attribute = ATTR_DIRECTORY;
396 ce->info.size = 0; 392 ce->info.size = 0;
397 ce->down = first_ce; 393 ce->down = first_ce;
@@ -399,7 +395,6 @@ static int sab_process_dir(struct dircache_entry *ce)
399 ce = dircache_gen_next(ce); 395 ce = dircache_gen_next(ce);
400 396
401 ce->d_name = ".."; 397 ce->d_name = "..";
402 ce->name_len = 3;
403 ce->info.attribute = ATTR_DIRECTORY; 398 ce->info.attribute = ATTR_DIRECTORY;
404 ce->info.size = 0; 399 ce->info.size = 0;
405 ce->down = first_ce->up; 400 ce->down = first_ce->up;
@@ -471,7 +466,7 @@ static struct dircache_entry* dircache_get_entry(const char *path, bool go_down)
471 while(cache_entry != NULL) 466 while(cache_entry != NULL)
472 { 467 {
473 /* skip unused entries */ 468 /* skip unused entries */
474 if(cache_entry->name_len == 0) 469 if(cache_entry->d_name == NULL)
475 { 470 {
476 cache_entry = cache_entry->next; 471 cache_entry = cache_entry->next;
477 continue; 472 continue;
@@ -928,29 +923,25 @@ void dircache_copy_path(const struct dircache_entry *entry, char *buf, int size)
928 /* first compute the necessary size */ 923 /* first compute the necessary size */
929 while(temp != NULL) 924 while(temp != NULL)
930 { 925 {
931 path_size += temp->name_len; /* '/' + d_name */ 926 path_size += strlen(temp->d_name) + sizeof('/');
932 temp = temp->up; 927 temp = temp->up;
933 } 928 }
934 929
935 /* now copy the path */ 930 /* now copy the path */
936 /* doesn't matter with trailing 0 because it's added later */
937 idx = path_size; 931 idx = path_size;
938 while(entry != NULL) 932 while(entry != NULL)
939 { 933 {
940 idx -= entry->name_len; 934 idx -= strlen(entry->d_name);
941 /* available size */ 935 /* available size */
942 int rem = size - idx; 936 int rem = size - idx;
943 937
944 if(rem >= 1) 938 if(rem >= 1)
945 { 939 {
946 buf[idx] = '/'; 940 buf[idx] = '/';
947 memcpy(buf + idx + 1, entry->d_name, MIN((size_t)(rem - 1), (size_t)(entry->name_len - 1))); 941 strlcpy(buf + idx + 1, entry->d_name, rem - 1);
948 } 942 }
949 entry = entry->up; 943 entry = entry->up;
950 } 944 }
951
952 /* add trailing 0 */
953 buf[MIN(path_size, size-1)] = 0;
954} 945}
955 946
956/* --- Directory cache live updating functions --- */ 947/* --- Directory cache live updating functions --- */
@@ -1005,22 +996,23 @@ static struct dircache_entry* dircache_new_entry(const char *path, int attribute
1005 while (entry->next != NULL) 996 while (entry->next != NULL)
1006 entry = entry->next; 997 entry = entry->next;
1007 998
1008 if (entry->name_len > 0) 999 if (entry->d_name != NULL)
1009 entry = dircache_gen_next(entry);
1010
1011 if (entry == NULL)
1012 { 1000 {
1013 dircache_initialized = false; 1001 entry = dircache_gen_next(entry);
1014 return NULL; 1002 if (entry == NULL)
1003 {
1004 dircache_initialized = false;
1005 return NULL;
1006 }
1015 } 1007 }
1016 1008
1017 entry->name_len = MIN(254, strlen(new)) + 1;
1018 entry->d_name = ((char *)dircache_root+dircache_size); 1009 entry->d_name = ((char *)dircache_root+dircache_size);
1019 entry->startcluster = 0; 1010 entry->startcluster = 0;
1020 memset(&entry->info, 0, sizeof(entry->info)); 1011 memset(&entry->info, 0, sizeof(entry->info));
1021 entry->info.attribute = attribute; 1012 entry->info.attribute = attribute;
1022 memcpy(entry->d_name, new, entry->name_len); 1013
1023 dircache_size += entry->name_len; 1014 strcpy(entry->d_name, new);
1015 dircache_size += strlen(entry->d_name);
1024 1016
1025 if (attribute & ATTR_DIRECTORY) 1017 if (attribute & ATTR_DIRECTORY)
1026 { 1018 {
@@ -1132,7 +1124,7 @@ void dircache_rmdir(const char *path)
1132 } 1124 }
1133 1125
1134 entry->down = NULL; 1126 entry->down = NULL;
1135 entry->name_len = 0; 1127 entry->d_name = NULL;
1136} 1128}
1137 1129
1138/* Remove a file from cache */ 1130/* Remove a file from cache */
@@ -1154,7 +1146,7 @@ void dircache_remove(const char *name)
1154 return ; 1146 return ;
1155 } 1147 }
1156 1148
1157 entry->name_len = 0; 1149 entry->d_name = NULL;
1158} 1150}
1159 1151
1160void dircache_rename(const char *oldpath, const char *newpath) 1152void dircache_rename(const char *oldpath, const char *newpath)
@@ -1178,7 +1170,7 @@ void dircache_rename(const char *oldpath, const char *newpath)
1178 } 1170 }
1179 1171
1180 /* Delete the old entry. */ 1172 /* Delete the old entry. */
1181 entry->name_len = 0; 1173 entry->d_name = NULL;
1182 1174
1183 /** If we rename the same filename twice in a row, we need to 1175 /** If we rename the same filename twice in a row, we need to
1184 * save the data, because the entry will be re-used. */ 1176 * save the data, because the entry will be re-used. */
@@ -1308,7 +1300,7 @@ struct dirent_cached* readdir_cached(DIR_CACHED* dir)
1308 if(dir->theent.info.attribute != -1) 1300 if(dir->theent.info.attribute != -1)
1309 ce = ce->next; 1301 ce = ce->next;
1310 /* skip unused entries */ 1302 /* skip unused entries */
1311 while(ce != NULL && ce->name_len == 0) 1303 while(ce != NULL && ce->d_name == NULL)
1312 ce = ce->next; 1304 ce = ce->next;
1313 1305
1314 if (ce == NULL) 1306 if (ce == NULL)
@@ -1321,7 +1313,7 @@ struct dirent_cached* readdir_cached(DIR_CACHED* dir)
1321 dir->theent.info = ce->info; 1313 dir->theent.info = ce->info;
1322 dir->internal_entry = ce; 1314 dir->internal_entry = ce;
1323 1315
1324 //logf("-> %s", ce->name); 1316 //logf("-> %s", ce->d_name);
1325 return &dir->theent; 1317 return &dir->theent;
1326} 1318}
1327 1319
diff --git a/firmware/include/dircache.h b/firmware/include/dircache.h
index 87aaa008a7..49e804c7f1 100644
--- a/firmware/include/dircache.h
+++ b/firmware/include/dircache.h
@@ -69,7 +69,6 @@ struct dircache_entry {
69 struct dircache_entry *up; 69 struct dircache_entry *up;
70 struct dircache_entry *down; 70 struct dircache_entry *down;
71 long startcluster; 71 long startcluster;
72 unsigned long name_len;
73 char *d_name; 72 char *d_name;
74}; 73};
75 74