comparison src/filelist.c @ 146:e8285b0c0c7d

prevent duplicate sidecar files; code cleanup
author nadvornik
date Wed, 21 Nov 2007 21:21:17 +0000
parents 8be2cc687304
children b2266996fa83
comparison
equal deleted inserted replaced
145:8be2cc687304 146:e8285b0c0c7d
666 fd->extension = extension_from_path(fd->path); 666 fd->extension = extension_from_path(fd->path);
667 if (fd->extension == NULL) 667 if (fd->extension == NULL)
668 fd->extension = fd->name + strlen(fd->name); 668 fd->extension = fd->name + strlen(fd->name);
669 } 669 }
670 670
671 static void file_data_check_sidecars(FileData *fd);
671 static GHashTable *file_data_pool = NULL; 672 static GHashTable *file_data_pool = NULL;
672 673
673 static FileData *file_data_new(const gchar *path_utf8, struct stat *st, gboolean check_sidecars) 674 static FileData *file_data_new(const gchar *path_utf8, struct stat *st, gboolean check_sidecars)
674 { 675 {
675 FileData *fd; 676 FileData *fd;
699 fd->magick = 0x12345678; 700 fd->magick = 0x12345678;
700 701
701 g_hash_table_insert(file_data_pool, fd->original_path, fd); 702 g_hash_table_insert(file_data_pool, fd->original_path, fd);
702 703
703 if (check_sidecars && sidecar_file_priority(fd->extension)) 704 if (check_sidecars && sidecar_file_priority(fd->extension))
704 { 705 file_data_check_sidecars(fd);
705 int i = 0; 706 return fd;
706 int base_len = fd->extension - fd->path; 707 }
707 GString *fname = g_string_new_len(fd->path, base_len); 708
708 FileData *parent_fd = NULL; 709 static void file_data_check_sidecars(FileData *fd)
709 GList *work = sidecar_ext_get_list(); 710 {
710 while (work) 711 int i = 0;
711 { 712 int base_len = fd->extension - fd->path;
712 /* check for possible sidecar files; 713 GString *fname = g_string_new_len(fd->path, base_len);
713 the sidecar files created here are referenced only via fd->sidecar_files or fd->parent, 714 FileData *parent_fd = NULL;
714 they have fd->ref set to 0 and file_data unref must chack and free them all together 715 GList *work = sidecar_ext_get_list();
715 (using fd->ref would cause loops and leaks) 716 while (work)
716 */ 717 {
717 718 /* check for possible sidecar files;
718 FileData *new_fd; 719 the sidecar files created here are referenced only via fd->sidecar_files or fd->parent,
720 they have fd->ref set to 0 and file_data unref must chack and free them all together
721 (using fd->ref would cause loops and leaks)
722 */
723
724 FileData *new_fd;
725
726 gchar *ext = work->data;
727 work = work->next;
728
729 if (strcmp(ext, fd->extension) == 0)
730 {
731 new_fd = fd; /* processing the original file */
732 }
733 else
734 {
735 struct stat nst;
736 g_string_truncate(fname, base_len);
737 g_string_append(fname, ext);
738
739 if (!stat_utf8(fname->str, &nst))
740 continue;
741
742 new_fd = file_data_new(fname->str, &nst, FALSE);
743 new_fd->ref--; /* do not use ref here */
744 }
719 745
720 gchar *ext = work->data; 746 if (!parent_fd)
721 work = work->next; 747 parent_fd = new_fd; /* parent is the one with the highest prio, found first */
722 748 else
723 if (strcmp(ext, fd->extension) == 0) 749 file_data_merge_sidecar_files(parent_fd, new_fd);
724 { 750 }
725 new_fd = fd; /* processing the original file */ 751 g_string_free(fname, TRUE);
726 } 752 }
727 else 753
728 {
729 struct stat nst;
730 g_string_truncate(fname, base_len);
731 g_string_append(fname, ext);
732
733 if (!stat_utf8(fname->str, &nst))
734 continue;
735
736 new_fd = file_data_new(fname->str, &nst, FALSE);
737 new_fd->ref--; /* do not use ref here */
738 }
739
740 if (!parent_fd)
741 parent_fd = new_fd; /* parent is the one with the highest prio, found first */
742 else
743 file_data_merge_sidecar_files(parent_fd, new_fd);
744
745 }
746 g_string_free(fname, TRUE);
747 }
748
749 return fd;
750 }
751 754
752 static FileData *file_data_new_local(const gchar *path, struct stat *st, gboolean check_sidecars) 755 static FileData *file_data_new_local(const gchar *path, struct stat *st, gboolean check_sidecars)
753 { 756 {
754 gchar *path_utf8 = path_to_utf8(path); 757 gchar *path_utf8 = path_to_utf8(path);
755 FileData *ret = file_data_new(path_utf8, st, check_sidecars); 758 FileData *ret = file_data_new(path_utf8, st, check_sidecars);
770 return file_data_new(path_utf8, &st, TRUE); 773 return file_data_new(path_utf8, &st, TRUE);
771 } 774 }
772 775
773 FileData *file_data_add_sidecar_file(FileData *target, FileData *sfd) 776 FileData *file_data_add_sidecar_file(FileData *target, FileData *sfd)
774 { 777 {
775 target->sidecar_files = g_list_append(target->sidecar_files, sfd);
776 sfd->parent = target; 778 sfd->parent = target;
779 if(!g_list_find(target->sidecar_files, sfd))
780 target->sidecar_files = g_list_prepend(target->sidecar_files, sfd);
777 return target; 781 return target;
778 } 782 }
779 783
780 FileData *file_data_merge_sidecar_files(FileData *target, FileData *source) 784 FileData *file_data_merge_sidecar_files(FileData *target, FileData *source)
781 { 785 {
784 788
785 work = source->sidecar_files; 789 work = source->sidecar_files;
786 while (work) 790 while (work)
787 { 791 {
788 FileData *sfd = work->data; 792 FileData *sfd = work->data;
789 sfd->parent = target; 793 file_data_add_sidecar_file(target, sfd);
790 work = work->next; 794 work = work->next;
791 } 795 }
792 796
793 target->sidecar_files = g_list_concat(target->sidecar_files, source->sidecar_files); 797 g_list_free(source->sidecar_files);
794 source->sidecar_files = NULL; 798 source->sidecar_files = NULL;
795 799
796 return target; 800 return target;
797 } 801 }
798 802