3 * Copyright (C) 2010 Adam Williams <broadcast at earthling dot net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "bcsignals.h"
31 #include <sys/types.h>
37 #include "filesystem.h"
46 FileItem::FileItem(char *path, char *name, int is_dir,
47 int64_t size, int month, int day, int year,
48 int64_t calendar_time, int item_no)
50 this->path = new char[strlen(path)];
51 this->name = new char[strlen(name)];
52 if(this->path) strcpy(this->path, path);
53 if(this->name) strcpy(this->name, name);
54 this->is_dir = is_dir;
59 this->calendar_time = calendar_time;
60 this->item_no = item_no;
70 if(this->path) delete [] this->path;
71 if(this->name) delete [] this->name;
84 int FileItem::set_path(char *path)
86 if(this->path) delete [] this->path;
87 this->path = new char[strlen(path) + 1];
88 strcpy(this->path, path);
92 int FileItem::set_name(char *name)
94 if(this->name) delete [] this->name;
95 this->name = new char[strlen(name) + 1];
96 strcpy(this->name, name);
102 FileSystem::FileSystem()
105 (void)getcwd(current_dir, BCTEXTLEN);
108 FileSystem::~FileSystem()
113 int FileSystem::reset_parameters()
118 strcpy(current_dir, "");
119 sort_order = SORT_ASCENDING;
120 sort_field = SORT_PATH;
124 int FileSystem::delete_directory()
126 dir_list.remove_all_objects();
130 int FileSystem::set_sort_order(int value)
132 this->sort_order = value;
136 int FileSystem::set_sort_field(int field)
138 this->sort_field = field;
142 // filename.with.dots.extension
144 // extension.dots.with.filename
146 int FileSystem::dot_reverse_filename(char *out, const char *in)
148 int i, i2, j=0, lastdot;
149 lastdot = strlen(in);
150 for ( i=strlen(in); --i >= 0; ) {
159 if (in[++i] != '.') {
160 while (i < lastdot) out[j++] = in[i++];
166 int FileSystem::path_ascending(const void *ptr1, const void *ptr2)
168 FileItem *item1 = *(FileItem**)ptr1;
169 FileItem *item2 = *(FileItem**)ptr2;
170 //printf("path_ascending %p %p\n", ptr1, ptr2);
171 int ret = strcasecmp(item1->name, item2->name);
172 if( ret != 0 ) return ret;
173 return item1->item_no - item2->item_no;
176 int FileSystem::path_descending(const void *ptr1, const void *ptr2)
178 FileItem *item1 = *(FileItem**)ptr1;
179 FileItem *item2 = *(FileItem**)ptr2;
180 int ret = strcasecmp(item2->name, item1->name);
181 if( ret != 0 ) return ret;
182 return item2->item_no - item1->item_no;
186 int FileSystem::size_ascending(const void *ptr1, const void *ptr2)
188 FileItem *item1 = *(FileItem**)ptr1;
189 FileItem *item2 = *(FileItem**)ptr2;
190 return item1->size == item2->size ?
191 item1->item_no - item2->item_no :
192 item1->size > item2->size;
195 int FileSystem::size_descending(const void *ptr1, const void *ptr2)
197 FileItem *item1 = *(FileItem**)ptr1;
198 FileItem *item2 = *(FileItem**)ptr2;
199 return item2->size == item1->size ?
200 item2->item_no - item1->item_no :
201 item2->size > item1->size;
205 int FileSystem::date_ascending(const void *ptr1, const void *ptr2)
207 FileItem *item1 = *(FileItem**)ptr1;
208 FileItem *item2 = *(FileItem**)ptr2;
209 return item1->calendar_time == item2->calendar_time ?
210 item1->item_no - item2->item_no :
211 item1->calendar_time > item2->calendar_time;
214 int FileSystem::date_descending(const void *ptr1, const void *ptr2)
216 FileItem *item1 = *(FileItem**)ptr1;
217 FileItem *item2 = *(FileItem**)ptr2;
218 return item2->calendar_time == item1->calendar_time ?
219 item2->item_no - item1->item_no :
220 item2->calendar_time > item1->calendar_time;
223 int FileSystem::ext_ascending(const void *ptr1, const void *ptr2)
225 FileItem *item1 = *(FileItem**)ptr1;
226 FileItem *item2 = *(FileItem**)ptr2;
227 char *ext1 = strrchr(item1->name,'.');
228 if( !ext1 ) ext1 = item1->name;
229 char *ext2 = strrchr(item2->name,'.');
230 if( !ext2 ) ext2 = item2->name;
231 int ret = strcasecmp(ext1, ext2);
232 if( ret ) return ret;
233 if( item1->item_no >= 0 && item2->item_no >= 0 )
234 return item1->item_no - item2->item_no;
235 char dotreversedname1[BCTEXTLEN], dotreversedname2[BCTEXTLEN];
236 dot_reverse_filename(dotreversedname1,item1->name);
237 dot_reverse_filename(dotreversedname2,item2->name);
238 return strcasecmp(dotreversedname1, dotreversedname2);
241 int FileSystem::ext_descending(const void *ptr1, const void *ptr2)
243 FileItem *item1 = *(FileItem**)ptr1;
244 FileItem *item2 = *(FileItem**)ptr2;
245 char *ext1 = strrchr(item1->name,'.');
246 if( !ext1 ) ext1 = item1->name;
247 char *ext2 = strrchr(item2->name,'.');
248 if( !ext2 ) ext2 = item2->name;
249 int ret = strcasecmp(ext2, ext1);
250 if( ret ) return ret;
251 if( item2->item_no >= 0 && item1->item_no >= 0 )
252 return item2->item_no - item1->item_no;
253 char dotreversedname1[BCTEXTLEN], dotreversedname2[BCTEXTLEN];
254 dot_reverse_filename(dotreversedname1,item1->name);
255 dot_reverse_filename(dotreversedname2,item2->name);
256 return strcasecmp(dotreversedname2, dotreversedname1);
259 int FileSystem::sort_table(ArrayList<FileItem*> *dir_list)
261 if(!dir_list || !dir_list->size()) return 0;
262 static int (*cmpr[][2])(const void *ptr1, const void *ptr2) = {
263 { &path_ascending, &path_descending },
264 { &size_ascending, &size_descending },
265 { &date_ascending, &date_descending },
266 { &ext_ascending, &ext_descending },
269 qsort(dir_list->values,
270 dir_list->size(), sizeof(FileItem*),
271 cmpr[sort_field][sort_order]);
276 int FileSystem::combine(ArrayList<FileItem*> *dir_list, ArrayList<FileItem*> *file_list)
280 sort_table(dir_list);
281 for(i = 0; i < dir_list->total; i++)
283 this->dir_list.append(dir_list->values[i]);
286 sort_table(file_list);
287 for(i = 0; i < file_list->total; i++)
289 this->dir_list.append(file_list->values[i]);
294 int FileSystem::test_filter(FileItem *file)
296 char *filter1 = 0, *filter2 = filter, *subfilter1, *subfilter2;
298 int done = 0, token_done;
299 int token_number = 0;
301 // Don't filter directories
302 if(file->is_dir) return 0;
304 // Empty filename string
305 if(!file->name) return 1;
310 filter1 = strchr(filter2, '[');
317 filter2 = strchr(filter1, ']');
322 for(i = 0; filter1 + i < filter2; i++)
323 string[i] = filter1[i];
328 strcpy(string, filter1);
335 strcpy(string, filter);
343 char *path = file->name;
351 subfilter2 = strchr(subfilter1, '*');
356 for(i = 0; subfilter1 + i < subfilter2; i++)
357 string2[i] = subfilter1[i];
363 strcpy(string2, subfilter1);
369 // Subfilter must exist at some later point in the string
370 if(subfilter1 > string)
372 if(!strstr(path, string2))
378 path = strstr(path, string2) + strlen(string2);
381 // Subfilter must exist at this point in the string
383 if(strncmp(path, string2, strlen(string2)))
384 // if(strncasecmp(path, string2, strlen(string2)))
390 path += strlen(string2);
393 // String must terminate after subfilter
403 subfilter1 = subfilter2 + 1;
404 // Let pass if no subfilter
405 }while(!token_done && !result);
408 }while(!done && result);
414 int FileSystem::scan_directory(const char *new_dir)
417 strcpy(current_dir, new_dir);
418 DIR *dirstream = opendir(current_dir);
419 if( !dirstream ) return 1; // failed to open directory
421 struct dirent64 *new_filename;
422 while( (new_filename = readdir64(dirstream)) != 0 ) {
423 FileItem *new_file = 0;
424 int include_this = 1;
426 // File is directory heirarchy
427 if(!strcmp(new_filename->d_name, ".") ||
428 !strcmp(new_filename->d_name, "..")) include_this = 0;
430 // File is hidden and we don't want all files
433 new_filename->d_name[0] == '.') include_this = 0;
438 new_file = new FileItem;
439 char full_path[BCTEXTLEN], name_only[BCTEXTLEN];
440 sprintf(full_path, "%s", current_dir);
441 add_end_slash(full_path);
442 strcat(full_path, new_filename->d_name);
443 strcpy(name_only, new_filename->d_name);
444 new_file->set_path(full_path);
445 new_file->set_name(name_only);
447 // Get information about the file.
449 if(!stat(full_path, &ostat))
451 new_file->size = ostat.st_size;
452 struct tm *mod_time = localtime(&(ostat.st_mtime));
453 new_file->month = mod_time->tm_mon + 1;
454 new_file->day = mod_time->tm_mday;
455 new_file->year = mod_time->tm_year + 1900;
456 new_file->calendar_time = ostat.st_mtime;
458 if(S_ISDIR(ostat.st_mode))
460 strcat(name_only, "/"); // is a directory
461 new_file->is_dir = 1;
464 // File is excluded from filter
465 if(include_this && test_filter(new_file)) include_this = 0;
466 //printf("FileSystem::update 3 %d %d\n", include_this, test_filter(new_file));
468 // File is not a directory and we just want directories
469 if(include_this && want_directory && !new_file->is_dir) include_this = 0;
473 printf("FileSystem::update %s %s\n", full_path, strerror(errno));
479 dir_list.append(new_file);
488 int FileSystem::update(const char *new_dir)
491 int result = scan_directory(new_dir);
492 // combine the directories and files in the master list
493 return !result ? update_sort() : result;
496 int FileSystem::update_sort()
498 ArrayList<FileItem*> directories, files;
499 for( int i=0; i< dir_list.size(); ++i ) {
500 FileItem *item = dir_list[i];
502 (item->is_dir ? &directories : &files)->append(item);
504 dir_list.remove_all();
505 // combine the directories and files in the master list
506 combine(&directories, &files);
512 int FileSystem::set_filter(const char *new_filter)
514 strcpy(filter, new_filter);
518 int FileSystem::set_show_all()
524 int FileSystem::set_want_directory()
530 int FileSystem::is_dir(const char *path) // return 0 if the text is a directory
532 if(!strlen(path)) return 0;
534 char new_dir[BCTEXTLEN];
535 struct stat ostat; // entire name is a directory
537 strcpy(new_dir, path);
538 complete_path(new_dir);
539 if(!stat(new_dir, &ostat) && S_ISDIR(ostat.st_mode))
545 int FileSystem::create_dir(const char *new_dir_)
547 char new_dir[BCTEXTLEN];
548 strcpy(new_dir, new_dir_);
549 complete_path(new_dir);
551 mkdir(new_dir, S_IREAD | S_IWRITE | S_IEXEC);
555 int FileSystem::parse_tildas(char *new_dir)
557 if(new_dir[0] == 0) return 1;
559 // Our home directory
560 if(new_dir[0] == '~')
563 if(new_dir[1] == '/' || new_dir[1] == 0)
565 // user's home directory
567 char string[BCTEXTLEN];
568 home = getenv("HOME");
570 // print starting after tilda
571 if(home) sprintf(string, "%s%s", home, &new_dir[1]);
572 strcpy(new_dir, string);
576 // Another user's home directory
578 char string[BCTEXTLEN], new_user[BCTEXTLEN];
582 for(i = 1, j = 0; new_dir[i] != 0 && new_dir[i] != '/'; i++, j++)
584 new_user[j] = new_dir[i];
589 while( (pw = getpwent()) != 0 )
592 if(!strcmp(pw->pw_name, new_user))
594 // print starting after tilda
595 sprintf(string, "%s%s", pw->pw_dir, &new_dir[i]);
596 strcpy(new_dir, string);
607 int FileSystem::parse_directories(char *new_dir)
609 //printf("FileSystem::parse_directories 1 %s\n", new_dir);
610 if( *new_dir != '/' && current_dir[0] ) { // expand to abs path
611 char string[BCTEXTLEN];
612 strcpy(string, current_dir);
613 add_end_slash(string);
614 strcat(string, new_dir);
615 strcpy(new_dir, string);
620 int FileSystem::parse_dots(char *new_dir)
622 // recursively remove ..s
627 len = strlen(new_dir);
629 for(i = 0, j = 1; !changed && j < len; i++, j++)
632 if(new_dir[i] == '.' && new_dir[j] == '.')
634 // Ignore if character after .. doesn't qualify
636 new_dir[j + 1] != ' ' &&
637 new_dir[j + 1] != '/')
640 // Ignore if character before .. doesn't qualify
642 new_dir[i - 1] != '/') continue;
645 while(new_dir[i] != '/' && i > 0)
647 // look for first / before ..
651 // find / before this /
653 while(new_dir[i] != '/' && i > 0)
655 // look for first / before first / before ..
659 // i now equals /first filename before ..
660 // look for first / after ..
661 while(new_dir[j] != '/' && j < len)
666 // j now equals /first filename after ..
669 new_dir[i++] = new_dir[j++];
673 // default to root directory
674 if((new_dir[0]) == 0) sprintf(new_dir, "/");
682 int FileSystem::complete_path(char *filename)
684 //printf("FileSystem::complete_path 1\n");
685 if(!strlen(filename)) return 1;
686 //printf("FileSystem::complete_path 1\n");
687 parse_tildas(filename);
688 //printf("FileSystem::complete_path 1\n");
689 parse_directories(filename);
690 //printf("FileSystem::complete_path 1\n");
691 parse_dots(filename);
692 // don't add end slash since this requires checking if dir
693 //printf("FileSystem::complete_path 2\n");
697 int FileSystem::extract_dir(char *out, const char *in)
702 // complete string is not directory
707 for(i = strlen(out); i > 0 && out[i - 1] != '/'; i--)
711 if(i >= 0) out[i] = 0;
716 int FileSystem::extract_name(char *out, const char *in, int test_dir)
720 if(test_dir && is_dir(in))
721 out[0] = 0; // complete string is directory
724 for(i = strlen(in)-1; i > 0 && in[i] != '/'; i--)
728 if(in[i] == '/') i++;
734 int FileSystem::join_names(char *out, const char *dir_in, const char *name_in)
737 int len = strlen(out);
741 if(len == 0 || out[len] != 0) result = 1; else len--;
745 if(out[len] != '/') strcat(out, "/");
748 strcat(out, name_in);
752 int64_t FileSystem::get_date(const char *filename)
754 struct stat file_status;
755 bzero(&file_status, sizeof(struct stat));
756 int result = stat(filename, &file_status);
757 return !result ? file_status.st_mtime : -1;
760 void FileSystem::set_date(const char *path, int64_t value)
762 struct utimbuf new_time;
763 new_time.actime = value;
764 new_time.modtime = value;
765 utime(path, &new_time);
768 int64_t FileSystem::get_size(char *filename)
770 struct stat file_status;
771 bzero(&file_status, sizeof(struct stat));
772 int result = stat(filename, &file_status);
773 return !result ? file_status.st_size : -1;
776 int FileSystem::change_dir(const char *new_dir, int update)
778 char new_dir_full[BCTEXTLEN];
780 strcpy(new_dir_full, new_dir);
782 complete_path(new_dir_full);
784 if(strcmp(new_dir_full, "/") &&
785 new_dir_full[strlen(new_dir_full) - 1] == '/')
786 new_dir_full[strlen(new_dir_full) - 1] = 0;
789 this->update(new_dir_full);
792 strcpy(current_dir, new_dir_full);
797 int FileSystem::set_current_dir(const char *new_dir)
799 strcpy(current_dir, new_dir);
803 int FileSystem::add_end_slash(char *new_dir)
805 if(new_dir[strlen(new_dir) - 1] != '/') strcat(new_dir, "/");
810 // collapse ".", "..", "//" eg. x/./..//y = y
811 char *FileSystem::basepath(const char *path)
813 char fpath[BCTEXTLEN];
814 unsigned len = strlen(path);
815 if( len >= sizeof(fpath) ) return 0;
817 char *flat = cstrdup("");
820 char *fn = fpath + len;
821 while( fn > fpath ) {
822 while( --fn >= fpath )
823 if( *fn == '/' ) { *fn = 0; break; }
824 fn = fn < fpath ? fpath : fn+1;
825 if( !*fn || !strcmp(fn, ".") ) continue;
826 if( !strcmp(fn, "..") ) { ++r; continue; }
827 if( r < 0 ) continue;
828 if( r > 0 ) { --r; continue; }
829 char *cp = cstrcat(3, "/",fn,flat);
830 delete [] flat; flat = cp;
834 char *cp = cstrcat(2, ".",flat);
835 delete [] flat; flat = cp;