00001
00005 #include "system.h"
00006
00007 #if defined(HAVE_PTHREAD_H) && !defined(__LCLINT__)
00008 #include <pthread.h>
00009 #endif
00010
00011 #include <rpmio_internal.h>
00012 #include <popt.h>
00013 #include "ugid.h"
00014 #include "debug.h"
00015
00016
00017
00018
00019
00020
00021 static int ftpMkdir(const char * path, mode_t mode)
00022
00023
00024 {
00025 int rc;
00026 if ((rc = ftpCmd("MKD", path, NULL)) != 0)
00027 return rc;
00028 #if NOTYET
00029 { char buf[20];
00030 sprintf(buf, " 0%o", mode);
00031 (void) ftpCmd("SITE CHMOD", path, buf);
00032 }
00033 #endif
00034 return rc;
00035 }
00036
00037 static int ftpChdir(const char * path)
00038
00039
00040 {
00041 return ftpCmd("CWD", path, NULL);
00042 }
00043
00044 static int ftpRmdir(const char * path)
00045
00046
00047 {
00048 return ftpCmd("RMD", path, NULL);
00049 }
00050
00051 static int ftpRename(const char * oldpath, const char * newpath)
00052
00053
00054 {
00055 int rc;
00056 if ((rc = ftpCmd("RNFR", oldpath, NULL)) != 0)
00057 return rc;
00058 return ftpCmd("RNTO", newpath, NULL);
00059 }
00060
00061 static int ftpUnlink(const char * path)
00062
00063
00064 {
00065 return ftpCmd("DELE", path, NULL);
00066 }
00067
00068
00069
00070 int Mkdir (const char * path, mode_t mode)
00071 {
00072 const char * lpath;
00073 int ut = urlPath(path, &lpath);
00074
00075 switch (ut) {
00076 case URL_IS_FTP:
00077 return ftpMkdir(path, mode);
00078 break;
00079 case URL_IS_HTTP:
00080 case URL_IS_PATH:
00081 path = lpath;
00082
00083 case URL_IS_UNKNOWN:
00084 break;
00085 case URL_IS_DASH:
00086 default:
00087 return -2;
00088 break;
00089 }
00090 return mkdir(path, mode);
00091 }
00092
00093 int Chdir (const char * path)
00094 {
00095 const char * lpath;
00096 int ut = urlPath(path, &lpath);
00097
00098 switch (ut) {
00099 case URL_IS_FTP:
00100 return ftpChdir(path);
00101 break;
00102 case URL_IS_HTTP:
00103 case URL_IS_PATH:
00104 path = lpath;
00105
00106 case URL_IS_UNKNOWN:
00107 break;
00108 case URL_IS_DASH:
00109 default:
00110 return -2;
00111 break;
00112 }
00113 return chdir(path);
00114 }
00115
00116 int Rmdir (const char * path)
00117 {
00118 const char * lpath;
00119 int ut = urlPath(path, &lpath);
00120
00121 switch (ut) {
00122 case URL_IS_FTP:
00123 return ftpRmdir(path);
00124 break;
00125 case URL_IS_HTTP:
00126 case URL_IS_PATH:
00127 path = lpath;
00128
00129 case URL_IS_UNKNOWN:
00130 break;
00131 case URL_IS_DASH:
00132 default:
00133 return -2;
00134 break;
00135 }
00136 return rmdir(path);
00137 }
00138
00139
00140
00141 int Rename (const char * oldpath, const char * newpath)
00142 {
00143 const char *oe = NULL;
00144 const char *ne = NULL;
00145 int oldut, newut;
00146
00147
00148 if (!strcmp(oldpath, newpath)) return 0;
00149
00150 oldut = urlPath(oldpath, &oe);
00151 switch (oldut) {
00152 case URL_IS_FTP:
00153 case URL_IS_HTTP:
00154 case URL_IS_PATH:
00155 case URL_IS_UNKNOWN:
00156 break;
00157 case URL_IS_DASH:
00158 default:
00159 return -2;
00160 break;
00161 }
00162
00163 newut = urlPath(newpath, &ne);
00164 switch (newut) {
00165 case URL_IS_FTP:
00166 if (_rpmio_debug)
00167 fprintf(stderr, "*** rename old %*s new %*s\n", (int)(oe - oldpath), oldpath, (int)(ne - newpath), newpath);
00168 if (!(oldut == newut && oe && ne && (oe - oldpath) == (ne - newpath) &&
00169 !xstrncasecmp(oldpath, newpath, (oe - oldpath))))
00170 return -2;
00171 return ftpRename(oldpath, newpath);
00172 break;
00173 case URL_IS_HTTP:
00174 case URL_IS_PATH:
00175 oldpath = oe;
00176 newpath = ne;
00177 break;
00178 case URL_IS_UNKNOWN:
00179 break;
00180 case URL_IS_DASH:
00181 default:
00182 return -2;
00183 break;
00184 }
00185 return rename(oldpath, newpath);
00186 }
00187
00188 int Link (const char * oldpath, const char * newpath)
00189 {
00190 const char *oe = NULL;
00191 const char *ne = NULL;
00192 int oldut, newut;
00193
00194 oldut = urlPath(oldpath, &oe);
00195 switch (oldut) {
00196 case URL_IS_FTP:
00197 case URL_IS_HTTP:
00198 case URL_IS_PATH:
00199 case URL_IS_UNKNOWN:
00200 break;
00201 case URL_IS_DASH:
00202 default:
00203 return -2;
00204 break;
00205 }
00206
00207 newut = urlPath(newpath, &ne);
00208 switch (newut) {
00209 case URL_IS_HTTP:
00210 case URL_IS_FTP:
00211 case URL_IS_PATH:
00212 if (_rpmio_debug)
00213 fprintf(stderr, "*** link old %*s new %*s\n", (int)(oe - oldpath), oldpath, (int)(ne - newpath), newpath);
00214 if (!(oldut == newut && oe && ne && (oe - oldpath) == (ne - newpath) &&
00215 !xstrncasecmp(oldpath, newpath, (oe - oldpath))))
00216 return -2;
00217 oldpath = oe;
00218 newpath = ne;
00219 break;
00220 case URL_IS_UNKNOWN:
00221 break;
00222 case URL_IS_DASH:
00223 default:
00224 return -2;
00225 break;
00226 }
00227 return link(oldpath, newpath);
00228 }
00229
00230
00231
00232 int Unlink(const char * path) {
00233 const char * lpath;
00234 int ut = urlPath(path, &lpath);
00235
00236 switch (ut) {
00237 case URL_IS_FTP:
00238 return ftpUnlink(path);
00239 break;
00240 case URL_IS_HTTP:
00241 case URL_IS_PATH:
00242 path = lpath;
00243
00244 case URL_IS_UNKNOWN:
00245 break;
00246 case URL_IS_DASH:
00247 default:
00248 return -2;
00249 break;
00250 }
00251 return unlink(path);
00252 }
00253
00254
00255
00256 #define g_strdup xstrdup
00257 #define g_free free
00258
00259
00260
00261
00262
00263 static int current_mday;
00264
00265 static int current_mon;
00266
00267 static int current_year;
00268
00269
00270 #define MAXCOLS 30
00271
00272
00273 static char *columns [MAXCOLS];
00274
00275 static int column_ptr [MAXCOLS];
00276
00277
00278 static int
00279 vfs_split_text (char *p)
00280
00281
00282 {
00283 char *original = p;
00284 int numcols;
00285
00286
00287 for (numcols = 0; *p && numcols < MAXCOLS; numcols++){
00288 while (*p == ' ' || *p == '\r' || *p == '\n'){
00289 *p = 0;
00290 p++;
00291 }
00292 columns [numcols] = p;
00293 column_ptr [numcols] = p - original;
00294 while (*p && *p != ' ' && *p != '\r' && *p != '\n')
00295 p++;
00296 }
00297 return numcols;
00298 }
00299
00300
00301
00302 static int
00303 is_num (int idx)
00304
00305 {
00306 if (!columns [idx] || columns [idx][0] < '0' || columns [idx][0] > '9')
00307 return 0;
00308 return 1;
00309 }
00310
00311
00312
00313 static int
00314 is_dos_date( const char *str)
00315
00316 {
00317 if (str != NULL && strlen(str) == 8 &&
00318 str[2] == str[5] && strchr("\\-/", (int)str[2]) != NULL)
00319 return 1;
00320 return 0;
00321 }
00322
00323
00324 static int
00325 is_week ( const char * str, struct tm * tim)
00326
00327 {
00328 static const char * week = "SunMonTueWedThuFriSat";
00329 const char * pos;
00330
00331
00332 if (str != NULL && (pos=strstr(week, str)) != NULL) {
00333
00334 if (tim != NULL)
00335 tim->tm_wday = (pos - week)/3;
00336 return 1;
00337 }
00338 return 0;
00339 }
00340
00341 static int
00342 is_month ( const char * str, struct tm * tim)
00343
00344 {
00345 static const char * month = "JanFebMarAprMayJunJulAugSepOctNovDec";
00346 const char * pos;
00347
00348
00349 if (str != NULL && (pos = strstr(month, str)) != NULL) {
00350
00351 if (tim != NULL)
00352 tim->tm_mon = (pos - month)/3;
00353 return 1;
00354 }
00355 return 0;
00356 }
00357
00358 static int
00359 is_time ( const char * str, struct tm * tim)
00360
00361 {
00362 const char * p, * p2;
00363
00364 if (str != NULL && (p = strchr(str, ':')) && (p2 = strrchr(str, ':'))) {
00365 if (p != p2) {
00366 if (sscanf (str, "%2d:%2d:%2d", &tim->tm_hour, &tim->tm_min, &tim->tm_sec) != 3)
00367 return 0;
00368 } else {
00369 if (sscanf (str, "%2d:%2d", &tim->tm_hour, &tim->tm_min) != 2)
00370 return 0;
00371 }
00372 } else
00373 return 0;
00374
00375 return 1;
00376 }
00377
00378 static int is_year( const char * str, struct tm * tim)
00379
00380 {
00381 long year;
00382
00383 if (str == NULL)
00384 return 0;
00385
00386 if (strchr(str,':'))
00387 return 0;
00388
00389 if (strlen(str) != 4)
00390 return 0;
00391
00392 if (sscanf(str, "%ld", &year) != 1)
00393 return 0;
00394
00395 if (year < 1900 || year > 3000)
00396 return 0;
00397
00398 tim->tm_year = (int) (year - 1900);
00399
00400 return 1;
00401 }
00402
00403
00404
00405
00406
00407
00408
00409 static int
00410 vfs_parse_filetype (char c)
00411
00412 {
00413 switch (c) {
00414 case 'd': return S_IFDIR;
00415 case 'b': return S_IFBLK;
00416 case 'c': return S_IFCHR;
00417 case 'l': return S_IFLNK;
00418 case 's':
00419 #ifdef IS_IFSOCK
00420 return S_IFSOCK;
00421 #endif
00422 case 'p': return S_IFIFO;
00423 case 'm': case 'n':
00424 case '-': case '?': return S_IFREG;
00425 default: return -1;
00426 }
00427 }
00428
00429 static int vfs_parse_filemode (const char *p)
00430
00431 {
00432 int res = 0;
00433 switch (*(p++)) {
00434 case 'r': res |= 0400; break;
00435 case '-': break;
00436 default: return -1;
00437 }
00438 switch (*(p++)) {
00439 case 'w': res |= 0200; break;
00440 case '-': break;
00441 default: return -1;
00442 }
00443 switch (*(p++)) {
00444 case 'x': res |= 0100; break;
00445 case 's': res |= 0100 | S_ISUID; break;
00446 case 'S': res |= S_ISUID; break;
00447 case '-': break;
00448 default: return -1;
00449 }
00450 switch (*(p++)) {
00451 case 'r': res |= 0040; break;
00452 case '-': break;
00453 default: return -1;
00454 }
00455 switch (*(p++)) {
00456 case 'w': res |= 0020; break;
00457 case '-': break;
00458 default: return -1;
00459 }
00460 switch (*(p++)) {
00461 case 'x': res |= 0010; break;
00462 case 's': res |= 0010 | S_ISGID; break;
00463 case 'l':
00464 case 'S': res |= S_ISGID; break;
00465 case '-': break;
00466 default: return -1;
00467 }
00468 switch (*(p++)) {
00469 case 'r': res |= 0004; break;
00470 case '-': break;
00471 default: return -1;
00472 }
00473 switch (*(p++)) {
00474 case 'w': res |= 0002; break;
00475 case '-': break;
00476 default: return -1;
00477 }
00478 switch (*(p++)) {
00479 case 'x': res |= 0001; break;
00480 case 't': res |= 0001 | S_ISVTX; break;
00481 case 'T': res |= S_ISVTX; break;
00482 case '-': break;
00483 default: return -1;
00484 }
00485 return res;
00486 }
00487
00488
00489 static int vfs_parse_filedate(int idx, time_t *t)
00490
00491 {
00492
00493 char *p;
00494 struct tm tim;
00495 int d[3];
00496 int got_year = 0;
00497
00498
00499 tim.tm_year = current_year;
00500 tim.tm_mon = current_mon;
00501 tim.tm_mday = current_mday;
00502 tim.tm_hour = 0;
00503 tim.tm_min = 0;
00504 tim.tm_sec = 0;
00505 tim.tm_isdst = -1;
00506
00507 p = columns [idx++];
00508
00509
00510 if(is_week(p, &tim))
00511 p = columns [idx++];
00512
00513
00514 if(is_month(p, &tim)){
00515
00516 if (is_num (idx))
00517 tim.tm_mday = (int)atol (columns [idx++]);
00518 else
00519 return 0;
00520
00521 } else {
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534 if (is_dos_date(p)){
00535
00536 p[2] = p[5] = '-';
00537
00538
00539 memset(d, 0, sizeof(d));
00540 if (sscanf(p, "%2d-%2d-%2d", &d[0], &d[1], &d[2]) == 3){
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550 d[0]--;
00551
00552 if(d[2] < 70)
00553 d[2] += 100;
00554
00555 tim.tm_mon = d[0];
00556 tim.tm_mday = d[1];
00557 tim.tm_year = d[2];
00558 got_year = 1;
00559 } else
00560 return 0;
00561 } else
00562 return 0;
00563 }
00564
00565
00566
00567 if (is_num (idx)) {
00568 if(is_time(columns[idx], &tim) || (got_year = is_year(columns[idx], &tim))) {
00569 idx++;
00570
00571
00572 if(is_num (idx) &&
00573 ((got_year = is_year(columns[idx], &tim)) || is_time(columns[idx], &tim)))
00574 idx++;
00575 }
00576 }
00577 else
00578 return 0;
00579
00580
00581
00582
00583
00584
00585
00586 if (!got_year &&
00587 current_mon < 6 && current_mon < tim.tm_mon &&
00588 tim.tm_mon - current_mon >= 6)
00589
00590 tim.tm_year--;
00591
00592 if ((*t = mktime(&tim)) < 0)
00593 *t = 0;
00594 return idx;
00595 }
00596
00597
00598
00599 static int
00600 vfs_parse_ls_lga (char * p, struct stat * st,
00601 const char ** filename,
00602 const char ** linkname)
00603
00604 {
00605 int idx, idx2, num_cols;
00606 int i;
00607 char *p_copy;
00608
00609 if (strncmp (p, "total", 5) == 0)
00610 return 0;
00611
00612 p_copy = g_strdup(p);
00613
00614
00615
00616 if ((i = vfs_parse_filetype(*(p++))) == -1)
00617 goto error;
00618
00619 st->st_mode = i;
00620 if (*p == ' ')
00621 p++;
00622 if (*p == '['){
00623 if (strlen (p) <= 8 || p [8] != ']')
00624 goto error;
00625
00626
00627 if (S_ISDIR (st->st_mode))
00628 st->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IXUSR | S_IXGRP | S_IXOTH);
00629 else
00630 st->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);
00631 p += 9;
00632
00633 } else {
00634 if ((i = vfs_parse_filemode(p)) == -1)
00635 goto error;
00636 st->st_mode |= i;
00637 p += 9;
00638
00639
00640 if (*p == '+')
00641 p++;
00642 }
00643
00644 g_free(p_copy);
00645 p_copy = g_strdup(p);
00646 num_cols = vfs_split_text (p);
00647
00648 st->st_nlink = atol (columns [0]);
00649 if (st->st_nlink < 0)
00650 goto error;
00651
00652 if (!is_num (1))
00653 #ifdef HACK
00654 st->st_uid = finduid (columns [1]);
00655 #else
00656 (void) unameToUid (columns [1], &st->st_uid);
00657 #endif
00658 else
00659 st->st_uid = (uid_t) atol (columns [1]);
00660
00661
00662 for (idx = 3; idx <= 5; idx++)
00663 if (is_month(columns [idx], NULL) || is_week(columns [idx], NULL) || is_dos_date(columns[idx]))
00664 break;
00665
00666 if (idx == 6 || (idx == 5 && !S_ISCHR (st->st_mode) && !S_ISBLK (st->st_mode)))
00667 goto error;
00668
00669
00670 if (idx == 3 || (idx == 4 && (S_ISCHR(st->st_mode) || S_ISBLK (st->st_mode))))
00671 idx2 = 2;
00672 else {
00673
00674 if (is_num (2))
00675 st->st_gid = (gid_t) atol (columns [2]);
00676 else
00677 #ifdef HACK
00678 st->st_gid = findgid (columns [2]);
00679 #else
00680 (void) gnameToGid (columns [1], &st->st_gid);
00681 #endif
00682 idx2 = 3;
00683 }
00684
00685
00686 if (S_ISCHR (st->st_mode) || S_ISBLK (st->st_mode)){
00687 int maj, min;
00688
00689 if (!is_num (idx2) || sscanf(columns [idx2], " %d,", &maj) != 1)
00690 goto error;
00691
00692 if (!is_num (++idx2) || sscanf(columns [idx2], " %d", &min) != 1)
00693 goto error;
00694
00695 #ifdef HAVE_ST_RDEV
00696 st->st_rdev = ((maj & 0xff) << 8) | (min & 0xffff00ff);
00697 #endif
00698 st->st_size = 0;
00699
00700 } else {
00701
00702 if (!is_num (idx2))
00703 goto error;
00704
00705 st->st_size = (size_t) atol (columns [idx2]);
00706 #ifdef HAVE_ST_RDEV
00707 st->st_rdev = 0;
00708 #endif
00709 }
00710
00711 idx = vfs_parse_filedate(idx, &st->st_mtime);
00712 if (!idx)
00713 goto error;
00714
00715 st->st_atime = st->st_ctime = st->st_mtime;
00716 st->st_dev = 0;
00717 st->st_ino = 0;
00718 #ifdef HAVE_ST_BLKSIZE
00719 st->st_blksize = 512;
00720 #endif
00721 #ifdef HAVE_ST_BLOCKS
00722 st->st_blocks = (st->st_size + 511) / 512;
00723 #endif
00724
00725 for (i = idx + 1, idx2 = 0; i < num_cols; i++ )
00726 if (strcmp (columns [i], "->") == 0){
00727 idx2 = i;
00728 break;
00729 }
00730
00731 if (((S_ISLNK (st->st_mode) ||
00732 (num_cols == idx + 3 && st->st_nlink > 1)))
00733 && idx2){
00734 int tlen;
00735 char *t;
00736
00737 if (filename){
00738 #ifdef HACK
00739 t = g_strndup (p_copy + column_ptr [idx], column_ptr [idx2] - column_ptr [idx] - 1);
00740 #else
00741 int nb = column_ptr [idx2] - column_ptr [idx] - 1;
00742 t = xmalloc(nb+1);
00743 strncpy(t, p_copy + column_ptr [idx], nb);
00744 #endif
00745 *filename = t;
00746 }
00747 if (linkname){
00748 t = g_strdup (p_copy + column_ptr [idx2+1]);
00749 tlen = strlen (t);
00750 if (t [tlen-1] == '\r' || t [tlen-1] == '\n')
00751 t [tlen-1] = 0;
00752 if (t [tlen-2] == '\r' || t [tlen-2] == '\n')
00753 t [tlen-2] = 0;
00754
00755 *linkname = t;
00756 }
00757 } else {
00758
00759
00760
00761 if (filename){
00762
00763
00764
00765 int tlen;
00766 char *t;
00767
00768 t = g_strdup (p_copy + column_ptr [idx]); idx++;
00769 tlen = strlen (t);
00770
00771 if (t [tlen-1] == '\r' || t [tlen-1] == '\n')
00772 t [tlen-1] = 0;
00773 if (t [tlen-2] == '\r' || t [tlen-2] == '\n')
00774 t [tlen-2] = 0;
00775
00776 *filename = t;
00777 }
00778 if (linkname)
00779 *linkname = NULL;
00780 }
00781 g_free (p_copy);
00782 return 1;
00783
00784 error:
00785 #ifdef HACK
00786 {
00787 static int errorcount = 0;
00788
00789 if (++errorcount < 5) {
00790 message_1s (1, "Could not parse:", p_copy);
00791 } else if (errorcount == 5)
00792 message_1s (1, "More parsing errors will be ignored.", "(sorry)" );
00793 }
00794 #endif
00795
00796
00797 if (p_copy != p)
00798
00799 g_free (p_copy);
00800 return 0;
00801 }
00802
00803
00804 typedef enum {
00805 DO_FTP_STAT = 1,
00806 DO_FTP_LSTAT = 2,
00807 DO_FTP_READLINK = 3,
00808 DO_FTP_ACCESS = 4,
00809 DO_FTP_GLOB = 5
00810 } ftpSysCall_t;
00811
00814
00815 static size_t ftpBufAlloced = 0;
00816
00819
00820 static char * ftpBuf = NULL;
00821
00822 #define alloca_strdup(_s) strcpy(alloca(strlen(_s)+1), (_s))
00823
00824
00825
00826 static int ftpNLST(const char * url, ftpSysCall_t ftpSysCall,
00827 struct stat * st,
00828 char * rlbuf, size_t rlbufsiz)
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 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 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)
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