Main Page Modules Data Structures File List Data Fields Globals Related Pages

# rpmio/rpmrpc.c

Go to the documentation of this file.

    00001
    00005 #include "system.h"
    00006
    00007 #if defined(HAVE_PTHREAD_H) && !defined(__LCLINT__)
    00008 #include <pthread.h>
    00009 #endif
    00010
    00011 #include <rpmio_internal.h>
    00012 #include <popt.h>
    00013 #include "ugid.h"
    00014 #include "debug.h"
    00015
    00016 /*@access FD_t@*/
    00017 /*@access urlinfo@*/
    00018
    00019 /* =============================================================== */
    00020 static int ftpMkdir(const char * path, /*@unused@*/ mode_t mode)
    00021         /*@globals fileSystem, internalState @*/
    00022         /*@modifies fileSystem, internalState @*/
    00023 {
    00024     int rc;
    00025     if ((rc = ftpCmd("MKD", path, NULL)) != 0)
    00026         return rc;
    00027 #if NOTYET
    00028     {   char buf[20];
    00029         sprintf(buf, " 0%o", mode);
    00030         (void) ftpCmd("SITE CHMOD", path, buf);
    00031     }
    00032 #endif
    00033     return rc;
    00034 }
    00035
    00036 static int ftpChdir(const char * path)
    00037         /*@globals fileSystem, internalState @*/
    00038         /*@modifies fileSystem, internalState @*/
    00039 {
    00040     return ftpCmd("CWD", path, NULL);
    00041 }
    00042
    00043 static int ftpRmdir(const char * path)
    00044         /*@globals fileSystem, internalState @*/
    00045         /*@modifies fileSystem, internalState @*/
    00046 {
    00047     return ftpCmd("RMD", path, NULL);
    00048 }
    00049
    00050 static int ftpRename(const char * oldpath, const char * newpath)
    00051         /*@globals fileSystem, internalState @*/
    00052         /*@modifies fileSystem, internalState @*/
    00053 {
    00054     int rc;
    00055     if ((rc = ftpCmd("RNFR", oldpath, NULL)) != 0)
    00056         return rc;
    00057     return ftpCmd("RNTO", newpath, NULL);
    00058 }
    00059
    00060 static int ftpUnlink(const char * path)
    00061         /*@globals fileSystem, internalState @*/
    00062         /*@modifies fileSystem, internalState @*/
    00063 {
    00064     return ftpCmd("DELE", path, NULL);
    00065 }
    00066
    00067 /* =============================================================== */
    00068 /* XXX rebuilddb.c: analogues to mkdir(2)/rmdir(2). */
    00069 int Mkdir (const char * path, mode_t mode)
    00070 {
    00071     const char * lpath;
    00072     int ut = urlPath(path, &lpath);
    00073
    00074     switch (ut) {
    00075     case URL_IS_FTP:
    00076         return ftpMkdir(path, mode);
    00077         /*@notreached@*/ break;
    00078     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
    00079     case URL_IS_PATH:
    00080         path = lpath;
    00081         /*@fallthrough@*/
    00082     case URL_IS_UNKNOWN:
    00083         break;
    00084     case URL_IS_DASH:
    00085     default:
    00086         return -2;
    00087         /*@notreached@*/ break;
    00088     }
    00089     return mkdir(path, mode);
    00090 }
    00091
    00092 int Chdir (const char * path)
    00093 {
    00094     const char * lpath;
    00095     int ut = urlPath(path, &lpath);
    00096
    00097     switch (ut) {
    00098     case URL_IS_FTP:
    00099         return ftpChdir(path);
    00100         /*@notreached@*/ break;
    00101     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
    00102     case URL_IS_PATH:
    00103         path = lpath;
    00104         /*@fallthrough@*/
    00105     case URL_IS_UNKNOWN:
    00106         break;
    00107     case URL_IS_DASH:
    00108     default:
    00109         return -2;
    00110         /*@notreached@*/ break;
    00111     }
    00112     return chdir(path);
    00113 }
    00114
    00115 int Rmdir (const char * path)
    00116 {
    00117     const char * lpath;
    00118     int ut = urlPath(path, &lpath);
    00119
    00120     switch (ut) {
    00121     case URL_IS_FTP:
    00122         return ftpRmdir(path);
    00123         /*@notreached@*/ break;
    00124     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
    00125     case URL_IS_PATH:
    00126         path = lpath;
    00127         /*@fallthrough@*/
    00128     case URL_IS_UNKNOWN:
    00129         break;
    00130     case URL_IS_DASH:
    00131     default:
    00132         return -2;
    00133         /*@notreached@*/ break;
    00134     }
    00135     return rmdir(path);
    00136 }
    00137
    00138 /* XXX rpmdb.c: analogue to rename(2). */
    00139
    00140 int Rename (const char * oldpath, const char * newpath)
    00141 {
    00142     const char *oe = NULL;
    00143     const char *ne = NULL;
    00144     int oldut, newut;
    00145
    00146     /* XXX lib/install.c used to rely on this behavior. */
    00147     if (!strcmp(oldpath, newpath)) return 0;
    00148
    00149     oldut = urlPath(oldpath, &oe);
    00150     switch (oldut) {
    00151     case URL_IS_FTP:            /* XXX WRONG WRONG WRONG */
    00152     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
    00153     case URL_IS_PATH:
    00154     case URL_IS_UNKNOWN:
    00155         break;
    00156     case URL_IS_DASH:
    00157     default:
    00158         return -2;
    00159         /*@notreached@*/ break;
    00160     }
    00161
    00162     newut = urlPath(newpath, &ne);
    00163     switch (newut) {
    00164     case URL_IS_FTP:
    00165 if (_rpmio_debug)
    00166 fprintf(stderr, "*** rename old %*s new %*s\n", (int)(oe - oldpath), oldpath, (int)(ne - newpath), newpath);
    00167         if (!(oldut == newut && oe && ne && (oe - oldpath) == (ne - newpath) &&
    00168             !xstrncasecmp(oldpath, newpath, (oe - oldpath))))
    00169             return -2;
    00170         return ftpRename(oldpath, newpath);
    00171         /*@notreached@*/ break;
    00172     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
    00173     case URL_IS_PATH:
    00174         oldpath = oe;
    00175         newpath = ne;
    00176         break;
    00177     case URL_IS_UNKNOWN:
    00178         break;
    00179     case URL_IS_DASH:
    00180     default:
    00181         return -2;
    00182         /*@notreached@*/ break;
    00183     }
    00184     return rename(oldpath, newpath);
    00185 }
    00186
    00187 int Link (const char * oldpath, const char * newpath)
    00188 {
    00189     const char *oe = NULL;
    00190     const char *ne = NULL;
    00191     int oldut, newut;
    00192
    00193     oldut = urlPath(oldpath, &oe);
    00194     switch (oldut) {
    00195     case URL_IS_FTP:            /* XXX WRONG WRONG WRONG */
    00196     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
    00197     case URL_IS_PATH:
    00198     case URL_IS_UNKNOWN:
    00199         break;
    00200     case URL_IS_DASH:
    00201     default:
    00202         return -2;
    00203         /*@notreached@*/ break;
    00204     }
    00205
    00206     newut = urlPath(newpath, &ne);
    00207     switch (newut) {
    00208     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
    00209     case URL_IS_FTP:            /* XXX WRONG WRONG WRONG */
    00210     case URL_IS_PATH:
    00211 if (_rpmio_debug)
    00212 fprintf(stderr, "*** link old %*s new %*s\n", (int)(oe - oldpath), oldpath, (int)(ne - newpath), newpath);
    00213         if (!(oldut == newut && oe && ne && (oe - oldpath) == (ne - newpath) &&
    00214             !xstrncasecmp(oldpath, newpath, (oe - oldpath))))
    00215             return -2;
    00216         oldpath = oe;
    00217         newpath = ne;
    00218         break;
    00219     case URL_IS_UNKNOWN:
    00220         break;
    00221     case URL_IS_DASH:
    00222     default:
    00223         return -2;
    00224         /*@notreached@*/ break;
    00225     }
    00226     return link(oldpath, newpath);
    00227 }
    00228
    00229 /* XXX build/build.c: analogue to unlink(2). */
    00230
    00231 int Unlink(const char * path) {
    00232     const char * lpath;
    00233     int ut = urlPath(path, &lpath);
    00234
    00235     switch (ut) {
    00236     case URL_IS_FTP:
    00237         return ftpUnlink(path);
    00238         /*@notreached@*/ break;
    00239     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
    00240     case URL_IS_PATH:
    00241         path = lpath;
    00242         /*@fallthrough@*/
    00243     case URL_IS_UNKNOWN:
    00244         break;
    00245     case URL_IS_DASH:
    00246     default:
    00247         return -2;
    00248         /*@notreached@*/ break;
    00249     }
    00250     return unlink(path);
    00251 }
    00252
    00253 /* XXX swiped from mc-4.5.39-pre9 vfs/ftpfs.c */
    00254
    00255 #define g_strdup        xstrdup
    00256 #define g_free          free
    00257
    00258 /*
    00259  * FIXME: this is broken. It depends on mc not crossing border on month!
    00260  */
    00261 /*@unchecked@*/
    00262 static int current_mday;
    00263 /*@unchecked@*/
    00264 static int current_mon;
    00265 /*@unchecked@*/
    00266 static int current_year;
    00267
    00268 /* Following stuff (parse_ls_lga) is used by ftpfs and extfs */
    00269 #define MAXCOLS         30
    00270
    00271 /*@unchecked@*/
    00272 static char *columns [MAXCOLS]; /* Points to the string in column n */
    00273 /*@unchecked@*/
    00274 static int   column_ptr [MAXCOLS]; /* Index from 0 to the starting positions of the columns */
    00275
    00276 /*@-boundswrite@*/
    00277 static int
    00278 vfs_split_text (char *p)
    00279         /*@globals columns, column_ptr @*/
    00280         /*@modifies *p, columns, column_ptr @*/
    00281 {
    00282     char *original = p;
    00283     int  numcols;
    00284
    00285
    00286     for (numcols = 0; *p && numcols < MAXCOLS; numcols++){
    00287         while (*p == ' ' || *p == '\r' || *p == '\n'){
    00288             *p = 0;
    00289             p++;
    00290         }
    00291         columns [numcols] = p;
    00292         column_ptr [numcols] = p - original;
    00293         while (*p && *p != ' ' && *p != '\r' && *p != '\n')
    00294             p++;
    00295     }
    00296     return numcols;
    00297 }
    00298 /*@=boundswrite@*/
    00299
    00300 /*@-boundsread@*/
    00301 static int
    00302 is_num (int idx)
    00303         /*@*/
    00304 {
    00305     if (!columns [idx] || columns [idx][0] < '0' || columns [idx][0] > '9')
    00306         return 0;
    00307     return 1;
    00308 }
    00309 /*@=boundsread@*/
    00310
    00311 /*@-boundsread@*/
    00312 static int
    00313 is_dos_date(/*@null@*/ const char *str)
    00314         /*@*/
    00315 {
    00316     if (str != NULL && strlen(str) == 8 &&
    00317                 str[2] == str[5] && strchr("\\-/", (int)str[2]) != NULL)
    00318         return 1;
    00319     return 0;
    00320 }
    00321 /*@=boundsread@*/
    00322
    00323 static int
    00324 is_week (/*@null@*/ const char * str, /*@out@*/ struct tm * tim)
    00325         /*@modifies *tim @*/
    00326 {
    00327 /*@observer@*/ static const char * week = "SunMonTueWedThuFriSat";
    00328     const char * pos;
    00329
    00330     /*@-observertrans -mayaliasunique@*/
    00331     if (str != NULL && (pos=strstr(week, str)) != NULL) {
    00332     /*@=observertrans =mayaliasunique@*/
    00333         if (tim != NULL)
    00334             tim->tm_wday = (pos - week)/3;
    00335         return 1;
    00336     }
    00337     return 0;
    00338 }
    00339
    00340 static int
    00341 is_month (/*@null@*/ const char * str, /*@out@*/ struct tm * tim)
    00342         /*@modifies *tim @*/
    00343 {
    00344 /*@observer@*/ static const char * month = "JanFebMarAprMayJunJulAugSepOctNovDec";
    00345     const char * pos;
    00346
    00347     /*@-observertrans -mayaliasunique@*/
    00348     if (str != NULL && (pos = strstr(month, str)) != NULL) {
    00349     /*@=observertrans -mayaliasunique@*/
    00350         if (tim != NULL)
    00351             tim->tm_mon = (pos - month)/3;
    00352         return 1;
    00353     }
    00354     return 0;
    00355 }
    00356
    00357 static int
    00358 is_time (/*@null@*/ const char * str, /*@out@*/ struct tm * tim)
    00359         /*@modifies *tim @*/
    00360 {
    00361     const char * p, * p2;
    00362
    00363     if (str != NULL && (p = strchr(str, ':')) && (p2 = strrchr(str, ':'))) {
    00364         if (p != p2) {
    00365             if (sscanf (str, "%2d:%2d:%2d", &tim->tm_hour, &tim->tm_min, &tim->tm_sec) != 3)
    00366                 return 0;
    00367         } else {
    00368             if (sscanf (str, "%2d:%2d", &tim->tm_hour, &tim->tm_min) != 2)
    00369                 return 0;
    00370         }
    00371     } else
    00372         return 0;
    00373
    00374     return 1;
    00375 }
    00376
    00377 static int is_year(/*@null@*/ const char * str, /*@out@*/ struct tm * tim)
    00378         /*@modifies *tim @*/
    00379 {
    00380     long year;
    00381
    00382     if (str == NULL)
    00383         return 0;
    00384
    00385     if (strchr(str,':'))
    00386         return 0;
    00387
    00388     if (strlen(str) != 4)
    00389         return 0;
    00390
    00391     if (sscanf(str, "%ld", &year) != 1)
    00392         return 0;
    00393
    00394     if (year < 1900 || year > 3000)
    00395         return 0;
    00396
    00397     tim->tm_year = (int) (year - 1900);
    00398
    00399     return 1;
    00400 }
    00401
    00402 /*
    00403  * FIXME: this is broken. Consider following entry:
    00404  * -rwx------   1 root     root            1 Aug 31 10:04 2904 1234
    00405  * where "2904 1234" is filename. Well, this code decodes it as year :-(.
    00406  */
    00407
    00408 static int
    00409 vfs_parse_filetype (char c)
    00410         /*@*/
    00411 {
    00412     switch (c) {
    00413         case 'd': return S_IFDIR;
    00414         case 'b': return S_IFBLK;
    00415         case 'c': return S_IFCHR;
    00416         case 'l': return S_IFLNK;
    00417         case 's':
    00418 #ifdef IS_IFSOCK /* And if not, we fall through to IFIFO, which is pretty close */
    00419                   return S_IFSOCK;
    00420 #endif
    00421         case 'p': return S_IFIFO;
    00422         case 'm': case 'n':             /* Don't know what these are :-) */
    00423         case '-': case '?': return S_IFREG;
    00424         default: return -1;
    00425     }
    00426 }
    00427
    00428 static int vfs_parse_filemode (const char *p)
    00429         /*@*/
    00430 {       /* converts rw-rw-rw- into 0666 */
    00431     int res = 0;
    00432     switch (*(p++)) {
    00433         case 'r': res |= 0400; break;
    00434         case '-': break;
    00435         default: return -1;
    00436     }
    00437     switch (*(p++)) {
    00438         case 'w': res |= 0200; break;
    00439         case '-': break;
    00440         default: return -1;
    00441     }
    00442     switch (*(p++)) {
    00443         case 'x': res |= 0100; break;
    00444         case 's': res |= 0100 | S_ISUID; break;
    00445         case 'S': res |= S_ISUID; break;
    00446         case '-': break;
    00447         default: return -1;
    00448     }
    00449     switch (*(p++)) {
    00450         case 'r': res |= 0040; break;
    00451         case '-': break;
    00452         default: return -1;
    00453     }
    00454     switch (*(p++)) {
    00455         case 'w': res |= 0020; break;
    00456         case '-': break;
    00457         default: return -1;
    00458     }
    00459     switch (*(p++)) {
    00460         case 'x': res |= 0010; break;
    00461         case 's': res |= 0010 | S_ISGID; break;
    00462         case 'l': /* Solaris produces these */
    00463         case 'S': res |= S_ISGID; break;
    00464         case '-': break;
    00465         default: return -1;
    00466     }
    00467     switch (*(p++)) {
    00468         case 'r': res |= 0004; break;
    00469         case '-': break;
    00470         default: return -1;
    00471     }
    00472     switch (*(p++)) {
    00473         case 'w': res |= 0002; break;
    00474         case '-': break;
    00475         default: return -1;
    00476     }
    00477     switch (*(p++)) {
    00478         case 'x': res |= 0001; break;
    00479         case 't': res |= 0001 | S_ISVTX; break;
    00480         case 'T': res |= S_ISVTX; break;
    00481         case '-': break;
    00482         default: return -1;
    00483     }
    00484     return res;
    00485 }
    00486
    00487 /*@-boundswrite@*/
    00488 static int vfs_parse_filedate(int idx, /*@out@*/ time_t *t)
    00489         /*@modifies *t @*/
    00490 {       /* This thing parses from idx in columns[] array */
    00491
    00492     char *p;
    00493     struct tm tim;
    00494     int d[3];
    00495     int got_year = 0;
    00496
    00497     /* Let's setup default time values */
    00498     tim.tm_year = current_year;
    00499     tim.tm_mon  = current_mon;
    00500     tim.tm_mday = current_mday;
    00501     tim.tm_hour = 0;
    00502     tim.tm_min  = 0;
    00503     tim.tm_sec  = 0;
    00504     tim.tm_isdst = -1; /* Let mktime() try to guess correct dst offset */
    00505
    00506     p = columns [idx++];
    00507
    00508     /* We eat weekday name in case of extfs */
    00509     if(is_week(p, &tim))
    00510         p = columns [idx++];
    00511
    00512     /* Month name */
    00513     if(is_month(p, &tim)){
    00514         /* And we expect, it followed by day number */
    00515         if (is_num (idx))
    00516             tim.tm_mday = (int)atol (columns [idx++]);
    00517         else
    00518             return 0; /* No day */
    00519
    00520     } else {
    00521         /* We usually expect:
    00522            Mon DD hh:mm
    00523            Mon DD  YYYY
    00524            But in case of extfs we allow these date formats:
    00525            Mon DD YYYY hh:mm
    00526            Mon DD hh:mm YYYY
    00527            Wek Mon DD hh:mm:ss YYYY
    00528            MM-DD-YY hh:mm
    00529            where Mon is Jan-Dec, DD, MM, YY two digit day, month, year,
    00530            YYYY four digit year, hh, mm, ss two digit hour, minute or second. */
    00531
    00532         /* Here just this special case with MM-DD-YY */
    00533         if (is_dos_date(p)){
    00534             /*@-mods@*/
    00535             p[2] = p[5] = '-';
    00536             /*@=mods@*/
    00537
    00538             memset(d, 0, sizeof(d));
    00539             if (sscanf(p, "%2d-%2d-%2d", &d[0], &d[1], &d[2]) == 3){
    00540             /*  We expect to get:
    00541                 1. MM-DD-YY
    00542                 2. DD-MM-YY
    00543                 3. YY-MM-DD
    00544                 4. YY-DD-MM  */
    00545
    00546                 /* Hmm... maybe, next time :)*/
    00547
    00548                 /* At last, MM-DD-YY */
    00549                 d[0]--; /* Months are zerobased */
    00550                 /* Y2K madness */
    00551                 if(d[2] < 70)
    00552                     d[2] += 100;
    00553
    00554                 tim.tm_mon  = d[0];
    00555                 tim.tm_mday = d[1];
    00556                 tim.tm_year = d[2];
    00557                 got_year = 1;
    00558             } else
    00559                 return 0; /* sscanf failed */
    00560         } else
    00561             return 0; /* unsupported format */
    00562     }
    00563
    00564     /* Here we expect to find time and/or year */
    00565
    00566     if (is_num (idx)) {
    00567         if(is_time(columns[idx], &tim) || (got_year = is_year(columns[idx], &tim))) {
    00568         idx++;
    00569
    00570         /* This is a special case for ctime() or Mon DD YYYY hh:mm */
    00571         if(is_num (idx) &&
    00572             ((got_year = is_year(columns[idx], &tim)) || is_time(columns[idx], &tim)))
    00573                 idx++; /* time & year or reverse */
    00574         } /* only time or date */
    00575     }
    00576     else
    00577         return 0; /* Nor time or date */
    00578
    00579     /*
    00580      * If the date is less than 6 months in the past, it is shown without year
    00581      * other dates in the past or future are shown with year but without time
    00582      * This does not check for years before 1900 ... I don't know, how
    00583      * to represent them at all
    00584      */
    00585     if (!got_year &&
    00586         current_mon < 6 && current_mon < tim.tm_mon &&
    00587         tim.tm_mon - current_mon >= 6)
    00588
    00589         tim.tm_year--;
    00590
    00591     if ((*t = mktime(&tim)) < 0)
    00592         *t = 0;
    00593     return idx;
    00594 }
    00595 /*@=boundswrite@*/
    00596
    00597 /*@-boundswrite@*/
    00598 static int
    00599 vfs_parse_ls_lga (char * p, /*@out@*/ struct stat * st,
    00600                 /*@out@*/ const char ** filename,
    00601                 /*@out@*/ const char ** linkname)
    00602         /*@modifies *p, *st, *filename, *linkname @*/
    00603 {
    00604     int idx, idx2, num_cols;
    00605     int i;
    00606     char *p_copy;
    00607
    00608     if (strncmp (p, "total", 5) == 0)
    00609         return 0;
    00610
    00611     p_copy = g_strdup(p);
    00612 /* XXX FIXME: parse out inode number from "NLST -lai ." */
    00613 /* XXX FIXME: parse out sizein blocks from "NLST -lais ." */
    00614
    00615     if ((i = vfs_parse_filetype(*(p++))) == -1)
    00616         goto error;
    00617
    00618     st->st_mode = i;
    00619     if (*p == ' ')      /* Notwell 4 */
    00620         p++;
    00621     if (*p == '['){
    00622         if (strlen (p) <= 8 || p [8] != ']')
    00623             goto error;
    00624         /* Should parse here the Notwell permissions :) */
    00625         /*@-unrecog@*/
    00626         if (S_ISDIR (st->st_mode))
    00627             st->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IXUSR | S_IXGRP | S_IXOTH);
    00628         else
    00629             st->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);
    00630         p += 9;
    00631         /*@=unrecog@*/
    00632     } else {
    00633         if ((i = vfs_parse_filemode(p)) == -1)
    00634             goto error;
    00635         st->st_mode |= i;
    00636         p += 9;
    00637
    00638         /* This is for an extra ACL attribute (HP-UX) */
    00639         if (*p == '+')
    00640             p++;
    00641     }
    00642
    00643     g_free(p_copy);
    00644     p_copy = g_strdup(p);
    00645     num_cols = vfs_split_text (p);
    00646
    00647     st->st_nlink = atol (columns [0]);
    00648     if (st->st_nlink < 0)
    00649         goto error;
    00650
    00651     if (!is_num (1))
    00652 #ifdef  HACK
    00653         st->st_uid = finduid (columns [1]);
    00654 #else
    00655         (void) unameToUid (columns [1], &st->st_uid);
    00656 #endif
    00657     else
    00658         st->st_uid = (uid_t) atol (columns [1]);
    00659
    00660     /* Mhm, the ls -lg did not produce a group field */
    00661     for (idx = 3; idx <= 5; idx++)
    00662         if (is_month(columns [idx], NULL) || is_week(columns [idx], NULL) || is_dos_date(columns[idx]))
    00663             break;
    00664
    00665     if (idx == 6 || (idx == 5 && !S_ISCHR (st->st_mode) && !S_ISBLK (st->st_mode)))
    00666         goto error;
    00667
    00668     /* We don't have gid */
    00669     if (idx == 3 || (idx == 4 && (S_ISCHR(st->st_mode) || S_ISBLK (st->st_mode))))
    00670         idx2 = 2;
    00671     else {
    00672         /* We have gid field */
    00673         if (is_num (2))
    00674             st->st_gid = (gid_t) atol (columns [2]);
    00675         else
    00676 #ifdef  HACK
    00677             st->st_gid = findgid (columns [2]);
    00678 #else
    00679             (void) gnameToGid (columns [1], &st->st_gid);
    00680 #endif
    00681         idx2 = 3;
    00682     }
    00683
    00684     /* This is device */
    00685     if (S_ISCHR (st->st_mode) || S_ISBLK (st->st_mode)){
    00686         int maj, min;
    00687
    00688         if (!is_num (idx2) || sscanf(columns [idx2], " %d,", &maj) != 1)
    00689             goto error;
    00690
    00691         if (!is_num (++idx2) || sscanf(columns [idx2], " %d", &min) != 1)
    00692             goto error;
    00693
    00694 #ifdef HAVE_ST_RDEV
    00695         st->st_rdev = ((maj & 0xff) << 8) | (min & 0xffff00ff);
    00696 #endif
    00697         st->st_size = 0;
    00698
    00699     } else {
    00700         /* Common file size */
    00701         if (!is_num (idx2))
    00702             goto error;
    00703
    00704         st->st_size = (size_t) atol (columns [idx2]);
    00705 #ifdef HAVE_ST_RDEV
    00706         st->st_rdev = 0;
    00707 #endif
    00708     }
    00709
    00710     idx = vfs_parse_filedate(idx, &st->st_mtime);
    00711     if (!idx)
    00712         goto error;
    00713     /* Use resulting time value */
    00714     st->st_atime = st->st_ctime = st->st_mtime;
    00715     st->st_dev = 0;
    00716     st->st_ino = 0;
    00717 #ifdef HAVE_ST_BLKSIZE
    00718     st->st_blksize = 512;
    00719 #endif
    00720 #ifdef HAVE_ST_BLOCKS
    00721     st->st_blocks = (st->st_size + 511) / 512;
    00722 #endif
    00723
    00724     for (i = idx + 1, idx2 = 0; i < num_cols; i++ )
    00725         if (strcmp (columns [i], "->") == 0){
    00726             idx2 = i;
    00727             break;
    00728         }
    00729
    00730     if (((S_ISLNK (st->st_mode) ||
    00731         (num_cols == idx + 3 && st->st_nlink > 1))) /* Maybe a hardlink? (in extfs) */
    00732         && idx2){
    00733         int tlen;
    00734         char *t;
    00735
    00736         if (filename){
    00737 #ifdef HACK
    00738             t = g_strndup (p_copy + column_ptr [idx], column_ptr [idx2] - column_ptr [idx] - 1);
    00739 #else
    00740             int nb = column_ptr [idx2] - column_ptr [idx] - 1;
    00741             t = xmalloc(nb+1);
    00742             strncpy(t, p_copy + column_ptr [idx], nb);
    00743 #endif
    00744             *filename = t;
    00745         }
    00746         if (linkname){
    00747             t = g_strdup (p_copy + column_ptr [idx2+1]);
    00748             tlen = strlen (t);
    00749             if (t [tlen-1] == '\r' || t [tlen-1] == '\n')
    00750                 t [tlen-1] = 0;
    00751             if (t [tlen-2] == '\r' || t [tlen-2] == '\n')
    00752                 t [tlen-2] = 0;
    00753
    00754             *linkname = t;
    00755         }
    00756     } else {
    00757         /* Extract the filename from the string copy, not from the columns
    00758          * this way we have a chance of entering hidden directories like ". ."
    00759          */
    00760         if (filename){
    00761             /*
    00762             *filename = g_strdup (columns [idx++]);
    00763             */
    00764             int tlen;
    00765             char *t;
    00766
    00767             t = g_strdup (p_copy + column_ptr [idx]); idx++;
    00768             tlen = strlen (t);
    00769             /* g_strchomp(); */
    00770             if (t [tlen-1] == '\r' || t [tlen-1] == '\n')
    00771                 t [tlen-1] = 0;
    00772             if (t [tlen-2] == '\r' || t [tlen-2] == '\n')
    00773                 t [tlen-2] = 0;
    00774
    00775             *filename = t;
    00776         }
    00777         if (linkname)
    00778             *linkname = NULL;
    00779     }
    00780     g_free (p_copy);
    00781     return 1;
    00782
    00783 error:
    00784 #ifdef  HACK
    00785     {
    00786       static int errorcount = 0;
    00787
    00788       if (++errorcount < 5) {
    00789         message_1s (1, "Could not parse:", p_copy);
    00790       } else if (errorcount == 5)
    00791         message_1s (1, "More parsing errors will be ignored.", "(sorry)" );
    00792     }
    00793 #endif
    00794
    00795     /*@-usereleased@*/
    00796     if (p_copy != p)            /* Carefull! */
    00797     /*@=usereleased@*/
    00798         g_free (p_copy);
    00799     return 0;
    00800 }
    00801 /*@=boundswrite@*/
    00802
    00803 typedef enum {
    00804         DO_FTP_STAT     = 1,
    00805         DO_FTP_LSTAT    = 2,
    00806         DO_FTP_READLINK = 3,
    00807         DO_FTP_ACCESS   = 4,
    00808         DO_FTP_GLOB     = 5
    00809 } ftpSysCall_t;
    00810
    00813 /*@unchecked@*/
    00814 static size_t ftpBufAlloced = 0;
    00815
    00818 /*@unchecked@*/
    00819 static /*@only@*/ char * ftpBuf = NULL;
    00820
    00821 #define alloca_strdup(_s)       strcpy(alloca(strlen(_s)+1), (_s))
    00822
    00823 /*@-boundswrite@*/
    00824 /*@-mods@*/
    00825 static int ftpNLST(const char * url, ftpSysCall_t ftpSysCall,
    00826                 /*@out@*/ /*@null@*/ struct stat * st,
    00827                 /*@out@*/ /*@null@*/ char * rlbuf, size_t rlbufsiz)
    00828         /*@globals fileSystem, internalState @*/
    00829         /*@modifies *st, *rlbuf, fileSystem, internalState @*/
    00830 {
    00831     FD_t fd;
    00832     const char * path;
    00833     int bufLength, moretodo;
    00834     const char *n, *ne, *o, *oe;
    00835     char * s;
    00836     char * se;
    00837     const char * urldn;
    00838     char * bn = NULL;
    00839     int nbn = 0;
    00840     urlinfo u;
    00841     int rc;
    00842
    00843     n = ne = o = oe = NULL;
    00844     (void) urlPath(url, &path);
    00845     if (*path == '\0')
    00846         return -2;
    00847
    00848     switch (ftpSysCall) {
    00849     case DO_FTP_GLOB:
    00850         fd = ftpOpen(url, 0, 0, &u);
    00851         if (fd == NULL || u == NULL)
    00852             return -1;
    00853
    00854         u->openError = ftpReq(fd, "LIST", path);
    00855         break;
    00856     default:
    00857         urldn = alloca_strdup(url);
    00858         /*@-branchstate@*/
    00859         if ((bn = strrchr(urldn, '/')) == NULL)
    00860             return -2;
    00861         else if (bn == path)
    00862             bn = ".";
    00863         else
    00864             *bn++ = '\0';
    00865         /*@=branchstate@*/
    00866         nbn = strlen(bn);
    00867
    00868         rc = ftpChdir(urldn);           /* XXX don't care about CWD */
    00869         if (rc < 0)
    00870             return rc;
    00871
    00872         fd = ftpOpen(url, 0, 0, &u);
    00873         if (fd == NULL || u == NULL)
    00874             return -1;
    00875
    00876         /* XXX possibly should do "NLST -lais" to get st_ino/st_blocks also */
    00877         u->openError = ftpReq(fd, "NLST", "-la");
    00878
    00879         if (bn == NULL || nbn <= 0) {
    00880             rc = -2;
    00881             goto exit;
    00882         }
    00883         break;
    00884     }
    00885
    00886     if (u->openError < 0) {
    00887         fd = fdLink(fd, "error data (ftpStat)");
    00888         rc = -2;
    00889         goto exit;
    00890     }
    00891
    00892     if (ftpBufAlloced == 0 || ftpBuf == NULL) {
    00893         ftpBufAlloced = _url_iobuf_size;
    00894         ftpBuf = xcalloc(ftpBufAlloced, sizeof(ftpBuf[0]));
    00895     }
    00896     *ftpBuf = '\0';
    00897
    00898     bufLength = 0;
    00899     moretodo = 1;
    00900
    00901     do {
    00902
    00903         /* XXX FIXME: realloc ftpBuf if < ~128 chars remain */
    00904         if ((ftpBufAlloced - bufLength) < (1024+80)) {
    00905             ftpBufAlloced <<= 2;
    00906             ftpBuf = xrealloc(ftpBuf, ftpBufAlloced);
    00907         }
    00908         s = se = ftpBuf + bufLength;
    00909         *se = '\0';
    00910
    00911         rc = fdFgets(fd, se, (ftpBufAlloced - bufLength));
    00912         if (rc <= 0) {
    00913             moretodo = 0;
    00914             break;
    00915         }
    00916         if (ftpSysCall == DO_FTP_GLOB) {        /* XXX HACK */
    00917             bufLength += strlen(se);
    00918             continue;
    00919         }
    00920
    00921         for (s = se; *s != '\0'; s = se) {
    00922             int bingo;
    00923
    00924             while (*se && *se != '\n') se++;
    00925             if (se > s && se[-1] == '\r') se[-1] = '\0';
    00926             if (*se == '\0')
    00927                 /*@innerbreak@*/ break;
    00928             *se++ = '\0';
    00929
    00930             if (!strncmp(s, "total ", sizeof("total ")-1))
    00931                 /*@innercontinue@*/ continue;
    00932
    00933             o = NULL;
    00934             for (bingo = 0, n = se; n >= s; n--) {
    00935                 switch (*n) {
    00936                 case '\0':
    00937                     oe = ne = n;
    00938                     /*@switchbreak@*/ break;
    00939                 case ' ':
    00940                     if (o || !(n[-3] == ' ' && n[-2] == '-' && n[-1] == '>')) {
    00941                         while (*(++n) == ' ')
    00942                             {};
    00943                         bingo++;
    00944                         /*@switchbreak@*/ break;
    00945                     }
    00946                     for (o = n + 1; *o == ' '; o++)
    00947                         {};
    00948                     n -= 3;
    00949                     ne = n;
    00950                     /*@switchbreak@*/ break;
    00951                 default:
    00952                     /*@switchbreak@*/ break;
    00953                 }
    00954                 if (bingo)
    00955                     /*@innerbreak@*/ break;
    00956             }
    00957
    00958             if (nbn != (ne - n))        /* Same name length? */
    00959                 /*@innercontinue@*/ continue;
    00960             if (strncmp(n, bn, nbn))    /* Same name? */
    00961                 /*@innercontinue@*/ continue;
    00962
    00963             moretodo = 0;
    00964             /*@innerbreak@*/ break;
    00965         }
    00966
    00967         if (moretodo && se > s) {
    00968             bufLength = se - s - 1;
    00969             if (s != ftpBuf)
    00970                 memmove(ftpBuf, s, bufLength);
    00971         } else {
    00972             bufLength = 0;
    00973         }
    00974     } while (moretodo);
    00975
    00976     switch (ftpSysCall) {
    00977     case DO_FTP_STAT:
    00978         if (o && oe) {
    00979             /* XXX FIXME: symlink, replace urldn/bn from [o,oe) and restart */
    00980         }
    00981         /*@fallthrough@*/
    00982     case DO_FTP_LSTAT:
    00983         if (st == NULL || !(n && ne)) {
    00984             rc = -1;
    00985         } else {
    00986             rc = ((vfs_parse_ls_lga(s, st, NULL, NULL) > 0) ? 0 : -1);
    00987         }
    00988         break;
    00989     case DO_FTP_READLINK:
    00990         if (rlbuf == NULL || !(o && oe)) {
    00991             rc = -1;
    00992         } else {
    00993             rc = oe - o;
    00994             if (rc > rlbufsiz)
    00995                 rc = rlbufsiz;
    00996             memcpy(rlbuf, o, rc);
    00997             if (rc < rlbufsiz)
    00998                 rlbuf[rc] = '\0';
    00999         }
    01000         break;
    01001     case DO_FTP_ACCESS:
    01002         rc = 0;         /* XXX WRONG WRONG WRONG */
    01003         break;
    01004     case DO_FTP_GLOB:
    01005         rc = 0;         /* XXX WRONG WRONG WRONG */
    01006         break;
    01007     }
    01008
    01009 exit:
    01010     (void) ufdClose(fd);
    01011     return rc;
    01012 }
    01013 /*@=mods@*/
    01014 /*@=boundswrite@*/
    01015
    01016 static const char * statstr(const struct stat * st,
    01017                 /*@returned@*/ /*@out@*/ char * buf)
    01018         /*@modifies *buf @*/
    01019 {
    01020     sprintf(buf,
    01021         "*** dev %x ino %x mode %0o nlink %d uid %d gid %d rdev %x size %x\n",
    01022         (unsigned)st->st_dev,
    01023         (unsigned)st->st_ino,
    01024         st->st_mode,
    01025         st->st_nlink,
    01026         st->st_uid,
    01027         st->st_gid,
    01028         (unsigned)st->st_rdev,
    01029         (unsigned)st->st_size);
    01030     return buf;
    01031 }
    01032
    01033 /*@unchecked@*/
    01034 static int ftp_st_ino = 0xdead0000;
    01035
    01036 static int ftpStat(const char * path, /*@out@*/ struct stat *st)
    01037         /*@globals fileSystem, internalState @*/
    01038         /*@modifies *st, fileSystem, internalState @*/
    01039 {
    01040     char buf[1024];
    01041     int rc;
    01042     rc = ftpNLST(path, DO_FTP_STAT, st, NULL, 0);
    01043     /* XXX fts(3) needs/uses st_ino, make something up for now. */
    01044     /*@-mods@*/
    01045     if (st->st_ino == 0)
    01046         st->st_ino = ftp_st_ino++;
    01047     /*@=mods@*/
    01048 if (_ftp_debug)
    01049 fprintf(stderr, "*** ftpStat(%s) rc %d\n%s", path, rc, statstr(st, buf));
    01050     return rc;
    01051 }
    01052
    01053 static int ftpLstat(const char * path, /*@out@*/ struct stat *st)
    01054         /*@globals fileSystem, internalState @*/
    01055         /*@modifies *st, fileSystem, internalState @*/
    01056 {
    01057     char buf[1024];
    01058     int rc;
    01059     rc = ftpNLST(path, DO_FTP_LSTAT, st, NULL, 0);
    01060     /* XXX fts(3) needs/uses st_ino, make something up for now. */
    01061     /*@-mods@*/
    01062     if (st->st_ino == 0)
    01063         st->st_ino = ftp_st_ino++;
    01064     /*@=mods@*/
    01065 if (_ftp_debug)
    01066 fprintf(stderr, "*** ftpLstat(%s) rc %d\n%s\n", path, rc, statstr(st, buf));
    01067     return rc;
    01068 }
    01069
    01070 static int ftpReadlink(const char * path, /*@out@*/ char * buf, size_t bufsiz)
    01071         /*@globals fileSystem, internalState @*/
    01072         /*@modifies *buf, fileSystem, internalState @*/
    01073 {
    01074     int rc;
    01075     rc = ftpNLST(path, DO_FTP_READLINK, NULL, buf, bufsiz);
    01076 if (_ftp_debug)
    01077 fprintf(stderr, "*** ftpReadlink(%s) rc %d\n", path, rc);
    01078     return rc;
    01079 }
    01080
    01081 struct __dirstream {
    01082     int fd;                     /* File descriptor.  */
    01083     char * data;                /* Directory block.  */
    01084     size_t allocation;          /* Space allocated for the block.  */
    01085     size_t size;                /* Total valid data in the block.  */
    01086     size_t offset;              /* Current offset into the block.  */
    01087     off_t filepos;              /* Position of next entry to read.  */
    01088 #if defined(HAVE_PTHREAD_H) && !defined(__LCLINT__)
    01089     pthread_mutex_t lock;       /* Mutex lock for this structure.  */
    01090 #endif
    01091 };
    01092
    01093 #if !defined(DT_DIR)
    01094 # define DT_UNKNOWN     0
    01095 # define DT_FIFO        1
    01096 # define DT_CHR         2
    01097 # define DT_DIR         4
    01098 # define DT_BLK         6
    01099 # define DT_REG         8
    01100 # define DT_LNK         10
    01101 # define DT_SOCK        12
    01102 # define DT_WHT         14
    01103 typedef struct __dirstream *    FTPDIR;
    01104 #else
    01105 typedef DIR *                   FTPDIR;
    01106 #endif
    01107
    01108 /*@unchecked@*/
    01109 static int ftpmagicdir = 0x8440291;
    01110 #define ISFTPMAGIC(_dir) (!memcmp((_dir), &ftpmagicdir, sizeof(ftpmagicdir)))
    01111
    01112 /*@-boundswrite@*/
    01113 /*@-type@*/ /* FIX: abstract DIR */
    01114 /*@null@*/
    01115 static DIR * ftpOpendir(const char * path)
    01116         /*@globals fileSystem, internalState @*/
    01117         /*@modifies fileSystem, internalState @*/
    01118 {
    01119     FTPDIR mydir;
    01120     struct dirent * dp;
    01121     size_t nb;
    01122     const char * s, * sb, * se;
    01123     const char ** av;
    01124     unsigned char * dt;
    01125     char * t;
    01126     int ac;
    01127     int c;
    01128     int rc;
    01129
    01130 if (_ftp_debug)
    01131 fprintf(stderr, "*** ftpOpendir(%s)\n", path);
    01132     rc = ftpNLST(path, DO_FTP_GLOB, NULL, NULL, 0);
    01133     if (rc)
    01134         return NULL;
    01135
    01136     /*
    01137      * ftpBuf now contains absolute paths, newline terminated.
    01138      * Calculate the number of bytes to hold the directory info.
    01139      */
    01140     nb = sizeof(".") + sizeof("..");
    01141     ac = 2;
    01142     sb = NULL;
    01143     s = se = ftpBuf;
    01144     while ((c = *se) != '\0') {
    01145         se++;
    01146         switch (c) {
    01147         case '/':
    01148             sb = se;
    01149             /*@switchbreak@*/ break;
    01150         case '\r':
    01151             if (sb == NULL) {
    01152                 for (sb = se; sb > s && sb[-1] != ' '; sb--)
    01153                     {};
    01154             }
    01155             ac++;
    01156             nb += (se - sb);
    01157
    01158             if (*se == '\n') se++;
    01159             sb = NULL;
    01160             s = se;
    01161             /*@switchbreak@*/ break;
    01162         default:
    01163             /*@switchbreak@*/ break;
    01164         }
    01165     }
    01166
    01167     nb += sizeof(*mydir) + sizeof(*dp) + ((ac + 1) * sizeof(*av)) + (ac + 1);
    01168     mydir = xcalloc(1, nb);
    01169     /*@-abstract@*/
    01170     dp = (struct dirent *) (mydir + 1);
    01171     av = (const char **) (dp + 1);
    01172     dt = (char *) (av + (ac + 1));
    01173     t = (char *) (dt + ac + 1);
    01174     /*@=abstract@*/
    01175
    01176     mydir->fd = ftpmagicdir;
    01177     mydir->data = (char *) dp;
    01178     mydir->allocation = nb;
    01179     mydir->size = ac;
    01180     mydir->offset = -1;
    01181     mydir->filepos = 0;
    01182
    01183     ac = 0;
    01184     /*@-dependenttrans -unrecog@*/
    01185     dt[ac] = DT_DIR;    av[ac++] = t;   t = stpcpy(t, ".");     t++;
    01186     dt[ac] = DT_DIR;    av[ac++] = t;   t = stpcpy(t, "..");    t++;
    01187     /*@=dependenttrans =unrecog@*/
    01188     sb = NULL;
    01189     s = se = ftpBuf;
    01190     while ((c = *se) != '\0') {
    01191         se++;
    01192         switch (c) {
    01193         case '/':
    01194             sb = se;
    01195             /*@switchbreak@*/ break;
    01196         case '\r':
    01197             /*@-dependenttrans@*/
    01198             av[ac] = t;
    01199             /*@=dependenttrans@*/
    01200             if (sb == NULL) {
    01201                 /*@-unrecog@*/
    01202                 switch(*s) {
    01203                 case 'p':
    01204                     dt[ac] = DT_FIFO;
    01205                     /*@innerbreak@*/ break;
    01206                 case 'c':
    01207                     dt[ac] = DT_CHR;
    01208                     /*@innerbreak@*/ break;
    01209                 case 'd':
    01210                     dt[ac] = DT_DIR;
    01211                     /*@innerbreak@*/ break;
    01212                 case 'b':
    01213                     dt[ac] = DT_BLK;
    01214                     /*@innerbreak@*/ break;
    01215                 case '-':
    01216                     dt[ac] = DT_REG;
    01217                     /*@innerbreak@*/ break;
    01218                 case 'l':
    01219                     dt[ac] = DT_LNK;
    01220                     /*@innerbreak@*/ break;
    01221                 case 's':
    01222                     dt[ac] = DT_SOCK;
    01223                     /*@innerbreak@*/ break;
    01224                 default:
    01225                     dt[ac] = DT_UNKNOWN;
    01226                     /*@innerbreak@*/ break;
    01227                 }
    01228                 /*@=unrecog@*/
    01229                 for (sb = se; sb > s && sb[-1] != ' '; sb--)
    01230                     {};
    01231             }
    01232             ac++;
    01233             t = stpncpy(t, sb, (se - sb));
    01234             t[-1] = '\0';
    01235             if (*se == '\n') se++;
    01236             sb = NULL;
    01237             s = se;
    01238             /*@switchbreak@*/ break;
    01239         default:
    01240             /*@switchbreak@*/ break;
    01241         }
    01242     }
    01243     av[ac] = NULL;
    01244
    01245     return (DIR *) mydir;
    01246 }
    01247 /*@=boundswrite@*/
    01248
    01249 /*@dependent@*/ /*@null@*/
    01250 static struct dirent * ftpReaddir(DIR * dir)
    01251         /*@globals fileSystem @*/
    01252         /*@modifies fileSystem @*/
    01253 {
    01254     FTPDIR mydir = (FTPDIR)dir;
    01255     struct dirent * dp;
    01256     const char ** av;
    01257     unsigned char * dt;
    01258     int ac;
    01259     int i;
    01260
    01261     /*@+voidabstract@*/
    01262     if (mydir == NULL || !ISFTPMAGIC(mydir) || mydir->data == NULL) {
    01263         /* XXX TODO: EBADF errno. */
    01264         return NULL;
    01265     }
    01266     /*@=voidabstract@*/
    01267
    01268     dp = (struct dirent *) mydir->data;
    01269     av = (const char **) (dp + 1);
    01270     ac = mydir->size;
    01271     dt = (char *) (av + (ac + 1));
    01272     i = mydir->offset + 1;
    01273
    01274 /*@-boundsread@*/
    01275     if (i < 0 || i >= ac || av[i] == NULL)
    01276         return NULL;
    01277 /*@=boundsread@*/
    01278
    01279     mydir->offset = i;
    01280
    01281     /* XXX glob(3) uses REAL_DIR_ENTRY(dp) test on d_ino */
    01282     dp->d_ino = i + 1;          /* W2DO? */
    01283     dp->d_reclen = 0;           /* W2DO? */
    01284
    01285 #if !defined(hpux)
    01286     dp->d_off = 0;              /* W2DO? */
    01287 /*@-boundsread@*/
    01288     dp->d_type = dt[i];
    01289 /*@=boundsread@*/
    01290 #endif
    01291
    01292     strncpy(dp->d_name, av[i], sizeof(dp->d_name));
    01293 /*@+voidabstract@*/
    01294 if (_ftp_debug)
    01295 fprintf(stderr, "*** ftpReaddir(%p) %p \"%s\"\n", (void *)mydir, dp, dp->d_name);
    01296 /*@=voidabstract@*/
    01297
    01298     return dp;
    01299 }
    01300 /*@=type@*/
    01301
    01302 static int ftpClosedir(/*@only@*/ DIR * dir)
    01303         /*@globals fileSystem @*/
    01304         /*@modifies dir, fileSystem @*/
    01305 {
    01306     FTPDIR mydir = (FTPDIR)dir;
    01307
    01308     /*@+voidabstract@*/
    01309 if (_ftp_debug)
    01310 fprintf(stderr, "*** ftpClosedir(%p)\n", (void *)mydir);
    01311     if (mydir == NULL || !ISFTPMAGIC(mydir)) {
    01312         /* XXX TODO: EBADF errno. */
    01313         return -1;
    01314     }
    01315     free((void *)mydir);
    01316     /*@=voidabstract@*/
    01317     mydir = NULL;
    01318     return 0;
    01319 }
    01320
    01321 int Stat(const char * path, struct stat * st)
    01322 {
    01323     const char * lpath;
    01324     int ut = urlPath(path, &lpath);
    01325
    01326 if (_rpmio_debug)
    01327 fprintf(stderr, "*** Stat(%s,%p)\n", path, st);
    01328     switch (ut) {
    01329     case URL_IS_FTP:
    01330         return ftpStat(path, st);
    01331         /*@notreached@*/ break;
    01332     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
    01333     case URL_IS_PATH:
    01334         path = lpath;
    01335         /*@fallthrough@*/
    01336     case URL_IS_UNKNOWN:
    01337         break;
    01338     case URL_IS_DASH:
    01339     default:
    01340         return -2;
    01341         /*@notreached@*/ break;
    01342     }
    01343     return stat(path, st);
    01344 }
    01345
    01346 int Lstat(const char * path, struct stat * st)
    01347 {
    01348     const char * lpath;
    01349     int ut = urlPath(path, &lpath);
    01350
    01351 if (_rpmio_debug)
    01352 fprintf(stderr, "*** Lstat(%s,%p)\n", path, st);
    01353     switch (ut) {
    01354     case URL_IS_FTP:
    01355         return ftpLstat(path, st);
    01356         /*@notreached@*/ break;
    01357     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
    01358     case URL_IS_PATH:
    01359         path = lpath;
    01360         /*@fallthrough@*/
    01361     case URL_IS_UNKNOWN:
    01362         break;
    01363     case URL_IS_DASH:
    01364     default:
    01365         return -2;
    01366         /*@notreached@*/ break;
    01367     }
    01368     return lstat(path, st);
    01369 }
    01370
    01371 int Readlink(const char * path, char * buf, size_t bufsiz)
    01372 {
    01373     const char * lpath;
    01374     int ut = urlPath(path, &lpath);
    01375
    01376     switch (ut) {
    01377     case URL_IS_FTP:
    01378         return ftpReadlink(path, buf, bufsiz);
    01379         /*@notreached@*/ break;
    01380     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
    01381     case URL_IS_PATH:
    01382         path = lpath;
    01383         /*@fallthrough@*/
    01384     case URL_IS_UNKNOWN:
    01385         break;
    01386     case URL_IS_DASH:
    01387     default:
    01388         return -2;
    01389         /*@notreached@*/ break;
    01390     }
    01391 /*@-compdef@*/
    01392     return readlink(path, buf, bufsiz);
    01393 /*@=compdef@*/
    01394 }
    01395
    01396 int Access(const char * path, int amode)
    01397 {
    01398     const char * lpath;
    01399     int ut = urlPath(path, &lpath);
    01400
    01401 if (_rpmio_debug)
    01402 fprintf(stderr, "*** Access(%s,%d)\n", path, amode);
    01403     switch (ut) {
    01404     case URL_IS_FTP:            /* XXX WRONG WRONG WRONG */
    01405     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
    01406     case URL_IS_PATH:
    01407         path = lpath;
    01408         /*@fallthrough@*/
    01409     case URL_IS_UNKNOWN:
    01410         break;
    01411     case URL_IS_DASH:
    01412     default:
    01413         return -2;
    01414         /*@notreached@*/ break;
    01415     }
    01416     return access(path, amode);
    01417 }
    01418
    01419 int Glob(const char *pattern, int flags,
    01420         int errfunc(const char * epath, int eerrno), glob_t *pglob)
    01421 {
    01422     const char * lpath;
    01423     int ut = urlPath(pattern, &lpath);
    01424
    01425 /*@-castfcnptr@*/
    01426 if (_rpmio_debug)
    01427 fprintf(stderr, "*** Glob(%s,0x%x,%p,%p)\n", pattern, (unsigned)flags, (void *)errfunc, pglob);
    01428 /*@=castfcnptr@*/
    01429     switch (ut) {
    01430     case URL_IS_FTP:
    01431 /*@-type@*/
    01432         pglob->gl_closedir = Closedir;
    01433         pglob->gl_readdir = Readdir;
    01434         pglob->gl_opendir = Opendir;
    01435         pglob->gl_lstat = Lstat;
    01436         pglob->gl_stat = Stat;
    01437 /*@=type@*/
    01438         flags |= GLOB_ALTDIRFUNC;
    01439         break;
    01440     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
    01441     case URL_IS_PATH:
    01442         pattern = lpath;
    01443         /*@fallthrough@*/
    01444     case URL_IS_UNKNOWN:
    01445         break;
    01446     case URL_IS_DASH:
    01447     default:
    01448         return -2;
    01449         /*@notreached@*/ break;
    01450     }
    01451     return glob(pattern, flags, errfunc, pglob);
    01452 }
    01453
    01454 void Globfree(glob_t *pglob)
    01455 {
    01456 if (_rpmio_debug)
    01457 fprintf(stderr, "*** Globfree(%p)\n", pglob);
    01458     globfree(pglob);
    01459 }
    01460
    01461 DIR * Opendir(const char * path)
    01462 {
    01463     const char * lpath;
    01464     int ut = urlPath(path, &lpath);
    01465
    01466 if (_rpmio_debug)
    01467 fprintf(stderr, "*** Opendir(%s)\n", path);
    01468     switch (ut) {
    01469     case URL_IS_FTP:
    01470         return ftpOpendir(path);
    01471         /*@notreached@*/ break;
    01472     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
    01473     case URL_IS_PATH:
    01474         path = lpath;
    01475         /*@fallthrough@*/
    01476     case URL_IS_UNKNOWN:
    01477         break;
    01478     case URL_IS_DASH:
    01479     default:
    01480         return NULL;
    01481         /*@notreached@*/ break;
    01482     }
    01483     /*@-dependenttrans@*/
    01484     return opendir(path);
    01485     /*@=dependenttrans@*/
    01486 }
    01487
    01488 /*@+voidabstract@*/
    01489 struct dirent * Readdir(DIR * dir)
    01490 {
    01491 if (_rpmio_debug)
    01492 fprintf(stderr, "*** Readdir(%p)\n", (void *)dir);
    01493     if (dir == NULL || ISFTPMAGIC(dir))
    01494         return ftpReaddir(dir);
    01495     return readdir(dir);
    01496 }
    01497
    01498 int Closedir(DIR * dir)
    01499 {
    01500 if (_rpmio_debug)
    01501 fprintf(stderr, "*** Closedir(%p)\n", (void *)dir);
    01502     if (dir == NULL || ISFTPMAGIC(dir))
    01503         return ftpClosedir(dir);
    01504     return closedir(dir);
    01505 }
    01506 /*@=voidabstract@*/