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