Factor -type problems into explicit code annotations.
[platform/upstream/rpm.git] / rpmio / rpmrpc.c
1 /*@-type@*/ /* LCL: function typedefs */
2 /** \ingroup rpmio
3  * \file rpmio/rpmrpc.c
4  */
5
6 #include "system.h"
7 #include <rpmio_internal.h>
8 #include <popt.h>
9 #include "ugid.h"
10 #include "debug.h"
11
12 /*@access FD_t@*/
13 /*@access urlinfo@*/
14
15 /*@-redecl@*/
16 extern int _rpmio_debug;
17 /*@=redecl@*/
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             p[2] = p[5] = '-';
528             
529             memset(d, 0, sizeof(d));
530             if (sscanf(p, "%2d-%2d-%2d", &d[0], &d[1], &d[2]) == 3){
531             /*  We expect to get:
532                 1. MM-DD-YY
533                 2. DD-MM-YY
534                 3. YY-MM-DD
535                 4. YY-DD-MM  */
536                 
537                 /* Hmm... maybe, next time :)*/
538                 
539                 /* At last, MM-DD-YY */
540                 d[0]--; /* Months are zerobased */
541                 /* Y2K madness */
542                 if(d[2] < 70)
543                     d[2] += 100;
544
545                 tim.tm_mon  = d[0];
546                 tim.tm_mday = d[1];
547                 tim.tm_year = d[2];
548                 got_year = 1;
549             } else
550                 return 0; /* sscanf failed */
551         } else
552             return 0; /* unsupported format */
553     }
554
555     /* Here we expect to find time and/or year */
556     
557     if (is_num (idx)) {
558         if(is_time(columns[idx], &tim) || (got_year = is_year(columns[idx], &tim))) {
559         idx++;
560
561         /* This is a special case for ctime() or Mon DD YYYY hh:mm */
562         if(is_num (idx) && 
563             ((got_year = is_year(columns[idx], &tim)) || is_time(columns[idx], &tim)))
564                 idx++; /* time & year or reverse */
565         } /* only time or date */
566     }
567     else 
568         return 0; /* Nor time or date */
569
570     /*
571      * If the date is less than 6 months in the past, it is shown without year
572      * other dates in the past or future are shown with year but without time
573      * This does not check for years before 1900 ... I don't know, how
574      * to represent them at all
575      */
576     if (!got_year &&
577         current_mon < 6 && current_mon < tim.tm_mon && 
578         tim.tm_mon - current_mon >= 6)
579
580         tim.tm_year--;
581
582     if ((*t = mktime(&tim)) < 0)
583         *t = 0;
584     return idx;
585 }
586
587 static int
588 vfs_parse_ls_lga (char * p, /*@out@*/ struct stat * st,
589                 /*@out@*/ const char ** filename,
590                 /*@out@*/ const char ** linkname)
591         /*@modifies *st, *filename, *linkname @*/
592 {
593     int idx, idx2, num_cols;
594     int i;
595     char *p_copy;
596     
597     if (strncmp (p, "total", 5) == 0)
598         return 0;
599
600     p_copy = g_strdup(p);
601 /* XXX FIXME: parse out inode number from "NLST -lai ." */
602 /* XXX FIXME: parse out sizein blocks from "NLST -lais ." */
603
604     if ((i = vfs_parse_filetype(*(p++))) == -1)
605         goto error;
606
607     st->st_mode = i;
608     if (*p == ' ')      /* Notwell 4 */
609         p++;
610     if (*p == '['){
611         if (strlen (p) <= 8 || p [8] != ']')
612             goto error;
613         /* Should parse here the Notwell permissions :) */
614         /*@-unrecog@*/
615         if (S_ISDIR (st->st_mode))
616             st->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IXUSR | S_IXGRP | S_IXOTH);
617         else
618             st->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);
619         p += 9;
620         /*@=unrecog@*/
621     } else {
622         if ((i = vfs_parse_filemode(p)) == -1)
623             goto error;
624         st->st_mode |= i;
625         p += 9;
626
627         /* This is for an extra ACL attribute (HP-UX) */
628         if (*p == '+')
629             p++;
630     }
631
632     g_free(p_copy);
633     p_copy = g_strdup(p);
634     num_cols = vfs_split_text (p);
635
636     st->st_nlink = atol (columns [0]);
637     if (st->st_nlink < 0)
638         goto error;
639
640     if (!is_num (1))
641 #ifdef  HACK
642         st->st_uid = finduid (columns [1]);
643 #else
644         (void) unameToUid (columns [1], &st->st_uid);
645 #endif
646     else
647         st->st_uid = (uid_t) atol (columns [1]);
648
649     /* Mhm, the ls -lg did not produce a group field */
650     for (idx = 3; idx <= 5; idx++) 
651         if (is_month(columns [idx], NULL) || is_week(columns [idx], NULL) || is_dos_date(columns[idx]))
652             break;
653
654     if (idx == 6 || (idx == 5 && !S_ISCHR (st->st_mode) && !S_ISBLK (st->st_mode)))
655         goto error;
656
657     /* We don't have gid */     
658     if (idx == 3 || (idx == 4 && (S_ISCHR(st->st_mode) || S_ISBLK (st->st_mode))))
659         idx2 = 2;
660     else { 
661         /* We have gid field */
662         if (is_num (2))
663             st->st_gid = (gid_t) atol (columns [2]);
664         else
665 #ifdef  HACK
666             st->st_gid = findgid (columns [2]);
667 #else
668             (void) gnameToGid (columns [1], &st->st_gid);
669 #endif
670         idx2 = 3;
671     }
672
673     /* This is device */
674     if (S_ISCHR (st->st_mode) || S_ISBLK (st->st_mode)){
675         int maj, min;
676         
677         if (!is_num (idx2) || sscanf(columns [idx2], " %d,", &maj) != 1)
678             goto error;
679         
680         if (!is_num (++idx2) || sscanf(columns [idx2], " %d", &min) != 1)
681             goto error;
682         
683 #ifdef HAVE_ST_RDEV
684         st->st_rdev = ((maj & 0xff) << 8) | (min & 0xffff00ff);
685 #endif
686         st->st_size = 0;
687         
688     } else {
689         /* Common file size */
690         if (!is_num (idx2))
691             goto error;
692         
693         st->st_size = (size_t) atol (columns [idx2]);
694 #ifdef HAVE_ST_RDEV
695         st->st_rdev = 0;
696 #endif
697     }
698
699     idx = vfs_parse_filedate(idx, &st->st_mtime);
700     if (!idx)
701         goto error;
702     /* Use resulting time value */
703     st->st_atime = st->st_ctime = st->st_mtime;
704     st->st_dev = 0;
705     st->st_ino = 0;
706 #ifdef HAVE_ST_BLKSIZE
707     st->st_blksize = 512;
708 #endif
709 #ifdef HAVE_ST_BLOCKS
710     st->st_blocks = (st->st_size + 511) / 512;
711 #endif
712
713     for (i = idx + 1, idx2 = 0; i < num_cols; i++ ) 
714         if (strcmp (columns [i], "->") == 0){
715             idx2 = i;
716             break;
717         }
718     
719     if (((S_ISLNK (st->st_mode) || 
720         (num_cols == idx + 3 && st->st_nlink > 1))) /* Maybe a hardlink? (in extfs) */
721         && idx2){
722         int tlen;
723         char *t;
724             
725         if (filename){
726 #ifdef HACK
727             t = g_strndup (p_copy + column_ptr [idx], column_ptr [idx2] - column_ptr [idx] - 1);
728 #else
729             int nb = column_ptr [idx2] - column_ptr [idx] - 1;
730             t = xmalloc(nb+1);
731             strncpy(t, p_copy + column_ptr [idx], nb);
732 #endif
733             *filename = t;
734         }
735         if (linkname){
736             t = g_strdup (p_copy + column_ptr [idx2+1]);
737             tlen = strlen (t);
738             if (t [tlen-1] == '\r' || t [tlen-1] == '\n')
739                 t [tlen-1] = 0;
740             if (t [tlen-2] == '\r' || t [tlen-2] == '\n')
741                 t [tlen-2] = 0;
742                 
743             *linkname = t;
744         }
745     } else {
746         /* Extract the filename from the string copy, not from the columns
747          * this way we have a chance of entering hidden directories like ". ."
748          */
749         if (filename){
750             /* 
751             *filename = g_strdup (columns [idx++]);
752             */
753             int tlen;
754             char *t;
755             
756             t = g_strdup (p_copy + column_ptr [idx++]);
757             tlen = strlen (t);
758             /* g_strchomp(); */
759             if (t [tlen-1] == '\r' || t [tlen-1] == '\n')
760                 t [tlen-1] = 0;
761             if (t [tlen-2] == '\r' || t [tlen-2] == '\n')
762                 t [tlen-2] = 0;
763             
764             *filename = t;
765         }
766         if (linkname)
767             *linkname = NULL;
768     }
769     g_free (p_copy);
770     return 1;
771
772 error:
773 #ifdef  HACK
774     {
775       static int errorcount = 0;
776
777       if (++errorcount < 5) {
778         message_1s (1, "Could not parse:", p_copy);
779       } else if (errorcount == 5)
780         message_1s (1, "More parsing errors will be ignored.", "(sorry)" );
781     }
782 #endif
783
784     /*@-usereleased@*/
785     if (p_copy != p)            /* Carefull! */
786     /*@=usereleased@*/
787         g_free (p_copy);
788     return 0;
789 }
790
791 typedef enum {
792         DO_FTP_STAT     = 1,
793         DO_FTP_LSTAT    = 2,
794         DO_FTP_READLINK = 3,
795         DO_FTP_ACCESS   = 4,
796         DO_FTP_GLOB     = 5
797 } ftpSysCall_t;
798
799 /**
800  */
801 /*@unchecked@*/
802 static size_t ftpBufAlloced = 0;
803
804 /**
805  */
806 /*@unchecked@*/
807 static /*@only@*/ char * ftpBuf = NULL;
808         
809 #define alloca_strdup(_s)       strcpy(alloca(strlen(_s)+1), (_s))
810
811 static int ftpNLST(const char * url, ftpSysCall_t ftpSysCall,
812                 /*@out@*/ /*@null@*/ struct stat * st,
813                 /*@out@*/ /*@null@*/ char * rlbuf, size_t rlbufsiz)
814         /*@globals fileSystem @*/
815         /*@modifies *st, *rlbuf, fileSystem @*/
816 {
817     FD_t fd;
818     const char * path;
819     int bufLength, moretodo;
820     const char *n, *ne, *o, *oe;
821     char * s;
822     char * se;
823     const char * urldn;
824     char * bn = NULL;
825     int nbn = 0;
826     urlinfo u;
827     int rc;
828
829     n = ne = o = oe = NULL;
830     (void) urlPath(url, &path);
831     if (*path == '\0')
832         return -2;
833
834     switch (ftpSysCall) {
835     case DO_FTP_GLOB:
836         fd = ftpOpen(url, 0, 0, &u);
837         if (fd == NULL || u == NULL)
838             return -1;
839
840         u->openError = ftpReq(fd, "NLST", path);
841         break;
842     default:
843         urldn = alloca_strdup(url);
844         /*@-branchstate@*/
845         if ((bn = strrchr(urldn, '/')) == NULL)
846             return -2;
847         else if (bn == path)
848             bn = ".";
849         else
850             *bn++ = '\0';
851         /*@=branchstate@*/
852         nbn = strlen(bn);
853
854         rc = ftpChdir(urldn);           /* XXX don't care about CWD */
855         if (rc < 0)
856             return rc;
857
858         fd = ftpOpen(url, 0, 0, &u);
859         if (fd == NULL || u == NULL)
860             return -1;
861
862         /* XXX possibly should do "NLST -lais" to get st_ino/st_blocks also */
863         u->openError = ftpReq(fd, "NLST", "-la");
864
865         if (bn == NULL || nbn <= 0) {
866             rc = -2;
867             goto exit;
868         }
869         break;
870     }
871
872     if (u->openError < 0) {
873         fd = fdLink(fd, "error data (ftpStat)");
874         rc = -2;
875         goto exit;
876     }
877
878     if (ftpBufAlloced == 0 || ftpBuf == NULL) {
879         ftpBufAlloced = url_iobuf_size;
880         ftpBuf = xcalloc(ftpBufAlloced, sizeof(ftpBuf[0]));
881     }
882     *ftpBuf = '\0';
883
884     bufLength = 0;
885     moretodo = 1;
886
887     do {
888
889         /* XXX FIXME: realloc ftpBuf is < ~128 chars remain */
890         if ((ftpBufAlloced - bufLength) < (1024+80)) {
891             ftpBufAlloced <<= 2;
892             ftpBuf = xrealloc(ftpBuf, ftpBufAlloced);
893         }
894         s = se = ftpBuf + bufLength;
895         *se = '\0';
896
897         rc = fdFgets(fd, se, (ftpBufAlloced - bufLength));
898         if (rc <= 0) {
899             moretodo = 0;
900             break;
901         }
902         if (ftpSysCall == DO_FTP_GLOB) {        /* XXX HACK */
903             bufLength += strlen(se);
904             continue;
905         }
906
907         for (s = se; *s != '\0'; s = se) {
908             int bingo;
909
910             while (*se && *se != '\n') se++;
911             if (se > s && se[-1] == '\r') se[-1] = '\0';
912             if (*se == '\0') 
913                 /*@innerbreak@*/ break;
914             *se++ = '\0';
915
916             if (!strncmp(s, "total ", sizeof("total ")-1))
917                 /*@innercontinue@*/ continue;
918
919             o = NULL;
920             for (bingo = 0, n = se; n >= s; n--) {
921                 switch (*n) {
922                 case '\0':
923                     oe = ne = n;
924                     /*@switchbreak@*/ break;
925                 case ' ':
926                     if (o || !(n[-3] == ' ' && n[-2] == '-' && n[-1] == '>')) {
927                         while (*(++n) == ' ')
928                             {};
929                         bingo++;
930                         /*@switchbreak@*/ break;
931                     }
932                     for (o = n + 1; *o == ' '; o++)
933                         {};
934                     n -= 3;
935                     ne = n;
936                     /*@switchbreak@*/ break;
937                 default:
938                     /*@switchbreak@*/ break;
939                 }
940                 if (bingo)
941                     /*@innerbreak@*/ break;
942             }
943
944             if (nbn != (ne - n))        /* Same name length? */
945                 /*@innercontinue@*/ continue;
946             if (strncmp(n, bn, nbn))    /* Same name? */
947                 /*@innercontinue@*/ continue;
948
949             moretodo = 0;
950             /*@innerbreak@*/ break;
951         }
952
953         if (moretodo && se > s) {
954             bufLength = se - s - 1;
955             if (s != ftpBuf)
956                 memmove(ftpBuf, s, bufLength);
957         } else {
958             bufLength = 0;
959         }
960     } while (moretodo);
961
962     switch (ftpSysCall) {
963     case DO_FTP_STAT:
964         if (o && oe) {
965             /* XXX FIXME: symlink, replace urldn/bn from [o,oe) and restart */
966         }
967         /*@fallthrough@*/
968     case DO_FTP_LSTAT:
969         if (st == NULL || !(n && ne)) {
970             rc = -1;
971         } else {
972             rc = ((vfs_parse_ls_lga(s, st, NULL, NULL) > 0) ? 0 : -1);
973         }
974         break;
975     case DO_FTP_READLINK:
976         if (rlbuf == NULL || !(o && oe)) {
977             rc = -1;
978         } else {
979             rc = oe - o;
980             if (rc > rlbufsiz)
981                 rc = rlbufsiz;
982             memcpy(rlbuf, o, rc);
983             if (rc < rlbufsiz)
984                 rlbuf[rc] = '\0';
985         }
986         break;
987     case DO_FTP_ACCESS:
988         rc = 0;         /* XXX WRONG WRONG WRONG */
989         break;
990     case DO_FTP_GLOB:
991         rc = 0;         /* XXX WRONG WRONG WRONG */
992         break;
993     }
994
995 exit:
996     (void) ufdClose(fd);
997     return rc;
998 }
999
1000 static int ftpStat(const char * path, /*@out@*/ struct stat *st)
1001         /*@globals fileSystem @*/
1002         /*@modifies *st, fileSystem @*/
1003 {
1004     return ftpNLST(path, DO_FTP_STAT, st, NULL, 0);
1005 }
1006
1007 static int ftpLstat(const char * path, /*@out@*/ struct stat *st)
1008         /*@globals fileSystem @*/
1009         /*@modifies *st, fileSystem @*/
1010 {
1011     int rc;
1012     rc = ftpNLST(path, DO_FTP_LSTAT, st, NULL, 0);
1013 if (_rpmio_debug)
1014 fprintf(stderr, "*** ftpLstat(%s) rc %d\n", path, rc);
1015     return rc;
1016 }
1017
1018 static int ftpReadlink(const char * path, /*@out@*/ char * buf, size_t bufsiz)
1019         /*@globals fileSystem @*/
1020         /*@modifies *buf, fileSystem @*/
1021 {
1022     return ftpNLST(path, DO_FTP_READLINK, NULL, buf, bufsiz);
1023 }
1024
1025 static int ftpGlob(const char * path, int flags,
1026                 int errfunc(const char * epath, int eerno),
1027                 /*@out@*/ glob_t * pglob)
1028         /*@globals fileSystem @*/
1029         /*@modifies *pglob, fileSystem @*/
1030 {
1031     int rc;
1032
1033     if (pglob == NULL)
1034         return -2;
1035     rc = ftpNLST(path, DO_FTP_GLOB, NULL, NULL, 0);
1036 /*@-castfcnptr@*/
1037 if (_rpmio_debug)
1038 fprintf(stderr, "*** ftpGlob(%s,0x%x,%p,%p) ftpNLST rc %d\n", path, (unsigned)flags, (void *)errfunc, pglob, rc);
1039 /*@=castfcnptr@*/
1040     if (rc)
1041         return rc;
1042     rc = poptParseArgvString(ftpBuf, &pglob->gl_pathc, (const char ***)&pglob->gl_pathv);
1043     pglob->gl_offs = -1;        /* XXX HACK HACK HACK */
1044     return rc;
1045 }
1046
1047 static void ftpGlobfree(glob_t * pglob)
1048         /*@modifies *pglob @*/
1049 {
1050 /*@-modfilesys@*/
1051 if (_rpmio_debug)
1052 fprintf(stderr, "*** ftpGlobfree(%p)\n", pglob);
1053 /*@=modfilesys@*/
1054     if (pglob->gl_offs == -1) { /* XXX HACK HACK HACK */
1055         free((void *)pglob->gl_pathv);
1056         pglob->gl_pathv = NULL;
1057     }
1058 }
1059
1060 int Stat(const char * path, struct stat * st)
1061 {
1062     const char * lpath;
1063     int ut = urlPath(path, &lpath);
1064
1065 if (_rpmio_debug)
1066 fprintf(stderr, "*** Stat(%s,%p)\n", path, st);
1067     switch (ut) {
1068     case URL_IS_FTP:
1069         return ftpStat(path, st);
1070         /*@notreached@*/ break;
1071     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
1072     case URL_IS_PATH:
1073         path = lpath;
1074         /*@fallthrough@*/
1075     case URL_IS_UNKNOWN:
1076         break;
1077     case URL_IS_DASH:
1078     default:
1079         return -2;
1080         /*@notreached@*/ break;
1081     }
1082     return stat(path, st);
1083 }
1084
1085 int Lstat(const char * path, struct stat * st)
1086 {
1087     const char * lpath;
1088     int ut = urlPath(path, &lpath);
1089
1090 if (_rpmio_debug)
1091 fprintf(stderr, "*** Lstat(%s,%p)\n", path, st);
1092     switch (ut) {
1093     case URL_IS_FTP:
1094         return ftpLstat(path, st);
1095         /*@notreached@*/ break;
1096     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
1097     case URL_IS_PATH:
1098         path = lpath;
1099         /*@fallthrough@*/
1100     case URL_IS_UNKNOWN:
1101         break;
1102     case URL_IS_DASH:
1103     default:
1104         return -2;
1105         /*@notreached@*/ break;
1106     }
1107     return lstat(path, st);
1108 }
1109
1110 int Readlink(const char * path, char * buf, size_t bufsiz)
1111 {
1112     const char * lpath;
1113     int ut = urlPath(path, &lpath);
1114
1115     switch (ut) {
1116     case URL_IS_FTP:
1117         return ftpReadlink(path, buf, bufsiz);
1118         /*@notreached@*/ break;
1119     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
1120     case URL_IS_PATH:
1121         path = lpath;
1122         /*@fallthrough@*/
1123     case URL_IS_UNKNOWN:
1124         break;
1125     case URL_IS_DASH:
1126     default:
1127         return -2;
1128         /*@notreached@*/ break;
1129     }
1130     return readlink(path, buf, bufsiz);
1131 }
1132
1133 int Access(const char * path, int amode)
1134 {
1135     const char * lpath;
1136     int ut = urlPath(path, &lpath);
1137
1138 if (_rpmio_debug)
1139 fprintf(stderr, "*** Access(%s,%d)\n", path, amode);
1140     switch (ut) {
1141     case URL_IS_FTP:            /* XXX WRONG WRONG WRONG */
1142     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
1143     case URL_IS_PATH:
1144         path = lpath;
1145         /*@fallthrough@*/
1146     case URL_IS_UNKNOWN:
1147         break;
1148     case URL_IS_DASH:
1149     default:
1150         return -2;
1151         /*@notreached@*/ break;
1152     }
1153     return access(path, amode);
1154 }
1155
1156 int Glob(const char *pattern, int flags,
1157         int errfunc(const char * epath, int eerrno), glob_t *pglob)
1158 {
1159     const char * lpath;
1160     int ut = urlPath(pattern, &lpath);
1161
1162 /*@-castfcnptr@*/
1163 if (_rpmio_debug)
1164 fprintf(stderr, "*** Glob(%s,0x%x,%p,%p)\n", pattern, (unsigned)flags, (void *)errfunc, pglob);
1165 /*@=castfcnptr@*/
1166     switch (ut) {
1167     case URL_IS_FTP:            /* XXX WRONG WRONG WRONG */
1168         return ftpGlob(pattern, flags, errfunc, pglob);
1169         /*@notreached@*/ break;
1170     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
1171     case URL_IS_PATH:
1172         pattern = lpath;
1173         /*@fallthrough@*/
1174     case URL_IS_UNKNOWN:
1175         break;
1176     case URL_IS_DASH:
1177     default:
1178         return -2;
1179         /*@notreached@*/ break;
1180     }
1181     return glob(pattern, flags, errfunc, pglob);
1182 }
1183
1184 void Globfree(glob_t *pglob)
1185 {
1186 if (_rpmio_debug)
1187 fprintf(stderr, "*** Globfree(%p)\n", pglob);
1188     /*@-branchstate@*/
1189     if (pglob->gl_offs == -1) /* XXX HACK HACK HACK */
1190         ftpGlobfree(pglob);
1191     else
1192         globfree(pglob);
1193     /*@=branchstate@*/
1194 }
1195
1196 DIR * Opendir(const char * path)
1197 {
1198     const char * lpath;
1199     int ut = urlPath(path, &lpath);
1200
1201 if (_rpmio_debug)
1202 fprintf(stderr, "*** Opendir(%s)\n", path);
1203     switch (ut) {
1204     case URL_IS_FTP:            /* XXX WRONG WRONG WRONG */
1205     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
1206     case URL_IS_PATH:
1207         path = lpath;
1208         /*@fallthrough@*/
1209     case URL_IS_UNKNOWN:
1210         break;
1211     case URL_IS_DASH:
1212     default:
1213         return NULL;
1214         /*@notreached@*/ break;
1215     }
1216     return opendir(path);
1217 }
1218
1219 /*@+voidabstract@*/
1220 struct dirent * Readdir(DIR * dir)
1221 {
1222 if (_rpmio_debug)
1223 fprintf(stderr, "*** Readdir(%p)\n", (void *)dir);
1224     return readdir(dir);
1225 }
1226
1227 int Closedir(DIR * dir)
1228 {
1229 if (_rpmio_debug)
1230 fprintf(stderr, "*** Closedir(%p)\n", (void *)dir);
1231     return closedir(dir);
1232 }
1233 /*@=voidabstract@*/
1234 /*@=type@*/