summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--firmware/common/dircache.c20
-rw-r--r--firmware/common/file.c37
-rw-r--r--firmware/include/dircache.h4
3 files changed, 52 insertions, 9 deletions
diff --git a/firmware/common/dircache.c b/firmware/common/dircache.c
index f9fd63b8e4..873e0f9cf4 100644
--- a/firmware/common/dircache.c
+++ b/firmware/common/dircache.c
@@ -798,12 +798,20 @@ void dircache_bind(int fd, const char *path)
798 fd_bindings[fd] = entry; 798 fd_bindings[fd] = entry;
799} 799}
800 800
801void dircache_update_filesize(int fd, long newsize) 801void dircache_update_filesize(int fd, long newsize, long startcluster)
802{ 802{
803 if (!dircache_initialized || fd < 0) 803 if (!dircache_initialized || fd < 0)
804 return ; 804 return ;
805 805
806 if (fd_bindings[fd] == NULL)
807 {
808 logf("dircache fd access error");
809 dircache_initialized = false;
810 return ;
811 }
812
806 fd_bindings[fd]->size = newsize; 813 fd_bindings[fd]->size = newsize;
814 fd_bindings[fd]->startcluster = startcluster;
807} 815}
808 816
809void dircache_mkdir(const char *path) 817void dircache_mkdir(const char *path)
@@ -915,13 +923,19 @@ void dircache_rename(const char *oldpath, const char *newpath)
915 newentry->wrtdate = oldentry.wrtdate; 923 newentry->wrtdate = oldentry.wrtdate;
916} 924}
917 925
918void dircache_add_file(const char *path) 926void dircache_add_file(const char *path, long startcluster)
919{ 927{
928 struct dircache_entry *entry;
929
920 if (!dircache_initialized) 930 if (!dircache_initialized)
921 return ; 931 return ;
922 932
923 logf("add file: %s", path); 933 logf("add file: %s", path);
924 dircache_new_entry(path, 0); 934 entry = dircache_new_entry(path, 0);
935 if (entry == NULL)
936 return ;
937
938 entry->startcluster = startcluster;
925} 939}
926 940
927DIRCACHED* opendir_cached(const char* name) 941DIRCACHED* opendir_cached(const char* name)
diff --git a/firmware/common/file.c b/firmware/common/file.c
index 761caeee4b..a0b4296495 100644
--- a/firmware/common/file.c
+++ b/firmware/common/file.c
@@ -99,9 +99,35 @@ int open(const char* pathname, int flags)
99 } 99 }
100 file->busy = true; 100 file->busy = true;
101 101
102#ifdef HAVE_DIRCACHE
103 if (dircache_is_enabled() && !file->write)
104 {
105 const struct dircache_entry *ce;
106
107 ce = dircache_get_entry_ptr(pathname);
108 if (!ce)
109 {
110 errno = ENOENT;
111 file->busy = false;
112 return -7;
113 }
114
115 fat_open(IF_MV2(unsupported at the moment,)
116 ce->startcluster,
117 &(file->fatfile),
118 NULL);
119 file->size = ce->size;
120 file->attr = ce->attribute;
121 file->cacheoffset = -1;
122 file->fileoffset = 0;
123
124 return fd;
125 }
126#endif
127
102 strncpy(pathnamecopy,pathname,sizeof(pathnamecopy)); 128 strncpy(pathnamecopy,pathname,sizeof(pathnamecopy));
103 pathnamecopy[sizeof(pathnamecopy)-1] = 0; 129 pathnamecopy[sizeof(pathnamecopy)-1] = 0;
104 130
105 /* locate filename */ 131 /* locate filename */
106 name=strrchr(pathnamecopy+1,'/'); 132 name=strrchr(pathnamecopy+1,'/');
107 if ( name ) { 133 if ( name ) {
@@ -120,7 +146,7 @@ int open(const char* pathname, int flags)
120 file->busy = false; 146 file->busy = false;
121 return -4; 147 return -4;
122 } 148 }
123 149
124 if(name[0] == 0) { 150 if(name[0] == 0) {
125 DEBUGF("Empty file name\n"); 151 DEBUGF("Empty file name\n");
126 errno = EINVAL; 152 errno = EINVAL;
@@ -156,7 +182,7 @@ int open(const char* pathname, int flags)
156 return rc * 10 - 6; 182 return rc * 10 - 6;
157 } 183 }
158#ifdef HAVE_DIRCACHE 184#ifdef HAVE_DIRCACHE
159 dircache_add_file(pathname); 185 dircache_add_file(pathname, file->fatfile.firstcluster);
160#endif 186#endif
161 file->size = 0; 187 file->size = 0;
162 file->attr = 0; 188 file->attr = 0;
@@ -394,6 +420,9 @@ int ftruncate(int fd, off_t size)
394 } 420 }
395 421
396 file->size = size; 422 file->size = size;
423#ifdef HAVE_DIRCACHE
424 dircache_update_filesize(fd, size, file->fatfile.firstcluster);
425#endif
397 426
398 return 0; 427 return 0;
399} 428}
@@ -569,7 +598,7 @@ static int readwrite(int fd, void* buf, long count, bool write)
569 { 598 {
570 file->size = file->fileoffset; 599 file->size = file->fileoffset;
571#ifdef HAVE_DIRCACHE 600#ifdef HAVE_DIRCACHE
572 dircache_update_filesize(fd, file->size); 601 dircache_update_filesize(fd, file->size, file->fatfile.firstcluster);
573#endif 602#endif
574 } 603 }
575 604
diff --git a/firmware/include/dircache.h b/firmware/include/dircache.h
index 93bc413dc9..48f29806e3 100644
--- a/firmware/include/dircache.h
+++ b/firmware/include/dircache.h
@@ -82,12 +82,12 @@ const struct dircache_entry *dircache_get_entry_ptr(const char *filename);
82void dircache_copy_path(const struct dircache_entry *entry, char *buf, int size); 82void dircache_copy_path(const struct dircache_entry *entry, char *buf, int size);
83 83
84void dircache_bind(int fd, const char *path); 84void dircache_bind(int fd, const char *path);
85void dircache_update_filesize(int fd, long newsize); 85void dircache_update_filesize(int fd, long newsize, long startcluster);
86void dircache_mkdir(const char *path); 86void dircache_mkdir(const char *path);
87void dircache_rmdir(const char *path); 87void dircache_rmdir(const char *path);
88void dircache_remove(const char *name); 88void dircache_remove(const char *name);
89void dircache_rename(const char *oldpath, const char *newpath); 89void dircache_rename(const char *oldpath, const char *newpath);
90void dircache_add_file(const char *path); 90void dircache_add_file(const char *path, long startcluster);
91 91
92DIRCACHED* opendir_cached(const char* name); 92DIRCACHED* opendir_cached(const char* name);
93struct dircache_entry* readdir_cached(DIRCACHED* dir); 93struct dircache_entry* readdir_cached(DIRCACHED* dir);