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
00016
00017
00018
00019
00020 static int ftpMkdir(const char * path, mode_t mode)
00021
00022
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
00038
00039 {
00040 return ftpCmd("CWD", path, NULL);
00041 }
00042
00043 static int ftpRmdir(const char * path)
00044
00045
00046 {
00047 return ftpCmd("RMD", path, NULL);
00048 }
00049
00050 static int ftpRename(const char * oldpath, const char * newpath)
00051
00052
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
00062
00063 {
00064 return ftpCmd("DELE", path, NULL);
00065 }
00066
00067
00068
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 break;
00078 case URL_IS_HTTP:
00079 case URL_IS_PATH:
00080 path = lpath;
00081
00082 case URL_IS_UNKNOWN:
00083 break;
00084 case URL_IS_DASH:
00085 default:
00086 return -2;
00087 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 break;
00101 case URL_IS_HTTP:
00102 case URL_IS_PATH:
00103 path = lpath;
00104
00105 case URL_IS_UNKNOWN:
00106 break;
00107 case URL_IS_DASH:
00108 default:
00109 return -2;
00110 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 break;
00124 case URL_IS_HTTP:
00125 case URL_IS_PATH:
00126 path = lpath;
00127
00128 case URL_IS_UNKNOWN:
00129 break;
00130 case URL_IS_DASH:
00131 default:
00132 return -2;
00133 break;
00134 }
00135 return rmdir(path);
00136 }
00137
00138
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
00147 if (!strcmp(oldpath, newpath)) return 0;
00148
00149 oldut = urlPath(oldpath, &oe);
00150 switch (oldut) {
00151 case URL_IS_FTP:
00152 case URL_IS_HTTP:
00153 case URL_IS_PATH:
00154 case URL_IS_UNKNOWN:
00155 break;
00156 case URL_IS_DASH:
00157 default:
00158 return -2;
00159 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 break;
00172 case URL_IS_HTTP:
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 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:
00196 case URL_IS_HTTP:
00197 case URL_IS_PATH:
00198 case URL_IS_UNKNOWN:
00199 break;
00200 case URL_IS_DASH:
00201 default:
00202 return -2;
00203 break;
00204 }
00205
00206 newut = urlPath(newpath, &ne);
00207 switch (newut) {
00208 case URL_IS_HTTP:
00209 case URL_IS_FTP:
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 break;
00225 }
00226 return link(oldpath, newpath);
00227 }
00228
00229
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 break;
00239 case URL_IS_HTTP:
00240 case URL_IS_PATH:
00241 path = lpath;
00242
00243 case URL_IS_UNKNOWN:
00244 break;
00245 case URL_IS_DASH:
00246 default:
00247 return -2;
00248 break;
00249 }
00250 return unlink(path);
00251 }
00252
00253
00254
00255 #define g_strdup xstrdup
00256 #define g_free free
00257
00258
00259
00260
00261
00262 static int current_mday;
00263
00264 static int current_mon;
00265
00266 static int current_year;
00267
00268
00269 #define MAXCOLS 30
00270
00271
00272 static char *columns [MAXCOLS];
00273
00274 static int column_ptr [MAXCOLS];
00275
00276
00277 static int
00278 vfs_split_text (char *p)
00279
00280
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
00299
00300
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
00310
00311
00312 static int
00313 is_dos_date( 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
00322
00323 static int
00324 is_week ( const char * str, struct tm * tim)
00325
00326 {
00327 static const char * week = "SunMonTueWedThuFriSat";
00328 const char * pos;
00329
00330
00331 if (str != NULL && (pos=strstr(week, str)) != NULL) {
00332
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 ( const char * str, struct tm * tim)
00342
00343 {
00344 static const char * month = "JanFebMarAprMayJunJulAugSepOctNovDec";
00345 const char * pos;
00346
00347
00348 if (str != NULL && (pos = strstr(month, str)) != NULL) {
00349
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 ( const char * str, struct tm * tim)
00359
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( const char * str, struct tm * tim)
00378
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
00404
00405
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
00419 return S_IFSOCK;
00420 #endif
00421 case 'p': return S_IFIFO;
00422 case 'm': case 'n':
00423 case '-': case '?': return S_IFREG;
00424 default: return -1;
00425 }
00426 }
00427
00428 static int vfs_parse_filemode (const char *p)
00429
00430 {
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':
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
00488 static int vfs_parse_filedate(int idx, time_t *t)
00489
00490 {
00491
00492 char *p;
00493 struct tm tim;
00494 int d[3];
00495 int got_year = 0;
00496
00497
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;
00505
00506 p = columns [idx++];
00507
00508
00509 if(is_week(p, &tim))
00510 p = columns [idx++];
00511
00512
00513 if(is_month(p, &tim)){
00514
00515 if (is_num (idx))
00516 tim.tm_mday = (int)atol (columns [idx++]);
00517 else
00518 return 0;
00519
00520 } else {
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533 if (is_dos_date(p)){
00534
00535 p[2] = p[5] = '-';
00536
00537
00538 memset(d, 0, sizeof(d));
00539 if (sscanf(p, "%2d-%2d-%2d", &d[0], &d[1], &d[2]) == 3){
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549 d[0]--;
00550
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;
00560 } else
00561 return 0;
00562 }
00563
00564
00565
00566 if (is_num (idx)) {
00567 if(is_time(columns[idx], &tim) || (got_year = is_year(columns[idx], &tim))) {
00568 idx++;
00569
00570
00571 if(is_num (idx) &&
00572 ((got_year = is_year(columns[idx], &tim)) || is_time(columns[idx], &tim)))
00573 idx++;
00574 }
00575 }
00576 else
00577 return 0;
00578
00579
00580
00581
00582
00583
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
00596
00597
00598 static int
00599 vfs_parse_ls_lga (char * p, struct stat * st,
00600 const char ** filename,
00601 const char ** linkname)
00602
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
00613
00614
00615 if ((i = vfs_parse_filetype(*(p++))) == -1)
00616 goto error;
00617
00618 st->st_mode = i;
00619 if (*p == ' ')
00620 p++;
00621 if (*p == '['){
00622 if (strlen (p) <= 8 || p [8] != ']')
00623 goto error;
00624
00625
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
00632 } else {
00633 if ((i = vfs_parse_filemode(p)) == -1)
00634 goto error;
00635 st->st_mode |= i;
00636 p += 9;
00637
00638
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
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
00669 if (idx == 3 || (idx == 4 && (S_ISCHR(st->st_mode) || S_ISBLK (st->st_mode))))
00670 idx2 = 2;
00671 else {
00672
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
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 & 0xff) << 8) | (min & 0xffff00ff);
00696 #endif
00697 st->st_size = 0;
00698
00699 } else {
00700
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
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)))
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
00758
00759
00760 if (filename){
00761
00762
00763
00764 int tlen;
00765 char *t;
00766
00767 t = g_strdup (p_copy + column_ptr [idx]); idx++;
00768 tlen = strlen (t);
00769
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
00796 if (p_copy != p)
00797
00798 g_free (p_copy);
00799 return 0;
00800 }
00801
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
00814 static size_t ftpBufAlloced = 0;
00815
00818
00819 static char * ftpBuf = NULL;
00820
00821 #define alloca_strdup(_s) strcpy(alloca(strlen(_s)+1), (_s))
00822
00823
00824
00825 static int ftpNLST(const char * url, ftpSysCall_t ftpSysCall,
00826 struct stat * st,
00827 char * rlbuf, size_t rlbufsiz)
00828
00829
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
00859 if ((bn = strrchr(urldn, '/')) == NULL)
00860 return -2;
00861 else if (bn == path)
00862 bn = ".";
00863 else
00864 *bn++ = '\0';
00865
00866 nbn = strlen(bn);
00867
00868 rc = ftpChdir(urldn);
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
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
00904 if ((ftpBufAlloced - bufLength) < (1024+80)) {
00905 ftpBufAlloced <<= 2;
00906 assert(ftpBufAlloced < (8*1024*1024));
00907 ftpBuf = xrealloc(ftpBuf, ftpBufAlloced);
00908 }
00909 s = se = ftpBuf + bufLength;
00910 *se = '\0';
00911
00912 rc = fdFgets(fd, se, (ftpBufAlloced - bufLength));
00913 if (rc <= 0) {
00914 moretodo = 0;
00915 break;
00916 }
00917 if (ftpSysCall == DO_FTP_GLOB) {
00918 bufLength += strlen(se);
00919 continue;
00920 }
00921
00922 for (s = se; *s != '\0'; s = se) {
00923 int bingo;
00924
00925 while (*se && *se != '\n') se++;
00926 if (se > s && se[-1] == '\r') se[-1] = '\0';
00927 if (*se == '\0')
00928 break;
00929 *se++ = '\0';
00930
00931 if (!strncmp(s, "total ", sizeof("total ")-1))
00932 continue;
00933
00934 o = NULL;
00935 for (bingo = 0, n = se; n >= s; n--) {
00936 switch (*n) {
00937 case '\0':
00938 oe = ne = n;
00939 break;
00940 case ' ':
00941 if (o || !(n[-3] == ' ' && n[-2] == '-' && n[-1] == '>')) {
00942 while (*(++n) == ' ')
00943 {};
00944 bingo++;
00945 break;
00946 }
00947 for (o = n + 1; *o == ' '; o++)
00948 {};
00949 n -= 3;
00950 ne = n;
00951 break;
00952 default:
00953 break;
00954 }
00955 if (bingo)
00956 break;
00957 }
00958
00959 if (nbn != (ne - n))
00960 continue;
00961 if (strncmp(n, bn, nbn))
00962 continue;
00963
00964 moretodo = 0;
00965 break;
00966 }
00967
00968 if (moretodo && se > s) {
00969 bufLength = se - s - 1;
00970 if (s != ftpBuf)
00971 memmove(ftpBuf, s, bufLength);
00972 } else {
00973 bufLength = 0;
00974 }
00975 } while (moretodo);
00976
00977 switch (ftpSysCall) {
00978 case DO_FTP_STAT:
00979 if (o && oe) {
00980
00981 }
00982
00983 case DO_FTP_LSTAT:
00984 if (st == NULL || !(n && ne)) {
00985 rc = -1;
00986 } else {
00987 rc = ((vfs_parse_ls_lga(s, st, NULL, NULL) > 0) ? 0 : -1);
00988 }
00989 break;
00990 case DO_FTP_READLINK:
00991 if (rlbuf == NULL || !(o && oe)) {
00992 rc = -1;
00993 } else {
00994 rc = oe - o;
00995 if (rc > rlbufsiz)
00996 rc = rlbufsiz;
00997 memcpy(rlbuf, o, rc);
00998 if (rc < rlbufsiz)
00999 rlbuf[rc] = '\0';
01000 }
01001 break;
01002 case DO_FTP_ACCESS:
01003 rc = 0;
01004 break;
01005 case DO_FTP_GLOB:
01006 rc = 0;
01007 break;
01008 }
01009
01010 exit:
01011 (void) ufdClose(fd);
01012 return rc;
01013 }
01014
01015
01016
01017 static const char * statstr(const struct stat * st,
01018 char * buf)
01019
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
01035 static int ftp_st_ino = 0xdead0000;
01036
01037 static int ftpStat(const char * path, struct stat *st)
01038
01039
01040 {
01041 char buf[1024];
01042 int rc;
01043 rc = ftpNLST(path, DO_FTP_STAT, st, NULL, 0);
01044
01045
01046 if (st->st_ino == 0)
01047 st->st_ino = ftp_st_ino++;
01048
01049 if (_ftp_debug)
01050 fprintf(stderr, "*** ftpStat(%s) rc %d\n%s", path, rc, statstr(st, buf));
01051 return rc;
01052 }
01053
01054 static int ftpLstat(const char * path, struct stat *st)
01055
01056
01057 {
01058 char buf[1024];
01059 int rc;
01060 rc = ftpNLST(path, DO_FTP_LSTAT, st, NULL, 0);
01061
01062
01063 if (st->st_ino == 0)
01064 st->st_ino = ftp_st_ino++;
01065
01066 if (_ftp_debug)
01067 fprintf(stderr, "*** ftpLstat(%s) rc %d\n%s\n", path, rc, statstr(st, buf));
01068 return rc;
01069 }
01070
01071 static int ftpReadlink(const char * path, char * buf, size_t bufsiz)
01072
01073
01074 {
01075 int rc;
01076 rc = ftpNLST(path, DO_FTP_READLINK, NULL, buf, bufsiz);
01077 if (_ftp_debug)
01078 fprintf(stderr, "*** ftpReadlink(%s) rc %d\n", path, rc);
01079 return rc;
01080 }
01081
01082 struct __dirstream {
01083 int fd;
01084 char * data;
01085 size_t allocation;
01086 size_t size;
01087 size_t offset;
01088 off_t filepos;
01089 #if defined(HAVE_PTHREAD_H) && !defined(__LCLINT__)
01090 pthread_mutex_t lock;
01091 #endif
01092 };
01093
01094 #if !defined(DT_DIR)
01095 # define DT_UNKNOWN 0
01096 # define DT_FIFO 1
01097 # define DT_CHR 2
01098 # define DT_DIR 4
01099 # define DT_BLK 6
01100 # define DT_REG 8
01101 # define DT_LNK 10
01102 # define DT_SOCK 12
01103 # define DT_WHT 14
01104 typedef struct __dirstream * FTPDIR;
01105 #else
01106 typedef DIR * FTPDIR;
01107 #endif
01108
01109
01110 static int ftpmagicdir = 0x8440291;
01111 #define ISFTPMAGIC(_dir) (!memcmp((_dir), &ftpmagicdir, sizeof(ftpmagicdir)))
01112
01113
01114
01115
01116 static DIR * ftpOpendir(const char * path)
01117
01118
01119 {
01120 FTPDIR mydir;
01121 struct dirent * dp;
01122 size_t nb;
01123 const char * s, * sb, * se;
01124 const char ** av;
01125 unsigned char * dt;
01126 char * t;
01127 int ac;
01128 int c;
01129 int rc;
01130
01131 if (_ftp_debug)
01132 fprintf(stderr, "*** ftpOpendir(%s)\n", path);
01133 rc = ftpNLST(path, DO_FTP_GLOB, NULL, NULL, 0);
01134 if (rc)
01135 return NULL;
01136
01137
01138
01139
01140
01141 nb = sizeof(".") + sizeof("..");
01142 ac = 2;
01143 sb = NULL;
01144 s = se = ftpBuf;
01145 while ((c = *se) != '\0') {
01146 se++;
01147 switch (c) {
01148 case '/':
01149 sb = se;
01150 break;
01151 case '\r':
01152 if (sb == NULL) {
01153 for (sb = se; sb > s && sb[-1] != ' '; sb--)
01154 {};
01155 }
01156 ac++;
01157 nb += (se - sb);
01158
01159 if (*se == '\n') se++;
01160 sb = NULL;
01161 s = se;
01162 break;
01163 default:
01164 break;
01165 }
01166 }
01167
01168 nb += sizeof(*mydir) + sizeof(*dp) + ((ac + 1) * sizeof(*av)) + (ac + 1);
01169 mydir = xcalloc(1, nb);
01170
01171 dp = (struct dirent *) (mydir + 1);
01172 av = (const char **) (dp + 1);
01173 dt = (char *) (av + (ac + 1));
01174 t = (char *) (dt + ac + 1);
01175
01176
01177 mydir->fd = ftpmagicdir;
01178
01179 mydir->data = (char *) dp;
01180
01181 mydir->allocation = nb;
01182 mydir->size = ac;
01183 mydir->offset = -1;
01184 mydir->filepos = 0;
01185
01186 ac = 0;
01187
01188 dt[ac] = DT_DIR; av[ac++] = t; t = stpcpy(t, "."); t++;
01189 dt[ac] = DT_DIR; av[ac++] = t; t = stpcpy(t, ".."); t++;
01190
01191 sb = NULL;
01192 s = se = ftpBuf;
01193 while ((c = *se) != '\0') {
01194 se++;
01195 switch (c) {
01196 case '/':
01197 sb = se;
01198 break;
01199 case '\r':
01200
01201 av[ac] = t;
01202
01203 if (sb == NULL) {
01204
01205 switch(*s) {
01206 case 'p':
01207 dt[ac] = DT_FIFO;
01208 break;
01209 case 'c':
01210 dt[ac] = DT_CHR;
01211 break;
01212 case 'd':
01213 dt[ac] = DT_DIR;
01214 break;
01215 case 'b':
01216 dt[ac] = DT_BLK;
01217 break;
01218 case '-':
01219 dt[ac] = DT_REG;
01220 break;
01221 case 'l':
01222 dt[ac] = DT_LNK;
01223 break;
01224 case 's':
01225 dt[ac] = DT_SOCK;
01226 break;
01227 default:
01228 dt[ac] = DT_UNKNOWN;
01229 break;
01230 }
01231
01232 for (sb = se; sb > s && sb[-1] != ' '; sb--)
01233 {};
01234 }
01235 ac++;
01236 t = stpncpy(t, sb, (se - sb));
01237 t[-1] = '\0';
01238 if (*se == '\n') se++;
01239 sb = NULL;
01240 s = se;
01241 break;
01242 default:
01243 break;
01244 }
01245 }
01246 av[ac] = NULL;
01247
01248
01249 return (DIR *) mydir;
01250
01251 }
01252
01253
01254
01255 static struct dirent * ftpReaddir(DIR * dir)
01256
01257
01258 {
01259 FTPDIR mydir = (FTPDIR)dir;
01260 struct dirent * dp;
01261 const char ** av;
01262 unsigned char * dt;
01263 int ac;
01264 int i;
01265
01266
01267 if (mydir == NULL || !ISFTPMAGIC(mydir) || mydir->data == NULL) {
01268
01269 return NULL;
01270 }
01271
01272
01273 dp = (struct dirent *) mydir->data;
01274 av = (const char **) (dp + 1);
01275 ac = mydir->size;
01276 dt = (char *) (av + (ac + 1));
01277 i = mydir->offset + 1;
01278
01279
01280 if (i < 0 || i >= ac || av[i] == NULL)
01281 return NULL;
01282
01283
01284 mydir->offset = i;
01285
01286
01287 dp->d_ino = i + 1;
01288 dp->d_reclen = 0;
01289
01290 #if !defined(hpux) && !defined(sun)
01291 dp->d_off = 0;
01292
01293 dp->d_type = dt[i];
01294
01295 #endif
01296
01297 strncpy(dp->d_name, av[i], sizeof(dp->d_name));
01298
01299 if (_ftp_debug)
01300 fprintf(stderr, "*** ftpReaddir(%p) %p \"%s\"\n", (void *)mydir, dp, dp->d_name);
01301
01302
01303 return dp;
01304 }
01305
01306
01307 static int ftpClosedir( DIR * dir)
01308
01309
01310 {
01311 FTPDIR mydir = (FTPDIR)dir;
01312
01313
01314 if (_ftp_debug)
01315 fprintf(stderr, "*** ftpClosedir(%p)\n", (void *)mydir);
01316 if (mydir == NULL || !ISFTPMAGIC(mydir)) {
01317
01318 return -1;
01319 }
01320 free((void *)mydir);
01321
01322 mydir = NULL;
01323 return 0;
01324 }
01325
01326 int Stat(const char * path, struct stat * st)
01327 {
01328 const char * lpath;
01329 int ut = urlPath(path, &lpath);
01330
01331 if (_rpmio_debug)
01332 fprintf(stderr, "*** Stat(%s,%p)\n", path, st);
01333 switch (ut) {
01334 case URL_IS_FTP:
01335 return ftpStat(path, st);
01336 break;
01337 case URL_IS_HTTP:
01338 case URL_IS_PATH:
01339 path = lpath;
01340
01341 case URL_IS_UNKNOWN:
01342 break;
01343 case URL_IS_DASH:
01344 default:
01345 return -2;
01346 break;
01347 }
01348 return stat(path, st);
01349 }
01350
01351 int Lstat(const char * path, struct stat * st)
01352 {
01353 const char * lpath;
01354 int ut = urlPath(path, &lpath);
01355
01356 if (_rpmio_debug)
01357 fprintf(stderr, "*** Lstat(%s,%p)\n", path, st);
01358 switch (ut) {
01359 case URL_IS_FTP:
01360 return ftpLstat(path, st);
01361 break;
01362 case URL_IS_HTTP:
01363 case URL_IS_PATH:
01364 path = lpath;
01365
01366 case URL_IS_UNKNOWN:
01367 break;
01368 case URL_IS_DASH:
01369 default:
01370 return -2;
01371 break;
01372 }
01373 return lstat(path, st);
01374 }
01375
01376 int Readlink(const char * path, char * buf, size_t bufsiz)
01377 {
01378 const char * lpath;
01379 int ut = urlPath(path, &lpath);
01380
01381 switch (ut) {
01382 case URL_IS_FTP:
01383 return ftpReadlink(path, buf, bufsiz);
01384 break;
01385 case URL_IS_HTTP:
01386 case URL_IS_PATH:
01387 path = lpath;
01388
01389 case URL_IS_UNKNOWN:
01390 break;
01391 case URL_IS_DASH:
01392 default:
01393 return -2;
01394 break;
01395 }
01396
01397 return readlink(path, buf, bufsiz);
01398
01399 }
01400
01401 int Access(const char * path, int amode)
01402 {
01403 const char * lpath;
01404 int ut = urlPath(path, &lpath);
01405
01406 if (_rpmio_debug)
01407 fprintf(stderr, "*** Access(%s,%d)\n", path, amode);
01408 switch (ut) {
01409 case URL_IS_FTP:
01410 case URL_IS_HTTP:
01411 case URL_IS_PATH:
01412 path = lpath;
01413
01414 case URL_IS_UNKNOWN:
01415 break;
01416 case URL_IS_DASH:
01417 default:
01418 return -2;
01419 break;
01420 }
01421 return access(path, amode);
01422 }
01423
01424 int Glob(const char *pattern, int flags,
01425 int errfunc(const char * epath, int eerrno), glob_t *pglob)
01426 {
01427 const char * lpath;
01428 int ut = urlPath(pattern, &lpath);
01429
01430
01431 if (_rpmio_debug)
01432 fprintf(stderr, "*** Glob(%s,0x%x,%p,%p)\n", pattern, (unsigned)flags, (void *)errfunc, pglob);
01433
01434 switch (ut) {
01435 case URL_IS_FTP:
01436
01437 pglob->gl_closedir = Closedir;
01438 pglob->gl_readdir = Readdir;
01439 pglob->gl_opendir = Opendir;
01440 pglob->gl_lstat = Lstat;
01441 pglob->gl_stat = Stat;
01442
01443 flags |= GLOB_ALTDIRFUNC;
01444 break;
01445 case URL_IS_HTTP:
01446 case URL_IS_PATH:
01447 pattern = lpath;
01448
01449 case URL_IS_UNKNOWN:
01450 break;
01451 case URL_IS_DASH:
01452 default:
01453 return -2;
01454 break;
01455 }
01456 return glob(pattern, flags, errfunc, pglob);
01457 }
01458
01459 void Globfree(glob_t *pglob)
01460 {
01461 if (_rpmio_debug)
01462 fprintf(stderr, "*** Globfree(%p)\n", pglob);
01463 globfree(pglob);
01464 }
01465
01466 DIR * Opendir(const char * path)
01467 {
01468 const char * lpath;
01469 int ut = urlPath(path, &lpath);
01470
01471 if (_rpmio_debug)
01472 fprintf(stderr, "*** Opendir(%s)\n", path);
01473 switch (ut) {
01474 case URL_IS_FTP:
01475 return ftpOpendir(path);
01476 break;
01477 case URL_IS_HTTP:
01478 case URL_IS_PATH:
01479 path = lpath;
01480
01481 case URL_IS_UNKNOWN:
01482 break;
01483 case URL_IS_DASH:
01484 default:
01485 return NULL;
01486 break;
01487 }
01488
01489 return opendir(path);
01490
01491 }
01492
01493
01494 struct dirent * Readdir(DIR * dir)
01495 {
01496 if (_rpmio_debug)
01497 fprintf(stderr, "*** Readdir(%p)\n", (void *)dir);
01498 if (dir == NULL || ISFTPMAGIC(dir))
01499 return ftpReaddir(dir);
01500 return readdir(dir);
01501 }
01502
01503 int Closedir(DIR * dir)
01504 {
01505 if (_rpmio_debug)
01506 fprintf(stderr, "*** Closedir(%p)\n", (void *)dir);
01507 if (dir == NULL || ISFTPMAGIC(dir))
01508 return ftpClosedir(dir);
01509 return closedir(dir);
01510 }
01511