6 #include <rpmio_internal.h>
14 extern int _rpmio_debug;
16 /* =============================================================== */
17 static int ftpMkdir(const char * path, /*@unused@*/ mode_t mode) {
19 if ((rc = ftpCmd("MKD", path, NULL)) != 0)
23 sprintf(buf, " 0%o", mode);
24 (void) ftpCmd("SITE CHMOD", path, buf);
30 static int ftpChdir(const char * path) {
31 return ftpCmd("CWD", path, NULL);
34 static int ftpRmdir(const char * path) {
35 return ftpCmd("RMD", path, NULL);
38 static int ftpRename(const char * oldpath, const char * newpath) {
40 if ((rc = ftpCmd("RNFR", oldpath, NULL)) != 0)
42 return ftpCmd("RNTO", newpath, NULL);
45 static int ftpUnlink(const char * path) {
46 return ftpCmd("DELE", path, NULL);
49 /* =============================================================== */
50 /* XXX rebuilddb.c: analogues to mkdir(2)/rmdir(2). */
51 int Mkdir (const char *path, mode_t mode) {
53 int ut = urlPath(path, &lpath);
57 return ftpMkdir(path, mode);
58 /*@notreached@*/ break;
59 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
68 /*@notreached@*/ break;
70 return mkdir(path, mode);
73 int Chdir (const char *path) {
75 int ut = urlPath(path, &lpath);
79 return ftpChdir(path);
80 /*@notreached@*/ break;
81 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
90 /*@notreached@*/ break;
95 int Rmdir (const char *path) {
97 int ut = urlPath(path, &lpath);
101 return ftpRmdir(path);
102 /*@notreached@*/ break;
103 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
112 /*@notreached@*/ break;
117 /* XXX rpmdb.c: analogue to rename(2). */
119 int Rename (const char *oldpath, const char * newpath) {
120 const char *oe = NULL;
121 const char *ne = NULL;
124 /* XXX lib/install.c used to rely on this behavior. */
125 if (!strcmp(oldpath, newpath)) return 0;
127 oldut = urlPath(oldpath, &oe);
129 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
130 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
137 /*@notreached@*/ break;
140 newut = urlPath(newpath, &ne);
144 fprintf(stderr, "*** rename old %*s new %*s\n", (int)(oe - oldpath), oldpath, (int)(ne - newpath), newpath);
145 if (!(oldut == newut && oe && ne && (oe - oldpath) == (ne - newpath) &&
146 !xstrncasecmp(oldpath, newpath, (oe - oldpath))))
148 return ftpRename(oldpath, newpath);
149 /*@notreached@*/ break;
150 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
160 /*@notreached@*/ break;
162 return rename(oldpath, newpath);
165 int Link (const char *oldpath, const char * newpath) {
166 const char *oe = NULL;
167 const char *ne = NULL;
170 oldut = urlPath(oldpath, &oe);
172 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
173 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
180 /*@notreached@*/ break;
183 newut = urlPath(newpath, &ne);
185 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
186 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
189 fprintf(stderr, "*** link old %*s new %*s\n", (int)(oe - oldpath), oldpath, (int)(ne - newpath), newpath);
190 if (!(oldut == newut && oe && ne && (oe - oldpath) == (ne - newpath) &&
191 !xstrncasecmp(oldpath, newpath, (oe - oldpath))))
201 /*@notreached@*/ break;
203 return link(oldpath, newpath);
206 /* XXX build/build.c: analogue to unlink(2). */
208 int Unlink(const char * path) {
210 int ut = urlPath(path, &lpath);
214 return ftpUnlink(path);
215 /*@notreached@*/ break;
216 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
225 /*@notreached@*/ break;
230 /* XXX swiped from mc-4.5.39-pre9 vfs/ftpfs.c */
232 #define g_strdup xstrdup
236 * FIXME: this is broken. It depends on mc not crossing border on month!
238 static int current_mday;
239 static int current_mon;
240 static int current_year;
242 /* Following stuff (parse_ls_lga) is used by ftpfs and extfs */
245 static char *columns [MAXCOLS]; /* Points to the string in column n */
246 static int column_ptr [MAXCOLS]; /* Index from 0 to the starting positions of the columns */
249 vfs_split_text (char *p)
255 for (numcols = 0; *p && numcols < MAXCOLS; numcols++){
256 while (*p == ' ' || *p == '\r' || *p == '\n'){
260 columns [numcols] = p;
261 column_ptr [numcols] = p - original;
262 while (*p && *p != ' ' && *p != '\r' && *p != '\n')
271 if (!columns [idx] || columns [idx][0] < '0' || columns [idx][0] > '9')
277 is_dos_date(char *str)
279 if (strlen(str) == 8 && str[2] == str[5] && strchr("\\-/", (int)str[2]) != NULL)
286 is_week (char *str, struct tm *tim)
288 static char *week = "SunMonTueWedThuFriSat";
291 if((pos=strstr(week, str)) != NULL){
293 tim->tm_wday = (pos - week)/3;
300 is_month (char *str, struct tm *tim)
302 static char *month = "JanFebMarAprMayJunJulAugSepOctNovDec";
305 if((pos=strstr(month, str)) != NULL){
307 tim->tm_mon = (pos - month)/3;
314 is_time (char *str, struct tm *tim)
318 if ((p=strchr(str, ':')) && (p2=strrchr(str, ':'))) {
320 if (sscanf (str, "%2d:%2d:%2d", &tim->tm_hour, &tim->tm_min, &tim->tm_sec) != 3)
324 if (sscanf (str, "%2d:%2d", &tim->tm_hour, &tim->tm_min) != 2)
334 static int is_year(char *str, struct tm *tim)
344 if (sscanf(str, "%ld", &year) != 1)
347 if (year < 1900 || year > 3000)
350 tim->tm_year = (int) (year - 1900);
356 * FIXME: this is broken. Consider following entry:
357 * -rwx------ 1 root root 1 Aug 31 10:04 2904 1234
358 * where "2904 1234" is filename. Well, this code decodes it as year :-(.
362 vfs_parse_filetype (char c)
365 case 'd': return S_IFDIR;
366 case 'b': return S_IFBLK;
367 case 'c': return S_IFCHR;
368 case 'l': return S_IFLNK;
370 #ifdef IS_IFSOCK /* And if not, we fall through to IFIFO, which is pretty close */
373 case 'p': return S_IFIFO;
374 case 'm': case 'n': /* Don't know what these are :-) */
375 case '-': case '?': return S_IFREG;
380 static int vfs_parse_filemode (char *p)
381 { /* converts rw-rw-rw- into 0666 */
384 case 'r': res |= 0400; break;
389 case 'w': res |= 0200; break;
394 case 'x': res |= 0100; break;
395 case 's': res |= 0100 | S_ISUID; break;
396 case 'S': res |= S_ISUID; break;
401 case 'r': res |= 0040; break;
406 case 'w': res |= 0020; break;
411 case 'x': res |= 0010; break;
412 case 's': res |= 0010 | S_ISGID; break;
413 case 'l': /* Solaris produces these */
414 case 'S': res |= S_ISGID; break;
419 case 'r': res |= 0004; break;
424 case 'w': res |= 0002; break;
429 case 'x': res |= 0001; break;
430 case 't': res |= 0001 | S_ISVTX; break;
431 case 'T': res |= S_ISVTX; break;
438 static int vfs_parse_filedate(int idx, time_t *t)
439 { /* This thing parses from idx in columns[] array */
446 /* Let's setup default time values */
447 tim.tm_year = current_year;
448 tim.tm_mon = current_mon;
449 tim.tm_mday = current_mday;
453 tim.tm_isdst = -1; /* Let mktime() try to guess correct dst offset */
457 /* We eat weekday name in case of extfs */
462 if(is_month(p, &tim)){
463 /* And we expect, it followed by day number */
465 tim.tm_mday = (int)atol (columns [idx++]);
467 return 0; /* No day */
470 /* We usually expect:
473 But in case of extfs we allow these date formats:
476 Wek Mon DD hh:mm:ss YYYY
478 where Mon is Jan-Dec, DD, MM, YY two digit day, month, year,
479 YYYY four digit year, hh, mm, ss two digit hour, minute or second. */
481 /* Here just this special case with MM-DD-YY */
485 if(sscanf(p, "%2d-%2d-%2d", &d[0], &d[1], &d[2]) == 3){
492 /* Hmm... maybe, next time :)*/
494 /* At last, MM-DD-YY */
495 d[0]--; /* Months are zerobased */
505 return 0; /* sscanf failed */
507 return 0; /* unsupported format */
510 /* Here we expect to find time and/or year */
513 if(is_time(columns[idx], &tim) || (got_year = is_year(columns[idx], &tim))) {
516 /* This is a special case for ctime() or Mon DD YYYY hh:mm */
518 ((got_year = is_year(columns[idx], &tim)) || is_time(columns[idx], &tim)))
519 idx++; /* time & year or reverse */
520 } /* only time or date */
523 return 0; /* Nor time or date */
526 * If the date is less than 6 months in the past, it is shown without year
527 * other dates in the past or future are shown with year but without time
528 * This does not check for years before 1900 ... I don't know, how
529 * to represent them at all
532 current_mon < 6 && current_mon < tim.tm_mon &&
533 tim.tm_mon - current_mon >= 6)
537 if ((*t = mktime(&tim)) < 0)
543 vfs_parse_ls_lga (char *p, struct stat *st, char **filename, char **linkname)
545 int idx, idx2, num_cols;
549 if (strncmp (p, "total", 5) == 0)
552 p_copy = g_strdup(p);
553 /* XXX FIXME: parse out inode number from "NLST -lai ." */
554 /* XXX FIXME: parse out sizein blocks from "NLST -lais ." */
556 if ((i = vfs_parse_filetype(*(p++))) == -1)
560 if (*p == ' ') /* Notwell 4 */
563 if (strlen (p) <= 8 || p [8] != ']')
565 /* Should parse here the Notwell permissions :) */
566 if (S_ISDIR (st->st_mode))
567 st->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IXUSR | S_IXGRP | S_IXOTH);
569 st->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);
572 if ((i = vfs_parse_filemode(p)) == -1)
577 /* This is for an extra ACL attribute (HP-UX) */
583 p_copy = g_strdup(p);
584 num_cols = vfs_split_text (p);
586 st->st_nlink = atol (columns [0]);
587 if (st->st_nlink < 0)
592 st->st_uid = finduid (columns [1]);
594 unameToUid (columns [1], &st->st_uid);
597 st->st_uid = (uid_t) atol (columns [1]);
599 /* Mhm, the ls -lg did not produce a group field */
600 for (idx = 3; idx <= 5; idx++)
601 if (is_month(columns [idx], NULL) || is_week(columns [idx], NULL) || is_dos_date(columns[idx]))
604 if (idx == 6 || (idx == 5 && !S_ISCHR (st->st_mode) && !S_ISBLK (st->st_mode)))
607 /* We don't have gid */
608 if (idx == 3 || (idx == 4 && (S_ISCHR(st->st_mode) || S_ISBLK (st->st_mode))))
611 /* We have gid field */
613 st->st_gid = (gid_t) atol (columns [2]);
616 st->st_gid = findgid (columns [2]);
618 gnameToGid (columns [1], &st->st_gid);
624 if (S_ISCHR (st->st_mode) || S_ISBLK (st->st_mode)){
627 if (!is_num (idx2) || sscanf(columns [idx2], " %d,", &maj) != 1)
630 if (!is_num (++idx2) || sscanf(columns [idx2], " %d", &min) != 1)
634 st->st_rdev = ((maj & 0xff) << 8) | (min & 0xffff00ff);
639 /* Common file size */
643 st->st_size = (size_t) atol (columns [idx2]);
649 idx = vfs_parse_filedate(idx, &st->st_mtime);
652 /* Use resulting time value */
653 st->st_atime = st->st_ctime = st->st_mtime;
656 #ifdef HAVE_ST_BLKSIZE
657 st->st_blksize = 512;
659 #ifdef HAVE_ST_BLOCKS
660 st->st_blocks = (st->st_size + 511) / 512;
663 for (i = idx + 1, idx2 = 0; i < num_cols; i++ )
664 if (strcmp (columns [i], "->") == 0){
669 if (((S_ISLNK (st->st_mode) ||
670 (num_cols == idx + 3 && st->st_nlink > 1))) /* Maybe a hardlink? (in extfs) */
677 t = g_strndup (p_copy + column_ptr [idx], column_ptr [idx2] - column_ptr [idx] - 1);
679 int nb = column_ptr [idx2] - column_ptr [idx] - 1;
681 strncpy(t, p_copy + column_ptr [idx], nb);
686 t = g_strdup (p_copy + column_ptr [idx2+1]);
688 if (t [tlen-1] == '\r' || t [tlen-1] == '\n')
690 if (t [tlen-2] == '\r' || t [tlen-2] == '\n')
696 /* Extract the filename from the string copy, not from the columns
697 * this way we have a chance of entering hidden directories like ". ."
701 *filename = g_strdup (columns [idx++]);
706 t = g_strdup (p_copy + column_ptr [idx++]);
709 if (t [tlen-1] == '\r' || t [tlen-1] == '\n')
711 if (t [tlen-2] == '\r' || t [tlen-2] == '\n')
725 static int errorcount = 0;
727 if (++errorcount < 5) {
728 message_1s (1, "Could not parse:", p_copy);
729 } else if (errorcount == 5)
730 message_1s (1, "More parsing errors will be ignored.", "(sorry)" );
734 if (p_copy != p) /* Carefull! */
746 static size_t ftpBufAlloced = 0;
747 static /*@only@*/ char * ftpBuf = NULL;
749 #define alloca_strdup(_s) strcpy(alloca(strlen(_s)+1), (_s))
751 static int ftpNLST(const char * url, ftpSysCall_t ftpSysCall,
752 struct stat * st, char * rlbuf, size_t rlbufsiz)
756 int bufLength, moretodo;
757 const char *n, *ne, *o, *oe;
766 n = ne = o = oe = NULL;
767 (void) urlPath(url, &path);
771 switch (ftpSysCall) {
773 fd = ftpOpen(url, 0, 0, &u);
774 if (fd == NULL || u == NULL)
777 u->openError = ftpReq(fd, "NLST", path);
780 urldn = alloca_strdup(url);
781 if ((bn = strrchr(urldn, '/')) == NULL)
789 rc = ftpChdir(urldn); /* XXX don't care about CWD */
793 fd = ftpOpen(url, 0, 0, &u);
794 if (fd == NULL || u == NULL)
797 /* XXX possibly should do "NLST -lais" to get st_ino/st_blocks also */
798 u->openError = ftpReq(fd, "NLST", "-la");
802 if (u->openError < 0) {
803 fd = fdLink(fd, "error data (ftpStat)");
808 if (ftpBufAlloced == 0 || ftpBuf == NULL) {
809 ftpBufAlloced = url_iobuf_size;
810 ftpBuf = xcalloc(ftpBufAlloced, sizeof(ftpBuf[0]));
819 /* XXX FIXME: realloc ftpBuf is < ~128 chars remain */
820 if ((ftpBufAlloced - bufLength) < (1024+80)) {
822 ftpBuf = xrealloc(ftpBuf, ftpBufAlloced);
824 s = se = ftpBuf + bufLength;
827 rc = fdFgets(fd, se, (ftpBufAlloced - bufLength));
832 if (ftpSysCall == DO_FTP_GLOB) { /* XXX HACK */
833 bufLength += strlen(se);
837 for (s = se; *s != '\0'; s = se) {
840 while (*se && *se != '\n') se++;
841 if (se > s && se[-1] == '\r') se[-1] = '\0';
842 if (*se == '\0') break;
845 if (!strncmp(s, "total ", sizeof("total ")-1)) continue;
848 for (bingo = 0, n = se; n >= s; n--) {
854 if (o || !(n[-3] == ' ' && n[-2] == '-' && n[-1] == '>')) {
855 while (*(++n) == ' ');
859 for (o = n + 1; *o == ' '; o++);
869 if (nbn != (ne - n)) continue; /* Same name length? */
870 if (strncmp(n, bn, nbn)) continue; /* Same name? */
876 if (moretodo && se > s) {
877 bufLength = se - s - 1;
879 memmove(ftpBuf, s, bufLength);
885 switch (ftpSysCall) {
888 /* XXX FIXME: symlink, replace urldn/bn from [o,oe) and restart */
892 if (st == NULL || !(n && ne)) {
895 rc = ((vfs_parse_ls_lga(s, st, NULL, NULL) > 0) ? 0 : -1);
898 case DO_FTP_READLINK:
899 if (rlbuf == NULL || !(o && oe)) {
905 memcpy(rlbuf, o, rc);
911 rc = 0; /* XXX WRONG WRONG WRONG */
914 rc = 0; /* XXX WRONG WRONG WRONG */
923 static int ftpStat(const char * path, struct stat *st)
925 return ftpNLST(path, DO_FTP_STAT, st, NULL, 0);
928 static int ftpLstat(const char * path, struct stat *st) {
930 rc = ftpNLST(path, DO_FTP_LSTAT, st, NULL, 0);
932 fprintf(stderr, "*** ftpLstat(%s) rc %d\n", path, rc);
936 static int ftpReadlink(const char * path, char * buf, size_t bufsiz) {
937 return ftpNLST(path, DO_FTP_READLINK, NULL, buf, bufsiz);
940 static int ftpGlob(const char * path, int flags,
941 int errfunc(const char * epath, int eerno), glob_t * pglob)
947 rc = ftpNLST(path, DO_FTP_GLOB, NULL, NULL, 0);
949 fprintf(stderr, "*** ftpGlob(%s,0x%x,%p,%p) ftpNLST rc %d\n", path, (unsigned)flags, errfunc, pglob, rc);
952 rc = poptParseArgvString(ftpBuf, &pglob->gl_pathc, (const char ***)&pglob->gl_pathv);
953 pglob->gl_offs = -1; /* XXX HACK HACK HACK */
957 static void ftpGlobfree(glob_t * pglob) {
959 fprintf(stderr, "*** ftpGlobfree(%p)\n", pglob);
960 if (pglob->gl_offs == -1) /* XXX HACK HACK HACK */
961 free((void *)pglob->gl_pathv);
964 int Stat(const char * path, struct stat * st) {
966 int ut = urlPath(path, &lpath);
969 fprintf(stderr, "*** Stat(%s,%p)\n", path, st);
972 return ftpStat(path, st);
973 /*@notreached@*/ break;
974 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
983 /*@notreached@*/ break;
985 return stat(path, st);
988 int Lstat(const char * path, struct stat * st) {
990 int ut = urlPath(path, &lpath);
993 fprintf(stderr, "*** Lstat(%s,%p)\n", path, st);
996 return ftpLstat(path, st);
997 /*@notreached@*/ break;
998 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1002 case URL_IS_UNKNOWN:
1007 /*@notreached@*/ break;
1009 return lstat(path, st);
1012 int Readlink(const char * path, char * buf, size_t bufsiz) {
1014 int ut = urlPath(path, &lpath);
1018 return ftpReadlink(path, buf, bufsiz);
1019 /*@notreached@*/ break;
1020 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1024 case URL_IS_UNKNOWN:
1029 /*@notreached@*/ break;
1031 return readlink(path, buf, bufsiz);
1034 int Access(const char * path, int amode) {
1036 int ut = urlPath(path, &lpath);
1039 fprintf(stderr, "*** Access(%s,%d)\n", path, amode);
1041 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
1042 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1046 case URL_IS_UNKNOWN:
1051 /*@notreached@*/ break;
1053 return access(path, amode);
1056 int Glob(const char *path, int flags,
1057 int errfunc(const char * epath, int eerrno), glob_t *pglob)
1060 int ut = urlPath(path, &lpath);
1063 fprintf(stderr, "*** Glob(%s,0x%x,%p,%p)\n", path, (unsigned)flags, errfunc, pglob);
1065 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
1066 return ftpGlob(path, flags, errfunc, pglob);
1067 /*@notreached@*/ break;
1068 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1072 case URL_IS_UNKNOWN:
1077 /*@notreached@*/ break;
1079 return glob(path, flags, errfunc, pglob);
1082 void Globfree(glob_t *pglob)
1085 fprintf(stderr, "*** Globfree(%p)\n", pglob);
1086 if (pglob->gl_offs == -1) /* XXX HACK HACK HACK */
1092 DIR * Opendir(const char * path)
1095 int ut = urlPath(path, &lpath);
1098 fprintf(stderr, "*** Opendir(%s)\n", path);
1100 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
1101 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1105 case URL_IS_UNKNOWN:
1110 /*@notreached@*/ break;
1112 return opendir(path);
1115 struct dirent * Readdir(DIR * dir)
1118 fprintf(stderr, "*** Readdir(%p)\n", dir);
1119 return readdir(dir);
1122 int Closedir(DIR * dir)
1125 fprintf(stderr, "*** Closedir(%p)\n", dir);
1126 return closedir(dir);