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 (const char * str, /*@out@*/ struct tm * tim)
288 /*@observer@*/ static const char * week = "SunMonTueWedThuFriSat";
291 /*@-observertrans -mayaliasunique@*/
292 if ((pos=strstr(week, str)) != NULL) {
293 /*@=observertrans =mayaliasunique@*/
295 tim->tm_wday = (pos - week)/3;
302 is_month (const char * str, /*@out@*/ struct tm * tim)
304 /*@observer@*/ static const char * month = "JanFebMarAprMayJunJulAugSepOctNovDec";
307 /*@-observertrans -mayaliasunique@*/
308 if ((pos=strstr(month, str)) != NULL) {
309 /*@=observertrans -mayaliasunique@*/
311 tim->tm_mon = (pos - month)/3;
318 is_time (const char * str, /*@out@*/ struct tm * tim)
320 const char * p, * p2;
322 if ((p=strchr(str, ':')) && (p2=strrchr(str, ':'))) {
324 if (sscanf (str, "%2d:%2d:%2d", &tim->tm_hour, &tim->tm_min, &tim->tm_sec) != 3)
328 if (sscanf (str, "%2d:%2d", &tim->tm_hour, &tim->tm_min) != 2)
338 static int is_year(const char * str, /*@out@*/ struct tm * tim)
348 if (sscanf(str, "%ld", &year) != 1)
351 if (year < 1900 || year > 3000)
354 tim->tm_year = (int) (year - 1900);
360 * FIXME: this is broken. Consider following entry:
361 * -rwx------ 1 root root 1 Aug 31 10:04 2904 1234
362 * where "2904 1234" is filename. Well, this code decodes it as year :-(.
366 vfs_parse_filetype (char c)
369 case 'd': return S_IFDIR;
370 case 'b': return S_IFBLK;
371 case 'c': return S_IFCHR;
372 case 'l': return S_IFLNK;
374 #ifdef IS_IFSOCK /* And if not, we fall through to IFIFO, which is pretty close */
377 case 'p': return S_IFIFO;
378 case 'm': case 'n': /* Don't know what these are :-) */
379 case '-': case '?': return S_IFREG;
384 static int vfs_parse_filemode (const char *p)
385 { /* converts rw-rw-rw- into 0666 */
388 case 'r': res |= 0400; break;
393 case 'w': res |= 0200; break;
398 case 'x': res |= 0100; break;
399 case 's': res |= 0100 | S_ISUID; break;
400 case 'S': res |= S_ISUID; break;
405 case 'r': res |= 0040; break;
410 case 'w': res |= 0020; break;
415 case 'x': res |= 0010; break;
416 case 's': res |= 0010 | S_ISGID; break;
417 case 'l': /* Solaris produces these */
418 case 'S': res |= S_ISGID; break;
423 case 'r': res |= 0004; break;
428 case 'w': res |= 0002; break;
433 case 'x': res |= 0001; break;
434 case 't': res |= 0001 | S_ISVTX; break;
435 case 'T': res |= S_ISVTX; break;
442 static int vfs_parse_filedate(int idx, time_t *t)
443 { /* This thing parses from idx in columns[] array */
450 /* Let's setup default time values */
451 tim.tm_year = current_year;
452 tim.tm_mon = current_mon;
453 tim.tm_mday = current_mday;
457 tim.tm_isdst = -1; /* Let mktime() try to guess correct dst offset */
461 /* We eat weekday name in case of extfs */
466 if(is_month(p, &tim)){
467 /* And we expect, it followed by day number */
469 tim.tm_mday = (int)atol (columns [idx++]);
471 return 0; /* No day */
474 /* We usually expect:
477 But in case of extfs we allow these date formats:
480 Wek Mon DD hh:mm:ss YYYY
482 where Mon is Jan-Dec, DD, MM, YY two digit day, month, year,
483 YYYY four digit year, hh, mm, ss two digit hour, minute or second. */
485 /* Here just this special case with MM-DD-YY */
489 memset(d, 0, sizeof(d));
490 if (sscanf(p, "%2d-%2d-%2d", &d[0], &d[1], &d[2]) == 3){
497 /* Hmm... maybe, next time :)*/
499 /* At last, MM-DD-YY */
500 d[0]--; /* Months are zerobased */
510 return 0; /* sscanf failed */
512 return 0; /* unsupported format */
515 /* Here we expect to find time and/or year */
518 if(is_time(columns[idx], &tim) || (got_year = is_year(columns[idx], &tim))) {
521 /* This is a special case for ctime() or Mon DD YYYY hh:mm */
523 ((got_year = is_year(columns[idx], &tim)) || is_time(columns[idx], &tim)))
524 idx++; /* time & year or reverse */
525 } /* only time or date */
528 return 0; /* Nor time or date */
531 * If the date is less than 6 months in the past, it is shown without year
532 * other dates in the past or future are shown with year but without time
533 * This does not check for years before 1900 ... I don't know, how
534 * to represent them at all
537 current_mon < 6 && current_mon < tim.tm_mon &&
538 tim.tm_mon - current_mon >= 6)
542 if ((*t = mktime(&tim)) < 0)
548 vfs_parse_ls_lga (char * p, /*@out@*/ struct stat * st,
549 /*@out@*/ const char ** filename,
550 /*@out@*/ const char ** linkname)
552 int idx, idx2, num_cols;
556 if (strncmp (p, "total", 5) == 0)
559 p_copy = g_strdup(p);
560 /* XXX FIXME: parse out inode number from "NLST -lai ." */
561 /* XXX FIXME: parse out sizein blocks from "NLST -lais ." */
563 if ((i = vfs_parse_filetype(*(p++))) == -1)
567 if (*p == ' ') /* Notwell 4 */
570 if (strlen (p) <= 8 || p [8] != ']')
572 /* Should parse here the Notwell permissions :) */
574 if (S_ISDIR (st->st_mode))
575 st->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IXUSR | S_IXGRP | S_IXOTH);
577 st->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);
581 if ((i = vfs_parse_filemode(p)) == -1)
586 /* This is for an extra ACL attribute (HP-UX) */
592 p_copy = g_strdup(p);
593 num_cols = vfs_split_text (p);
595 st->st_nlink = atol (columns [0]);
596 if (st->st_nlink < 0)
601 st->st_uid = finduid (columns [1]);
603 (void) unameToUid (columns [1], &st->st_uid);
606 st->st_uid = (uid_t) atol (columns [1]);
608 /* Mhm, the ls -lg did not produce a group field */
609 for (idx = 3; idx <= 5; idx++)
610 if (is_month(columns [idx], NULL) || is_week(columns [idx], NULL) || is_dos_date(columns[idx]))
613 if (idx == 6 || (idx == 5 && !S_ISCHR (st->st_mode) && !S_ISBLK (st->st_mode)))
616 /* We don't have gid */
617 if (idx == 3 || (idx == 4 && (S_ISCHR(st->st_mode) || S_ISBLK (st->st_mode))))
620 /* We have gid field */
622 st->st_gid = (gid_t) atol (columns [2]);
625 st->st_gid = findgid (columns [2]);
627 (void) gnameToGid (columns [1], &st->st_gid);
633 if (S_ISCHR (st->st_mode) || S_ISBLK (st->st_mode)){
636 if (!is_num (idx2) || sscanf(columns [idx2], " %d,", &maj) != 1)
639 if (!is_num (++idx2) || sscanf(columns [idx2], " %d", &min) != 1)
643 st->st_rdev = ((maj & 0xff) << 8) | (min & 0xffff00ff);
648 /* Common file size */
652 st->st_size = (size_t) atol (columns [idx2]);
658 idx = vfs_parse_filedate(idx, &st->st_mtime);
661 /* Use resulting time value */
662 st->st_atime = st->st_ctime = st->st_mtime;
665 #ifdef HAVE_ST_BLKSIZE
666 st->st_blksize = 512;
668 #ifdef HAVE_ST_BLOCKS
669 st->st_blocks = (st->st_size + 511) / 512;
672 for (i = idx + 1, idx2 = 0; i < num_cols; i++ )
673 if (strcmp (columns [i], "->") == 0){
678 if (((S_ISLNK (st->st_mode) ||
679 (num_cols == idx + 3 && st->st_nlink > 1))) /* Maybe a hardlink? (in extfs) */
686 t = g_strndup (p_copy + column_ptr [idx], column_ptr [idx2] - column_ptr [idx] - 1);
688 int nb = column_ptr [idx2] - column_ptr [idx] - 1;
690 strncpy(t, p_copy + column_ptr [idx], nb);
695 t = g_strdup (p_copy + column_ptr [idx2+1]);
697 if (t [tlen-1] == '\r' || t [tlen-1] == '\n')
699 if (t [tlen-2] == '\r' || t [tlen-2] == '\n')
705 /* Extract the filename from the string copy, not from the columns
706 * this way we have a chance of entering hidden directories like ". ."
710 *filename = g_strdup (columns [idx++]);
715 t = g_strdup (p_copy + column_ptr [idx++]);
718 if (t [tlen-1] == '\r' || t [tlen-1] == '\n')
720 if (t [tlen-2] == '\r' || t [tlen-2] == '\n')
734 static int errorcount = 0;
736 if (++errorcount < 5) {
737 message_1s (1, "Could not parse:", p_copy);
738 } else if (errorcount == 5)
739 message_1s (1, "More parsing errors will be ignored.", "(sorry)" );
744 if (p_copy != p) /* Carefull! */
757 static size_t ftpBufAlloced = 0;
758 static /*@only@*/ char * ftpBuf = NULL;
760 #define alloca_strdup(_s) strcpy(alloca(strlen(_s)+1), (_s))
762 static int ftpNLST(const char * url, ftpSysCall_t ftpSysCall,
763 /*@out@*/ struct stat * st, char * rlbuf, size_t rlbufsiz)
767 int bufLength, moretodo;
768 const char *n, *ne, *o, *oe;
777 n = ne = o = oe = NULL;
778 (void) urlPath(url, &path);
782 switch (ftpSysCall) {
784 fd = ftpOpen(url, 0, 0, &u);
785 if (fd == NULL || u == NULL)
788 u->openError = ftpReq(fd, "NLST", path);
791 urldn = alloca_strdup(url);
792 if ((bn = strrchr(urldn, '/')) == NULL)
800 rc = ftpChdir(urldn); /* XXX don't care about CWD */
804 fd = ftpOpen(url, 0, 0, &u);
805 if (fd == NULL || u == NULL)
808 /* XXX possibly should do "NLST -lais" to get st_ino/st_blocks also */
809 u->openError = ftpReq(fd, "NLST", "-la");
813 if (u->openError < 0) {
814 fd = fdLink(fd, "error data (ftpStat)");
819 if (ftpBufAlloced == 0 || ftpBuf == NULL) {
820 ftpBufAlloced = url_iobuf_size;
821 ftpBuf = xcalloc(ftpBufAlloced, sizeof(ftpBuf[0]));
830 /* XXX FIXME: realloc ftpBuf is < ~128 chars remain */
831 if ((ftpBufAlloced - bufLength) < (1024+80)) {
833 ftpBuf = xrealloc(ftpBuf, ftpBufAlloced);
835 s = se = ftpBuf + bufLength;
838 rc = fdFgets(fd, se, (ftpBufAlloced - bufLength));
843 if (ftpSysCall == DO_FTP_GLOB) { /* XXX HACK */
844 bufLength += strlen(se);
848 for (s = se; *s != '\0'; s = se) {
851 while (*se && *se != '\n') se++;
852 if (se > s && se[-1] == '\r') se[-1] = '\0';
853 if (*se == '\0') break;
856 if (!strncmp(s, "total ", sizeof("total ")-1)) continue;
859 for (bingo = 0, n = se; n >= s; n--) {
865 if (o || !(n[-3] == ' ' && n[-2] == '-' && n[-1] == '>')) {
866 while (*(++n) == ' ');
870 for (o = n + 1; *o == ' '; o++);
880 if (nbn != (ne - n)) continue; /* Same name length? */
881 if (strncmp(n, bn, nbn)) continue; /* Same name? */
887 if (moretodo && se > s) {
888 bufLength = se - s - 1;
890 memmove(ftpBuf, s, bufLength);
896 switch (ftpSysCall) {
899 /* XXX FIXME: symlink, replace urldn/bn from [o,oe) and restart */
903 if (st == NULL || !(n && ne)) {
906 rc = ((vfs_parse_ls_lga(s, st, NULL, NULL) > 0) ? 0 : -1);
909 case DO_FTP_READLINK:
910 if (rlbuf == NULL || !(o && oe)) {
916 memcpy(rlbuf, o, rc);
922 rc = 0; /* XXX WRONG WRONG WRONG */
925 rc = 0; /* XXX WRONG WRONG WRONG */
934 static int ftpStat(const char * path, /*@out@*/ struct stat *st)
936 return ftpNLST(path, DO_FTP_STAT, st, NULL, 0);
939 static int ftpLstat(const char * path, /*@out@*/ struct stat *st) {
941 rc = ftpNLST(path, DO_FTP_LSTAT, st, NULL, 0);
943 fprintf(stderr, "*** ftpLstat(%s) rc %d\n", path, rc);
947 static int ftpReadlink(const char * path, char * buf, size_t bufsiz) {
948 return ftpNLST(path, DO_FTP_READLINK, NULL, buf, bufsiz);
951 static int ftpGlob(const char * path, int flags,
952 int errfunc(const char * epath, int eerno),
953 /*@out@*/ glob_t * pglob)
959 rc = ftpNLST(path, DO_FTP_GLOB, NULL, NULL, 0);
962 fprintf(stderr, "*** ftpGlob(%s,0x%x,%p,%p) ftpNLST rc %d\n", path, (unsigned)flags, (void *)errfunc, pglob, rc);
966 rc = poptParseArgvString(ftpBuf, &pglob->gl_pathc, (const char ***)&pglob->gl_pathv);
967 pglob->gl_offs = -1; /* XXX HACK HACK HACK */
971 static void ftpGlobfree(glob_t * pglob) {
973 fprintf(stderr, "*** ftpGlobfree(%p)\n", pglob);
974 if (pglob->gl_offs == -1) { /* XXX HACK HACK HACK */
975 free((void *)pglob->gl_pathv);
976 pglob->gl_pathv = NULL;
980 int Stat(const char * path, struct stat * st) {
982 int ut = urlPath(path, &lpath);
985 fprintf(stderr, "*** Stat(%s,%p)\n", path, st);
988 return ftpStat(path, st);
989 /*@notreached@*/ break;
990 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
999 /*@notreached@*/ break;
1001 return stat(path, st);
1004 int Lstat(const char * path, struct stat * st) {
1006 int ut = urlPath(path, &lpath);
1009 fprintf(stderr, "*** Lstat(%s,%p)\n", path, st);
1012 return ftpLstat(path, st);
1013 /*@notreached@*/ break;
1014 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1018 case URL_IS_UNKNOWN:
1023 /*@notreached@*/ break;
1025 return lstat(path, st);
1028 int Readlink(const char * path, char * buf, size_t bufsiz) {
1030 int ut = urlPath(path, &lpath);
1034 return ftpReadlink(path, buf, bufsiz);
1035 /*@notreached@*/ break;
1036 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1040 case URL_IS_UNKNOWN:
1045 /*@notreached@*/ break;
1047 return readlink(path, buf, bufsiz);
1050 int Access(const char * path, int amode) {
1052 int ut = urlPath(path, &lpath);
1055 fprintf(stderr, "*** Access(%s,%d)\n", path, amode);
1057 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
1058 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1062 case URL_IS_UNKNOWN:
1067 /*@notreached@*/ break;
1069 return access(path, amode);
1072 int Glob(const char *path, int flags,
1073 int errfunc(const char * epath, int eerrno), glob_t *pglob)
1076 int ut = urlPath(path, &lpath);
1080 fprintf(stderr, "*** Glob(%s,0x%x,%p,%p)\n", path, (unsigned)flags, (void *)errfunc, pglob);
1083 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
1084 return ftpGlob(path, flags, errfunc, pglob);
1085 /*@notreached@*/ break;
1086 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1090 case URL_IS_UNKNOWN:
1095 /*@notreached@*/ break;
1097 return glob(path, flags, errfunc, pglob);
1100 void Globfree(glob_t *pglob)
1103 fprintf(stderr, "*** Globfree(%p)\n", pglob);
1104 if (pglob->gl_offs == -1) /* XXX HACK HACK HACK */
1110 DIR * Opendir(const char * path)
1113 int ut = urlPath(path, &lpath);
1116 fprintf(stderr, "*** Opendir(%s)\n", path);
1118 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
1119 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1123 case URL_IS_UNKNOWN:
1128 /*@notreached@*/ break;
1130 return opendir(path);
1134 struct dirent * Readdir(DIR * dir)
1137 fprintf(stderr, "*** Readdir(%p)\n", (void *)dir);
1138 return readdir(dir);
1141 int Closedir(DIR * dir)
1144 fprintf(stderr, "*** Closedir(%p)\n", (void *)dir);
1145 return closedir(dir);