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