6 #include <rpmio_internal.h>
15 extern int _rpmio_debug;
18 /* =============================================================== */
19 static int ftpMkdir(const char * path, /*@unused@*/ mode_t mode)
20 /*@modifies fileSystem @*/
23 if ((rc = ftpCmd("MKD", path, NULL)) != 0)
27 sprintf(buf, " 0%o", mode);
28 (void) ftpCmd("SITE CHMOD", path, buf);
34 static int ftpChdir(const char * path)
35 /*@modifies fileSystem @*/
37 return ftpCmd("CWD", path, NULL);
40 static int ftpRmdir(const char * path)
41 /*@modifies fileSystem @*/
43 return ftpCmd("RMD", path, NULL);
46 static int ftpRename(const char * oldpath, const char * newpath)
47 /*@modifies fileSystem @*/
50 if ((rc = ftpCmd("RNFR", oldpath, NULL)) != 0)
52 return ftpCmd("RNTO", newpath, NULL);
55 static int ftpUnlink(const char * path)
56 /*@modifies fileSystem @*/
58 return ftpCmd("DELE", path, NULL);
61 /* =============================================================== */
62 /* XXX rebuilddb.c: analogues to mkdir(2)/rmdir(2). */
63 int Mkdir (const char * path, mode_t mode)
66 int ut = urlPath(path, &lpath);
70 return ftpMkdir(path, mode);
71 /*@notreached@*/ break;
72 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
81 /*@notreached@*/ break;
83 return mkdir(path, mode);
86 int Chdir (const char * path)
89 int ut = urlPath(path, &lpath);
93 return ftpChdir(path);
94 /*@notreached@*/ break;
95 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
104 /*@notreached@*/ break;
109 int Rmdir (const char * path)
112 int ut = urlPath(path, &lpath);
116 return ftpRmdir(path);
117 /*@notreached@*/ break;
118 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
127 /*@notreached@*/ break;
132 /* XXX rpmdb.c: analogue to rename(2). */
134 int Rename (const char * oldpath, const char * newpath)
136 const char *oe = NULL;
137 const char *ne = NULL;
140 /* XXX lib/install.c used to rely on this behavior. */
141 if (!strcmp(oldpath, newpath)) return 0;
143 oldut = urlPath(oldpath, &oe);
145 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
146 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
153 /*@notreached@*/ break;
156 newut = urlPath(newpath, &ne);
160 fprintf(stderr, "*** rename old %*s new %*s\n", (int)(oe - oldpath), oldpath, (int)(ne - newpath), newpath);
161 if (!(oldut == newut && oe && ne && (oe - oldpath) == (ne - newpath) &&
162 !xstrncasecmp(oldpath, newpath, (oe - oldpath))))
164 return ftpRename(oldpath, newpath);
165 /*@notreached@*/ break;
166 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
176 /*@notreached@*/ break;
178 return rename(oldpath, newpath);
181 int Link (const char * oldpath, const char * newpath)
183 const char *oe = NULL;
184 const char *ne = NULL;
187 oldut = urlPath(oldpath, &oe);
189 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
190 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
197 /*@notreached@*/ break;
200 newut = urlPath(newpath, &ne);
202 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
203 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
206 fprintf(stderr, "*** link old %*s new %*s\n", (int)(oe - oldpath), oldpath, (int)(ne - newpath), newpath);
207 if (!(oldut == newut && oe && ne && (oe - oldpath) == (ne - newpath) &&
208 !xstrncasecmp(oldpath, newpath, (oe - oldpath))))
218 /*@notreached@*/ break;
220 return link(oldpath, newpath);
223 /* XXX build/build.c: analogue to unlink(2). */
225 int Unlink(const char * path) {
227 int ut = urlPath(path, &lpath);
231 return ftpUnlink(path);
232 /*@notreached@*/ break;
233 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
242 /*@notreached@*/ break;
247 /* XXX swiped from mc-4.5.39-pre9 vfs/ftpfs.c */
249 #define g_strdup xstrdup
253 * FIXME: this is broken. It depends on mc not crossing border on month!
255 static int current_mday;
256 static int current_mon;
257 static int current_year;
259 /* Following stuff (parse_ls_lga) is used by ftpfs and extfs */
262 static char *columns [MAXCOLS]; /* Points to the string in column n */
263 static int column_ptr [MAXCOLS]; /* Index from 0 to the starting positions of the columns */
266 vfs_split_text (char *p)
267 /*@modifies *p, columns, column_ptr @*/
273 for (numcols = 0; *p && numcols < MAXCOLS; numcols++){
274 while (*p == ' ' || *p == '\r' || *p == '\n'){
278 columns [numcols] = p;
279 column_ptr [numcols] = p - original;
280 while (*p && *p != ' ' && *p != '\r' && *p != '\n')
290 if (!columns [idx] || columns [idx][0] < '0' || columns [idx][0] > '9')
296 is_dos_date(/*@null@*/ const char *str)
299 if (str != NULL && strlen(str) == 8 &&
300 str[2] == str[5] && strchr("\\-/", (int)str[2]) != NULL)
306 is_week (/*@null@*/ const char * str, /*@out@*/ struct tm * tim)
309 /*@observer@*/ static const char * week = "SunMonTueWedThuFriSat";
312 /*@-observertrans -mayaliasunique@*/
313 if (str != NULL && (pos=strstr(week, str)) != NULL) {
314 /*@=observertrans =mayaliasunique@*/
316 tim->tm_wday = (pos - week)/3;
323 is_month (/*@null@*/ const char * str, /*@out@*/ struct tm * tim)
326 /*@observer@*/ static const char * month = "JanFebMarAprMayJunJulAugSepOctNovDec";
329 /*@-observertrans -mayaliasunique@*/
330 if (str != NULL && (pos = strstr(month, str)) != NULL) {
331 /*@=observertrans -mayaliasunique@*/
333 tim->tm_mon = (pos - month)/3;
340 is_time (/*@null@*/ const char * str, /*@out@*/ struct tm * tim)
343 const char * p, * p2;
345 if (str != NULL && (p = strchr(str, ':')) && (p2 = strrchr(str, ':'))) {
347 if (sscanf (str, "%2d:%2d:%2d", &tim->tm_hour, &tim->tm_min, &tim->tm_sec) != 3)
350 if (sscanf (str, "%2d:%2d", &tim->tm_hour, &tim->tm_min) != 2)
359 static int is_year(/*@null@*/ const char * str, /*@out@*/ struct tm * tim)
370 if (strlen(str) != 4)
373 if (sscanf(str, "%ld", &year) != 1)
376 if (year < 1900 || year > 3000)
379 tim->tm_year = (int) (year - 1900);
385 * FIXME: this is broken. Consider following entry:
386 * -rwx------ 1 root root 1 Aug 31 10:04 2904 1234
387 * where "2904 1234" is filename. Well, this code decodes it as year :-(.
391 vfs_parse_filetype (char c)
395 case 'd': return S_IFDIR;
396 case 'b': return S_IFBLK;
397 case 'c': return S_IFCHR;
398 case 'l': return S_IFLNK;
400 #ifdef IS_IFSOCK /* And if not, we fall through to IFIFO, which is pretty close */
403 case 'p': return S_IFIFO;
404 case 'm': case 'n': /* Don't know what these are :-) */
405 case '-': case '?': return S_IFREG;
410 static int vfs_parse_filemode (const char *p)
412 { /* converts rw-rw-rw- into 0666 */
415 case 'r': res |= 0400; break;
420 case 'w': res |= 0200; break;
425 case 'x': res |= 0100; break;
426 case 's': res |= 0100 | S_ISUID; break;
427 case 'S': res |= S_ISUID; break;
432 case 'r': res |= 0040; break;
437 case 'w': res |= 0020; break;
442 case 'x': res |= 0010; break;
443 case 's': res |= 0010 | S_ISGID; break;
444 case 'l': /* Solaris produces these */
445 case 'S': res |= S_ISGID; break;
450 case 'r': res |= 0004; break;
455 case 'w': res |= 0002; break;
460 case 'x': res |= 0001; break;
461 case 't': res |= 0001 | S_ISVTX; break;
462 case 'T': res |= S_ISVTX; break;
469 static int vfs_parse_filedate(int idx, time_t *t)
471 { /* This thing parses from idx in columns[] array */
478 /* Let's setup default time values */
479 tim.tm_year = current_year;
480 tim.tm_mon = current_mon;
481 tim.tm_mday = current_mday;
485 tim.tm_isdst = -1; /* Let mktime() try to guess correct dst offset */
489 /* We eat weekday name in case of extfs */
494 if(is_month(p, &tim)){
495 /* And we expect, it followed by day number */
497 tim.tm_mday = (int)atol (columns [idx++]);
499 return 0; /* No day */
502 /* We usually expect:
505 But in case of extfs we allow these date formats:
508 Wek Mon DD hh:mm:ss YYYY
510 where Mon is Jan-Dec, DD, MM, YY two digit day, month, year,
511 YYYY four digit year, hh, mm, ss two digit hour, minute or second. */
513 /* Here just this special case with MM-DD-YY */
517 memset(d, 0, sizeof(d));
518 if (sscanf(p, "%2d-%2d-%2d", &d[0], &d[1], &d[2]) == 3){
525 /* Hmm... maybe, next time :)*/
527 /* At last, MM-DD-YY */
528 d[0]--; /* Months are zerobased */
538 return 0; /* sscanf failed */
540 return 0; /* unsupported format */
543 /* Here we expect to find time and/or year */
546 if(is_time(columns[idx], &tim) || (got_year = is_year(columns[idx], &tim))) {
549 /* This is a special case for ctime() or Mon DD YYYY hh:mm */
551 ((got_year = is_year(columns[idx], &tim)) || is_time(columns[idx], &tim)))
552 idx++; /* time & year or reverse */
553 } /* only time or date */
556 return 0; /* Nor time or date */
559 * If the date is less than 6 months in the past, it is shown without year
560 * other dates in the past or future are shown with year but without time
561 * This does not check for years before 1900 ... I don't know, how
562 * to represent them at all
565 current_mon < 6 && current_mon < tim.tm_mon &&
566 tim.tm_mon - current_mon >= 6)
570 if ((*t = mktime(&tim)) < 0)
576 vfs_parse_ls_lga (char * p, /*@out@*/ struct stat * st,
577 /*@out@*/ const char ** filename,
578 /*@out@*/ const char ** linkname)
579 /*@modifies *st, *filename, *linkname @*/
581 int idx, idx2, num_cols;
585 if (strncmp (p, "total", 5) == 0)
588 p_copy = g_strdup(p);
589 /* XXX FIXME: parse out inode number from "NLST -lai ." */
590 /* XXX FIXME: parse out sizein blocks from "NLST -lais ." */
592 if ((i = vfs_parse_filetype(*(p++))) == -1)
596 if (*p == ' ') /* Notwell 4 */
599 if (strlen (p) <= 8 || p [8] != ']')
601 /* Should parse here the Notwell permissions :) */
603 if (S_ISDIR (st->st_mode))
604 st->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IXUSR | S_IXGRP | S_IXOTH);
606 st->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);
610 if ((i = vfs_parse_filemode(p)) == -1)
615 /* This is for an extra ACL attribute (HP-UX) */
621 p_copy = g_strdup(p);
622 num_cols = vfs_split_text (p);
624 st->st_nlink = atol (columns [0]);
625 if (st->st_nlink < 0)
630 st->st_uid = finduid (columns [1]);
632 (void) unameToUid (columns [1], &st->st_uid);
635 st->st_uid = (uid_t) atol (columns [1]);
637 /* Mhm, the ls -lg did not produce a group field */
638 for (idx = 3; idx <= 5; idx++)
639 if (is_month(columns [idx], NULL) || is_week(columns [idx], NULL) || is_dos_date(columns[idx]))
642 if (idx == 6 || (idx == 5 && !S_ISCHR (st->st_mode) && !S_ISBLK (st->st_mode)))
645 /* We don't have gid */
646 if (idx == 3 || (idx == 4 && (S_ISCHR(st->st_mode) || S_ISBLK (st->st_mode))))
649 /* We have gid field */
651 st->st_gid = (gid_t) atol (columns [2]);
654 st->st_gid = findgid (columns [2]);
656 (void) gnameToGid (columns [1], &st->st_gid);
662 if (S_ISCHR (st->st_mode) || S_ISBLK (st->st_mode)){
665 if (!is_num (idx2) || sscanf(columns [idx2], " %d,", &maj) != 1)
668 if (!is_num (++idx2) || sscanf(columns [idx2], " %d", &min) != 1)
672 st->st_rdev = ((maj & 0xff) << 8) | (min & 0xffff00ff);
677 /* Common file size */
681 st->st_size = (size_t) atol (columns [idx2]);
687 idx = vfs_parse_filedate(idx, &st->st_mtime);
690 /* Use resulting time value */
691 st->st_atime = st->st_ctime = st->st_mtime;
694 #ifdef HAVE_ST_BLKSIZE
695 st->st_blksize = 512;
697 #ifdef HAVE_ST_BLOCKS
698 st->st_blocks = (st->st_size + 511) / 512;
701 for (i = idx + 1, idx2 = 0; i < num_cols; i++ )
702 if (strcmp (columns [i], "->") == 0){
707 if (((S_ISLNK (st->st_mode) ||
708 (num_cols == idx + 3 && st->st_nlink > 1))) /* Maybe a hardlink? (in extfs) */
715 t = g_strndup (p_copy + column_ptr [idx], column_ptr [idx2] - column_ptr [idx] - 1);
717 int nb = column_ptr [idx2] - column_ptr [idx] - 1;
719 strncpy(t, p_copy + column_ptr [idx], nb);
724 t = g_strdup (p_copy + column_ptr [idx2+1]);
726 if (t [tlen-1] == '\r' || t [tlen-1] == '\n')
728 if (t [tlen-2] == '\r' || t [tlen-2] == '\n')
734 /* Extract the filename from the string copy, not from the columns
735 * this way we have a chance of entering hidden directories like ". ."
739 *filename = g_strdup (columns [idx++]);
744 t = g_strdup (p_copy + column_ptr [idx++]);
747 if (t [tlen-1] == '\r' || t [tlen-1] == '\n')
749 if (t [tlen-2] == '\r' || t [tlen-2] == '\n')
763 static int errorcount = 0;
765 if (++errorcount < 5) {
766 message_1s (1, "Could not parse:", p_copy);
767 } else if (errorcount == 5)
768 message_1s (1, "More parsing errors will be ignored.", "(sorry)" );
773 if (p_copy != p) /* Carefull! */
786 static size_t ftpBufAlloced = 0;
787 static /*@only@*/ char * ftpBuf = NULL;
789 #define alloca_strdup(_s) strcpy(alloca(strlen(_s)+1), (_s))
791 static int ftpNLST(const char * url, ftpSysCall_t ftpSysCall,
792 /*@out@*/ struct stat * st, char * rlbuf, size_t rlbufsiz)
793 /*@modifies *st, *rlbuf, fileSystem @*/
797 int bufLength, moretodo;
798 const char *n, *ne, *o, *oe;
807 n = ne = o = oe = NULL;
808 (void) urlPath(url, &path);
812 switch (ftpSysCall) {
814 fd = ftpOpen(url, 0, 0, &u);
815 if (fd == NULL || u == NULL)
818 u->openError = ftpReq(fd, "NLST", path);
821 urldn = alloca_strdup(url);
822 if ((bn = strrchr(urldn, '/')) == NULL)
830 rc = ftpChdir(urldn); /* XXX don't care about CWD */
834 fd = ftpOpen(url, 0, 0, &u);
835 if (fd == NULL || u == NULL)
838 /* XXX possibly should do "NLST -lais" to get st_ino/st_blocks also */
839 u->openError = ftpReq(fd, "NLST", "-la");
843 if (u->openError < 0) {
844 fd = fdLink(fd, "error data (ftpStat)");
849 if (bn == NULL || nbn <= 0) {
854 if (ftpBufAlloced == 0 || ftpBuf == NULL) {
855 ftpBufAlloced = url_iobuf_size;
856 ftpBuf = xcalloc(ftpBufAlloced, sizeof(ftpBuf[0]));
865 /* XXX FIXME: realloc ftpBuf is < ~128 chars remain */
866 if ((ftpBufAlloced - bufLength) < (1024+80)) {
868 ftpBuf = xrealloc(ftpBuf, ftpBufAlloced);
870 s = se = ftpBuf + bufLength;
873 rc = fdFgets(fd, se, (ftpBufAlloced - bufLength));
878 if (ftpSysCall == DO_FTP_GLOB) { /* XXX HACK */
879 bufLength += strlen(se);
883 for (s = se; *s != '\0'; s = se) {
886 while (*se && *se != '\n') se++;
887 if (se > s && se[-1] == '\r') se[-1] = '\0';
889 /*@innerbreak@*/ break;
892 if (!strncmp(s, "total ", sizeof("total ")-1)) continue;
895 for (bingo = 0, n = se; n >= s; n--) {
901 if (o || !(n[-3] == ' ' && n[-2] == '-' && n[-1] == '>')) {
902 while (*(++n) == ' ')
907 for (o = n + 1; *o == ' '; o++)
916 /*@innerbreak@*/ break;
919 if (nbn != (ne - n)) continue; /* Same name length? */
920 if (strncmp(n, bn, nbn)) continue; /* Same name? */
923 /*@innerbreak@*/ break;
926 if (moretodo && se > s) {
927 bufLength = se - s - 1;
929 memmove(ftpBuf, s, bufLength);
935 switch (ftpSysCall) {
938 /* XXX FIXME: symlink, replace urldn/bn from [o,oe) and restart */
942 if (st == NULL || !(n && ne)) {
945 rc = ((vfs_parse_ls_lga(s, st, NULL, NULL) > 0) ? 0 : -1);
948 case DO_FTP_READLINK:
949 if (rlbuf == NULL || !(o && oe)) {
955 memcpy(rlbuf, o, rc);
961 rc = 0; /* XXX WRONG WRONG WRONG */
964 rc = 0; /* XXX WRONG WRONG WRONG */
973 static int ftpStat(const char * path, /*@out@*/ struct stat *st)
976 return ftpNLST(path, DO_FTP_STAT, st, NULL, 0);
979 static int ftpLstat(const char * path, /*@out@*/ struct stat *st)
983 rc = ftpNLST(path, DO_FTP_LSTAT, st, NULL, 0);
985 fprintf(stderr, "*** ftpLstat(%s) rc %d\n", path, rc);
989 static int ftpReadlink(const char * path, char * buf, size_t bufsiz)
992 return ftpNLST(path, DO_FTP_READLINK, NULL, buf, bufsiz);
995 static int ftpGlob(const char * path, int flags,
996 int errfunc(const char * epath, int eerno),
997 /*@out@*/ glob_t * pglob)
998 /*@modifies *pglob, fileSystem @*/
1004 rc = ftpNLST(path, DO_FTP_GLOB, NULL, NULL, 0);
1007 fprintf(stderr, "*** ftpGlob(%s,0x%x,%p,%p) ftpNLST rc %d\n", path, (unsigned)flags, (void *)errfunc, pglob, rc);
1011 rc = poptParseArgvString(ftpBuf, &pglob->gl_pathc, (const char ***)&pglob->gl_pathv);
1012 pglob->gl_offs = -1; /* XXX HACK HACK HACK */
1016 static void ftpGlobfree(glob_t * pglob)
1017 /*@modifies *pglob @*/
1020 fprintf(stderr, "*** ftpGlobfree(%p)\n", pglob);
1021 if (pglob->gl_offs == -1) { /* XXX HACK HACK HACK */
1022 free((void *)pglob->gl_pathv);
1023 pglob->gl_pathv = NULL;
1027 int Stat(const char * path, struct stat * st)
1030 int ut = urlPath(path, &lpath);
1033 fprintf(stderr, "*** Stat(%s,%p)\n", path, st);
1036 return ftpStat(path, st);
1037 /*@notreached@*/ break;
1038 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1042 case URL_IS_UNKNOWN:
1047 /*@notreached@*/ break;
1049 return stat(path, st);
1052 int Lstat(const char * path, struct stat * st)
1055 int ut = urlPath(path, &lpath);
1058 fprintf(stderr, "*** Lstat(%s,%p)\n", path, st);
1061 return ftpLstat(path, st);
1062 /*@notreached@*/ break;
1063 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1067 case URL_IS_UNKNOWN:
1072 /*@notreached@*/ break;
1074 return lstat(path, st);
1077 int Readlink(const char * path, char * buf, size_t bufsiz)
1080 int ut = urlPath(path, &lpath);
1084 return ftpReadlink(path, buf, bufsiz);
1085 /*@notreached@*/ break;
1086 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1090 case URL_IS_UNKNOWN:
1095 /*@notreached@*/ break;
1097 return readlink(path, buf, bufsiz);
1100 int Access(const char * path, int amode)
1103 int ut = urlPath(path, &lpath);
1106 fprintf(stderr, "*** Access(%s,%d)\n", path, amode);
1108 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
1109 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1113 case URL_IS_UNKNOWN:
1118 /*@notreached@*/ break;
1120 return access(path, amode);
1123 int Glob(const char *path, int flags,
1124 int errfunc(const char * epath, int eerrno), glob_t *pglob)
1127 int ut = urlPath(path, &lpath);
1131 fprintf(stderr, "*** Glob(%s,0x%x,%p,%p)\n", path, (unsigned)flags, (void *)errfunc, pglob);
1134 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
1135 return ftpGlob(path, flags, errfunc, pglob);
1136 /*@notreached@*/ break;
1137 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1141 case URL_IS_UNKNOWN:
1146 /*@notreached@*/ break;
1148 return glob(path, flags, errfunc, pglob);
1151 void Globfree(glob_t *pglob)
1154 fprintf(stderr, "*** Globfree(%p)\n", pglob);
1155 if (pglob->gl_offs == -1) /* XXX HACK HACK HACK */
1161 DIR * Opendir(const char * path)
1164 int ut = urlPath(path, &lpath);
1167 fprintf(stderr, "*** Opendir(%s)\n", path);
1169 case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
1170 case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1174 case URL_IS_UNKNOWN:
1179 /*@notreached@*/ break;
1181 return opendir(path);
1185 struct dirent * Readdir(DIR * dir)
1188 fprintf(stderr, "*** Readdir(%p)\n", (void *)dir);
1189 return readdir(dir);
1192 int Closedir(DIR * dir)
1195 fprintf(stderr, "*** Closedir(%p)\n", (void *)dir);
1196 return closedir(dir);