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