git @ Cat's Eye Technologies linapple / cf6ddd9
Fixed bug that caused large directories several minutes to appear: use scandir with sorting instead of readdir followed by bubble sort; improve file size representation. ghedger authored 6 years ago T. Joseph Carter committed 3 years ago
1 changed file(s) with 68 addition(s) and 71 deletion(s). Raw diff Collapse all Expand all
2424 #include "list.h"
2525
2626 #include "DiskChoose.h"
27
27 #include <errno.h>
2828 //#include "ctype.h"
2929
3030 //#include <stdio.h>
5353
5454 // delay after key pressed (in milliseconds??)
5555 #define KEY_DELAY 25
56
57 #define MAX_FILENAME 36
5658
5759 /////////////////////////////////////////////////////////////////////
5860 /* FONT prev decls */
7981 if(stat(tempname, &info) == -1) return 0;
8082 if(S_ISDIR(info.st_mode)) return 1; // seems to be directory
8183 if(S_ISREG(info.st_mode)) {
82 if(size != NULL) *size = (int)(info.st_size / 1024); // get file size in Kbytes?!
84 if(size != NULL)
85 *size = (int)(info.st_size / 1024); // get file size in Kbytes?!
8386 return 2; // regular file
8487 }
8588
122125 dp = opendir (incoming_dir); // open and read incoming directory
123126 char *tmp;
124127
125 int i,j, B, N; // for cycles, beginning and end of list
128 int i; // for cycles, beginning and end of list
126129
127130 // build prev dir
128131 if(strcmp(incoming_dir, "/")) {
132135 tmp = new char[5];
133136 strcpy(tmp, "<UP>");
134137 sizes.Add(tmp); // add sign of directory
135 B = 1;
136 }
137 else B = 0; // for sorting dirs
138 }
138139 if (dp != NULL)
139140 {
140 while (ep = readdir (dp)) // first looking for directories
141 {
142 int what = getstat(incoming_dir, ep->d_name, NULL);
143 if (strlen(ep->d_name) > 0 && /*strcmp(ep->d_name,".")*/// omit "." (cur dir)
144 ep->d_name[0] != '.'/*strcmp(ep->d_name,"..")*/ && what == 1) // is directory!
145 {
146 tmp = new char[strlen(ep->d_name)+1]; // add entity to list
147 strcpy(tmp, ep->d_name);
148 files.Add(tmp);
149 tmp = new char[6];
150 strcpy(tmp, "<DIR>");
151 sizes.Add(tmp); // add sign of directory
152 } /* if */
153
154 }
141 {
142 struct dirent **namelist;
143 int n;
144
145 n = scandir(incoming_dir, &namelist, NULL, alphasort);
146 if (n < 0)
147 perror("scandir");
148 else {
149 i = 0;
150 while (i < n) {
151 int what = getstat(incoming_dir, namelist[i]->d_name, NULL);
152 if (namelist[i]->d_name && strlen(namelist[i]->d_name) > 0 &&
153 namelist[i]->d_name[0] != '.' && what == 1) // is directory!
154 {
155 tmp = new char[strlen(namelist[i]->d_name)+1]; // add this entity to list
156 strcpy(tmp, namelist[i]->d_name);
157 files.Add(tmp);
158 tmp = new char[6];
159 strcpy(tmp, "<DIR>");
160 sizes.Add(tmp); // add sign of directory
161 }
162 i++;
163 }
164 free(namelist);
165 }
166 }
155167 }
156168 #else
157169 /* Windows specific functions of reading directory structure */
164176 tmp = new char[5];
165177 strcpy(tmp, "<UP>");
166178 sizes.Add(tmp); // add sign of directory
167 B = 1;
168 }
169 else B = 0; // for sorting dirs
179 }
170180
171181
172182 WIN32_FIND_DATA finfo;
201211 } /* if */
202212
203213 #endif
204 // sort directories. Please, don't laugh at my bubble sorting - it the simplest thing I've ever seen --bb
205 if(files.Length() > 2)
206 {
207 N = files.Length() - 1;
208 // B = 1;`- defined above
209 for(i = N; i > B; i--)
210 for(j = B; j < i; j++)
211 if(strcasecmp(files[j], files[j + 1]) > 0)
212 {
213 files.Swap(j,j + 1);
214 sizes.Swap(j,j + 1);
215 }
216
217 }
218 B = files.Length(); // start for files
219214 #ifndef _WIN32
220215 /* POSIX specific routines of reading directory structure */
221216
222217 (void) rewinddir (dp); // to the start
223 // now get all regular files
224 while (ep = readdir (dp))
225 {
226 int fsize;
227
228 if (strlen(ep->d_name) > 4 && ep->d_name[0] != '.'
229 && (getstat(incoming_dir, ep->d_name, &fsize) == 2)) // is normal file!
230 {
231 tmp = new char[strlen(ep->d_name)+1]; // add this entity to list
232 strcpy(tmp, ep->d_name);
233 files.Add(tmp);
234 tmp = new char[10]; // 1400000KB
235 snprintf(tmp, 9, "%dKB", fsize);
236 sizes.Add(tmp); // add this size to list
237 } /* if */
238
239 }
218 {
219 struct dirent **namelist;
220 int n, fsize = 0;
221
222 n = scandir(incoming_dir, &namelist, NULL, alphasort);
223 if (n < 0)
224 perror("scandir");
225 else {
226 int i = 0;
227 while (i < n) {
228 if (strlen(namelist[i]->d_name) > 4 && namelist[i]->d_name[0] != '.'
229 && (getstat(incoming_dir, namelist[i]->d_name, &fsize) == 2)) // is normal file!
230 {
231 tmp = new char[strlen(namelist[i]->d_name)+1]; // add this entity to list
232 strcpy(tmp, namelist[i]->d_name);
233 files.Add(tmp);
234 tmp = new char[10]; // 1400000KB
235 if (1000 > fsize) {
236 snprintf(tmp, 9, "%4dK", fsize);
237 } else if (1000000 > fsize) {
238 snprintf(tmp, 9, "%4dM", (int) (fsize / 1000));
239 } else {
240 snprintf(tmp, 9, "%4dG", (int) (fsize / 1000000));
241 }
242 sizes.Add(tmp); // add this size to list
243 }
244 i++;
245 }
246 free(namelist);
247 }
248 }
249
240250 (void) closedir (dp);
241251 #else
242252 /* Windows specific functions of reading directory structure */
272282
273283 #endif
274284 // do sorting for files
275 if(files.Length() > 2 && B < files.Length())
276 {
277 N = files.Length() - 1;
278 // B = 1;
279 for(i = N; i > B; i--)
280 for(j = B; j < i; j++)
281 if(strcasecmp(files[j], files[j + 1]) > 0)
282 {
283 files.Swap(j,j + 1);
284 sizes.Swap(j,j + 1);
285 }
286 }
287
288285
289286 // Count out cursor position and file number output
290287 act_file = *index_file;
367364 // print file name with enlarged font
368365 char ch;
369366 ch = 0;
370 if(strlen(tmp) > 46) { ch = tmp[46]; tmp[46] = 0;} //cut-off too long string
367 if(strlen(tmp) > MAX_FILENAME) { ch = tmp[MAX_FILENAME]; tmp[MAX_FILENAME] = 0;} //cut-off too long string
371368 font_print(4, TOPX + (i - first_file) * 15 * facy, tmp, screen, 1.7*facx, 1.5*facy); // show name
372369 font_print(sx - 70 * facx, TOPX + (i - first_file) * 15 * facy, siz, screen, 1.7*facx, 1.5*facy);// show info (dir or size)
373 if(ch) tmp[46] = ch; //restore cut-off char
370 if(ch) tmp[MAX_FILENAME] = ch; //restore cut-off char
374371
375372 } /* if */
376373 i++; // next item