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, /*@out@*/ 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@*/ /*@null@*/ struct stat * st,
793
/*@out@*/ /*@null@*/ char * rlbuf, size_t rlbufsiz)
794
/*@modifies *st, *rlbuf, fileSystem @*/
798
int bufLength, moretodo;
799
const char *n, *ne, *o, *oe;
808
n = ne = o = oe = NULL;
809
(void) urlPath(url, &path);
813
switch (ftpSysCall) {
815
fd = ftpOpen(url, 0, 0, &u);
816
if (fd == NULL || u == NULL)
819
u->openError = ftpReq(fd, "NLST", path);
822
urldn = alloca_strdup(url);
823
if ((bn = strrchr(urldn, '/')) == NULL)
831
rc = ftpChdir(urldn); /* XXX don't care about CWD */
835
fd = ftpOpen(url, 0, 0, &u);
836
if (fd == NULL || u == NULL)
839
/* XXX possibly should do "NLST -lais" to get st_ino/st_blocks also */
840
u->openError = ftpReq(fd, "NLST", "-la");
842
if (bn == NULL || nbn <= 0) {
849
if (u->openError < 0) {
850
fd = fdLink(fd, "error data (ftpStat)");
855
if (ftpBufAlloced == 0 || ftpBuf == NULL) {
856
ftpBufAlloced = url_iobuf_size;
857
ftpBuf = xcalloc(ftpBufAlloced, sizeof(ftpBuf[0]));
866
/* XXX FIXME: realloc ftpBuf is < ~128 chars remain */
867
if ((ftpBufAlloced - bufLength) < (1024+80)) {
869
ftpBuf = xrealloc(ftpBuf, ftpBufAlloced);
871
s = se = ftpBuf + bufLength;
874
rc = fdFgets(fd, se, (ftpBufAlloced - bufLength));
879
if (ftpSysCall == DO_FTP_GLOB) { /* XXX HACK */
880
bufLength += strlen(se);
884
for (s = se; *s != '\0'; s = se) {
887
while (*se && *se != '\n') se++;
888
if (se > s && se[-1] == '\r') se[-1] = '\0';
890
/*@innerbreak@*/ break;
893
if (!strncmp(s, "total ", sizeof("total ")-1)) continue;
896
for (bingo = 0, n = se; n >= s; n--) {
902
if (o || !(n[-3] == ' ' && n[-2] == '-' && n[-1] == '>')) {
903
while (*(++n) == ' ')
908
for (o = n + 1; *o == ' '; o++)
917
/*@innerbreak@*/ break;
920
if (nbn != (ne - n)) continue; /* Same name length? */
921
if (strncmp(n, bn, nbn)) continue; /* Same name? */
924
/*@innerbreak@*/ break;
927
if (moretodo && se > s) {
928
bufLength = se - s - 1;
930
memmove(ftpBuf, s, bufLength);
936
switch (ftpSysCall) {
939
/* XXX FIXME: symlink, replace urldn/bn from [o,oe) and restart */
943
if (st == NULL || !(n && ne)) {
946
rc = ((vfs_parse_ls_lga(s, st, NULL, NULL) > 0) ? 0 : -1);
949
case DO_FTP_READLINK:
950
if (rlbuf == NULL || !(o && oe)) {
956
memcpy(rlbuf, o, rc);
962
rc = 0; /* XXX WRONG WRONG WRONG */
965
rc = 0; /* XXX WRONG WRONG WRONG */
974
static int ftpStat(const char * path, /*@out@*/ struct stat *st)
977
return ftpNLST(path, DO_FTP_STAT, st, NULL, 0);
980
static int ftpLstat(const char * path, /*@out@*/ struct stat *st)
984
rc = ftpNLST(path, DO_FTP_LSTAT, st, NULL, 0);
986
fprintf(stderr, "*** ftpLstat(%s) rc %d\n", path, rc);
990
static int ftpReadlink(const char * path, /*@out@*/ char * buf, size_t bufsiz)
993
return ftpNLST(path, DO_FTP_READLINK, NULL, buf, bufsiz);
996
static int ftpGlob(const char * path, int flags,
997
int errfunc(const char * epath, int eerno),
998
/*@out@*/ glob_t * pglob)
999
/*@modifies *pglob, fileSystem @*/
1005
rc = ftpNLST(path, DO_FTP_GLOB, NULL, NULL, 0);
1008
fprintf(stderr, "*** ftpGlob(%s,0x%x,%p,%p) ftpNLST rc %d\n", path, (unsigned)flags, (void *)errfunc, pglob, rc);
1012
rc = poptParseArgvString(ftpBuf, &pglob->gl_pathc, (const char ***)&pglob->gl_pathv);
1013
pglob->gl_offs = -1; /* XXX HACK HACK HACK */
1017
static void ftpGlobfree(glob_t * pglob)
1018
/*@modifies *pglob @*/
1021
fprintf(stderr, "*** ftpGlobfree(%p)\n", pglob);
1022
if (pglob->gl_offs == -1) { /* XXX HACK HACK HACK */
1023
free((void *)pglob->gl_pathv);
1024
pglob->gl_pathv = NULL;
1028
int Stat(const char * path, struct stat * st)
1031
int ut = urlPath(path, &lpath);
1034
fprintf(stderr, "*** Stat(%s,%p)\n", path, st);
1037
return ftpStat(path, st);
1038
/*@notreached@*/ break;
1039
case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1043
case URL_IS_UNKNOWN:
1048
/*@notreached@*/ break;
1050
return stat(path, st);
1053
int Lstat(const char * path, struct stat * st)
1056
int ut = urlPath(path, &lpath);
1059
fprintf(stderr, "*** Lstat(%s,%p)\n", path, st);
1062
return ftpLstat(path, st);
1063
/*@notreached@*/ break;
1064
case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1068
case URL_IS_UNKNOWN:
1073
/*@notreached@*/ break;
1075
return lstat(path, st);
1078
int Readlink(const char * path, char * buf, size_t bufsiz)
1081
int ut = urlPath(path, &lpath);
1085
return ftpReadlink(path, buf, bufsiz);
1086
/*@notreached@*/ break;
1087
case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1091
case URL_IS_UNKNOWN:
1096
/*@notreached@*/ break;
1098
return readlink(path, buf, bufsiz);
1101
int Access(const char * path, int amode)
1104
int ut = urlPath(path, &lpath);
1107
fprintf(stderr, "*** Access(%s,%d)\n", path, amode);
1109
case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
1110
case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1114
case URL_IS_UNKNOWN:
1119
/*@notreached@*/ break;
1121
return access(path, amode);
1124
int Glob(const char *path, int flags,
1125
int errfunc(const char * epath, int eerrno), glob_t *pglob)
1128
int ut = urlPath(path, &lpath);
1132
fprintf(stderr, "*** Glob(%s,0x%x,%p,%p)\n", path, (unsigned)flags, (void *)errfunc, pglob);
1135
case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
1136
return ftpGlob(path, flags, errfunc, pglob);
1137
/*@notreached@*/ break;
1138
case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1142
case URL_IS_UNKNOWN:
1147
/*@notreached@*/ break;
1149
return glob(path, flags, errfunc, pglob);
1152
void Globfree(glob_t *pglob)
1155
fprintf(stderr, "*** Globfree(%p)\n", pglob);
1156
if (pglob->gl_offs == -1) /* XXX HACK HACK HACK */
1162
DIR * Opendir(const char * path)
1165
int ut = urlPath(path, &lpath);
1168
fprintf(stderr, "*** Opendir(%s)\n", path);
1170
case URL_IS_FTP: /* XXX WRONG WRONG WRONG */
1171
case URL_IS_HTTP: /* XXX WRONG WRONG WRONG */
1175
case URL_IS_UNKNOWN:
1180
/*@notreached@*/ break;
1182
return opendir(path);
1186
struct dirent * Readdir(DIR * dir)
1189
fprintf(stderr, "*** Readdir(%p)\n", (void *)dir);
1190
return readdir(dir);
1193
int Closedir(DIR * dir)
1196
fprintf(stderr, "*** Closedir(%p)\n", (void *)dir);
1197
return closedir(dir);