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