fix: prevent segfault if malicious server sends 1 GB of data through ftpNLST.
[platform/upstream/rpm.git] / rpmio / rpmrpc.c
1 /** \ingroup rpmio
2  * \file rpmio/rpmrpc.c
3  */
4
5 #include "system.h"
6
7 #if defined(HAVE_PTHREAD_H) && !defined(__LCLINT__)
8 #include <pthread.h>
9 #endif
10
11 #include <rpmio_internal.h>
12 #include "ugid.h"
13 #include "debug.h"
14
15 /*@access DIR@*/
16 /*@access FD_t@*/
17 /*@access urlinfo@*/
18
19 /* =============================================================== */
20 static int ftpMkdir(const char * path, /*@unused@*/ mode_t mode)
21         /*@globals fileSystem, internalState @*/
22         /*@modifies fileSystem, internalState @*/
23 {
24     int rc;
25     if ((rc = ftpCmd("MKD", path, NULL)) != 0)
26         return rc;
27 #if NOTYET
28     {   char buf[20];
29         sprintf(buf, " 0%o", mode);
30         (void) ftpCmd("SITE CHMOD", path, buf);
31     }
32 #endif
33     return rc;
34 }
35
36 static int ftpChdir(const char * path)
37         /*@globals fileSystem, internalState @*/
38         /*@modifies fileSystem, internalState @*/
39 {
40     return ftpCmd("CWD", path, NULL);
41 }
42
43 static int ftpRmdir(const char * path)
44         /*@globals fileSystem, internalState @*/
45         /*@modifies fileSystem, internalState @*/
46 {
47     return ftpCmd("RMD", path, NULL);
48 }
49
50 static int ftpRename(const char * oldpath, const char * newpath)
51         /*@globals fileSystem, internalState @*/
52         /*@modifies fileSystem, internalState @*/
53 {
54     int rc;
55     if ((rc = ftpCmd("RNFR", oldpath, NULL)) != 0)
56         return rc;
57     return ftpCmd("RNTO", newpath, NULL);
58 }
59
60 static int ftpUnlink(const char * path)
61         /*@globals fileSystem, internalState @*/
62         /*@modifies fileSystem, internalState @*/
63 {
64     return ftpCmd("DELE", path, NULL);
65 }
66
67 /* =============================================================== */
68 /* XXX rebuilddb.c: analogues to mkdir(2)/rmdir(2). */
69 int Mkdir (const char * path, mode_t mode)
70 {
71     const char * lpath;
72     int ut = urlPath(path, &lpath);
73
74     switch (ut) {
75     case URL_IS_FTP:
76         return ftpMkdir(path, mode);
77         /*@notreached@*/ break;
78     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
79     case URL_IS_PATH:
80         path = lpath;
81         /*@fallthrough@*/
82     case URL_IS_UNKNOWN:
83         break;
84     case URL_IS_DASH:
85     default:
86         return -2;
87         /*@notreached@*/ break;
88     }
89     return mkdir(path, mode);
90 }
91
92 int Chdir (const char * path)
93 {
94     const char * lpath;
95     int ut = urlPath(path, &lpath);
96
97     switch (ut) {
98     case URL_IS_FTP:
99         return ftpChdir(path);
100         /*@notreached@*/ break;
101     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
102     case URL_IS_PATH:
103         path = lpath;
104         /*@fallthrough@*/
105     case URL_IS_UNKNOWN:
106         break;
107     case URL_IS_DASH:
108     default:
109         return -2;
110         /*@notreached@*/ break;
111     }
112     return chdir(path);
113 }
114
115 int Rmdir (const char * path)
116 {
117     const char * lpath;
118     int ut = urlPath(path, &lpath);
119
120     switch (ut) {
121     case URL_IS_FTP:
122         return ftpRmdir(path);
123         /*@notreached@*/ break;
124     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
125     case URL_IS_PATH:
126         path = lpath;
127         /*@fallthrough@*/
128     case URL_IS_UNKNOWN:
129         break;
130     case URL_IS_DASH:
131     default:
132         return -2;
133         /*@notreached@*/ break;
134     }
135     return rmdir(path);
136 }
137
138 /* XXX rpmdb.c: analogue to rename(2). */
139
140 int Rename (const char * oldpath, const char * newpath)
141 {
142     const char *oe = NULL;
143     const char *ne = NULL;
144     int oldut, newut;
145
146     /* XXX lib/install.c used to rely on this behavior. */
147     if (!strcmp(oldpath, newpath)) return 0;
148
149     oldut = urlPath(oldpath, &oe);
150     switch (oldut) {
151     case URL_IS_FTP:            /* XXX WRONG WRONG WRONG */
152     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
153     case URL_IS_PATH:
154     case URL_IS_UNKNOWN:
155         break;
156     case URL_IS_DASH:
157     default:
158         return -2;
159         /*@notreached@*/ break;
160     }
161
162     newut = urlPath(newpath, &ne);
163     switch (newut) {
164     case URL_IS_FTP:
165 if (_rpmio_debug)
166 fprintf(stderr, "*** rename old %*s new %*s\n", (int)(oe - oldpath), oldpath, (int)(ne - newpath), newpath);
167         if (!(oldut == newut && oe && ne && (oe - oldpath) == (ne - newpath) &&
168             !xstrncasecmp(oldpath, newpath, (oe - oldpath))))
169             return -2;
170         return ftpRename(oldpath, newpath);
171         /*@notreached@*/ break;
172     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
173     case URL_IS_PATH:
174         oldpath = oe;
175         newpath = ne;
176         break;
177     case URL_IS_UNKNOWN:
178         break;
179     case URL_IS_DASH:
180     default:
181         return -2;
182         /*@notreached@*/ break;
183     }
184     return rename(oldpath, newpath);
185 }
186
187 int Link (const char * oldpath, const char * newpath)
188 {
189     const char *oe = NULL;
190     const char *ne = NULL;
191     int oldut, newut;
192
193     oldut = urlPath(oldpath, &oe);
194     switch (oldut) {
195     case URL_IS_FTP:            /* XXX WRONG WRONG WRONG */
196     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
197     case URL_IS_PATH:
198     case URL_IS_UNKNOWN:
199         break;
200     case URL_IS_DASH:
201     default:
202         return -2;
203         /*@notreached@*/ break;
204     }
205
206     newut = urlPath(newpath, &ne);
207     switch (newut) {
208     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
209     case URL_IS_FTP:            /* XXX WRONG WRONG WRONG */
210     case URL_IS_PATH:
211 if (_rpmio_debug)
212 fprintf(stderr, "*** link old %*s new %*s\n", (int)(oe - oldpath), oldpath, (int)(ne - newpath), newpath);
213         if (!(oldut == newut && oe && ne && (oe - oldpath) == (ne - newpath) &&
214             !xstrncasecmp(oldpath, newpath, (oe - oldpath))))
215             return -2;
216         oldpath = oe;
217         newpath = ne;
218         break;
219     case URL_IS_UNKNOWN:
220         break;
221     case URL_IS_DASH:
222     default:
223         return -2;
224         /*@notreached@*/ break;
225     }
226     return link(oldpath, newpath);
227 }
228
229 /* XXX build/build.c: analogue to unlink(2). */
230
231 int Unlink(const char * path) {
232     const char * lpath;
233     int ut = urlPath(path, &lpath);
234
235     switch (ut) {
236     case URL_IS_FTP:
237         return ftpUnlink(path);
238         /*@notreached@*/ break;
239     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
240     case URL_IS_PATH:
241         path = lpath;
242         /*@fallthrough@*/
243     case URL_IS_UNKNOWN:
244         break;
245     case URL_IS_DASH:
246     default:
247         return -2;
248         /*@notreached@*/ break;
249     }
250     return unlink(path);
251 }
252
253 /* XXX swiped from mc-4.5.39-pre9 vfs/ftpfs.c */
254
255 #define g_strdup        xstrdup
256 #define g_free          free
257
258 /*
259  * FIXME: this is broken. It depends on mc not crossing border on month!
260  */
261 /*@unchecked@*/
262 static int current_mday;
263 /*@unchecked@*/
264 static int current_mon;
265 /*@unchecked@*/
266 static int current_year;
267
268 /* Following stuff (parse_ls_lga) is used by ftpfs and extfs */
269 #define MAXCOLS         30
270
271 /*@unchecked@*/
272 static char *columns [MAXCOLS]; /* Points to the string in column n */
273 /*@unchecked@*/
274 static int   column_ptr [MAXCOLS]; /* Index from 0 to the starting positions of the columns */
275
276 /*@-boundswrite@*/
277 static int
278 vfs_split_text (char *p)
279         /*@globals columns, column_ptr @*/
280         /*@modifies *p, columns, column_ptr @*/
281 {
282     char *original = p;
283     int  numcols;
284
285
286     for (numcols = 0; *p && numcols < MAXCOLS; numcols++){
287         while (*p == ' ' || *p == '\r' || *p == '\n'){
288             *p = 0;
289             p++;
290         }
291         columns [numcols] = p;
292         column_ptr [numcols] = p - original;
293         while (*p && *p != ' ' && *p != '\r' && *p != '\n')
294             p++;
295     }
296     return numcols;
297 }
298 /*@=boundswrite@*/
299
300 /*@-boundsread@*/
301 static int
302 is_num (int idx)
303         /*@*/
304 {
305     if (!columns [idx] || columns [idx][0] < '0' || columns [idx][0] > '9')
306         return 0;
307     return 1;
308 }
309 /*@=boundsread@*/
310
311 /*@-boundsread@*/
312 static int
313 is_dos_date(/*@null@*/ const char *str)
314         /*@*/
315 {
316     if (str != NULL && strlen(str) == 8 &&
317                 str[2] == str[5] && strchr("\\-/", (int)str[2]) != NULL)
318         return 1;
319     return 0;
320 }
321 /*@=boundsread@*/
322
323 static int
324 is_week (/*@null@*/ const char * str, /*@out@*/ struct tm * tim)
325         /*@modifies *tim @*/
326 {
327 /*@observer@*/ static const char * week = "SunMonTueWedThuFriSat";
328     const char * pos;
329
330     /*@-observertrans -mayaliasunique@*/
331     if (str != NULL && (pos=strstr(week, str)) != NULL) {
332     /*@=observertrans =mayaliasunique@*/
333         if (tim != NULL)
334             tim->tm_wday = (pos - week)/3;
335         return 1;
336     }
337     return 0;    
338 }
339
340 static int
341 is_month (/*@null@*/ const char * str, /*@out@*/ struct tm * tim)
342         /*@modifies *tim @*/
343 {
344 /*@observer@*/ static const char * month = "JanFebMarAprMayJunJulAugSepOctNovDec";
345     const char * pos;
346     
347     /*@-observertrans -mayaliasunique@*/
348     if (str != NULL && (pos = strstr(month, str)) != NULL) {
349     /*@=observertrans -mayaliasunique@*/
350         if (tim != NULL)
351             tim->tm_mon = (pos - month)/3;
352         return 1;
353     }
354     return 0;
355 }
356
357 static int
358 is_time (/*@null@*/ const char * str, /*@out@*/ struct tm * tim)
359         /*@modifies *tim @*/
360 {
361     const char * p, * p2;
362
363     if (str != NULL && (p = strchr(str, ':')) && (p2 = strrchr(str, ':'))) {
364         if (p != p2) {
365             if (sscanf (str, "%2d:%2d:%2d", &tim->tm_hour, &tim->tm_min, &tim->tm_sec) != 3)
366                 return 0;
367         } else {
368             if (sscanf (str, "%2d:%2d", &tim->tm_hour, &tim->tm_min) != 2)
369                 return 0;
370         }
371     } else 
372         return 0;
373     
374     return 1;
375 }
376
377 static int is_year(/*@null@*/ const char * str, /*@out@*/ struct tm * tim)
378         /*@modifies *tim @*/
379 {
380     long year;
381
382     if (str == NULL)
383         return 0;
384
385     if (strchr(str,':'))
386         return 0;
387
388     if (strlen(str) != 4)
389         return 0;
390
391     if (sscanf(str, "%ld", &year) != 1)
392         return 0;
393
394     if (year < 1900 || year > 3000)
395         return 0;
396
397     tim->tm_year = (int) (year - 1900);
398
399     return 1;
400 }
401
402 /*
403  * FIXME: this is broken. Consider following entry:
404  * -rwx------   1 root     root            1 Aug 31 10:04 2904 1234
405  * where "2904 1234" is filename. Well, this code decodes it as year :-(.
406  */
407
408 static int
409 vfs_parse_filetype (char c)
410         /*@*/
411 {
412     switch (c) {
413         case 'd': return S_IFDIR; 
414         case 'b': return S_IFBLK;
415         case 'c': return S_IFCHR;
416         case 'l': return S_IFLNK;
417         case 's':
418 #ifdef IS_IFSOCK /* And if not, we fall through to IFIFO, which is pretty close */
419                   return S_IFSOCK;
420 #endif
421         case 'p': return S_IFIFO;
422         case 'm': case 'n':             /* Don't know what these are :-) */
423         case '-': case '?': return S_IFREG;
424         default: return -1;
425     }
426 }
427
428 static int vfs_parse_filemode (const char *p)
429         /*@*/
430 {       /* converts rw-rw-rw- into 0666 */
431     int res = 0;
432     switch (*(p++)) {
433         case 'r': res |= 0400; break;
434         case '-': break;
435         default: return -1;
436     }
437     switch (*(p++)) {
438         case 'w': res |= 0200; break;
439         case '-': break;
440         default: return -1;
441     }
442     switch (*(p++)) {
443         case 'x': res |= 0100; break;
444         case 's': res |= 0100 | S_ISUID; break;
445         case 'S': res |= S_ISUID; break;
446         case '-': break;
447         default: return -1;
448     }
449     switch (*(p++)) {
450         case 'r': res |= 0040; break;
451         case '-': break;
452         default: return -1;
453     }
454     switch (*(p++)) {
455         case 'w': res |= 0020; break;
456         case '-': break;
457         default: return -1;
458     }
459     switch (*(p++)) {
460         case 'x': res |= 0010; break;
461         case 's': res |= 0010 | S_ISGID; break;
462         case 'l': /* Solaris produces these */
463         case 'S': res |= S_ISGID; break;
464         case '-': break;
465         default: return -1;
466     }
467     switch (*(p++)) {
468         case 'r': res |= 0004; break;
469         case '-': break;
470         default: return -1;
471     }
472     switch (*(p++)) {
473         case 'w': res |= 0002; break;
474         case '-': break;
475         default: return -1;
476     }
477     switch (*(p++)) {
478         case 'x': res |= 0001; break;
479         case 't': res |= 0001 | S_ISVTX; break;
480         case 'T': res |= S_ISVTX; break;
481         case '-': break;
482         default: return -1;
483     }
484     return res;
485 }
486
487 /*@-boundswrite@*/
488 static int vfs_parse_filedate(int idx, /*@out@*/ time_t *t)
489         /*@modifies *t @*/
490 {       /* This thing parses from idx in columns[] array */
491
492     char *p;
493     struct tm tim;
494     int d[3];
495     int got_year = 0;
496
497     /* Let's setup default time values */
498     tim.tm_year = current_year;
499     tim.tm_mon  = current_mon;
500     tim.tm_mday = current_mday;
501     tim.tm_hour = 0;
502     tim.tm_min  = 0;
503     tim.tm_sec  = 0;
504     tim.tm_isdst = -1; /* Let mktime() try to guess correct dst offset */
505     
506     p = columns [idx++];
507     
508     /* We eat weekday name in case of extfs */
509     if(is_week(p, &tim))
510         p = columns [idx++];
511
512     /* Month name */
513     if(is_month(p, &tim)){
514         /* And we expect, it followed by day number */
515         if (is_num (idx))
516             tim.tm_mday = (int)atol (columns [idx++]);
517         else
518             return 0; /* No day */
519
520     } else {
521         /* We usually expect:
522            Mon DD hh:mm
523            Mon DD  YYYY
524            But in case of extfs we allow these date formats:
525            Mon DD YYYY hh:mm
526            Mon DD hh:mm YYYY
527            Wek Mon DD hh:mm:ss YYYY
528            MM-DD-YY hh:mm
529            where Mon is Jan-Dec, DD, MM, YY two digit day, month, year,
530            YYYY four digit year, hh, mm, ss two digit hour, minute or second. */
531
532         /* Here just this special case with MM-DD-YY */
533         if (is_dos_date(p)){
534             /*@-mods@*/
535             p[2] = p[5] = '-';
536             /*@=mods@*/
537             
538             memset(d, 0, sizeof(d));
539             if (sscanf(p, "%2d-%2d-%2d", &d[0], &d[1], &d[2]) == 3){
540             /*  We expect to get:
541                 1. MM-DD-YY
542                 2. DD-MM-YY
543                 3. YY-MM-DD
544                 4. YY-DD-MM  */
545                 
546                 /* Hmm... maybe, next time :)*/
547                 
548                 /* At last, MM-DD-YY */
549                 d[0]--; /* Months are zerobased */
550                 /* Y2K madness */
551                 if(d[2] < 70)
552                     d[2] += 100;
553
554                 tim.tm_mon  = d[0];
555                 tim.tm_mday = d[1];
556                 tim.tm_year = d[2];
557                 got_year = 1;
558             } else
559                 return 0; /* sscanf failed */
560         } else
561             return 0; /* unsupported format */
562     }
563
564     /* Here we expect to find time and/or year */
565     
566     if (is_num (idx)) {
567         if(is_time(columns[idx], &tim) || (got_year = is_year(columns[idx], &tim))) {
568         idx++;
569
570         /* This is a special case for ctime() or Mon DD YYYY hh:mm */
571         if(is_num (idx) && 
572             ((got_year = is_year(columns[idx], &tim)) || is_time(columns[idx], &tim)))
573                 idx++; /* time & year or reverse */
574         } /* only time or date */
575     }
576     else 
577         return 0; /* Nor time or date */
578
579     /*
580      * If the date is less than 6 months in the past, it is shown without year
581      * other dates in the past or future are shown with year but without time
582      * This does not check for years before 1900 ... I don't know, how
583      * to represent them at all
584      */
585     if (!got_year &&
586         current_mon < 6 && current_mon < tim.tm_mon && 
587         tim.tm_mon - current_mon >= 6)
588
589         tim.tm_year--;
590
591     if ((*t = mktime(&tim)) < 0)
592         *t = 0;
593     return idx;
594 }
595 /*@=boundswrite@*/
596
597 /*@-boundswrite@*/
598 static int
599 vfs_parse_ls_lga (char * p, /*@out@*/ struct stat * st,
600                 /*@out@*/ const char ** filename,
601                 /*@out@*/ const char ** linkname)
602         /*@modifies *p, *st, *filename, *linkname @*/
603 {
604     int idx, idx2, num_cols;
605     int i;
606     char *p_copy;
607     
608     if (strncmp (p, "total", 5) == 0)
609         return 0;
610
611     p_copy = g_strdup(p);
612 /* XXX FIXME: parse out inode number from "NLST -lai ." */
613 /* XXX FIXME: parse out sizein blocks from "NLST -lais ." */
614
615     if ((i = vfs_parse_filetype(*(p++))) == -1)
616         goto error;
617
618     st->st_mode = i;
619     if (*p == ' ')      /* Notwell 4 */
620         p++;
621     if (*p == '['){
622         if (strlen (p) <= 8 || p [8] != ']')
623             goto error;
624         /* Should parse here the Notwell permissions :) */
625         /*@-unrecog@*/
626         if (S_ISDIR (st->st_mode))
627             st->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IXUSR | S_IXGRP | S_IXOTH);
628         else
629             st->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);
630         p += 9;
631         /*@=unrecog@*/
632     } else {
633         if ((i = vfs_parse_filemode(p)) == -1)
634             goto error;
635         st->st_mode |= i;
636         p += 9;
637
638         /* This is for an extra ACL attribute (HP-UX) */
639         if (*p == '+')
640             p++;
641     }
642
643     g_free(p_copy);
644     p_copy = g_strdup(p);
645     num_cols = vfs_split_text (p);
646
647     st->st_nlink = atol (columns [0]);
648     if (st->st_nlink < 0)
649         goto error;
650
651     if (!is_num (1))
652 #ifdef  HACK
653         st->st_uid = finduid (columns [1]);
654 #else
655         (void) unameToUid (columns [1], &st->st_uid);
656 #endif
657     else
658         st->st_uid = (uid_t) atol (columns [1]);
659
660     /* Mhm, the ls -lg did not produce a group field */
661     for (idx = 3; idx <= 5; idx++) 
662         if (is_month(columns [idx], NULL) || is_week(columns [idx], NULL) || is_dos_date(columns[idx]))
663             break;
664
665     if (idx == 6 || (idx == 5 && !S_ISCHR (st->st_mode) && !S_ISBLK (st->st_mode)))
666         goto error;
667
668     /* We don't have gid */     
669     if (idx == 3 || (idx == 4 && (S_ISCHR(st->st_mode) || S_ISBLK (st->st_mode))))
670         idx2 = 2;
671     else { 
672         /* We have gid field */
673         if (is_num (2))
674             st->st_gid = (gid_t) atol (columns [2]);
675         else
676 #ifdef  HACK
677             st->st_gid = findgid (columns [2]);
678 #else
679             (void) gnameToGid (columns [1], &st->st_gid);
680 #endif
681         idx2 = 3;
682     }
683
684     /* This is device */
685     if (S_ISCHR (st->st_mode) || S_ISBLK (st->st_mode)){
686         int maj, min;
687         
688         if (!is_num (idx2) || sscanf(columns [idx2], " %d,", &maj) != 1)
689             goto error;
690         
691         if (!is_num (++idx2) || sscanf(columns [idx2], " %d", &min) != 1)
692             goto error;
693         
694 #ifdef HAVE_ST_RDEV
695         st->st_rdev = ((maj & 0xff) << 8) | (min & 0xffff00ff);
696 #endif
697         st->st_size = 0;
698         
699     } else {
700         /* Common file size */
701         if (!is_num (idx2))
702             goto error;
703         
704         st->st_size = (size_t) atol (columns [idx2]);
705 #ifdef HAVE_ST_RDEV
706         st->st_rdev = 0;
707 #endif
708     }
709
710     idx = vfs_parse_filedate(idx, &st->st_mtime);
711     if (!idx)
712         goto error;
713     /* Use resulting time value */
714     st->st_atime = st->st_ctime = st->st_mtime;
715     st->st_dev = 0;
716     st->st_ino = 0;
717 #ifdef HAVE_ST_BLKSIZE
718     st->st_blksize = 512;
719 #endif
720 #ifdef HAVE_ST_BLOCKS
721     st->st_blocks = (st->st_size + 511) / 512;
722 #endif
723
724     for (i = idx + 1, idx2 = 0; i < num_cols; i++ ) 
725         if (strcmp (columns [i], "->") == 0){
726             idx2 = i;
727             break;
728         }
729     
730     if (((S_ISLNK (st->st_mode) || 
731         (num_cols == idx + 3 && st->st_nlink > 1))) /* Maybe a hardlink? (in extfs) */
732         && idx2){
733         int tlen;
734         char *t;
735             
736         if (filename){
737 #ifdef HACK
738             t = g_strndup (p_copy + column_ptr [idx], column_ptr [idx2] - column_ptr [idx] - 1);
739 #else
740             int nb = column_ptr [idx2] - column_ptr [idx] - 1;
741             t = xmalloc(nb+1);
742             strncpy(t, p_copy + column_ptr [idx], nb);
743 #endif
744             *filename = t;
745         }
746         if (linkname){
747             t = g_strdup (p_copy + column_ptr [idx2+1]);
748             tlen = strlen (t);
749             if (t [tlen-1] == '\r' || t [tlen-1] == '\n')
750                 t [tlen-1] = 0;
751             if (t [tlen-2] == '\r' || t [tlen-2] == '\n')
752                 t [tlen-2] = 0;
753                 
754             *linkname = t;
755         }
756     } else {
757         /* Extract the filename from the string copy, not from the columns
758          * this way we have a chance of entering hidden directories like ". ."
759          */
760         if (filename){
761             /* 
762             *filename = g_strdup (columns [idx++]);
763             */
764             int tlen;
765             char *t;
766             
767             t = g_strdup (p_copy + column_ptr [idx]); idx++;
768             tlen = strlen (t);
769             /* g_strchomp(); */
770             if (t [tlen-1] == '\r' || t [tlen-1] == '\n')
771                 t [tlen-1] = 0;
772             if (t [tlen-2] == '\r' || t [tlen-2] == '\n')
773                 t [tlen-2] = 0;
774             
775             *filename = t;
776         }
777         if (linkname)
778             *linkname = NULL;
779     }
780     g_free (p_copy);
781     return 1;
782
783 error:
784 #ifdef  HACK
785     {
786       static int errorcount = 0;
787
788       if (++errorcount < 5) {
789         message_1s (1, "Could not parse:", p_copy);
790       } else if (errorcount == 5)
791         message_1s (1, "More parsing errors will be ignored.", "(sorry)" );
792     }
793 #endif
794
795     /*@-usereleased@*/
796     if (p_copy != p)            /* Carefull! */
797     /*@=usereleased@*/
798         g_free (p_copy);
799     return 0;
800 }
801 /*@=boundswrite@*/
802
803 typedef enum {
804         DO_FTP_STAT     = 1,
805         DO_FTP_LSTAT    = 2,
806         DO_FTP_READLINK = 3,
807         DO_FTP_ACCESS   = 4,
808         DO_FTP_GLOB     = 5
809 } ftpSysCall_t;
810
811 /**
812  */
813 /*@unchecked@*/
814 static size_t ftpBufAlloced = 0;
815
816 /**
817  */
818 /*@unchecked@*/
819 static /*@only@*/ char * ftpBuf = NULL;
820         
821 #define alloca_strdup(_s)       strcpy(alloca(strlen(_s)+1), (_s))
822
823 /*@-boundswrite@*/
824 /*@-mods@*/
825 static int ftpNLST(const char * url, ftpSysCall_t ftpSysCall,
826                 /*@out@*/ /*@null@*/ struct stat * st,
827                 /*@out@*/ /*@null@*/ char * rlbuf, size_t rlbufsiz)
828         /*@globals fileSystem, internalState @*/
829         /*@modifies *st, *rlbuf, fileSystem, internalState @*/
830 {
831     FD_t fd;
832     const char * path;
833     int bufLength, moretodo;
834     const char *n, *ne, *o, *oe;
835     char * s;
836     char * se;
837     const char * urldn;
838     char * bn = NULL;
839     int nbn = 0;
840     urlinfo u;
841     int rc;
842
843     n = ne = o = oe = NULL;
844     (void) urlPath(url, &path);
845     if (*path == '\0')
846         return -2;
847
848     switch (ftpSysCall) {
849     case DO_FTP_GLOB:
850         fd = ftpOpen(url, 0, 0, &u);
851         if (fd == NULL || u == NULL)
852             return -1;
853
854         u->openError = ftpReq(fd, "LIST", path);
855         break;
856     default:
857         urldn = alloca_strdup(url);
858         /*@-branchstate@*/
859         if ((bn = strrchr(urldn, '/')) == NULL)
860             return -2;
861         else if (bn == path)
862             bn = ".";
863         else
864             *bn++ = '\0';
865         /*@=branchstate@*/
866         nbn = strlen(bn);
867
868         rc = ftpChdir(urldn);           /* XXX don't care about CWD */
869         if (rc < 0)
870             return rc;
871
872         fd = ftpOpen(url, 0, 0, &u);
873         if (fd == NULL || u == NULL)
874             return -1;
875
876         /* XXX possibly should do "NLST -lais" to get st_ino/st_blocks also */
877         u->openError = ftpReq(fd, "NLST", "-la");
878
879         if (bn == NULL || nbn <= 0) {
880             rc = -2;
881             goto exit;
882         }
883         break;
884     }
885
886     if (u->openError < 0) {
887         fd = fdLink(fd, "error data (ftpStat)");
888         rc = -2;
889         goto exit;
890     }
891
892     if (ftpBufAlloced == 0 || ftpBuf == NULL) {
893         ftpBufAlloced = _url_iobuf_size;
894         ftpBuf = xcalloc(ftpBufAlloced, sizeof(ftpBuf[0]));
895     }
896     *ftpBuf = '\0';
897
898     bufLength = 0;
899     moretodo = 1;
900
901     do {
902
903         /* XXX FIXME: realloc ftpBuf if < ~128 chars remain */
904         if ((ftpBufAlloced - bufLength) < (1024+80)) {
905             ftpBufAlloced <<= 2;
906             assert(ftpBufAlloced < (8*1024*1024));
907             ftpBuf = xrealloc(ftpBuf, ftpBufAlloced);
908         }
909         s = se = ftpBuf + bufLength;
910         *se = '\0';
911
912         rc = fdFgets(fd, se, (ftpBufAlloced - bufLength));
913         if (rc <= 0) {
914             moretodo = 0;
915             break;
916         }
917         if (ftpSysCall == DO_FTP_GLOB) {        /* XXX HACK */
918             bufLength += strlen(se);
919             continue;
920         }
921
922         for (s = se; *s != '\0'; s = se) {
923             int bingo;
924
925             while (*se && *se != '\n') se++;
926             if (se > s && se[-1] == '\r') se[-1] = '\0';
927             if (*se == '\0') 
928                 /*@innerbreak@*/ break;
929             *se++ = '\0';
930
931             if (!strncmp(s, "total ", sizeof("total ")-1))
932                 /*@innercontinue@*/ continue;
933
934             o = NULL;
935             for (bingo = 0, n = se; n >= s; n--) {
936                 switch (*n) {
937                 case '\0':
938                     oe = ne = n;
939                     /*@switchbreak@*/ break;
940                 case ' ':
941                     if (o || !(n[-3] == ' ' && n[-2] == '-' && n[-1] == '>')) {
942                         while (*(++n) == ' ')
943                             {};
944                         bingo++;
945                         /*@switchbreak@*/ break;
946                     }
947                     for (o = n + 1; *o == ' '; o++)
948                         {};
949                     n -= 3;
950                     ne = n;
951                     /*@switchbreak@*/ break;
952                 default:
953                     /*@switchbreak@*/ break;
954                 }
955                 if (bingo)
956                     /*@innerbreak@*/ break;
957             }
958
959             if (nbn != (ne - n))        /* Same name length? */
960                 /*@innercontinue@*/ continue;
961             if (strncmp(n, bn, nbn))    /* Same name? */
962                 /*@innercontinue@*/ continue;
963
964             moretodo = 0;
965             /*@innerbreak@*/ break;
966         }
967
968         if (moretodo && se > s) {
969             bufLength = se - s - 1;
970             if (s != ftpBuf)
971                 memmove(ftpBuf, s, bufLength);
972         } else {
973             bufLength = 0;
974         }
975     } while (moretodo);
976
977     switch (ftpSysCall) {
978     case DO_FTP_STAT:
979         if (o && oe) {
980             /* XXX FIXME: symlink, replace urldn/bn from [o,oe) and restart */
981         }
982         /*@fallthrough@*/
983     case DO_FTP_LSTAT:
984         if (st == NULL || !(n && ne)) {
985             rc = -1;
986         } else {
987             rc = ((vfs_parse_ls_lga(s, st, NULL, NULL) > 0) ? 0 : -1);
988         }
989         break;
990     case DO_FTP_READLINK:
991         if (rlbuf == NULL || !(o && oe)) {
992             rc = -1;
993         } else {
994             rc = oe - o;
995             if (rc > rlbufsiz)
996                 rc = rlbufsiz;
997             memcpy(rlbuf, o, rc);
998             if (rc < rlbufsiz)
999                 rlbuf[rc] = '\0';
1000         }
1001         break;
1002     case DO_FTP_ACCESS:
1003         rc = 0;         /* XXX WRONG WRONG WRONG */
1004         break;
1005     case DO_FTP_GLOB:
1006         rc = 0;         /* XXX WRONG WRONG WRONG */
1007         break;
1008     }
1009
1010 exit:
1011     (void) ufdClose(fd);
1012     return rc;
1013 }
1014 /*@=mods@*/
1015 /*@=boundswrite@*/
1016
1017 static const char * statstr(const struct stat * st,
1018                 /*@returned@*/ /*@out@*/ char * buf)
1019         /*@modifies *buf @*/
1020 {
1021     sprintf(buf,
1022         "*** dev %x ino %x mode %0o nlink %d uid %d gid %d rdev %x size %x\n",
1023         (unsigned)st->st_dev,
1024         (unsigned)st->st_ino,
1025         st->st_mode,
1026         (unsigned)st->st_nlink,
1027         st->st_uid,
1028         st->st_gid,
1029         (unsigned)st->st_rdev,
1030         (unsigned)st->st_size);
1031     return buf;
1032 }
1033
1034 /*@unchecked@*/
1035 static int ftp_st_ino = 0xdead0000;
1036
1037 static int ftpStat(const char * path, /*@out@*/ struct stat *st)
1038         /*@globals fileSystem, internalState @*/
1039         /*@modifies *st, fileSystem, internalState @*/
1040 {
1041     char buf[1024];
1042     int rc;
1043     rc = ftpNLST(path, DO_FTP_STAT, st, NULL, 0);
1044     /* XXX fts(3) needs/uses st_ino, make something up for now. */
1045     /*@-mods@*/
1046     if (st->st_ino == 0)
1047         st->st_ino = ftp_st_ino++;
1048     /*@=mods@*/
1049 if (_ftp_debug)
1050 fprintf(stderr, "*** ftpStat(%s) rc %d\n%s", path, rc, statstr(st, buf));
1051     return rc;
1052 }
1053
1054 static int ftpLstat(const char * path, /*@out@*/ struct stat *st)
1055         /*@globals fileSystem, internalState @*/
1056         /*@modifies *st, fileSystem, internalState @*/
1057 {
1058     char buf[1024];
1059     int rc;
1060     rc = ftpNLST(path, DO_FTP_LSTAT, st, NULL, 0);
1061     /* XXX fts(3) needs/uses st_ino, make something up for now. */
1062     /*@-mods@*/
1063     if (st->st_ino == 0)
1064         st->st_ino = ftp_st_ino++;
1065     /*@=mods@*/
1066 if (_ftp_debug)
1067 fprintf(stderr, "*** ftpLstat(%s) rc %d\n%s\n", path, rc, statstr(st, buf));
1068     return rc;
1069 }
1070
1071 static int ftpReadlink(const char * path, /*@out@*/ char * buf, size_t bufsiz)
1072         /*@globals fileSystem, internalState @*/
1073         /*@modifies *buf, fileSystem, internalState @*/
1074 {
1075     int rc;
1076     rc = ftpNLST(path, DO_FTP_READLINK, NULL, buf, bufsiz);
1077 if (_ftp_debug)
1078 fprintf(stderr, "*** ftpReadlink(%s) rc %d\n", path, rc);
1079     return rc;
1080 }
1081
1082 struct __dirstream {
1083     int fd;                     /* File descriptor.  */
1084     char * data;                /* Directory block.  */
1085     size_t allocation;          /* Space allocated for the block.  */
1086     size_t size;                /* Total valid data in the block.  */
1087     size_t offset;              /* Current offset into the block.  */
1088     off_t filepos;              /* Position of next entry to read.  */
1089 #if defined(HAVE_PTHREAD_H) && !defined(__LCLINT__)
1090     pthread_mutex_t lock;       /* Mutex lock for this structure.  */
1091 #endif
1092 };
1093
1094 #if !defined(DT_DIR)
1095 # define DT_UNKNOWN     0
1096 # define DT_FIFO        1
1097 # define DT_CHR         2
1098 # define DT_DIR         4
1099 # define DT_BLK         6
1100 # define DT_REG         8
1101 # define DT_LNK         10
1102 # define DT_SOCK        12
1103 # define DT_WHT         14
1104 typedef struct __dirstream *    FTPDIR;
1105 #else
1106 typedef DIR *                   FTPDIR;
1107 #endif
1108
1109 /*@unchecked@*/
1110 static int ftpmagicdir = 0x8440291;
1111 #define ISFTPMAGIC(_dir) (!memcmp((_dir), &ftpmagicdir, sizeof(ftpmagicdir)))
1112
1113 /*@-boundswrite@*/
1114 /*@-type@*/ /* FIX: abstract DIR */
1115 /*@null@*/
1116 static DIR * ftpOpendir(const char * path)
1117         /*@globals fileSystem, internalState @*/
1118         /*@modifies fileSystem, internalState @*/
1119 {
1120     FTPDIR mydir;
1121     struct dirent * dp;
1122     size_t nb;
1123     const char * s, * sb, * se;
1124     const char ** av;
1125     unsigned char * dt;
1126     char * t;
1127     int ac;
1128     int c;
1129     int rc;
1130
1131 if (_ftp_debug)
1132 fprintf(stderr, "*** ftpOpendir(%s)\n", path);
1133     rc = ftpNLST(path, DO_FTP_GLOB, NULL, NULL, 0);
1134     if (rc)
1135         return NULL;
1136
1137     /*
1138      * ftpBuf now contains absolute paths, newline terminated.
1139      * Calculate the number of bytes to hold the directory info.
1140      */
1141     nb = sizeof(".") + sizeof("..");
1142     ac = 2;
1143     sb = NULL;
1144     s = se = ftpBuf;
1145     while ((c = *se) != '\0') {
1146         se++;
1147         switch (c) {
1148         case '/':
1149             sb = se;
1150             /*@switchbreak@*/ break;
1151         case '\r':
1152             if (sb == NULL) {
1153                 for (sb = se; sb > s && sb[-1] != ' '; sb--)
1154                     {};
1155             }
1156             ac++;
1157             nb += (se - sb);
1158
1159             if (*se == '\n') se++;
1160             sb = NULL;
1161             s = se;
1162             /*@switchbreak@*/ break;
1163         default:
1164             /*@switchbreak@*/ break;
1165         }
1166     }
1167
1168     nb += sizeof(*mydir) + sizeof(*dp) + ((ac + 1) * sizeof(*av)) + (ac + 1);
1169     mydir = xcalloc(1, nb);
1170     /*@-abstract@*/
1171     dp = (struct dirent *) (mydir + 1);
1172     av = (const char **) (dp + 1);
1173     dt = (char *) (av + (ac + 1));
1174     t = (char *) (dt + ac + 1);
1175     /*@=abstract@*/
1176
1177     mydir->fd = ftpmagicdir;
1178 /*@-usereleased@*/
1179     mydir->data = (char *) dp;
1180 /*@=usereleased@*/
1181     mydir->allocation = nb;
1182     mydir->size = ac;
1183     mydir->offset = -1;
1184     mydir->filepos = 0;
1185
1186     ac = 0;
1187     /*@-dependenttrans -unrecog@*/
1188     dt[ac] = DT_DIR;    av[ac++] = t;   t = stpcpy(t, ".");     t++;
1189     dt[ac] = DT_DIR;    av[ac++] = t;   t = stpcpy(t, "..");    t++;
1190     /*@=dependenttrans =unrecog@*/
1191     sb = NULL;
1192     s = se = ftpBuf;
1193     while ((c = *se) != '\0') {
1194         se++;
1195         switch (c) {
1196         case '/':
1197             sb = se;
1198             /*@switchbreak@*/ break;
1199         case '\r':
1200             /*@-dependenttrans@*/
1201             av[ac] = t;
1202             /*@=dependenttrans@*/
1203             if (sb == NULL) {
1204                 /*@-unrecog@*/
1205                 switch(*s) {
1206                 case 'p':
1207                     dt[ac] = DT_FIFO;
1208                     /*@innerbreak@*/ break;
1209                 case 'c':
1210                     dt[ac] = DT_CHR;
1211                     /*@innerbreak@*/ break;
1212                 case 'd':
1213                     dt[ac] = DT_DIR;
1214                     /*@innerbreak@*/ break;
1215                 case 'b':
1216                     dt[ac] = DT_BLK;
1217                     /*@innerbreak@*/ break;
1218                 case '-':
1219                     dt[ac] = DT_REG;
1220                     /*@innerbreak@*/ break;
1221                 case 'l':
1222                     dt[ac] = DT_LNK;
1223                     /*@innerbreak@*/ break;
1224                 case 's':
1225                     dt[ac] = DT_SOCK;
1226                     /*@innerbreak@*/ break;
1227                 default:
1228                     dt[ac] = DT_UNKNOWN;
1229                     /*@innerbreak@*/ break;
1230                 }
1231                 /*@=unrecog@*/
1232                 for (sb = se; sb > s && sb[-1] != ' '; sb--)
1233                     {};
1234             }
1235             ac++;
1236             t = stpncpy(t, sb, (se - sb));
1237             t[-1] = '\0';
1238             if (*se == '\n') se++;
1239             sb = NULL;
1240             s = se;
1241             /*@switchbreak@*/ break;
1242         default:
1243             /*@switchbreak@*/ break;
1244         }
1245     }
1246     av[ac] = NULL;
1247
1248 /*@-kepttrans@*/
1249     return (DIR *) mydir;
1250 /*@=kepttrans@*/
1251 }
1252 /*@=boundswrite@*/
1253
1254 /*@dependent@*/ /*@null@*/
1255 static struct dirent * ftpReaddir(DIR * dir)
1256         /*@globals fileSystem @*/
1257         /*@modifies fileSystem @*/
1258 {
1259     FTPDIR mydir = (FTPDIR)dir;
1260     struct dirent * dp;
1261     const char ** av;
1262     unsigned char * dt;
1263     int ac;
1264     int i;
1265
1266     /*@+voidabstract@*/
1267     if (mydir == NULL || !ISFTPMAGIC(mydir) || mydir->data == NULL) {
1268         /* XXX TODO: EBADF errno. */
1269         return NULL;
1270     }
1271     /*@=voidabstract@*/
1272
1273     dp = (struct dirent *) mydir->data;
1274     av = (const char **) (dp + 1);
1275     ac = mydir->size;
1276     dt = (char *) (av + (ac + 1));
1277     i = mydir->offset + 1;
1278
1279 /*@-boundsread@*/
1280     if (i < 0 || i >= ac || av[i] == NULL)
1281         return NULL;
1282 /*@=boundsread@*/
1283
1284     mydir->offset = i;
1285
1286     /* XXX glob(3) uses REAL_DIR_ENTRY(dp) test on d_ino */
1287     dp->d_ino = i + 1;          /* W2DO? */
1288     dp->d_reclen = 0;           /* W2DO? */
1289
1290 #if !defined(hpux) && !defined(sun)
1291     dp->d_off = 0;              /* W2DO? */
1292 /*@-boundsread@*/
1293     dp->d_type = dt[i];
1294 /*@=boundsread@*/
1295 #endif
1296
1297     strncpy(dp->d_name, av[i], sizeof(dp->d_name));
1298 /*@+voidabstract@*/
1299 if (_ftp_debug)
1300 fprintf(stderr, "*** ftpReaddir(%p) %p \"%s\"\n", (void *)mydir, dp, dp->d_name);
1301 /*@=voidabstract@*/
1302     
1303     return dp;
1304 }
1305 /*@=type@*/
1306
1307 static int ftpClosedir(/*@only@*/ DIR * dir)
1308         /*@globals fileSystem @*/
1309         /*@modifies dir, fileSystem @*/
1310 {
1311     FTPDIR mydir = (FTPDIR)dir;
1312
1313     /*@+voidabstract@*/
1314 if (_ftp_debug)
1315 fprintf(stderr, "*** ftpClosedir(%p)\n", (void *)mydir);
1316     if (mydir == NULL || !ISFTPMAGIC(mydir)) {
1317         /* XXX TODO: EBADF errno. */
1318         return -1;
1319     }
1320     free((void *)mydir);
1321     /*@=voidabstract@*/
1322     mydir = NULL;
1323     return 0;
1324 }
1325
1326 int Stat(const char * path, struct stat * st)
1327 {
1328     const char * lpath;
1329     int ut = urlPath(path, &lpath);
1330
1331 if (_rpmio_debug)
1332 fprintf(stderr, "*** Stat(%s,%p)\n", path, st);
1333     switch (ut) {
1334     case URL_IS_FTP:
1335         return ftpStat(path, st);
1336         /*@notreached@*/ break;
1337     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
1338     case URL_IS_PATH:
1339         path = lpath;
1340         /*@fallthrough@*/
1341     case URL_IS_UNKNOWN:
1342         break;
1343     case URL_IS_DASH:
1344     default:
1345         return -2;
1346         /*@notreached@*/ break;
1347     }
1348     return stat(path, st);
1349 }
1350
1351 int Lstat(const char * path, struct stat * st)
1352 {
1353     const char * lpath;
1354     int ut = urlPath(path, &lpath);
1355
1356 if (_rpmio_debug)
1357 fprintf(stderr, "*** Lstat(%s,%p)\n", path, st);
1358     switch (ut) {
1359     case URL_IS_FTP:
1360         return ftpLstat(path, st);
1361         /*@notreached@*/ break;
1362     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
1363     case URL_IS_PATH:
1364         path = lpath;
1365         /*@fallthrough@*/
1366     case URL_IS_UNKNOWN:
1367         break;
1368     case URL_IS_DASH:
1369     default:
1370         return -2;
1371         /*@notreached@*/ break;
1372     }
1373     return lstat(path, st);
1374 }
1375
1376 int Readlink(const char * path, char * buf, size_t bufsiz)
1377 {
1378     const char * lpath;
1379     int ut = urlPath(path, &lpath);
1380
1381     switch (ut) {
1382     case URL_IS_FTP:
1383         return ftpReadlink(path, buf, bufsiz);
1384         /*@notreached@*/ break;
1385     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
1386     case URL_IS_PATH:
1387         path = lpath;
1388         /*@fallthrough@*/
1389     case URL_IS_UNKNOWN:
1390         break;
1391     case URL_IS_DASH:
1392     default:
1393         return -2;
1394         /*@notreached@*/ break;
1395     }
1396 /*@-compdef@*/
1397     return readlink(path, buf, bufsiz);
1398 /*@=compdef@*/
1399 }
1400
1401 int Access(const char * path, int amode)
1402 {
1403     const char * lpath;
1404     int ut = urlPath(path, &lpath);
1405
1406 if (_rpmio_debug)
1407 fprintf(stderr, "*** Access(%s,%d)\n", path, amode);
1408     switch (ut) {
1409     case URL_IS_FTP:            /* XXX WRONG WRONG WRONG */
1410     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
1411     case URL_IS_PATH:
1412         path = lpath;
1413         /*@fallthrough@*/
1414     case URL_IS_UNKNOWN:
1415         break;
1416     case URL_IS_DASH:
1417     default:
1418         return -2;
1419         /*@notreached@*/ break;
1420     }
1421     return access(path, amode);
1422 }
1423
1424 int Glob(const char *pattern, int flags,
1425         int errfunc(const char * epath, int eerrno), glob_t *pglob)
1426 {
1427     const char * lpath;
1428     int ut = urlPath(pattern, &lpath);
1429
1430 /*@-castfcnptr@*/
1431 if (_rpmio_debug)
1432 fprintf(stderr, "*** Glob(%s,0x%x,%p,%p)\n", pattern, (unsigned)flags, (void *)errfunc, pglob);
1433 /*@=castfcnptr@*/
1434     switch (ut) {
1435     case URL_IS_FTP:
1436 /*@-type@*/
1437         pglob->gl_closedir = Closedir;
1438         pglob->gl_readdir = Readdir;
1439         pglob->gl_opendir = Opendir;
1440         pglob->gl_lstat = Lstat;
1441         pglob->gl_stat = Stat;
1442 /*@=type@*/
1443         flags |= GLOB_ALTDIRFUNC;
1444         break;
1445     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
1446     case URL_IS_PATH:
1447         pattern = lpath;
1448         /*@fallthrough@*/
1449     case URL_IS_UNKNOWN:
1450         break;
1451     case URL_IS_DASH:
1452     default:
1453         return -2;
1454         /*@notreached@*/ break;
1455     }
1456     return glob(pattern, flags, errfunc, pglob);
1457 }
1458
1459 void Globfree(glob_t *pglob)
1460 {
1461 if (_rpmio_debug)
1462 fprintf(stderr, "*** Globfree(%p)\n", pglob);
1463     globfree(pglob);
1464 }
1465
1466 DIR * Opendir(const char * path)
1467 {
1468     const char * lpath;
1469     int ut = urlPath(path, &lpath);
1470
1471 if (_rpmio_debug)
1472 fprintf(stderr, "*** Opendir(%s)\n", path);
1473     switch (ut) {
1474     case URL_IS_FTP:
1475         return ftpOpendir(path);
1476         /*@notreached@*/ break;
1477     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
1478     case URL_IS_PATH:
1479         path = lpath;
1480         /*@fallthrough@*/
1481     case URL_IS_UNKNOWN:
1482         break;
1483     case URL_IS_DASH:
1484     default:
1485         return NULL;
1486         /*@notreached@*/ break;
1487     }
1488     /*@-dependenttrans@*/
1489     return opendir(path);
1490     /*@=dependenttrans@*/
1491 }
1492
1493 /*@+voidabstract@*/
1494 struct dirent * Readdir(DIR * dir)
1495 {
1496 if (_rpmio_debug)
1497 fprintf(stderr, "*** Readdir(%p)\n", (void *)dir);
1498     if (dir == NULL || ISFTPMAGIC(dir))
1499         return ftpReaddir(dir);
1500     return readdir(dir);
1501 }
1502
1503 int Closedir(DIR * dir)
1504 {
1505 if (_rpmio_debug)
1506 fprintf(stderr, "*** Closedir(%p)\n", (void *)dir);
1507     if (dir == NULL || ISFTPMAGIC(dir))
1508         return ftpClosedir(dir);
1509     return closedir(dir);
1510 }
1511 /*@=voidabstract@*/