7 #include <rpmio_internal.h>
11 extern int _rpmio_debug;
13 /* =============================================================== */
14 static int ftpMkdir(const char * path, /*@unused@*/ mode_t mode) {
16 if ((rc = ftpCmd("MKD", path, NULL)) != 0)
20 sprintf(buf, " 0%o", mode);
21 (void) ftpCmd("SITE CHMOD", path, buf);
27 static int ftpChdir(const char * path) {
28 return ftpCmd("CWD", path, NULL);
31 static int ftpRmdir(const char * path) {
32 return ftpCmd("RMD", path, NULL);
35 static int ftpRename(const char * oldpath, const char * newpath) {
37 if ((rc = ftpCmd("RNFR", oldpath, NULL)) != 0)
39 return ftpCmd("RNTO", newpath, NULL);
42 static int ftpUnlink(const char * path) {
43 return ftpCmd("DELE", path, NULL);
46 /* =============================================================== */
47 /* XXX rebuilddb.c: analogues to mkdir(2)/rmdir(2). */
48 int Mkdir (const char *path, mode_t mode) {
50 int ut = urlPath(path, &lpath);
54 return ftpMkdir(path, mode);
55 /*@notreached@*/ break;
56 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
65 /*@notreached@*/ break;
67 return mkdir(path, mode);
70 int Chdir (const char *path) {
72 int ut = urlPath(path, &lpath);
76 return ftpChdir(path);
77 /*@notreached@*/ break;
78 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
87 /*@notreached@*/ break;
92 int Rmdir (const char *path) {
94 int ut = urlPath(path, &lpath);
98 return ftpRmdir(path);
99 /*@notreached@*/ break;
100 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
109 /*@notreached@*/ break;
114 /* XXX rpmdb.c: analogue to rename(2). */
116 int Rename (const char *oldpath, const char * newpath) {
117 const char *oe = NULL;
118 const char *ne = NULL;
121 /* XXX lib/install.c used to rely on this behavior. */
122 if (!strcmp(oldpath, newpath)) return 0;
124 oldut = urlPath(oldpath, &oe);
126 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
127 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
134 /*@notreached@*/ break;
137 newut = urlPath(newpath, &ne);
141 fprintf(stderr, "*** rename old %*s new %*s\n", (int)(oe - oldpath), oldpath, (int)(ne - newpath), newpath);
142 if (!(oldut == newut && oe && ne && (oe - oldpath) == (ne - newpath) &&
143 !strncasecmp(oldpath, newpath, (oe - oldpath))))
145 return ftpRename(oldpath, newpath);
146 /*@notreached@*/ break;
147 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
157 /*@notreached@*/ break;
159 return rename(oldpath, newpath);
162 int Link (const char *oldpath, const char * newpath) {
163 const char *oe = NULL;
164 const char *ne = NULL;
167 oldut = urlPath(oldpath, &oe);
169 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
170 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
177 /*@notreached@*/ break;
180 newut = urlPath(newpath, &ne);
182 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
183 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
186 fprintf(stderr, "*** link old %*s new %*s\n", (int)(oe - oldpath), oldpath, (int)(ne - newpath), newpath);
187 if (!(oldut == newut && oe && ne && (oe - oldpath) == (ne - newpath) &&
188 !strncasecmp(oldpath, newpath, (oe - oldpath))))
198 /*@notreached@*/ break;
200 return link(oldpath, newpath);
203 /* XXX build/build.c: analogue to unlink(2). */
205 int Unlink(const char * path) {
207 int ut = urlPath(path, &lpath);
211 return ftpUnlink(path);
212 /*@notreached@*/ break;
213 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
222 /*@notreached@*/ break;
227 /* XXX swiped from mc-4.5.39-pre9 vfs/ftpfs.c */
229 #define g_strdup xstrdup
233 * FIXME: this is broken. It depends on mc not crossing border on month!
235 static int current_mday;
236 static int current_mon;
237 static int current_year;
239 /* Following stuff (parse_ls_lga) is used by ftpfs and extfs */
242 static char *columns [MAXCOLS]; /* Points to the string in column n */
243 static int column_ptr [MAXCOLS]; /* Index from 0 to the starting positions of the columns */
246 vfs_split_text (char *p)
252 for (numcols = 0; *p && numcols < MAXCOLS; numcols++){
253 while (*p == ' ' || *p == '\r' || *p == '\n'){
257 columns [numcols] = p;
258 column_ptr [numcols] = p - original;
259 while (*p && *p != ' ' && *p != '\r' && *p != '\n')
268 if (!columns [idx] || columns [idx][0] < '0' || columns [idx][0] > '9')
274 is_dos_date(char *str)
276 if (strlen(str) == 8 && str[2] == str[5] && strchr("\\-/", (int)str[2]) != NULL)
283 is_week (char *str, struct tm *tim)
285 static char *week = "SunMonTueWedThuFriSat";
288 if((pos=strstr(week, str)) != NULL){
290 tim->tm_wday = (pos - week)/3;
297 is_month (char *str, struct tm *tim)
299 static char *month = "JanFebMarAprMayJunJulAugSepOctNovDec";
302 if((pos=strstr(month, str)) != NULL){
304 tim->tm_mon = (pos - month)/3;
311 is_time (char *str, struct tm *tim)
315 if ((p=strchr(str, ':')) && (p2=strrchr(str, ':'))) {
317 if (sscanf (str, "%2d:%2d:%2d", &tim->tm_hour, &tim->tm_min, &tim->tm_sec) != 3)
321 if (sscanf (str, "%2d:%2d", &tim->tm_hour, &tim->tm_min) != 2)
331 static int is_year(char *str, struct tm *tim)
341 if (sscanf(str, "%ld", &year) != 1)
344 if (year < 1900 || year > 3000)
347 tim->tm_year = (int) (year - 1900);
353 * FIXME: this is broken. Consider following entry:
354 * -rwx------ 1 root root 1 Aug 31 10:04 2904 1234
355 * where "2904 1234" is filename. Well, this code decodes it as year :-(.
359 vfs_parse_filetype (char c)
362 case 'd': return S_IFDIR;
363 case 'b': return S_IFBLK;
364 case 'c': return S_IFCHR;
365 case 'l': return S_IFLNK;
367 #ifdef IS_IFSOCK /* And if not, we fall through to IFIFO, which is pretty close */
370 case 'p': return S_IFIFO;
371 case 'm': case 'n': /* Don't know what these are :-) */
372 case '-': case '?': return S_IFREG;
377 static int vfs_parse_filemode (char *p)
378 { /* converts rw-rw-rw- into 0666 */
381 case 'r': res |= 0400; break;
386 case 'w': res |= 0200; break;
391 case 'x': res |= 0100; break;
392 case 's': res |= 0100 | S_ISUID; break;
393 case 'S': res |= S_ISUID; break;
398 case 'r': res |= 0040; break;
403 case 'w': res |= 0020; break;
408 case 'x': res |= 0010; break;
409 case 's': res |= 0010 | S_ISGID; break;
410 case 'l': /* Solaris produces these */
411 case 'S': res |= S_ISGID; break;
416 case 'r': res |= 0004; break;
421 case 'w': res |= 0002; break;
426 case 'x': res |= 0001; break;
427 case 't': res |= 0001 | S_ISVTX; break;
428 case 'T': res |= S_ISVTX; break;
435 static int vfs_parse_filedate(int idx, time_t *t)
436 { /* This thing parses from idx in columns[] array */
443 /* Let's setup default time values */
444 tim.tm_year = current_year;
445 tim.tm_mon = current_mon;
446 tim.tm_mday = current_mday;
450 tim.tm_isdst = -1; /* Let mktime() try to guess correct dst offset */
454 /* We eat weekday name in case of extfs */
459 if(is_month(p, &tim)){
460 /* And we expect, it followed by day number */
462 tim.tm_mday = (int)atol (columns [idx++]);
464 return 0; /* No day */
467 /* We usually expect:
470 But in case of extfs we allow these date formats:
473 Wek Mon DD hh:mm:ss YYYY
475 where Mon is Jan-Dec, DD, MM, YY two digit day, month, year,
476 YYYY four digit year, hh, mm, ss two digit hour, minute or second. */
478 /* Here just this special case with MM-DD-YY */
482 if(sscanf(p, "%2d-%2d-%2d", &d[0], &d[1], &d[2]) == 3){
489 /* Hmm... maybe, next time :)*/
491 /* At last, MM-DD-YY */
492 d[0]--; /* Months are zerobased */
502 return 0; /* sscanf failed */
504 return 0; /* unsupported format */
507 /* Here we expect to find time and/or year */
510 if(is_time(columns[idx], &tim) || (got_year = is_year(columns[idx], &tim))) {
513 /* This is a special case for ctime() or Mon DD YYYY hh:mm */
515 ((got_year = is_year(columns[idx], &tim)) || is_time(columns[idx], &tim)))
516 idx++; /* time & year or reverse */
517 } /* only time or date */
520 return 0; /* Nor time or date */
523 * If the date is less than 6 months in the past, it is shown without year
524 * other dates in the past or future are shown with year but without time
525 * This does not check for years before 1900 ... I don't know, how
526 * to represent them at all
529 current_mon < 6 && current_mon < tim.tm_mon &&
530 tim.tm_mon - current_mon >= 6)
534 if ((*t = mktime(&tim)) < 0)
540 vfs_parse_ls_lga (char *p, struct stat *s, char **filename, char **linkname)
542 int idx, idx2, num_cols;
546 if (strncmp (p, "total", 5) == 0)
549 p_copy = g_strdup(p);
550 /* XXX FIXME: parse out inode number from "NLST -lai ." */
551 /* XXX FIXME: parse out sizein blocks from "NLST -lais ." */
553 if ((i = vfs_parse_filetype(*(p++))) == -1)
557 if (*p == ' ') /* Notwell 4 */
560 if (strlen (p) <= 8 || p [8] != ']')
562 /* Should parse here the Notwell permissions :) */
563 if (S_ISDIR (s->st_mode))
564 s->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IXUSR | S_IXGRP | S_IXOTH);
566 s->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);
569 if ((i = vfs_parse_filemode(p)) == -1)
574 /* This is for an extra ACL attribute (HP-UX) */
580 p_copy = g_strdup(p);
581 num_cols = vfs_split_text (p);
583 s->st_nlink = atol (columns [0]);
589 s->st_uid = finduid (columns [1]);
591 unameToUid (columns [1], &s->st_uid);
594 s->st_uid = (uid_t) atol (columns [1]);
596 /* Mhm, the ls -lg did not produce a group field */
597 for (idx = 3; idx <= 5; idx++)
598 if (is_month(columns [idx], NULL) || is_week(columns [idx], NULL) || is_dos_date(columns[idx]))
601 if (idx == 6 || (idx == 5 && !S_ISCHR (s->st_mode) && !S_ISBLK (s->st_mode)))
604 /* We don't have gid */
605 if (idx == 3 || (idx == 4 && (S_ISCHR(s->st_mode) || S_ISBLK (s->st_mode))))
608 /* We have gid field */
610 s->st_gid = (gid_t) atol (columns [2]);
613 s->st_gid = findgid (columns [2]);
615 gnameToGid (columns [1], &s->st_gid);
621 if (S_ISCHR (s->st_mode) || S_ISBLK (s->st_mode)){
624 if (!is_num (idx2) || sscanf(columns [idx2], " %d,", &maj) != 1)
627 if (!is_num (++idx2) || sscanf(columns [idx2], " %d", &min) != 1)
631 s->st_rdev = ((maj & 0xff) << 8) | (min & 0xffff00ff);
636 /* Common file size */
640 s->st_size = (size_t) atol (columns [idx2]);
646 idx = vfs_parse_filedate(idx, &s->st_mtime);
649 /* Use resulting time value */
650 s->st_atime = s->st_ctime = s->st_mtime;
653 #ifdef HAVE_ST_BLKSIZE
656 #ifdef HAVE_ST_BLOCKS
657 s->st_blocks = (s->st_size + 511) / 512;
660 for (i = idx + 1, idx2 = 0; i < num_cols; i++ )
661 if (strcmp (columns [i], "->") == 0){
666 if (((S_ISLNK (s->st_mode) ||
667 (num_cols == idx + 3 && s->st_nlink > 1))) /* Maybe a hardlink? (in extfs) */
674 s = g_strndup (p_copy + column_ptr [idx], column_ptr [idx2] - column_ptr [idx] - 1);
676 int nb = column_ptr [idx2] - column_ptr [idx] - 1;
678 strncpy(s, p_copy + column_ptr [idx], nb);
683 s = g_strdup (p_copy + column_ptr [idx2+1]);
685 if (s [p-1] == '\r' || s [p-1] == '\n')
687 if (s [p-2] == '\r' || s [p-2] == '\n')
693 /* Extract the filename from the string copy, not from the columns
694 * this way we have a chance of entering hidden directories like ". ."
698 *filename = g_strdup (columns [idx++]);
703 s = g_strdup (p_copy + column_ptr [idx++]);
706 if (s [p-1] == '\r' || s [p-1] == '\n')
708 if (s [p-2] == '\r' || s [p-2] == '\n')
722 static int errorcount = 0;
724 if (++errorcount < 5) {
725 message_1s (1, "Could not parse:", p_copy);
726 } else if (errorcount == 5)
727 message_1s (1, "More parsing errors will be ignored.", "(sorry)" );
731 if (p_copy != p) /* Carefull! */
743 static size_t ftpBufAlloced = 0;
744 static char * ftpBuf = NULL;
746 #define alloca_strdup(_s) strcpy(alloca(strlen(_s)+1), (_s))
748 static int ftpNLST(const char * url, ftpSysCall_t ftpSysCall,
749 struct stat * st, char * rlbuf, size_t rlbufsiz)
753 int bufLength, moretodo;
754 const char *n, *ne, *o, *oe;
763 n = ne = o = oe = NULL;
764 (void) urlPath(url, &path);
768 switch (ftpSysCall) {
770 fd = ftpOpen(url, 0, 0, &u);
771 if (fd == NULL || u == NULL)
774 u->openError = ftpReq(fd, "NLST", path);
777 urldn = alloca_strdup(url);
778 if ((bn = strrchr(urldn, '/')) == NULL)
786 rc = ftpChdir(urldn); /* XXX don't care about CWD */
790 fd = ftpOpen(url, 0, 0, &u);
791 if (fd == NULL || u == NULL)
794 /* XXX possibly should do "NLST -lais" to get st_ino/st_blocks also */
795 u->openError = ftpReq(fd, "NLST", "-la");
799 if (u->openError < 0) {
800 fd = fdLink(fd, "error data (ftpStat)");
805 if (ftpBufAlloced == 0 || ftpBuf == NULL) {
806 ftpBufAlloced = url_iobuf_size;
807 ftpBuf = xcalloc(ftpBufAlloced, sizeof(ftpBuf[0]));
816 /* XXX FIXME: realloc ftpBuf is < ~128 chars remain */
817 if ((ftpBufAlloced - bufLength) < (1024+80)) {
819 ftpBuf = xrealloc(ftpBuf, ftpBufAlloced);
821 s = se = ftpBuf + bufLength;
824 rc = fdFgets(fd, se, (ftpBufAlloced - bufLength));
829 if (ftpSysCall == DO_FTP_GLOB) { /* XXX HACK */
830 bufLength += strlen(se);
834 for (s = se; *s != '\0'; s = se) {
837 while (*se && *se != '\n') se++;
838 if (se > s && se[-1] == '\r') se[-1] = '\0';
839 if (*se == '\0') break;
842 if (!strncmp(s, "total ", sizeof("total ")-1)) continue;
845 for (bingo = 0, n = se; n >= s; n--) {
851 if (o || !(n[-3] == ' ' && n[-2] == '-' && n[-1] == '>')) {
852 while (*(++n) == ' ');
856 for (o = n + 1; *o == ' '; o++);
866 if (nbn != (ne - n)) continue; /* Same name length? */
867 if (strncmp(n, bn, nbn)) continue; /* Same name? */
873 if (moretodo && se > s) {
874 bufLength = se - s - 1;
876 memmove(ftpBuf, s, bufLength);
882 switch (ftpSysCall) {
885 /* XXX FIXME: symlink, replace urldn/bn from [o,oe) and restart */
889 if (st == NULL || !(n && ne)) {
892 rc = ((vfs_parse_ls_lga(s, st, NULL, NULL) > 0) ? 0 : -1);
895 case DO_FTP_READLINK:
896 if (rlbuf == NULL || !(o && oe)) {
902 memcpy(rlbuf, o, rc);
908 rc = 0; /* XXX WRONG WRONG WRONG */
911 rc = 0; /* XXX WRONG WRONG WRONG */
920 static int ftpStat(const char * path, struct stat *st)
922 return ftpNLST(path, DO_FTP_STAT, st, NULL, 0);
925 static int ftpLstat(const char * path, struct stat *st) {
927 rc = ftpNLST(path, DO_FTP_LSTAT, st, NULL, 0);
929 fprintf(stderr, "*** ftpLstat(%s) rc %d\n", path, rc);
933 static int ftpReadlink(const char * path, char * buf, size_t bufsiz) {
934 return ftpNLST(path, DO_FTP_READLINK, NULL, buf, bufsiz);
937 static int ftpGlob(const char * path, int flags,
938 int errfunc(const char * epath, int eerno), glob_t * pglob)
944 rc = ftpNLST(path, DO_FTP_GLOB, NULL, NULL, 0);
946 fprintf(stderr, "*** ftpGlob(%s,0x%x,%p,%p) ftpNLST rc %d\n", path, flags, errfunc, pglob, rc);
949 rc = poptParseArgvString(ftpBuf, &pglob->gl_pathc, (const char ***)&pglob->gl_pathv);
950 pglob->gl_offs = -1; /* XXX HACK HACK HACK */
954 static void ftpGlobfree(glob_t * pglob) {
956 fprintf(stderr, "*** ftpGlobfree(%p)\n", pglob);
957 if (pglob->gl_offs == -1) /* XXX HACK HACK HACK */
958 xfree(pglob->gl_pathv);
961 int Stat(const char * path, struct stat * st) {
963 int ut = urlPath(path, &lpath);
966 fprintf(stderr, "*** Stat(%s,%p)\n", path, st);
969 return ftpStat(path, st);
970 /*@notreached@*/ break;
971 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
980 /*@notreached@*/ break;
982 return stat(path, st);
985 int Lstat(const char * path, struct stat * st) {
987 int ut = urlPath(path, &lpath);
990 fprintf(stderr, "*** Lstat(%s,%p)\n", path, st);
993 return ftpLstat(path, st);
994 /*@notreached@*/ break;
995 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1004 /*@notreached@*/ break;
1006 return lstat(path, st);
1009 int Readlink(const char * path, char * buf, size_t bufsiz) {
1011 int ut = urlPath(path, &lpath);
1015 return ftpReadlink(path, buf, bufsiz);
1016 /*@notreached@*/ break;
1017 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1021 case URL_IS_UNKNOWN:
1026 /*@notreached@*/ break;
1028 return readlink(path, buf, bufsiz);
1031 int Access(const char * path, int amode) {
1033 int ut = urlPath(path, &lpath);
1036 fprintf(stderr, "*** Access(%s,%d)\n", path, amode);
1038 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
1039 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1043 case URL_IS_UNKNOWN:
1048 /*@notreached@*/ break;
1050 return access(path, amode);
1053 int Glob(const char *path, int flags,
1054 int errfunc(const char * epath, int eerrno), glob_t *pglob)
1057 int ut = urlPath(path, &lpath);
1060 fprintf(stderr, "*** Glob(%s,0x%x,%p,%p)\n", path, flags, errfunc, pglob);
1062 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
1063 return ftpGlob(path, flags, errfunc, pglob);
1064 /*@notreached@*/ break;
1065 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1069 case URL_IS_UNKNOWN:
1074 /*@notreached@*/ break;
1076 return glob(path, flags, errfunc, pglob);
1079 void Globfree(glob_t *pglob)
1082 fprintf(stderr, "*** Globfree(%p)\n", pglob);
1083 if (pglob->gl_offs == -1) /* XXX HACK HACK HACK */
1089 DIR * Opendir(const char * path)
1092 int ut = urlPath(path, &lpath);
1095 fprintf(stderr, "*** Opendir(%s)\n", path);
1097 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
1098 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1102 case URL_IS_UNKNOWN:
1107 /*@notreached@*/ break;
1109 return opendir(path);
1112 struct dirent * Readdir(DIR * dir)
1115 fprintf(stderr, "*** Readdir(%p)\n", dir);
1116 return readdir(dir);
1119 int Closedir(DIR * dir)
1122 fprintf(stderr, "*** Closedir(%p)\n", dir);
1123 return closedir(dir);