- fix: use size_t consistently, avoid segfault on ia64.
[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 <popt.h>
13 #include "ugid.h"
14 #include "debug.h"
15
16 /*@access DIR@*/
17 /*@access FD_t@*/
18 /*@access urlinfo@*/
19
20 /* =============================================================== */
21 static int ftpMkdir(const char * path, /*@unused@*/ mode_t mode)
22         /*@globals fileSystem, internalState @*/
23         /*@modifies fileSystem, internalState @*/
24 {
25     int rc;
26     if ((rc = ftpCmd("MKD", path, NULL)) != 0)
27         return rc;
28 #if NOTYET
29     {   char buf[20];
30         sprintf(buf, " 0%o", mode);
31         (void) ftpCmd("SITE CHMOD", path, buf);
32     }
33 #endif
34     return rc;
35 }
36
37 static int ftpChdir(const char * path)
38         /*@globals fileSystem, internalState @*/
39         /*@modifies fileSystem, internalState @*/
40 {
41     return ftpCmd("CWD", path, NULL);
42 }
43
44 static int ftpRmdir(const char * path)
45         /*@globals fileSystem, internalState @*/
46         /*@modifies fileSystem, internalState @*/
47 {
48     return ftpCmd("RMD", path, NULL);
49 }
50
51 static int ftpRename(const char * oldpath, const char * newpath)
52         /*@globals fileSystem, internalState @*/
53         /*@modifies fileSystem, internalState @*/
54 {
55     int rc;
56     if ((rc = ftpCmd("RNFR", oldpath, NULL)) != 0)
57         return rc;
58     return ftpCmd("RNTO", newpath, NULL);
59 }
60
61 static int ftpUnlink(const char * path)
62         /*@globals fileSystem, internalState @*/
63         /*@modifies fileSystem, internalState @*/
64 {
65     return ftpCmd("DELE", path, NULL);
66 }
67
68 /* =============================================================== */
69 /* XXX rebuilddb.c: analogues to mkdir(2)/rmdir(2). */
70 int Mkdir (const char * path, mode_t mode)
71 {
72     const char * lpath;
73     int ut = urlPath(path, &lpath);
74
75     switch (ut) {
76     case URL_IS_FTP:
77         return ftpMkdir(path, mode);
78         /*@notreached@*/ break;
79     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
80     case URL_IS_PATH:
81         path = lpath;
82         /*@fallthrough@*/
83     case URL_IS_UNKNOWN:
84         break;
85     case URL_IS_DASH:
86     default:
87         return -2;
88         /*@notreached@*/ break;
89     }
90     return mkdir(path, mode);
91 }
92
93 int Chdir (const char * path)
94 {
95     const char * lpath;
96     int ut = urlPath(path, &lpath);
97
98     switch (ut) {
99     case URL_IS_FTP:
100         return ftpChdir(path);
101         /*@notreached@*/ break;
102     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
103     case URL_IS_PATH:
104         path = lpath;
105         /*@fallthrough@*/
106     case URL_IS_UNKNOWN:
107         break;
108     case URL_IS_DASH:
109     default:
110         return -2;
111         /*@notreached@*/ break;
112     }
113     return chdir(path);
114 }
115
116 int Rmdir (const char * path)
117 {
118     const char * lpath;
119     int ut = urlPath(path, &lpath);
120
121     switch (ut) {
122     case URL_IS_FTP:
123         return ftpRmdir(path);
124         /*@notreached@*/ break;
125     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
126     case URL_IS_PATH:
127         path = lpath;
128         /*@fallthrough@*/
129     case URL_IS_UNKNOWN:
130         break;
131     case URL_IS_DASH:
132     default:
133         return -2;
134         /*@notreached@*/ break;
135     }
136     return rmdir(path);
137 }
138
139 /* XXX rpmdb.c: analogue to rename(2). */
140
141 int Rename (const char * oldpath, const char * newpath)
142 {
143     const char *oe = NULL;
144     const char *ne = NULL;
145     int oldut, newut;
146
147     /* XXX lib/install.c used to rely on this behavior. */
148     if (!strcmp(oldpath, newpath)) return 0;
149
150     oldut = urlPath(oldpath, &oe);
151     switch (oldut) {
152     case URL_IS_FTP:            /* XXX WRONG WRONG WRONG */
153     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
154     case URL_IS_PATH:
155     case URL_IS_UNKNOWN:
156         break;
157     case URL_IS_DASH:
158     default:
159         return -2;
160         /*@notreached@*/ break;
161     }
162
163     newut = urlPath(newpath, &ne);
164     switch (newut) {
165     case URL_IS_FTP:
166 if (_rpmio_debug)
167 fprintf(stderr, "*** rename old %*s new %*s\n", (int)(oe - oldpath), oldpath, (int)(ne - newpath), newpath);
168         if (!(oldut == newut && oe && ne && (oe - oldpath) == (ne - newpath) &&
169             !xstrncasecmp(oldpath, newpath, (oe - oldpath))))
170             return -2;
171         return ftpRename(oldpath, newpath);
172         /*@notreached@*/ break;
173     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
174     case URL_IS_PATH:
175         oldpath = oe;
176         newpath = ne;
177         break;
178     case URL_IS_UNKNOWN:
179         break;
180     case URL_IS_DASH:
181     default:
182         return -2;
183         /*@notreached@*/ break;
184     }
185     return rename(oldpath, newpath);
186 }
187
188 int Link (const char * oldpath, const char * newpath)
189 {
190     const char *oe = NULL;
191     const char *ne = NULL;
192     int oldut, newut;
193
194     oldut = urlPath(oldpath, &oe);
195     switch (oldut) {
196     case URL_IS_FTP:            /* XXX WRONG WRONG WRONG */
197     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
198     case URL_IS_PATH:
199     case URL_IS_UNKNOWN:
200         break;
201     case URL_IS_DASH:
202     default:
203         return -2;
204         /*@notreached@*/ break;
205     }
206
207     newut = urlPath(newpath, &ne);
208     switch (newut) {
209     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
210     case URL_IS_FTP:            /* XXX WRONG WRONG WRONG */
211     case URL_IS_PATH:
212 if (_rpmio_debug)
213 fprintf(stderr, "*** link old %*s new %*s\n", (int)(oe - oldpath), oldpath, (int)(ne - newpath), newpath);
214         if (!(oldut == newut && oe && ne && (oe - oldpath) == (ne - newpath) &&
215             !xstrncasecmp(oldpath, newpath, (oe - oldpath))))
216             return -2;
217         oldpath = oe;
218         newpath = ne;
219         break;
220     case URL_IS_UNKNOWN:
221         break;
222     case URL_IS_DASH:
223     default:
224         return -2;
225         /*@notreached@*/ break;
226     }
227     return link(oldpath, newpath);
228 }
229
230 /* XXX build/build.c: analogue to unlink(2). */
231
232 int Unlink(const char * path) {
233     const char * lpath;
234     int ut = urlPath(path, &lpath);
235
236     switch (ut) {
237     case URL_IS_FTP:
238         return ftpUnlink(path);
239         /*@notreached@*/ break;
240     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
241     case URL_IS_PATH:
242         path = lpath;
243         /*@fallthrough@*/
244     case URL_IS_UNKNOWN:
245         break;
246     case URL_IS_DASH:
247     default:
248         return -2;
249         /*@notreached@*/ break;
250     }
251     return unlink(path);
252 }
253
254 /* XXX swiped from mc-4.5.39-pre9 vfs/ftpfs.c */
255
256 #define g_strdup        xstrdup
257 #define g_free          free
258
259 /*
260  * FIXME: this is broken. It depends on mc not crossing border on month!
261  */
262 /*@unchecked@*/
263 static int current_mday;
264 /*@unchecked@*/
265 static int current_mon;
266 /*@unchecked@*/
267 static int current_year;
268
269 /* Following stuff (parse_ls_lga) is used by ftpfs and extfs */
270 #define MAXCOLS         30
271
272 /*@unchecked@*/
273 static char *columns [MAXCOLS]; /* Points to the string in column n */
274 /*@unchecked@*/
275 static int   column_ptr [MAXCOLS]; /* Index from 0 to the starting positions of the columns */
276
277 /*@-boundswrite@*/
278 static int
279 vfs_split_text (char *p)
280         /*@globals columns, column_ptr @*/
281         /*@modifies *p, columns, column_ptr @*/
282 {
283     char *original = p;
284     int  numcols;
285
286
287     for (numcols = 0; *p && numcols < MAXCOLS; numcols++){
288         while (*p == ' ' || *p == '\r' || *p == '\n'){
289             *p = 0;
290             p++;
291         }
292         columns [numcols] = p;
293         column_ptr [numcols] = p - original;
294         while (*p && *p != ' ' && *p != '\r' && *p != '\n')
295             p++;
296     }
297     return numcols;
298 }
299 /*@=boundswrite@*/
300
301 /*@-boundsread@*/
302 static int
303 is_num (int idx)
304         /*@*/
305 {
306     if (!columns [idx] || columns [idx][0] < '0' || columns [idx][0] > '9')
307         return 0;
308     return 1;
309 }
310 /*@=boundsread@*/
311
312 /*@-boundsread@*/
313 static int
314 is_dos_date(/*@null@*/ const char *str)
315         /*@*/
316 {
317     if (str != NULL && strlen(str) == 8 &&
318                 str[2] == str[5] && strchr("\\-/", (int)str[2]) != NULL)
319         return 1;
320     return 0;
321 }
322 /*@=boundsread@*/
323
324 static int
325 is_week (/*@null@*/ const char * str, /*@out@*/ struct tm * tim)
326         /*@modifies *tim @*/
327 {
328 /*@observer@*/ static const char * week = "SunMonTueWedThuFriSat";
329     const char * pos;
330
331     /*@-observertrans -mayaliasunique@*/
332     if (str != NULL && (pos=strstr(week, str)) != NULL) {
333     /*@=observertrans =mayaliasunique@*/
334         if (tim != NULL)
335             tim->tm_wday = (pos - week)/3;
336         return 1;
337     }
338     return 0;    
339 }
340
341 static int
342 is_month (/*@null@*/ const char * str, /*@out@*/ struct tm * tim)
343         /*@modifies *tim @*/
344 {
345 /*@observer@*/ static const char * month = "JanFebMarAprMayJunJulAugSepOctNovDec";
346     const char * pos;
347     
348     /*@-observertrans -mayaliasunique@*/
349     if (str != NULL && (pos = strstr(month, str)) != NULL) {
350     /*@=observertrans -mayaliasunique@*/
351         if (tim != NULL)
352             tim->tm_mon = (pos - month)/3;
353         return 1;
354     }
355     return 0;
356 }
357
358 static int
359 is_time (/*@null@*/ const char * str, /*@out@*/ struct tm * tim)
360         /*@modifies *tim @*/
361 {
362     const char * p, * p2;
363
364     if (str != NULL && (p = strchr(str, ':')) && (p2 = strrchr(str, ':'))) {
365         if (p != p2) {
366             if (sscanf (str, "%2d:%2d:%2d", &tim->tm_hour, &tim->tm_min, &tim->tm_sec) != 3)
367                 return 0;
368         } else {
369             if (sscanf (str, "%2d:%2d", &tim->tm_hour, &tim->tm_min) != 2)
370                 return 0;
371         }
372     } else 
373         return 0;
374     
375     return 1;
376 }
377
378 static int is_year(/*@null@*/ const char * str, /*@out@*/ struct tm * tim)
379         /*@modifies *tim @*/
380 {
381     long year;
382
383     if (str == NULL)
384         return 0;
385
386     if (strchr(str,':'))
387         return 0;
388
389     if (strlen(str) != 4)
390         return 0;
391
392     if (sscanf(str, "%ld", &year) != 1)
393         return 0;
394
395     if (year < 1900 || year > 3000)
396         return 0;
397
398     tim->tm_year = (int) (year - 1900);
399
400     return 1;
401 }
402
403 /*
404  * FIXME: this is broken. Consider following entry:
405  * -rwx------   1 root     root            1 Aug 31 10:04 2904 1234
406  * where "2904 1234" is filename. Well, this code decodes it as year :-(.
407  */
408
409 static int
410 vfs_parse_filetype (char c)
411         /*@*/
412 {
413     switch (c) {
414         case 'd': return S_IFDIR; 
415         case 'b': return S_IFBLK;
416         case 'c': return S_IFCHR;
417         case 'l': return S_IFLNK;
418         case 's':
419 #ifdef IS_IFSOCK /* And if not, we fall through to IFIFO, which is pretty close */
420                   return S_IFSOCK;
421 #endif
422         case 'p': return S_IFIFO;
423         case 'm': case 'n':             /* Don't know what these are :-) */
424         case '-': case '?': return S_IFREG;
425         default: return -1;
426     }
427 }
428
429 static int vfs_parse_filemode (const char *p)
430         /*@*/
431 {       /* converts rw-rw-rw- into 0666 */
432     int res = 0;
433     switch (*(p++)) {
434         case 'r': res |= 0400; break;
435         case '-': break;
436         default: return -1;
437     }
438     switch (*(p++)) {
439         case 'w': res |= 0200; break;
440         case '-': break;
441         default: return -1;
442     }
443     switch (*(p++)) {
444         case 'x': res |= 0100; break;
445         case 's': res |= 0100 | S_ISUID; break;
446         case 'S': res |= S_ISUID; break;
447         case '-': break;
448         default: return -1;
449     }
450     switch (*(p++)) {
451         case 'r': res |= 0040; break;
452         case '-': break;
453         default: return -1;
454     }
455     switch (*(p++)) {
456         case 'w': res |= 0020; break;
457         case '-': break;
458         default: return -1;
459     }
460     switch (*(p++)) {
461         case 'x': res |= 0010; break;
462         case 's': res |= 0010 | S_ISGID; break;
463         case 'l': /* Solaris produces these */
464         case 'S': res |= S_ISGID; break;
465         case '-': break;
466         default: return -1;
467     }
468     switch (*(p++)) {
469         case 'r': res |= 0004; break;
470         case '-': break;
471         default: return -1;
472     }
473     switch (*(p++)) {
474         case 'w': res |= 0002; break;
475         case '-': break;
476         default: return -1;
477     }
478     switch (*(p++)) {
479         case 'x': res |= 0001; break;
480         case 't': res |= 0001 | S_ISVTX; break;
481         case 'T': res |= S_ISVTX; break;
482         case '-': break;
483         default: return -1;
484     }
485     return res;
486 }
487
488 /*@-boundswrite@*/
489 static int vfs_parse_filedate(int idx, /*@out@*/ time_t *t)
490         /*@modifies *t @*/
491 {       /* This thing parses from idx in columns[] array */
492
493     char *p;
494     struct tm tim;
495     int d[3];
496     int got_year = 0;
497
498     /* Let's setup default time values */
499     tim.tm_year = current_year;
500     tim.tm_mon  = current_mon;
501     tim.tm_mday = current_mday;
502     tim.tm_hour = 0;
503     tim.tm_min  = 0;
504     tim.tm_sec  = 0;
505     tim.tm_isdst = -1; /* Let mktime() try to guess correct dst offset */
506     
507     p = columns [idx++];
508     
509     /* We eat weekday name in case of extfs */
510     if(is_week(p, &tim))
511         p = columns [idx++];
512
513     /* Month name */
514     if(is_month(p, &tim)){
515         /* And we expect, it followed by day number */
516         if (is_num (idx))
517             tim.tm_mday = (int)atol (columns [idx++]);
518         else
519             return 0; /* No day */
520
521     } else {
522         /* We usually expect:
523            Mon DD hh:mm
524            Mon DD  YYYY
525            But in case of extfs we allow these date formats:
526            Mon DD YYYY hh:mm
527            Mon DD hh:mm YYYY
528            Wek Mon DD hh:mm:ss YYYY
529            MM-DD-YY hh:mm
530            where Mon is Jan-Dec, DD, MM, YY two digit day, month, year,
531            YYYY four digit year, hh, mm, ss two digit hour, minute or second. */
532
533         /* Here just this special case with MM-DD-YY */
534         if (is_dos_date(p)){
535             /*@-mods@*/
536             p[2] = p[5] = '-';
537             /*@=mods@*/
538             
539             memset(d, 0, sizeof(d));
540             if (sscanf(p, "%2d-%2d-%2d", &d[0], &d[1], &d[2]) == 3){
541             /*  We expect to get:
542                 1. MM-DD-YY
543                 2. DD-MM-YY
544                 3. YY-MM-DD
545                 4. YY-DD-MM  */
546                 
547                 /* Hmm... maybe, next time :)*/
548                 
549                 /* At last, MM-DD-YY */
550                 d[0]--; /* Months are zerobased */
551                 /* Y2K madness */
552                 if(d[2] < 70)
553                     d[2] += 100;
554
555                 tim.tm_mon  = d[0];
556                 tim.tm_mday = d[1];
557                 tim.tm_year = d[2];
558                 got_year = 1;
559             } else
560                 return 0; /* sscanf failed */
561         } else
562             return 0; /* unsupported format */
563     }
564
565     /* Here we expect to find time and/or year */
566     
567     if (is_num (idx)) {
568         if(is_time(columns[idx], &tim) || (got_year = is_year(columns[idx], &tim))) {
569         idx++;
570
571         /* This is a special case for ctime() or Mon DD YYYY hh:mm */
572         if(is_num (idx) && 
573             ((got_year = is_year(columns[idx], &tim)) || is_time(columns[idx], &tim)))
574                 idx++; /* time & year or reverse */
575         } /* only time or date */
576     }
577     else 
578         return 0; /* Nor time or date */
579
580     /*
581      * If the date is less than 6 months in the past, it is shown without year
582      * other dates in the past or future are shown with year but without time
583      * This does not check for years before 1900 ... I don't know, how
584      * to represent them at all
585      */
586     if (!got_year &&
587         current_mon < 6 && current_mon < tim.tm_mon && 
588         tim.tm_mon - current_mon >= 6)
589
590         tim.tm_year--;
591
592     if ((*t = mktime(&tim)) < 0)
593         *t = 0;
594     return idx;
595 }
596 /*@=boundswrite@*/
597
598 /*@-boundswrite@*/
599 static int
600 vfs_parse_ls_lga (char * p, /*@out@*/ struct stat * st,
601                 /*@out@*/ const char ** filename,
602                 /*@out@*/ const char ** linkname)
603         /*@modifies *p, *st, *filename, *linkname @*/
604 {
605     int idx, idx2, num_cols;
606     int i;
607     char *p_copy;
608     
609     if (strncmp (p, "total", 5) == 0)
610         return 0;
611
612     p_copy = g_strdup(p);
613 /* XXX FIXME: parse out inode number from "NLST -lai ." */
614 /* XXX FIXME: parse out sizein blocks from "NLST -lais ." */
615
616     if ((i = vfs_parse_filetype(*(p++))) == -1)
617         goto error;
618
619     st->st_mode = i;
620     if (*p == ' ')      /* Notwell 4 */
621         p++;
622     if (*p == '['){
623         if (strlen (p) <= 8 || p [8] != ']')
624             goto error;
625         /* Should parse here the Notwell permissions :) */
626         /*@-unrecog@*/
627         if (S_ISDIR (st->st_mode))
628             st->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IXUSR | S_IXGRP | S_IXOTH);
629         else
630             st->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);
631         p += 9;
632         /*@=unrecog@*/
633     } else {
634         if ((i = vfs_parse_filemode(p)) == -1)
635             goto error;
636         st->st_mode |= i;
637         p += 9;
638
639         /* This is for an extra ACL attribute (HP-UX) */
640         if (*p == '+')
641             p++;
642     }
643
644     g_free(p_copy);
645     p_copy = g_strdup(p);
646     num_cols = vfs_split_text (p);
647
648     st->st_nlink = atol (columns [0]);
649     if (st->st_nlink < 0)
650         goto error;
651
652     if (!is_num (1))
653 #ifdef  HACK
654         st->st_uid = finduid (columns [1]);
655 #else
656         (void) unameToUid (columns [1], &st->st_uid);
657 #endif
658     else
659         st->st_uid = (uid_t) atol (columns [1]);
660
661     /* Mhm, the ls -lg did not produce a group field */
662     for (idx = 3; idx <= 5; idx++) 
663         if (is_month(columns [idx], NULL) || is_week(columns [idx], NULL) || is_dos_date(columns[idx]))
664             break;
665
666     if (idx == 6 || (idx == 5 && !S_ISCHR (st->st_mode) && !S_ISBLK (st->st_mode)))
667         goto error;
668
669     /* We don't have gid */     
670     if (idx == 3 || (idx == 4 && (S_ISCHR(st->st_mode) || S_ISBLK (st->st_mode))))
671         idx2 = 2;
672     else { 
673         /* We have gid field */
674         if (is_num (2))
675             st->st_gid = (gid_t) atol (columns [2]);
676         else
677 #ifdef  HACK
678             st->st_gid = findgid (columns [2]);
679 #else
680             (void) gnameToGid (columns [1], &st->st_gid);
681 #endif
682         idx2 = 3;
683     }
684
685     /* This is device */
686     if (S_ISCHR (st->st_mode) || S_ISBLK (st->st_mode)){
687         int maj, min;
688         
689         if (!is_num (idx2) || sscanf(columns [idx2], " %d,", &maj) != 1)
690             goto error;
691         
692         if (!is_num (++idx2) || sscanf(columns [idx2], " %d", &min) != 1)
693             goto error;
694         
695 #ifdef HAVE_ST_RDEV
696         st->st_rdev = ((maj & 0xff) << 8) | (min & 0xffff00ff);
697 #endif
698         st->st_size = 0;
699         
700     } else {
701         /* Common file size */
702         if (!is_num (idx2))
703             goto error;
704         
705         st->st_size = (size_t) atol (columns [idx2]);
706 #ifdef HAVE_ST_RDEV
707         st->st_rdev = 0;
708 #endif
709     }
710
711     idx = vfs_parse_filedate(idx, &st->st_mtime);
712     if (!idx)
713         goto error;
714     /* Use resulting time value */
715     st->st_atime = st->st_ctime = st->st_mtime;
716     st->st_dev = 0;
717     st->st_ino = 0;
718 #ifdef HAVE_ST_BLKSIZE
719     st->st_blksize = 512;
720 #endif
721 #ifdef HAVE_ST_BLOCKS
722     st->st_blocks = (st->st_size + 511) / 512;
723 #endif
724
725     for (i = idx + 1, idx2 = 0; i < num_cols; i++ ) 
726         if (strcmp (columns [i], "->") == 0){
727             idx2 = i;
728             break;
729         }
730     
731     if (((S_ISLNK (st->st_mode) || 
732         (num_cols == idx + 3 && st->st_nlink > 1))) /* Maybe a hardlink? (in extfs) */
733         && idx2){
734         int tlen;
735         char *t;
736             
737         if (filename){
738 #ifdef HACK
739             t = g_strndup (p_copy + column_ptr [idx], column_ptr [idx2] - column_ptr [idx] - 1);
740 #else
741             int nb = column_ptr [idx2] - column_ptr [idx] - 1;
742             t = xmalloc(nb+1);
743             strncpy(t, p_copy + column_ptr [idx], nb);
744 #endif
745             *filename = t;
746         }
747         if (linkname){
748             t = g_strdup (p_copy + column_ptr [idx2+1]);
749             tlen = strlen (t);
750             if (t [tlen-1] == '\r' || t [tlen-1] == '\n')
751                 t [tlen-1] = 0;
752             if (t [tlen-2] == '\r' || t [tlen-2] == '\n')
753                 t [tlen-2] = 0;
754                 
755             *linkname = t;
756         }
757     } else {
758         /* Extract the filename from the string copy, not from the columns
759          * this way we have a chance of entering hidden directories like ". ."
760          */
761         if (filename){
762             /* 
763             *filename = g_strdup (columns [idx++]);
764             */
765             int tlen;
766             char *t;
767             
768             t = g_strdup (p_copy + column_ptr [idx]); idx++;
769             tlen = strlen (t);
770             /* g_strchomp(); */
771             if (t [tlen-1] == '\r' || t [tlen-1] == '\n')
772                 t [tlen-1] = 0;
773             if (t [tlen-2] == '\r' || t [tlen-2] == '\n')
774                 t [tlen-2] = 0;
775             
776             *filename = t;
777         }
778         if (linkname)
779             *linkname = NULL;
780     }
781     g_free (p_copy);
782     return 1;
783
784 error:
785 #ifdef  HACK
786     {
787       static int errorcount = 0;
788
789       if (++errorcount < 5) {
790         message_1s (1, "Could not parse:", p_copy);
791       } else if (errorcount == 5)
792         message_1s (1, "More parsing errors will be ignored.", "(sorry)" );
793     }
794 #endif
795
796     /*@-usereleased@*/
797     if (p_copy != p)            /* Carefull! */
798     /*@=usereleased@*/
799         g_free (p_copy);
800     return 0;
801 }
802 /*@=boundswrite@*/
803
804 typedef enum {
805         DO_FTP_STAT     = 1,
806         DO_FTP_LSTAT    = 2,
807         DO_FTP_READLINK = 3,
808         DO_FTP_ACCESS   = 4,
809         DO_FTP_GLOB     = 5
810 } ftpSysCall_t;
811
812 /**
813  */
814 /*@unchecked@*/
815 static size_t ftpBufAlloced = 0;
816
817 /**
818  */
819 /*@unchecked@*/
820 static /*@only@*/ char * ftpBuf = NULL;
821         
822 #define alloca_strdup(_s)       strcpy(alloca(strlen(_s)+1), (_s))
823
824 /*@-boundswrite@*/
825 /*@-mods@*/
826 static int ftpNLST(const char * url, ftpSysCall_t ftpSysCall,
827                 /*@out@*/ /*@null@*/ struct stat * st,
828                 /*@out@*/ /*@null@*/ char * rlbuf, size_t rlbufsiz)
829         /*@globals fileSystem, internalState @*/
830         /*@modifies *st, *rlbuf, fileSystem, internalState @*/
831 {
832     FD_t fd;
833     const char * path;
834     int bufLength, moretodo;
835     const char *n, *ne, *o, *oe;
836     char * s;
837     char * se;
838     const char * urldn;
839     char * bn = NULL;
840     int nbn = 0;
841     urlinfo u;
842     int rc;
843
844     n = ne = o = oe = NULL;
845     (void) urlPath(url, &path);
846     if (*path == '\0')
847         return -2;
848
849     switch (ftpSysCall) {
850     case DO_FTP_GLOB:
851         fd = ftpOpen(url, 0, 0, &u);
852         if (fd == NULL || u == NULL)
853             return -1;
854
855         u->openError = ftpReq(fd, "LIST", path);
856         break;
857     default:
858         urldn = alloca_strdup(url);
859         /*@-branchstate@*/
860         if ((bn = strrchr(urldn, '/')) == NULL)
861             return -2;
862         else if (bn == path)
863             bn = ".";
864         else
865             *bn++ = '\0';
866         /*@=branchstate@*/
867         nbn = strlen(bn);
868
869         rc = ftpChdir(urldn);           /* XXX don't care about CWD */
870         if (rc < 0)
871             return rc;
872
873         fd = ftpOpen(url, 0, 0, &u);
874         if (fd == NULL || u == NULL)
875             return -1;
876
877         /* XXX possibly should do "NLST -lais" to get st_ino/st_blocks also */
878         u->openError = ftpReq(fd, "NLST", "-la");
879
880         if (bn == NULL || nbn <= 0) {
881             rc = -2;
882             goto exit;
883         }
884         break;
885     }
886
887     if (u->openError < 0) {
888         fd = fdLink(fd, "error data (ftpStat)");
889         rc = -2;
890         goto exit;
891     }
892
893     if (ftpBufAlloced == 0 || ftpBuf == NULL) {
894         ftpBufAlloced = _url_iobuf_size;
895         ftpBuf = xcalloc(ftpBufAlloced, sizeof(ftpBuf[0]));
896     }
897     *ftpBuf = '\0';
898
899     bufLength = 0;
900     moretodo = 1;
901
902     do {
903
904         /* XXX FIXME: realloc ftpBuf if < ~128 chars remain */
905         if ((ftpBufAlloced - bufLength) < (1024+80)) {
906             ftpBufAlloced <<= 2;
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)
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@*/