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

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 "ugid.h"
00013 #include "debug.h"
00014 
00015 /*@access DIR @*/
00016 /*@access FD_t @*/
00017 /*@access urlinfo @*/
00018 
00019 /* =============================================================== */
00020 static int ftpMkdir(const char * path, /*@unused@*/ mode_t mode)
00021         /*@globals h_errno, 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 h_errno, fileSystem, internalState @*/
00038         /*@modifies fileSystem, internalState @*/
00039 {
00040     return ftpCmd("CWD", path, NULL);
00041 }
00042 
00043 static int ftpRmdir(const char * path)
00044         /*@globals h_errno, 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 h_errno, 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 h_errno, 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         unsigned 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 & 0x000000ffU) << 8) | (min & 0x000000ffU);
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 static int ftpNLST(const char * url, ftpSysCall_t ftpSysCall,
00825                 /*@out@*/ /*@null@*/ struct stat * st,
00826                 /*@out@*/ /*@null@*/ char * rlbuf, size_t rlbufsiz)
00827         /*@globals ftpBufAlloced, ftpBuf,
00828                 h_errno, fileSystem, internalState @*/
00829         /*@modifies *st, *rlbuf, ftpBufAlloced, ftpBuf,
00830                 fileSystem, internalState @*/
00831 {
00832     FD_t fd;
00833     const char * path;
00834     int bufLength, moretodo;
00835     const char *n, *ne, *o, *oe;
00836     char * s;
00837     char * se;
00838     const char * urldn;
00839     char * bn = NULL;
00840     int nbn = 0;
00841     urlinfo u;
00842     int rc;
00843 
00844     n = ne = o = oe = NULL;
00845     (void) urlPath(url, &path);
00846     if (*path == '\0')
00847         return -2;
00848 
00849     switch (ftpSysCall) {
00850     case DO_FTP_GLOB:
00851         fd = ftpOpen(url, 0, 0, &u);
00852         if (fd == NULL || u == NULL)
00853             return -1;
00854 
00855         u->openError = ftpReq(fd, "LIST", path);
00856         break;
00857     default:
00858         urldn = alloca_strdup(url);
00859         /*@-branchstate@*/
00860         if ((bn = strrchr(urldn, '/')) == NULL)
00861             return -2;
00862         else if (bn == path)
00863             bn = ".";
00864         else
00865             *bn++ = '\0';
00866         /*@=branchstate@*/
00867         nbn = strlen(bn);
00868 
00869         rc = ftpChdir(urldn);           /* XXX don't care about CWD */
00870         if (rc < 0)
00871             return rc;
00872 
00873         fd = ftpOpen(url, 0, 0, &u);
00874         if (fd == NULL || u == NULL)
00875             return -1;
00876 
00877         /* XXX possibly should do "NLST -lais" to get st_ino/st_blocks also */
00878         u->openError = ftpReq(fd, "NLST", "-la");
00879 
00880         if (bn == NULL || nbn <= 0) {
00881             rc = -2;
00882             goto exit;
00883         }
00884         break;
00885     }
00886 
00887     if (u->openError < 0) {
00888         fd = fdLink(fd, "error data (ftpStat)");
00889         rc = -2;
00890         goto exit;
00891     }
00892 
00893     if (ftpBufAlloced == 0 || ftpBuf == NULL) {
00894         ftpBufAlloced = _url_iobuf_size;
00895         ftpBuf = xcalloc(ftpBufAlloced, sizeof(ftpBuf[0]));
00896     }
00897     *ftpBuf = '\0';
00898 
00899     bufLength = 0;
00900     moretodo = 1;
00901 
00902     do {
00903 
00904         /* XXX FIXME: realloc ftpBuf if < ~128 chars remain */
00905         if ((ftpBufAlloced - bufLength) < (1024+80)) {
00906             ftpBufAlloced <<= 2;
00907             assert(ftpBufAlloced < (8*1024*1024));
00908             ftpBuf = xrealloc(ftpBuf, ftpBufAlloced);
00909         }
00910         s = se = ftpBuf + bufLength;
00911         *se = '\0';
00912 
00913         rc = fdFgets(fd, se, (ftpBufAlloced - bufLength));
00914         if (rc <= 0) {
00915             moretodo = 0;
00916             break;
00917         }
00918         if (ftpSysCall == DO_FTP_GLOB) {        /* XXX HACK */
00919             bufLength += strlen(se);
00920             continue;
00921         }
00922 
00923         for (s = se; *s != '\0'; s = se) {
00924             int bingo;
00925 
00926             while (*se && *se != '\n') se++;
00927             if (se > s && se[-1] == '\r') se[-1] = '\0';
00928             if (*se == '\0') 
00929                 /*@innerbreak@*/ break;
00930             *se++ = '\0';
00931 
00932             if (!strncmp(s, "total ", sizeof("total ")-1))
00933                 /*@innercontinue@*/ continue;
00934 
00935             o = NULL;
00936             for (bingo = 0, n = se; n >= s; n--) {
00937                 switch (*n) {
00938                 case '\0':
00939                     oe = ne = n;
00940                     /*@switchbreak@*/ break;
00941                 case ' ':
00942                     if (o || !(n[-3] == ' ' && n[-2] == '-' && n[-1] == '>')) {
00943                         while (*(++n) == ' ')
00944                             {};
00945                         bingo++;
00946                         /*@switchbreak@*/ break;
00947                     }
00948                     for (o = n + 1; *o == ' '; o++)
00949                         {};
00950                     n -= 3;
00951                     ne = n;
00952                     /*@switchbreak@*/ break;
00953                 default:
00954                     /*@switchbreak@*/ break;
00955                 }
00956                 if (bingo)
00957                     /*@innerbreak@*/ break;
00958             }
00959 
00960             if (nbn != (ne - n))        /* Same name length? */
00961                 /*@innercontinue@*/ continue;
00962             if (strncmp(n, bn, nbn))    /* Same name? */
00963                 /*@innercontinue@*/ continue;
00964 
00965             moretodo = 0;
00966             /*@innerbreak@*/ break;
00967         }
00968 
00969         if (moretodo && se > s) {
00970             bufLength = se - s - 1;
00971             if (s != ftpBuf)
00972                 memmove(ftpBuf, s, bufLength);
00973         } else {
00974             bufLength = 0;
00975         }
00976     } while (moretodo);
00977 
00978     switch (ftpSysCall) {
00979     case DO_FTP_STAT:
00980         if (o && oe) {
00981             /* XXX FIXME: symlink, replace urldn/bn from [o,oe) and restart */
00982         }
00983         /*@fallthrough@*/
00984     case DO_FTP_LSTAT:
00985         if (st == NULL || !(n && ne)) {
00986             rc = -1;
00987         } else {
00988             rc = ((vfs_parse_ls_lga(s, st, NULL, NULL) > 0) ? 0 : -1);
00989         }
00990         break;
00991     case DO_FTP_READLINK:
00992         if (rlbuf == NULL || !(o && oe)) {
00993             rc = -1;
00994         } else {
00995             rc = oe - o;
00996             if (rc > rlbufsiz)
00997                 rc = rlbufsiz;
00998             memcpy(rlbuf, o, rc);
00999             if (rc < rlbufsiz)
01000                 rlbuf[rc] = '\0';
01001         }
01002         break;
01003     case DO_FTP_ACCESS:
01004         rc = 0;         /* XXX WRONG WRONG WRONG */
01005         break;
01006     case DO_FTP_GLOB:
01007         rc = 0;         /* XXX WRONG WRONG WRONG */
01008         break;
01009     }
01010 
01011 exit:
01012     (void) ufdClose(fd);
01013     return rc;
01014 }
01015 /*@=boundswrite@*/
01016 
01017 static const char * statstr(const struct stat * st,
01018                 /*@returned@*/ /*@out@*/ char * buf)
01019         /*@modifies *buf @*/
01020 {
01021     sprintf(buf,
01022         "*** dev %x ino %x mode %0o nlink %d uid %d gid %d rdev %x size %x\n",
01023         (unsigned)st->st_dev,
01024         (unsigned)st->st_ino,
01025         st->st_mode,
01026         (unsigned)st->st_nlink,
01027         st->st_uid,
01028         st->st_gid,
01029         (unsigned)st->st_rdev,
01030         (unsigned)st->st_size);
01031     return buf;
01032 }
01033 
01034 /*@unchecked@*/
01035 static int ftp_st_ino = 0xdead0000;
01036 
01037 static int ftpStat(const char * path, /*@out@*/ struct stat *st)
01038         /*@globals ftp_st_ino, h_errno, fileSystem, internalState @*/
01039         /*@modifies *st, ftp_st_ino, fileSystem, internalState @*/
01040 {
01041     char buf[1024];
01042     int rc;
01043     rc = ftpNLST(path, DO_FTP_STAT, st, NULL, 0);
01044     /* XXX fts(3) needs/uses st_ino, make something up for now. */
01045     if (st->st_ino == 0)
01046         st->st_ino = ftp_st_ino++;
01047 if (_ftp_debug)
01048 fprintf(stderr, "*** ftpStat(%s) rc %d\n%s", path, rc, statstr(st, buf));
01049     return rc;
01050 }
01051 
01052 static int ftpLstat(const char * path, /*@out@*/ struct stat *st)
01053         /*@globals ftp_st_ino, h_errno, fileSystem, internalState @*/
01054         /*@modifies *st, ftp_st_ino, fileSystem, internalState @*/
01055 {
01056     char buf[1024];
01057     int rc;
01058     rc = ftpNLST(path, DO_FTP_LSTAT, st, NULL, 0);
01059     /* XXX fts(3) needs/uses st_ino, make something up for now. */
01060     if (st->st_ino == 0)
01061         st->st_ino = ftp_st_ino++;
01062 if (_ftp_debug)
01063 fprintf(stderr, "*** ftpLstat(%s) rc %d\n%s\n", path, rc, statstr(st, buf));
01064     return rc;
01065 }
01066 
01067 static int ftpReadlink(const char * path, /*@out@*/ char * buf, size_t bufsiz)
01068         /*@globals h_errno, fileSystem, internalState @*/
01069         /*@modifies *buf, fileSystem, internalState @*/
01070 {
01071     int rc;
01072     rc = ftpNLST(path, DO_FTP_READLINK, NULL, buf, bufsiz);
01073 if (_ftp_debug)
01074 fprintf(stderr, "*** ftpReadlink(%s) rc %d\n", path, rc);
01075     return rc;
01076 }
01077 
01078 struct __dirstream {
01079     int fd;                     /* File descriptor.  */
01080     char * data;                /* Directory block.  */
01081     size_t allocation;          /* Space allocated for the block.  */
01082     size_t size;                /* Total valid data in the block.  */
01083     size_t offset;              /* Current offset into the block.  */
01084     off_t filepos;              /* Position of next entry to read.  */
01085 #if defined(HAVE_PTHREAD_H)
01086     pthread_mutex_t lock;       /* Mutex lock for this structure.  */
01087 #endif
01088 };
01089 
01090 #if !defined(DT_DIR)
01091 # define DT_UNKNOWN     0
01092 # define DT_FIFO        1
01093 # define DT_CHR         2
01094 # define DT_DIR         4
01095 # define DT_BLK         6
01096 # define DT_REG         8
01097 # define DT_LNK         10
01098 # define DT_SOCK        12
01099 # define DT_WHT         14
01100 typedef struct __dirstream *    FTPDIR;
01101 #else
01102 typedef DIR *                   FTPDIR;
01103 #endif
01104 
01105 /*@unchecked@*/
01106 static int ftpmagicdir = 0x8440291;
01107 #define ISFTPMAGIC(_dir) (!memcmp((_dir), &ftpmagicdir, sizeof(ftpmagicdir)))
01108 
01109 /*@-boundswrite@*/
01110 /*@null@*/
01111 static DIR * ftpOpendir(const char * path)
01112         /*@globals h_errno, fileSystem, internalState @*/
01113         /*@modifies fileSystem, internalState @*/
01114 {
01115     FTPDIR mydir;
01116     struct dirent * dp;
01117     size_t nb;
01118     const char * s, * sb, * se;
01119     const char ** av;
01120     unsigned char * dt;
01121     char * t;
01122     int ac;
01123     int c;
01124     int rc;
01125 
01126 if (_ftp_debug)
01127 fprintf(stderr, "*** ftpOpendir(%s)\n", path);
01128     rc = ftpNLST(path, DO_FTP_GLOB, NULL, NULL, 0);
01129     if (rc)
01130         return NULL;
01131 
01132     /*
01133      * ftpBuf now contains absolute paths, newline terminated.
01134      * Calculate the number of bytes to hold the directory info.
01135      */
01136     nb = sizeof(".") + sizeof("..");
01137     ac = 2;
01138     sb = NULL;
01139     s = se = ftpBuf;
01140     while ((c = *se) != '\0') {
01141         se++;
01142         switch (c) {
01143         case '/':
01144             sb = se;
01145             /*@switchbreak@*/ break;
01146         case '\r':
01147             if (sb == NULL) {
01148                 for (sb = se; sb > s && sb[-1] != ' '; sb--)
01149                     {};
01150             }
01151             ac++;
01152             nb += (se - sb);
01153 
01154             if (*se == '\n') se++;
01155             sb = NULL;
01156             s = se;
01157             /*@switchbreak@*/ break;
01158         default:
01159             /*@switchbreak@*/ break;
01160         }
01161     }
01162 
01163     nb += sizeof(*mydir) + sizeof(*dp) + ((ac + 1) * sizeof(*av)) + (ac + 1);
01164     mydir = xcalloc(1, nb);
01165     /*@-abstract@*/
01166     dp = (struct dirent *) (mydir + 1);
01167     av = (const char **) (dp + 1);
01168     dt = (char *) (av + (ac + 1));
01169     t = (char *) (dt + ac + 1);
01170     /*@=abstract@*/
01171 
01172     mydir->fd = ftpmagicdir;
01173 /*@-usereleased@*/
01174     mydir->data = (char *) dp;
01175 /*@=usereleased@*/
01176     mydir->allocation = nb;
01177     mydir->size = ac;
01178     mydir->offset = -1;
01179     mydir->filepos = 0;
01180 
01181     ac = 0;
01182     /*@-dependenttrans -unrecog@*/
01183     dt[ac] = DT_DIR;    av[ac++] = t;   t = stpcpy(t, ".");     t++;
01184     dt[ac] = DT_DIR;    av[ac++] = t;   t = stpcpy(t, "..");    t++;
01185     /*@=dependenttrans =unrecog@*/
01186     sb = NULL;
01187     s = se = ftpBuf;
01188     while ((c = *se) != '\0') {
01189         se++;
01190         switch (c) {
01191         case '/':
01192             sb = se;
01193             /*@switchbreak@*/ break;
01194         case '\r':
01195             /*@-dependenttrans@*/
01196             av[ac] = t;
01197             /*@=dependenttrans@*/
01198             if (sb == NULL) {
01199                 /*@-unrecog@*/
01200                 switch(*s) {
01201                 case 'p':
01202                     dt[ac] = DT_FIFO;
01203                     /*@innerbreak@*/ break;
01204                 case 'c':
01205                     dt[ac] = DT_CHR;
01206                     /*@innerbreak@*/ break;
01207                 case 'd':
01208                     dt[ac] = DT_DIR;
01209                     /*@innerbreak@*/ break;
01210                 case 'b':
01211                     dt[ac] = DT_BLK;
01212                     /*@innerbreak@*/ break;
01213                 case '-':
01214                     dt[ac] = DT_REG;
01215                     /*@innerbreak@*/ break;
01216                 case 'l':
01217                     dt[ac] = DT_LNK;
01218                     /*@innerbreak@*/ break;
01219                 case 's':
01220                     dt[ac] = DT_SOCK;
01221                     /*@innerbreak@*/ break;
01222                 default:
01223                     dt[ac] = DT_UNKNOWN;
01224                     /*@innerbreak@*/ break;
01225                 }
01226                 /*@=unrecog@*/
01227                 for (sb = se; sb > s && sb[-1] != ' '; sb--)
01228                     {};
01229             }
01230             ac++;
01231             t = stpncpy(t, sb, (se - sb));
01232             t[-1] = '\0';
01233             if (*se == '\n') se++;
01234             sb = NULL;
01235             s = se;
01236             /*@switchbreak@*/ break;
01237         default:
01238             /*@switchbreak@*/ break;
01239         }
01240     }
01241     av[ac] = NULL;
01242 
01243 /*@-kepttrans@*/
01244     return (DIR *) mydir;
01245 /*@=kepttrans@*/
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     if (mydir == NULL || !ISFTPMAGIC(mydir) || mydir->data == NULL) {
01262         /* XXX TODO: EBADF errno. */
01263         return NULL;
01264     }
01265 
01266     dp = (struct dirent *) mydir->data;
01267     av = (const char **) (dp + 1);
01268     ac = mydir->size;
01269     dt = (char *) (av + (ac + 1));
01270     i = mydir->offset + 1;
01271 
01272 /*@-boundsread@*/
01273     if (i < 0 || i >= ac || av[i] == NULL)
01274         return NULL;
01275 /*@=boundsread@*/
01276 
01277     mydir->offset = i;
01278 
01279     /* XXX glob(3) uses REAL_DIR_ENTRY(dp) test on d_ino */
01280 /*@-type@*/
01281     dp->d_ino = i + 1;          /* W2DO? */
01282     dp->d_reclen = 0;           /* W2DO? */
01283 
01284 #if !defined(hpux) && !defined(sun)
01285     dp->d_off = 0;              /* W2DO? */
01286 /*@-boundsread@*/
01287     dp->d_type = dt[i];
01288 /*@=boundsread@*/
01289 #endif
01290 /*@=type@*/
01291 
01292     strncpy(dp->d_name, av[i], sizeof(dp->d_name));
01293 if (_ftp_debug)
01294 fprintf(stderr, "*** ftpReaddir(%p) %p \"%s\"\n", (void *)mydir, dp, dp->d_name);
01295     
01296     return dp;
01297 }
01298 
01299 static int ftpClosedir(/*@only@*/ DIR * dir)
01300         /*@globals fileSystem @*/
01301         /*@modifies dir, fileSystem @*/
01302 {
01303     FTPDIR mydir = (FTPDIR)dir;
01304 
01305 if (_ftp_debug)
01306 fprintf(stderr, "*** ftpClosedir(%p)\n", (void *)mydir);
01307     if (mydir == NULL || !ISFTPMAGIC(mydir)) {
01308         /* XXX TODO: EBADF errno. */
01309         return -1;
01310     }
01311     free((void *)mydir);
01312     mydir = NULL;
01313     return 0;
01314 }
01315 
01316 int Stat(const char * path, struct stat * st)
01317 {
01318     const char * lpath;
01319     int ut = urlPath(path, &lpath);
01320 
01321 if (_rpmio_debug)
01322 fprintf(stderr, "*** Stat(%s,%p)\n", path, st);
01323     switch (ut) {
01324     case URL_IS_FTP:
01325         return ftpStat(path, st);
01326         /*@notreached@*/ break;
01327     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
01328     case URL_IS_PATH:
01329         path = lpath;
01330         /*@fallthrough@*/
01331     case URL_IS_UNKNOWN:
01332         break;
01333     case URL_IS_DASH:
01334     default:
01335         return -2;
01336         /*@notreached@*/ break;
01337     }
01338     return stat(path, st);
01339 }
01340 
01341 int Lstat(const char * path, struct stat * st)
01342 {
01343     const char * lpath;
01344     int ut = urlPath(path, &lpath);
01345 
01346 if (_rpmio_debug)
01347 fprintf(stderr, "*** Lstat(%s,%p)\n", path, st);
01348     switch (ut) {
01349     case URL_IS_FTP:
01350         return ftpLstat(path, st);
01351         /*@notreached@*/ break;
01352     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
01353     case URL_IS_PATH:
01354         path = lpath;
01355         /*@fallthrough@*/
01356     case URL_IS_UNKNOWN:
01357         break;
01358     case URL_IS_DASH:
01359     default:
01360         return -2;
01361         /*@notreached@*/ break;
01362     }
01363     return lstat(path, st);
01364 }
01365 
01366 int Readlink(const char * path, char * buf, size_t bufsiz)
01367 {
01368     const char * lpath;
01369     int ut = urlPath(path, &lpath);
01370 
01371     switch (ut) {
01372     case URL_IS_FTP:
01373         return ftpReadlink(path, buf, bufsiz);
01374         /*@notreached@*/ break;
01375     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
01376     case URL_IS_PATH:
01377         path = lpath;
01378         /*@fallthrough@*/
01379     case URL_IS_UNKNOWN:
01380         break;
01381     case URL_IS_DASH:
01382     default:
01383         return -2;
01384         /*@notreached@*/ break;
01385     }
01386 /*@-compdef@*/ /* FIX: *buf is undefined */
01387     return readlink(path, buf, bufsiz);
01388 /*@=compdef@*/
01389 }
01390 
01391 int Access(const char * path, int amode)
01392 {
01393     const char * lpath;
01394     int ut = urlPath(path, &lpath);
01395 
01396 if (_rpmio_debug)
01397 fprintf(stderr, "*** Access(%s,%d)\n", path, amode);
01398     switch (ut) {
01399     case URL_IS_FTP:            /* XXX WRONG WRONG WRONG */
01400     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
01401     case URL_IS_PATH:
01402         path = lpath;
01403         /*@fallthrough@*/
01404     case URL_IS_UNKNOWN:
01405         break;
01406     case URL_IS_DASH:
01407     default:
01408         return -2;
01409         /*@notreached@*/ break;
01410     }
01411     return access(path, amode);
01412 }
01413 
01414 /* glob_pattern_p() taken from bash
01415  * Copyright (C) 1985, 1988, 1989 Free Software Foundation, Inc.
01416  *
01417  * Return nonzero if PATTERN has any special globbing chars in it.
01418  */
01419 int Glob_pattern_p (const char * pattern, int quote)
01420 {
01421     const char *p;
01422     int open = 0;
01423     char c;
01424   
01425     (void) urlPath(pattern, &p);
01426     while ((c = *p++) != '\0')
01427         switch (c) {
01428         case '?':
01429         case '*':
01430             return (1);
01431         case '\\':
01432             if (quote && p[1] != '\0')
01433                 p++;
01434             continue;
01435 
01436         case '[':
01437             open = 1;
01438             continue;
01439         case ']':
01440             if (open)
01441                 return (1);
01442             continue;      
01443 
01444         case '+':
01445         case '@':
01446         case '!':
01447             if (*p == '(')
01448                 return (1);
01449             continue;
01450         }
01451 
01452     return (0);
01453 }
01454 
01455 int Glob_error(/*@unused@*/const char * epath, /*@unused@*/ int eerrno)
01456 {
01457     return 1;
01458 }
01459 
01460 int Glob(const char *pattern, int flags,
01461         int errfunc(const char * epath, int eerrno), glob_t *pglob)
01462 {
01463     const char * lpath;
01464     int ut = urlPath(pattern, &lpath);
01465 
01466 /*@-castfcnptr@*/
01467 if (_rpmio_debug)
01468 fprintf(stderr, "*** Glob(%s,0x%x,%p,%p)\n", pattern, (unsigned)flags, (void *)errfunc, pglob);
01469 /*@=castfcnptr@*/
01470     switch (ut) {
01471     case URL_IS_FTP:
01472 /*@-type@*/
01473         pglob->gl_closedir = Closedir;
01474         pglob->gl_readdir = Readdir;
01475         pglob->gl_opendir = Opendir;
01476         pglob->gl_lstat = Lstat;
01477         pglob->gl_stat = Stat;
01478 /*@=type@*/
01479         flags |= GLOB_ALTDIRFUNC;
01480         flags &= ~GLOB_TILDE;
01481         break;
01482     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
01483         flags &= ~GLOB_TILDE;
01484         /*@fallthrough@*/
01485     case URL_IS_PATH:
01486         pattern = lpath;
01487         /*@fallthrough@*/
01488     case URL_IS_UNKNOWN:
01489         break;
01490     case URL_IS_DASH:
01491     default:
01492         return -2;
01493         /*@notreached@*/ break;
01494     }
01495     return glob(pattern, flags, errfunc, pglob);
01496 }
01497 
01498 void Globfree(glob_t *pglob)
01499 {
01500 if (_rpmio_debug)
01501 fprintf(stderr, "*** Globfree(%p)\n", pglob);
01502     globfree(pglob);
01503 }
01504 
01505 DIR * Opendir(const char * path)
01506 {
01507     const char * lpath;
01508     int ut = urlPath(path, &lpath);
01509 
01510 if (_rpmio_debug)
01511 fprintf(stderr, "*** Opendir(%s)\n", path);
01512     switch (ut) {
01513     case URL_IS_FTP:
01514         return ftpOpendir(path);
01515         /*@notreached@*/ break;
01516     case URL_IS_HTTP:           /* XXX WRONG WRONG WRONG */
01517     case URL_IS_PATH:
01518         path = lpath;
01519         /*@fallthrough@*/
01520     case URL_IS_UNKNOWN:
01521         break;
01522     case URL_IS_DASH:
01523     default:
01524         return NULL;
01525         /*@notreached@*/ break;
01526     }
01527     /*@-dependenttrans@*/
01528     return opendir(path);
01529     /*@=dependenttrans@*/
01530 }
01531 
01532 struct dirent * Readdir(DIR * dir)
01533 {
01534 if (_rpmio_debug)
01535 fprintf(stderr, "*** Readdir(%p)\n", (void *)dir);
01536     if (dir == NULL || ISFTPMAGIC(dir))
01537         return ftpReaddir(dir);
01538     return readdir(dir);
01539 }
01540 
01541 int Closedir(DIR * dir)
01542 {
01543 if (_rpmio_debug)
01544 fprintf(stderr, "*** Closedir(%p)\n", (void *)dir);
01545     if (dir == NULL || ISFTPMAGIC(dir))
01546         return ftpClosedir(dir);
01547     return closedir(dir);
01548 }

Generated on Thu Aug 14 14:17:55 2008 for rpm by  doxygen 1.3.9.1