22
33static void archiver_func (void );
44static int recursive_archive (const char * path , const struct stat * sb , int typeflag , struct FTW * ftwbuf );
5- static void extractor_thread (struct archive * a );
5+ static void extractor_thread (struct archive * a , const char * current_dir );
66
77static struct archive * archive ;
88
@@ -69,12 +69,17 @@ static int recursive_archive(const char *path, const struct stat *sb, int typefl
6969
7070int try_extractor (void ) {
7171 struct archive * a ;
72+ char current_dir [PATH_MAX + 1 ];
7273
7374 a = archive_read_new ();
7475 archive_read_support_filter_all (a );
7576 archive_read_support_format_all (a );
7677 if ((a ) && (archive_read_open_filename (a , thread_h -> filename , BUFF_SIZE ) == ARCHIVE_OK )) {
77- extractor_thread (a );
78+ strcpy (current_dir , thread_h -> filename );
79+ char * tmp = strrchr (current_dir , '/' );
80+ int len = strlen (current_dir ) - strlen (tmp );
81+ current_dir [len ] = '\0' ;
82+ extractor_thread (a , current_dir );
7883 return 0 ;
7984 }
8085 archive_read_free (a );
@@ -87,37 +92,30 @@ int try_extractor(void) {
8792 * While there are headers inside the archive being read, it goes on copying data from
8893 * the read archive to the disk.
8994 */
90- static void extractor_thread (struct archive * a ) {
95+ static void extractor_thread (struct archive * a , const char * current_dir ) {
9196 struct archive * ext ;
9297 struct archive_entry * entry ;
93- int len , num = 0 ;
98+ int len , num ;
9499 int flags = ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_ACL | ARCHIVE_EXTRACT_FFLAGS ;
95- char buff [BUFF_SIZE ], current_dir [PATH_MAX + 1 ], fullpathname [PATH_MAX + 1 ];
96- char * tmp ;
100+ char buff [BUFF_SIZE ], fullpathname [PATH_MAX + 1 ];
97101 char name [PATH_MAX + 1 ], tmp_name [PATH_MAX + 1 ];
98102
99- strcpy (current_dir , thread_h -> filename );
100- tmp = strrchr (current_dir , '/' );
101- len = strlen (current_dir ) - strlen (tmp );
102- current_dir [len ] = '\0' ;
103103 ext = archive_write_disk_new ();
104104 archive_write_disk_set_options (ext , flags );
105105 archive_write_disk_set_standard_lookup (ext );
106106 while (archive_read_next_header (a , & entry ) != ARCHIVE_EOF ) {
107107 strcpy (name , archive_entry_pathname (entry ));
108- /* only perform overwrite check if on the first entry, or if num != 1 */
109- if (( len == strlen ( current_dir )) || ( num != 1 )) {
110- /* avoid overwriting a file/dir in path if it has the same name of a file being extracted there */
108+ num = 0 ;
109+ /* avoid overwriting a file/dir in path if it has the same name of a file being extracted there */
110+ if ( strchr ( name , '/' )) {
111111 strcpy (tmp_name , strchr (name , '/' ));
112- len = strlen (name ) - strlen (tmp_name );
113- if (num == 0 ) {
114- while (access (name , F_OK ) != -1 ) {
115- num ++ ;
116- sprintf (name + len , "%d%s" , num , tmp_name );
117- }
118- } else {
119- sprintf (name + len , "%d%s" , num , tmp_name );
120- }
112+ } else {
113+ tmp_name [0 ] = '\0' ;
114+ }
115+ len = strlen (name ) - strlen (tmp_name );
116+ while (access (name , F_OK ) != -1 ) {
117+ num ++ ;
118+ sprintf (name + len , "%d%s" , num , tmp_name );
121119 }
122120 sprintf (fullpathname , "%s/%s" , current_dir , name );
123121 archive_entry_set_pathname (entry , fullpathname );
0 commit comments