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);
487 int FileSystem::update(const char *new_dir)
490 int result = scan_directory(new_dir);
491 // combine the directories and files in the master list
492 return !result ? update_sort() : result;
495 int FileSystem::update_sort()
497 ArrayList<FileItem*> directories, files;
498 for( int i=0; i< dir_list.size(); ++i ) {
499 FileItem *item = dir_list[i];
501 (item->is_dir ? &directories : &files)->append(item);
503 dir_list.remove_all();
504 // combine the directories and files in the master list
505 combine(&directories, &files);
511 int FileSystem::set_filter(const char *new_filter)
513 strcpy(filter, new_filter);
517 int FileSystem::set_show_all()
523 int FileSystem::set_want_directory()
529 int FileSystem::is_dir(const char *path) // return 0 if the text is a directory
531 if(!strlen(path)) return 0;
533 char new_dir[BCTEXTLEN];
534 struct stat ostat; // entire name is a directory
536 strcpy(new_dir, path);
537 complete_path(new_dir);
538 if(!stat(new_dir, &ostat) && S_ISDIR(ostat.st_mode))
544 int FileSystem::create_dir(const char *new_dir_)
546 char new_dir[BCTEXTLEN];
547 strcpy(new_dir, new_dir_);
548 complete_path(new_dir);
550 mkdir(new_dir, S_IREAD | S_IWRITE | S_IEXEC);
554 int FileSystem::parse_tildas(char *new_dir)
556 if(new_dir[0] == 0) return 1;
558 // Our home directory
559 if(new_dir[0] == '~')
562 if(new_dir[1] == '/' || new_dir[1] == 0)
564 // user's home directory
566 char string[BCTEXTLEN];
567 home = getenv("HOME");
569 // print starting after tilda
570 if(home) sprintf(string, "%s%s", home, &new_dir[1]);
571 strcpy(new_dir, string);
575 // Another user's home directory
577 char string[BCTEXTLEN], new_user[BCTEXTLEN];
581 for(i = 1, j = 0; new_dir[i] != 0 && new_dir[i] != '/'; i++, j++)
583 new_user[j] = new_dir[i];
588 while( (pw = getpwent()) != 0 )
591 if(!strcmp(pw->pw_name, new_user))
593 // print starting after tilda
594 sprintf(string, "%s%s", pw->pw_dir, &new_dir[i]);
595 strcpy(new_dir, string);
606 int FileSystem::parse_directories(char *new_dir)
608 //printf("FileSystem::parse_directories 1 %s\n", new_dir);
609 if( *new_dir != '/' ) { // expand to abs path
610 char string[BCTEXTLEN];
611 strcpy(string, current_dir);
612 add_end_slash(string);
613 strcat(string, new_dir);
614 strcpy(new_dir, string);
619 int FileSystem::parse_dots(char *new_dir)
621 // recursively remove ..s
626 len = strlen(new_dir);
628 for(i = 0, j = 1; !changed && j < len; i++, j++)
631 if(new_dir[i] == '.' && new_dir[j] == '.')
633 // Ignore if character after .. doesn't qualify
635 new_dir[j + 1] != ' ' &&
636 new_dir[j + 1] != '/')
639 // Ignore if character before .. doesn't qualify
641 new_dir[i - 1] != '/') continue;
644 while(new_dir[i] != '/' && i > 0)
646 // look for first / before ..
650 // find / before this /
652 while(new_dir[i] != '/' && i > 0)
654 // look for first / before first / before ..
658 // i now equals /first filename before ..
659 // look for first / after ..
660 while(new_dir[j] != '/' && j < len)
665 // j now equals /first filename after ..
668 new_dir[i++] = new_dir[j++];
672 // default to root directory
673 if((new_dir[0]) == 0) sprintf(new_dir, "/");
681 int FileSystem::complete_path(char *filename)
683 //printf("FileSystem::complete_path 1\n");
684 if(!strlen(filename)) return 1;
685 //printf("FileSystem::complete_path 1\n");
686 parse_tildas(filename);
687 //printf("FileSystem::complete_path 1\n");
688 parse_directories(filename);
689 //printf("FileSystem::complete_path 1\n");
690 parse_dots(filename);
691 // don't add end slash since this requires checking if dir
692 //printf("FileSystem::complete_path 2\n");
696 int FileSystem::extract_dir(char *out, const char *in)
701 // complete string is not directory
706 for(i = strlen(out); i > 0 && out[i - 1] != '/'; i--)
710 if(i >= 0) out[i] = 0;
715 int FileSystem::extract_name(char *out, const char *in, int test_dir)
719 if(test_dir && is_dir(in))
720 out[0] = 0; // complete string is directory
723 for(i = strlen(in)-1; i > 0 && in[i] != '/'; i--)
727 if(in[i] == '/') i++;
733 int FileSystem::join_names(char *out, const char *dir_in, const char *name_in)
736 int len = strlen(out);
740 if(len == 0 || out[len] != 0) result = 1; else len--;
744 if(out[len] != '/') strcat(out, "/");
747 strcat(out, name_in);
751 int64_t FileSystem::get_date(const char *filename)
753 struct stat file_status;
754 bzero(&file_status, sizeof(struct stat));
755 int result = stat(filename, &file_status);
756 return !result ? file_status.st_mtime : -1;
759 void FileSystem::set_date(const char *path, int64_t value)
761 struct utimbuf new_time;
762 new_time.actime = value;
763 new_time.modtime = value;
764 utime(path, &new_time);
767 int64_t FileSystem::get_size(char *filename)
769 struct stat file_status;
770 bzero(&file_status, sizeof(struct stat));
771 int result = stat(filename, &file_status);
772 return !result ? file_status.st_size : -1;
775 int FileSystem::change_dir(const char *new_dir, int update)
777 char new_dir_full[BCTEXTLEN];
779 strcpy(new_dir_full, new_dir);
781 complete_path(new_dir_full);
783 if(strcmp(new_dir_full, "/") &&
784 new_dir_full[strlen(new_dir_full) - 1] == '/')
785 new_dir_full[strlen(new_dir_full) - 1] = 0;
788 this->update(new_dir_full);
791 strcpy(current_dir, new_dir_full);
796 int FileSystem::set_current_dir(const char *new_dir)
798 strcpy(current_dir, new_dir);
802 int FileSystem::add_end_slash(char *new_dir)
804 if(new_dir[strlen(new_dir) - 1] != '/') strcat(new_dir, "/");
809 // collapse ".", "..", "//" eg. x/./..//y = y
810 char *FileSystem::basepath(const char *path)
812 char fpath[BCTEXTLEN];
813 unsigned len = strlen(path);
814 if( len >= sizeof(fpath) ) return 0;
816 char *flat = cstrdup("");
819 char *fn = fpath + len;
820 while( fn > fpath ) {
821 while( --fn >= fpath )
822 if( *fn == '/' ) { *fn = 0; break; }
823 fn = fn < fpath ? fpath : fn+1;
824 if( !*fn || !strcmp(fn, ".") ) continue;
825 if( !strcmp(fn, "..") ) { ++r; continue; }
826 if( r < 0 ) continue;
827 if( r > 0 ) { --r; continue; }
828 char *cp = cstrcat(3, "/",fn,flat);
829 delete [] flat; flat = cp;
833 char *cp = cstrcat(2, ".",flat);
834 delete [] flat; flat = cp;