6 #include <rpmio_internal.h>
15 extern int _rpmio_debug;
18 /* =============================================================== */
19 static int ftpMkdir(const char * path, /*@unused@*/ mode_t mode) {
21 if ((rc = ftpCmd("MKD", path, NULL)) != 0)
25 sprintf(buf, " 0%o", mode);
26 (void) ftpCmd("SITE CHMOD", path, buf);
32 static int ftpChdir(const char * path) {
33 return ftpCmd("CWD", path, NULL);
36 static int ftpRmdir(const char * path) {
37 return ftpCmd("RMD", path, NULL);
40 static int ftpRename(const char * oldpath, const char * newpath) {
42 if ((rc = ftpCmd("RNFR", oldpath, NULL)) != 0)
44 return ftpCmd("RNTO", newpath, NULL);
47 static int ftpUnlink(const char * path) {
48 return ftpCmd("DELE", path, NULL);
51 /* =============================================================== */
52 /* XXX rebuilddb.c: analogues to mkdir(2)/rmdir(2). */
53 int Mkdir (const char *path, mode_t mode) {
55 int ut = urlPath(path, &lpath);
59 return ftpMkdir(path, mode);
60 /*@notreached@*/ break;
61 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
70 /*@notreached@*/ break;
72 return mkdir(path, mode);
75 int Chdir (const char *path) {
77 int ut = urlPath(path, &lpath);
81 return ftpChdir(path);
82 /*@notreached@*/ break;
83 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
92 /*@notreached@*/ break;
97 int Rmdir (const char *path) {
99 int ut = urlPath(path, &lpath);
103 return ftpRmdir(path);
104 /*@notreached@*/ break;
105 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
114 /*@notreached@*/ break;
119 /* XXX rpmdb.c: analogue to rename(2). */
121 int Rename (const char *oldpath, const char * newpath) {
122 const char *oe = NULL;
123 const char *ne = NULL;
126 /* XXX lib/install.c used to rely on this behavior. */
127 if (!strcmp(oldpath, newpath)) return 0;
129 oldut = urlPath(oldpath, &oe);
131 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
132 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
139 /*@notreached@*/ break;
142 newut = urlPath(newpath, &ne);
146 fprintf(stderr, "*** rename old %*s new %*s\n", (int)(oe - oldpath), oldpath, (int)(ne - newpath), newpath);
147 if (!(oldut == newut && oe && ne && (oe - oldpath) == (ne - newpath) &&
148 !xstrncasecmp(oldpath, newpath, (oe - oldpath))))
150 return ftpRename(oldpath, newpath);
151 /*@notreached@*/ break;
152 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
162 /*@notreached@*/ break;
164 return rename(oldpath, newpath);
167 int Link (const char *oldpath, const char * newpath) {
168 const char *oe = NULL;
169 const char *ne = NULL;
172 oldut = urlPath(oldpath, &oe);
174 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
175 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
182 /*@notreached@*/ break;
185 newut = urlPath(newpath, &ne);
187 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
188 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
191 fprintf(stderr, "*** link old %*s new %*s\n", (int)(oe - oldpath), oldpath, (int)(ne - newpath), newpath);
192 if (!(oldut == newut && oe && ne && (oe - oldpath) == (ne - newpath) &&
193 !xstrncasecmp(oldpath, newpath, (oe - oldpath))))
203 /*@notreached@*/ break;
205 return link(oldpath, newpath);
208 /* XXX build/build.c: analogue to unlink(2). */
210 int Unlink(const char * path) {
212 int ut = urlPath(path, &lpath);
216 return ftpUnlink(path);
217 /*@notreached@*/ break;
218 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
227 /*@notreached@*/ break;
232 /* XXX swiped from mc-4.5.39-pre9 vfs/ftpfs.c */
234 #define g_strdup xstrdup
238 * FIXME: this is broken. It depends on mc not crossing border on month!
240 static int current_mday;
241 static int current_mon;
242 static int current_year;
244 /* Following stuff (parse_ls_lga) is used by ftpfs and extfs */
247 static char *columns [MAXCOLS]; /* Points to the string in column n */
248 static int column_ptr [MAXCOLS]; /* Index from 0 to the starting positions of the columns */
251 vfs_split_text (char *p)
257 for (numcols = 0; *p && numcols < MAXCOLS; numcols++){
258 while (*p == ' ' || *p == '\r' || *p == '\n'){
262 columns [numcols] = p;
263 column_ptr [numcols] = p - original;
264 while (*p && *p != ' ' && *p != '\r' && *p != '\n')
273 if (!columns [idx] || columns [idx][0] < '0' || columns [idx][0] > '9')
279 is_dos_date(/*@null@*/ const char *str)
281 if (str != NULL && strlen(str) == 8 &&
282 str[2] == str[5] && strchr("\\-/", (int)str[2]) != NULL)
288 is_week (/*@null@*/ const char * str, /*@out@*/ struct tm * tim)
290 /*@observer@*/ static const char * week = "SunMonTueWedThuFriSat";
293 /*@-observertrans -mayaliasunique@*/
294 if (str != NULL && (pos=strstr(week, str)) != NULL) {
295 /*@=observertrans =mayaliasunique@*/
297 tim->tm_wday = (pos - week)/3;
304 is_month (/*@null@*/ const char * str, /*@out@*/ struct tm * tim)
306 /*@observer@*/ static const char * month = "JanFebMarAprMayJunJulAugSepOctNovDec";
309 /*@-observertrans -mayaliasunique@*/
310 if (str != NULL && (pos = strstr(month, str)) != NULL) {
311 /*@=observertrans -mayaliasunique@*/
313 tim->tm_mon = (pos - month)/3;
320 is_time (/*@null@*/ const char * str, /*@out@*/ struct tm * tim)
322 const char * p, * p2;
324 if (str != NULL && (p = strchr(str, ':')) && (p2 = strrchr(str, ':'))) {
326 if (sscanf (str, "%2d:%2d:%2d", &tim->tm_hour, &tim->tm_min, &tim->tm_sec) != 3)
329 if (sscanf (str, "%2d:%2d", &tim->tm_hour, &tim->tm_min) != 2)
338 static int is_year(/*@null@*/ const char * str, /*@out@*/ struct tm * tim)
348 if (strlen(str) != 4)
351 if (sscanf(str, "%ld", &year) != 1)
354 if (year < 1900 || year > 3000)
357 tim->tm_year = (int) (year - 1900);
363 * FIXME: this is broken. Consider following entry:
364 * -rwx------ 1 root root 1 Aug 31 10:04 2904 1234
365 * where "2904 1234" is filename. Well, this code decodes it as year :-(.
369 vfs_parse_filetype (char c)
372 case 'd': return S_IFDIR;
373 case 'b': return S_IFBLK;
374 case 'c': return S_IFCHR;
375 case 'l': return S_IFLNK;
377 #ifdef IS_IFSOCK /* And if not, we fall through to IFIFO, which is pretty close */
380 case 'p': return S_IFIFO;
381 case 'm': case 'n': /* Don't know what these are :-) */
382 case '-': case '?': return S_IFREG;
387 static int vfs_parse_filemode (const char *p)
388 { /* converts rw-rw-rw- into 0666 */
391 case 'r': res |= 0400; break;
396 case 'w': res |= 0200; break;
401 case 'x': res |= 0100; break;
402 case 's': res |= 0100 | S_ISUID; break;
403 case 'S': res |= S_ISUID; break;
408 case 'r': res |= 0040; break;
413 case 'w': res |= 0020; break;
418 case 'x': res |= 0010; break;
419 case 's': res |= 0010 | S_ISGID; break;
420 case 'l': /* Solaris produces these */
421 case 'S': res |= S_ISGID; break;
426 case 'r': res |= 0004; break;
431 case 'w': res |= 0002; break;
436 case 'x': res |= 0001; break;
437 case 't': res |= 0001 | S_ISVTX; break;
438 case 'T': res |= S_ISVTX; break;
445 static int vfs_parse_filedate(int idx, time_t *t)
446 { /* This thing parses from idx in columns[] array */
453 /* Let's setup default time values */
454 tim.tm_year = current_year;
455 tim.tm_mon = current_mon;
456 tim.tm_mday = current_mday;
460 tim.tm_isdst = -1; /* Let mktime() try to guess correct dst offset */
464 /* We eat weekday name in case of extfs */
469 if(is_month(p, &tim)){
470 /* And we expect, it followed by day number */
472 tim.tm_mday = (int)atol (columns [idx++]);
474 return 0; /* No day */
477 /* We usually expect:
480 But in case of extfs we allow these date formats:
483 Wek Mon DD hh:mm:ss YYYY
485 where Mon is Jan-Dec, DD, MM, YY two digit day, month, year,
486 YYYY four digit year, hh, mm, ss two digit hour, minute or second. */
488 /* Here just this special case with MM-DD-YY */
492 memset(d, 0, sizeof(d));
493 if (sscanf(p, "%2d-%2d-%2d", &d[0], &d[1], &d[2]) == 3){
500 /* Hmm... maybe, next time :)*/
502 /* At last, MM-DD-YY */
503 d[0]--; /* Months are zerobased */
513 return 0; /* sscanf failed */
515 return 0; /* unsupported format */
518 /* Here we expect to find time and/or year */
521 if(is_time(columns[idx], &tim) || (got_year = is_year(columns[idx], &tim))) {
524 /* This is a special case for ctime() or Mon DD YYYY hh:mm */
526 ((got_year = is_year(columns[idx], &tim)) || is_time(columns[idx], &tim)))
527 idx++; /* time & year or reverse */
528 } /* only time or date */
531 return 0; /* Nor time or date */
534 * If the date is less than 6 months in the past, it is shown without year
535 * other dates in the past or future are shown with year but without time
536 * This does not check for years before 1900 ... I don't know, how
537 * to represent them at all
540 current_mon < 6 && current_mon < tim.tm_mon &&
541 tim.tm_mon - current_mon >= 6)
545 if ((*t = mktime(&tim)) < 0)
551 vfs_parse_ls_lga (char * p, /*@out@*/ struct stat * st,
552 /*@out@*/ const char ** filename,
553 /*@out@*/ const char ** linkname)
555 int idx, idx2, num_cols;
559 if (strncmp (p, "total", 5) == 0)
562 p_copy = g_strdup(p);
563 /* XXX FIXME: parse out inode number from "NLST -lai ." */
564 /* XXX FIXME: parse out sizein blocks from "NLST -lais ." */
566 if ((i = vfs_parse_filetype(*(p++))) == -1)
570 if (*p == ' ') /* Notwell 4 */
573 if (strlen (p) <= 8 || p [8] != ']')
575 /* Should parse here the Notwell permissions :) */
577 if (S_ISDIR (st->st_mode))
578 st->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IXUSR | S_IXGRP | S_IXOTH);
580 st->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);
584 if ((i = vfs_parse_filemode(p)) == -1)
589 /* This is for an extra ACL attribute (HP-UX) */
595 p_copy = g_strdup(p);
596 num_cols = vfs_split_text (p);
598 st->st_nlink = atol (columns [0]);
599 if (st->st_nlink < 0)
604 st->st_uid = finduid (columns [1]);
606 (void) unameToUid (columns [1], &st->st_uid);
609 st->st_uid = (uid_t) atol (columns [1]);
611 /* Mhm, the ls -lg did not produce a group field */
612 for (idx = 3; idx <= 5; idx++)
613 if (is_month(columns [idx], NULL) || is_week(columns [idx], NULL) || is_dos_date(columns[idx]))
616 if (idx == 6 || (idx == 5 && !S_ISCHR (st->st_mode) && !S_ISBLK (st->st_mode)))
619 /* We don't have gid */
620 if (idx == 3 || (idx == 4 && (S_ISCHR(st->st_mode) || S_ISBLK (st->st_mode))))
623 /* We have gid field */
625 st->st_gid = (gid_t) atol (columns [2]);
628 st->st_gid = findgid (columns [2]);
630 (void) gnameToGid (columns [1], &st->st_gid);
636 if (S_ISCHR (st->st_mode) || S_ISBLK (st->st_mode)){
639 if (!is_num (idx2) || sscanf(columns [idx2], " %d,", &maj) != 1)
642 if (!is_num (++idx2) || sscanf(columns [idx2], " %d", &min) != 1)
646 st->st_rdev = ((maj & 0xff) << 8) | (min & 0xffff00ff);
651 /* Common file size */
655 st->st_size = (size_t) atol (columns [idx2]);
661 idx = vfs_parse_filedate(idx, &st->st_mtime);
664 /* Use resulting time value */
665 st->st_atime = st->st_ctime = st->st_mtime;
668 #ifdef HAVE_ST_BLKSIZE
669 st->st_blksize = 512;
671 #ifdef HAVE_ST_BLOCKS
672 st->st_blocks = (st->st_size + 511) / 512;
675 for (i = idx + 1, idx2 = 0; i < num_cols; i++ )
676 if (strcmp (columns [i], "->") == 0){
681 if (((S_ISLNK (st->st_mode) ||
682 (num_cols == idx + 3 && st->st_nlink > 1))) /* Maybe a hardlink? (in extfs) */
689 t = g_strndup (p_copy + column_ptr [idx], column_ptr [idx2] - column_ptr [idx] - 1);
691 int nb = column_ptr [idx2] - column_ptr [idx] - 1;
693 strncpy(t, p_copy + column_ptr [idx], nb);
698 t = g_strdup (p_copy + column_ptr [idx2+1]);
700 if (t [tlen-1] == '\r' || t [tlen-1] == '\n')
702 if (t [tlen-2] == '\r' || t [tlen-2] == '\n')
708 /* Extract the filename from the string copy, not from the columns
709 * this way we have a chance of entering hidden directories like ". ."
713 *filename = g_strdup (columns [idx++]);
718 t = g_strdup (p_copy + column_ptr [idx++]);
721 if (t [tlen-1] == '\r' || t [tlen-1] == '\n')
723 if (t [tlen-2] == '\r' || t [tlen-2] == '\n')
737 static int errorcount = 0;
739 if (++errorcount < 5) {
740 message_1s (1, "Could not parse:", p_copy);
741 } else if (errorcount == 5)
742 message_1s (1, "More parsing errors will be ignored.", "(sorry)" );
747 if (p_copy != p) /* Carefull! */
760 static size_t ftpBufAlloced = 0;
761 static /*@only@*/ char * ftpBuf = NULL;
763 #define alloca_strdup(_s) strcpy(alloca(strlen(_s)+1), (_s))
765 static int ftpNLST(const char * url, ftpSysCall_t ftpSysCall,
766 /*@out@*/ struct stat * st, char * rlbuf, size_t rlbufsiz)
770 int bufLength, moretodo;
771 const char *n, *ne, *o, *oe;
780 n = ne = o = oe = NULL;
781 (void) urlPath(url, &path);
785 switch (ftpSysCall) {
787 fd = ftpOpen(url, 0, 0, &u);
788 if (fd == NULL || u == NULL)
791 u->openError = ftpReq(fd, "NLST", path);
794 urldn = alloca_strdup(url);
795 if ((bn = strrchr(urldn, '/')) == NULL)
803 rc = ftpChdir(urldn); /* XXX don't care about CWD */
807 fd = ftpOpen(url, 0, 0, &u);
808 if (fd == NULL || u == NULL)
811 /* XXX possibly should do "NLST -lais" to get st_ino/st_blocks also */
812 u->openError = ftpReq(fd, "NLST", "-la");
816 if (u->openError < 0) {
817 fd = fdLink(fd, "error data (ftpStat)");
822 if (bn == NULL || nbn <= 0) {
827 if (ftpBufAlloced == 0 || ftpBuf == NULL) {
828 ftpBufAlloced = url_iobuf_size;
829 ftpBuf = xcalloc(ftpBufAlloced, sizeof(ftpBuf[0]));
838 /* XXX FIXME: realloc ftpBuf is < ~128 chars remain */
839 if ((ftpBufAlloced - bufLength) < (1024+80)) {
841 ftpBuf = xrealloc(ftpBuf, ftpBufAlloced);
843 s = se = ftpBuf + bufLength;
846 rc = fdFgets(fd, se, (ftpBufAlloced - bufLength));
851 if (ftpSysCall == DO_FTP_GLOB) { /* XXX HACK */
852 bufLength += strlen(se);
856 for (s = se; *s != '\0'; s = se) {
859 while (*se && *se != '\n') se++;
860 if (se > s && se[-1] == '\r') se[-1] = '\0';
861 if (*se == '\0') break;
864 if (!strncmp(s, "total ", sizeof("total ")-1)) continue;
867 for (bingo = 0, n = se; n >= s; n--) {
873 if (o || !(n[-3] == ' ' && n[-2] == '-' && n[-1] == '>')) {
874 while (*(++n) == ' ');
878 for (o = n + 1; *o == ' '; o++);
888 if (nbn != (ne - n)) continue; /* Same name length? */
889 if (strncmp(n, bn, nbn)) continue; /* Same name? */
895 if (moretodo && se > s) {
896 bufLength = se - s - 1;
898 memmove(ftpBuf, s, bufLength);
904 switch (ftpSysCall) {
907 /* XXX FIXME: symlink, replace urldn/bn from [o,oe) and restart */
911 if (st == NULL || !(n && ne)) {
914 rc = ((vfs_parse_ls_lga(s, st, NULL, NULL) > 0) ? 0 : -1);
917 case DO_FTP_READLINK:
918 if (rlbuf == NULL || !(o && oe)) {
924 memcpy(rlbuf, o, rc);
930 rc = 0; /* XXX WRONG WRONG WRONG */
933 rc = 0; /* XXX WRONG WRONG WRONG */
942 static int ftpStat(const char * path, /*@out@*/ struct stat *st)
944 return ftpNLST(path, DO_FTP_STAT, st, NULL, 0);
947 static int ftpLstat(const char * path, /*@out@*/ struct stat *st) {
949 rc = ftpNLST(path, DO_FTP_LSTAT, st, NULL, 0);
951 fprintf(stderr, "*** ftpLstat(%s) rc %d\n", path, rc);
955 static int ftpReadlink(const char * path, char * buf, size_t bufsiz) {
956 return ftpNLST(path, DO_FTP_READLINK, NULL, buf, bufsiz);
959 static int ftpGlob(const char * path, int flags,
960 int errfunc(const char * epath, int eerno),
961 /*@out@*/ glob_t * pglob)
967 rc = ftpNLST(path, DO_FTP_GLOB, NULL, NULL, 0);
970 fprintf(stderr, "*** ftpGlob(%s,0x%x,%p,%p) ftpNLST rc %d\n", path, (unsigned)flags, (void *)errfunc, pglob, rc);
974 rc = poptParseArgvString(ftpBuf, &pglob->gl_pathc, (const char ***)&pglob->gl_pathv);
975 pglob->gl_offs = -1; /* XXX HACK HACK HACK */
979 static void ftpGlobfree(glob_t * pglob) {
981 fprintf(stderr, "*** ftpGlobfree(%p)\n", pglob);
982 if (pglob->gl_offs == -1) { /* XXX HACK HACK HACK */
983 free((void *)pglob->gl_pathv);
984 pglob->gl_pathv = NULL;
988 int Stat(const char * path, struct stat * st) {
990 int ut = urlPath(path, &lpath);
993 fprintf(stderr, "*** Stat(%s,%p)\n", path, st);
996 return ftpStat(path, st);
997 /*@notreached@*/ break;
998 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1002 case URL_IS_UNKNOWN:
1007 /*@notreached@*/ break;
1009 return stat(path, st);
1012 int Lstat(const char * path, struct stat * st) {
1014 int ut = urlPath(path, &lpath);
1017 fprintf(stderr, "*** Lstat(%s,%p)\n", path, st);
1020 return ftpLstat(path, st);
1021 /*@notreached@*/ break;
1022 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1026 case URL_IS_UNKNOWN:
1031 /*@notreached@*/ break;
1033 return lstat(path, st);
1036 int Readlink(const char * path, char * buf, size_t bufsiz) {
1038 int ut = urlPath(path, &lpath);
1042 return ftpReadlink(path, buf, bufsiz);
1043 /*@notreached@*/ break;
1044 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1048 case URL_IS_UNKNOWN:
1053 /*@notreached@*/ break;
1055 return readlink(path, buf, bufsiz);
1058 int Access(const char * path, int amode) {
1060 int ut = urlPath(path, &lpath);
1063 fprintf(stderr, "*** Access(%s,%d)\n", path, amode);
1065 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
1066 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1070 case URL_IS_UNKNOWN:
1075 /*@notreached@*/ break;
1077 return access(path, amode);
1080 int Glob(const char *path, int flags,
1081 int errfunc(const char * epath, int eerrno), glob_t *pglob)
1084 int ut = urlPath(path, &lpath);
1088 fprintf(stderr, "*** Glob(%s,0x%x,%p,%p)\n", path, (unsigned)flags, (void *)errfunc, pglob);
1091 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
1092 return ftpGlob(path, flags, errfunc, pglob);
1093 /*@notreached@*/ break;
1094 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1098 case URL_IS_UNKNOWN:
1103 /*@notreached@*/ break;
1105 return glob(path, flags, errfunc, pglob);
1108 void Globfree(glob_t *pglob)
1111 fprintf(stderr, "*** Globfree(%p)\n", pglob);
1112 if (pglob->gl_offs == -1) /* XXX HACK HACK HACK */
1118 DIR * Opendir(const char * path)
1121 int ut = urlPath(path, &lpath);
1124 fprintf(stderr, "*** Opendir(%s)\n", path);
1126 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
1127 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1131 case URL_IS_UNKNOWN:
1136 /*@notreached@*/ break;
1138 return opendir(path);
1142 struct dirent * Readdir(DIR * dir)
1145 fprintf(stderr, "*** Readdir(%p)\n", (void *)dir);
1146 return readdir(dir);
1149 int Closedir(DIR * dir)
1152 fprintf(stderr, "*** Closedir(%p)\n", (void *)dir);
1153 return closedir(dir);