@@ -29,65 +29,53 @@ extern LIBACQUIRE_EXPORT const char *get_download_dir(void);
2929
3030#include <string.h>
3131
32- bool is_downloaded (const char * url , enum Checksum checksum , const char * hash ,
33- const char * target_location ) {
34- char full_local_fname [NAME_MAX + 1 ];
32+ bool is_downloaded (const char * url_or_path , enum Checksum checksum ,
33+ const char * hash , const char * target_location ) {
34+
35+ char path_buffer [NAME_MAX + 1 ];
36+ const char * file_to_check ;
3537 char * filename_from_url = NULL ;
36- const char * filename ;
3738 struct acquire_handle * verify_handle ;
3839 int result ;
39- bool is_verified = false;
4040
41- if (is_url (url )) {
42- filename_from_url = get_path_from_url (url );
43- filename = filename_from_url ;
44- } else {
45- filename = url ;
46- }
47-
48- if (filename == NULL || * filename == '\0' || hash == NULL ) {
49- free (filename_from_url );
41+ if (url_or_path == NULL || hash == NULL ) {
5042 return false;
5143 }
5244
53- if (target_location == NULL ) {
54- target_location = get_download_dir ();
55- }
56-
57- if (is_file (filename )) {
58- size_t len = strlen (filename );
59- if (len > NAME_MAX )
60- len = NAME_MAX - 1 ;
61- memcpy (full_local_fname , filename , len );
62- full_local_fname [len ] = '\0' ;
63- } else {
64- if (!is_directory (target_location ) && !is_file (target_location )) {
45+ if (is_url (url_or_path )) {
46+ filename_from_url = get_path_from_url (url_or_path );
47+ if (filename_from_url == NULL || * filename_from_url == '\0' ) {
6548 free (filename_from_url );
66- return false;
49+ return false; /* Cannot determine filename from URL. */
6750 }
68-
69- snprintf (full_local_fname , NAME_MAX + 1 , "%s" PATH_SEP "%s" ,
70- target_location , filename );
71-
72- if (!is_file (full_local_fname )) {
51+ if (target_location == NULL ) {
52+ target_location = get_download_dir ();
53+ }
54+ if (!is_directory (target_location )) {
7355 free (filename_from_url );
74- return false;
56+ return false; /* Download directory is invalid. */
7557 }
58+ snprintf (path_buffer , sizeof (path_buffer ), "%s%s%s" , target_location ,
59+ PATH_SEP , filename_from_url );
60+ file_to_check = path_buffer ;
61+ free (filename_from_url );
62+ } else {
63+ file_to_check = url_or_path ;
64+ }
65+
66+ if (!is_file (file_to_check )) {
67+ return false;
7668 }
7769
7870 verify_handle = acquire_handle_init ();
7971 if (!verify_handle ) {
80- free (filename_from_url );
8172 return false;
8273 }
8374
84- result = acquire_verify_sync (verify_handle , full_local_fname , checksum , hash );
75+ result = acquire_verify_sync (verify_handle , file_to_check , checksum , hash );
8576 acquire_handle_free (verify_handle );
8677
87- is_verified = (result == 0 );
88-
89- free (filename_from_url );
90- return is_verified ;
78+ return result == 0 ;
9179}
9280
9381#endif /* defined(LIBACQUIRE_IMPLEMENTATION) && \
0 commit comments