@@ -338,20 +338,24 @@ repo_add_pkg(struct repo *repo, const char *file, bool force)
338338 return r ;
339339}
340340
341+ static const char *
342+ safe_dirname (char * dst , size_t dstsz , const char * path )
343+ {
344+ if (strlcpy (dst , path , dstsz ) >= dstsz ) {
345+ errno = ENOBUFS ;
346+ return NULL ;
347+ }
348+ return dirname (dst );
349+ }
350+
341351static int
342352find_repo (struct repo * repos , unsigned int nrepos , const char * file , struct repo * * outp )
343353{
344354 char tmp [PATH_MAX ];
345355 const char * dir ;
346356 int r ;
347357
348- if (strlcpy (tmp , file , sizeof (tmp )) >= sizeof (tmp )) {
349- xbps_error_printf (
350- "failed to copy path: %s: %s\n" , file , strerror (ENOBUFS ));
351- return - ENOBUFS ;
352- }
353-
354- dir = dirname (tmp );
358+ dir = safe_dirname (tmp , sizeof (tmp ), file );
355359 if (!dir ) {
356360 r = - errno ;
357361 xbps_error_printf ("failed to get directory from path: %s: %s\n" ,
@@ -612,6 +616,42 @@ repos_check_stage(struct repo *repos, unsigned nrepos)
612616 return r ;
613617}
614618
619+ static xbps_array_t
620+ repos_from_argv (int argc , char * * argv )
621+ {
622+ char tmp [PATH_MAX ];
623+ xbps_array_t res ;
624+
625+ res = xbps_array_create ();
626+ if (!res ) {
627+ errno = - xbps_error_oom ();
628+ return NULL ;
629+ }
630+
631+ for (int i = 0 ; i < argc ; i ++ ) {
632+ const char * dir ;
633+ int r ;
634+ dir = safe_dirname (tmp , sizeof (tmp ), argv [0 ]);
635+ if (!dir ) {
636+ r = - errno ;
637+ xbps_error_printf (
638+ "failed to get dirname: %s: %s\n" , argv [0 ], strerror (- r ));
639+ xbps_object_release (res );
640+ errno = - r ;
641+ return NULL ;
642+ }
643+ if (xbps_match_string_in_array (res , dir ))
644+ continue ;
645+ if (!xbps_array_add_cstring (res , dir )) {
646+ xbps_object_release (res );
647+ errno = - xbps_error_oom ();
648+ return NULL ;
649+ }
650+ }
651+
652+ return res ;
653+ }
654+
615655int
616656index_add (struct xbps_handle * xhp , int argc , char * * argv , bool force , const char * compression , xbps_array_t repo_args )
617657{
@@ -620,30 +660,12 @@ index_add(struct xbps_handle *xhp, int argc, char **argv, bool force, const char
620660 unsigned nrepos ;
621661 int r ;
622662
663+ // Backwards compatibiltiy if no repo args are given, in this case
664+ // add the repos based on the supplied packages...
623665 if (!repo_args ) {
624- char tmp [PATH_MAX ];
625- const char * repodir ;
626- repo_args = xbps_array_create ();
627- if (!repo_args ) {
628- xbps_error_oom ();
666+ repo_args = repos_from_argv (argc , argv );
667+ if (!repo_args )
629668 return EXIT_FAILURE ;
630- }
631- if (strlcpy (tmp , argv [0 ], sizeof (tmp )) >= sizeof (tmp )) {
632- xbps_error_printf ("failed to copy path: %s: %s\n" , argv [0 ],
633- strerror (ENOBUFS ));
634- return EXIT_FAILURE ;
635- }
636- repodir = dirname (tmp );
637- if (!repodir ) {
638- xbps_error_printf ("failed to get dirname: %s: %s\n" , tmp , strerror (errno ));
639- xbps_object_release (repo_args );
640- return EXIT_FAILURE ;
641- }
642- if (!xbps_array_add_cstring (repo_args , repodir )) {
643- xbps_error_oom ();
644- xbps_object_release (repo_args );
645- return EXIT_FAILURE ;
646- }
647669 }
648670
649671 nrepos = xbps_array_count (repo_args );
0 commit comments