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 & 0x000000ffU) << 8) | (min & 0x000000ffU);
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 static int ftpNLST(const char * url, ftpSysCall_t ftpSysCall,
00825 struct stat * st,
00826 char * rlbuf, size_t rlbufsiz)
00827
00828
00829
00830
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
00860 if ((bn = strrchr(urldn, '/')) == NULL)
00861 return -2;
00862 else if (bn == path)
00863 bn = ".";
00864 else
00865 *bn++ = '\0';
00866
00867 nbn = strlen(bn);
00868
00869 rc = ftpChdir(urldn);
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
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
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) {
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 break;
00930 *se++ = '\0';
00931
00932 if (!strncmp(s, "total ", sizeof("total ")-1))
00933 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 break;
00941 case ' ':
00942 if (o || !(n[-3] == ' ' && n[-2] == '-' && n[-1] == '>')) {
00943 while (*(++n) == ' ')
00944 {};
00945 bingo++;
00946 break;
00947 }
00948 for (o = n + 1; *o == ' '; o++)
00949 {};
00950 n -= 3;
00951 ne = n;
00952 break;
00953 default:
00954 break;
00955 }
00956 if (bingo)
00957 break;
00958 }
00959
00960 if (nbn != (ne - n))
00961 continue;
00962 if (strncmp(n, bn, nbn))
00963 continue;
00964
00965 moretodo = 0;
00966 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
00982 }
00983
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;
01005 break;
01006 case DO_FTP_GLOB:
01007 rc = 0;
01008 break;
01009 }
01010
01011 exit:
01012 (void) ufdClose(fd);
01013 return rc;
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 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, struct stat *st)
01053
01054
01055 {
01056 char buf[1024];
01057 int rc;
01058 rc = ftpNLST(path, DO_FTP_LSTAT, st, NULL, 0);
01059
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, char * buf, size_t bufsiz)
01068
01069
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;
01080 char * data;
01081 size_t allocation;
01082 size_t size;
01083 size_t offset;
01084 off_t filepos;
01085 #if defined(HAVE_PTHREAD_H)
01086 pthread_mutex_t lock;
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
01106 static int ftpmagicdir = 0x8440291;
01107 #define ISFTPMAGIC(_dir) (!memcmp((_dir), &ftpmagicdir, sizeof(ftpmagicdir)))
01108
01109
01110
01111 static DIR * ftpOpendir(const char * path)
01112
01113
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
01134
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 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 break;
01158 default:
01159 break;
01160 }
01161 }
01162
01163 nb += sizeof(*mydir) + sizeof(*dp) + ((ac + 1) * sizeof(*av)) + (ac + 1);
01164 mydir = xcalloc(1, nb);
01165
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
01171
01172 mydir->fd = ftpmagicdir;
01173
01174 mydir->data = (char *) dp;
01175
01176 mydir->allocation = nb;
01177 mydir->size = ac;
01178 mydir->offset = -1;
01179 mydir->filepos = 0;
01180
01181 ac = 0;
01182
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
01186 sb = NULL;
01187 s = se = ftpBuf;
01188 while ((c = *se) != '\0') {
01189 se++;
01190 switch (c) {
01191 case '/':
01192 sb = se;
01193 break;
01194 case '\r':
01195
01196 av[ac] = t;
01197
01198 if (sb == NULL) {
01199
01200 switch(*s) {
01201 case 'p':
01202 dt[ac] = DT_FIFO;
01203 break;
01204 case 'c':
01205 dt[ac] = DT_CHR;
01206 break;
01207 case 'd':
01208 dt[ac] = DT_DIR;
01209 break;
01210 case 'b':
01211 dt[ac] = DT_BLK;
01212 break;
01213 case '-':
01214 dt[ac] = DT_REG;
01215 break;
01216 case 'l':
01217 dt[ac] = DT_LNK;
01218 break;
01219 case 's':
01220 dt[ac] = DT_SOCK;
01221 break;
01222 default:
01223 dt[ac] = DT_UNKNOWN;
01224 break;
01225 }
01226
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 break;
01237 default:
01238 break;
01239 }
01240 }
01241 av[ac] = NULL;
01242
01243
01244 return (DIR *) mydir;
01245
01246 }
01247
01248
01249
01250 static struct dirent * ftpReaddir(DIR * dir)
01251
01252
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
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
01273 if (i < 0 || i >= ac || av[i] == NULL)
01274 return NULL;
01275
01276
01277 mydir->offset = i;
01278
01279
01280
01281 dp->d_ino = i + 1;
01282 dp->d_reclen = 0;
01283
01284 #if !defined(hpux) && !defined(sun)
01285 dp->d_off = 0;
01286
01287 dp->d_type = dt[i];
01288
01289 #endif
01290
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( DIR * dir)
01300
01301
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
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 break;
01327 case URL_IS_HTTP:
01328 case URL_IS_PATH:
01329 path = lpath;
01330
01331 case URL_IS_UNKNOWN:
01332 break;
01333 case URL_IS_DASH:
01334 default:
01335 return -2;
01336 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 break;
01352 case URL_IS_HTTP:
01353 case URL_IS_PATH:
01354 path = lpath;
01355
01356 case URL_IS_UNKNOWN:
01357 break;
01358 case URL_IS_DASH:
01359 default:
01360 return -2;
01361 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 break;
01375 case URL_IS_HTTP:
01376 case URL_IS_PATH:
01377 path = lpath;
01378
01379 case URL_IS_UNKNOWN:
01380 break;
01381 case URL_IS_DASH:
01382 default:
01383 return -2;
01384 break;
01385 }
01386
01387 return readlink(path, buf, bufsiz);
01388
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:
01400 case URL_IS_HTTP:
01401 case URL_IS_PATH:
01402 path = lpath;
01403
01404 case URL_IS_UNKNOWN:
01405 break;
01406 case URL_IS_DASH:
01407 default:
01408 return -2;
01409 break;
01410 }
01411 return access(path, amode);
01412 }
01413
01414 int Glob(const char *pattern, int flags,
01415 int errfunc(const char * epath, int eerrno), glob_t *pglob)
01416 {
01417 const char * lpath;
01418 int ut = urlPath(pattern, &lpath);
01419
01420
01421 if (_rpmio_debug)
01422 fprintf(stderr, "*** Glob(%s,0x%x,%p,%p)\n", pattern, (unsigned)flags, (void *)errfunc, pglob);
01423
01424 switch (ut) {
01425 case URL_IS_FTP:
01426
01427 pglob->gl_closedir = Closedir;
01428 pglob->gl_readdir = Readdir;
01429 pglob->gl_opendir = Opendir;
01430 pglob->gl_lstat = Lstat;
01431 pglob->gl_stat = Stat;
01432
01433 flags |= GLOB_ALTDIRFUNC;
01434 break;
01435 case URL_IS_HTTP:
01436 case URL_IS_PATH:
01437 pattern = lpath;
01438
01439 case URL_IS_UNKNOWN:
01440 break;
01441 case URL_IS_DASH:
01442 default:
01443 return -2;
01444 break;
01445 }
01446 return glob(pattern, flags, errfunc, pglob);
01447 }
01448
01449 void Globfree(glob_t *pglob)
01450 {
01451 if (_rpmio_debug)
01452 fprintf(stderr, "*** Globfree(%p)\n", pglob);
01453 globfree(pglob);
01454 }
01455
01456 DIR * Opendir(const char * path)
01457 {
01458 const char * lpath;
01459 int ut = urlPath(path, &lpath);
01460
01461 if (_rpmio_debug)
01462 fprintf(stderr, "*** Opendir(%s)\n", path);
01463 switch (ut) {
01464 case URL_IS_FTP:
01465 return ftpOpendir(path);
01466 break;
01467 case URL_IS_HTTP:
01468 case URL_IS_PATH:
01469 path = lpath;
01470
01471 case URL_IS_UNKNOWN:
01472 break;
01473 case URL_IS_DASH:
01474 default:
01475 return NULL;
01476 break;
01477 }
01478
01479 return opendir(path);
01480
01481 }
01482
01483 struct dirent * Readdir(DIR * dir)
01484 {
01485 if (_rpmio_debug)
01486 fprintf(stderr, "*** Readdir(%p)\n", (void *)dir);
01487 if (dir == NULL || ISFTPMAGIC(dir))
01488 return ftpReaddir(dir);
01489 return readdir(dir);
01490 }
01491
01492 int Closedir(DIR * dir)
01493 {
01494 if (_rpmio_debug)
01495 fprintf(stderr, "*** Closedir(%p)\n", (void *)dir);
01496 if (dir == NULL || ISFTPMAGIC(dir))
01497 return ftpClosedir(dir);
01498 return closedir(dir);
01499 }