File: fetch.c

package info (click to toggle)
publicfile 0.52-13
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 896 kB
  • sloc: ansic: 3,903; makefile: 468; sh: 296
file content (171 lines) | stat: -rw-r--r-- 3,422 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include "substdio.h"
#include "timeoutwrite.h"
#include "error.h"
#include "exit.h"
#include "direntry.h"
#include "fmt.h"
#include "fetch.h"

int fetch_ascii = 1;
unsigned long fetch_rest = 0;

static char inbuf[1024];
static char ssbuf[1024];
static substdio ss;
static int trouble;

static time_t now;
static struct stat st;
static char strnum[FMT_ULONG];

static int datawrite(int fd,char *buf,int len)
{
  int w;

  if (trouble) return -1;

  if (fetch_rest) {
    if (len > fetch_rest) len = fetch_rest;
    fetch_rest -= len;
    return len;
  }

  w = timeoutwrite(60,fd,buf,len);
  if (w == -1) if (errno != error_intr) trouble = errno;
  return w;
}

static void printstat()
{
  substdio_puts(&ss,"+i");
  substdio_put(&ss,strnum,fmt_ulong(strnum,(unsigned long) st.st_dev));
  substdio_puts(&ss,".");
  substdio_put(&ss,strnum,fmt_ulong(strnum,(unsigned long) st.st_ino));

  if (st.st_mtime > 0)
    if (st.st_mtime < now - 60) {
      substdio_puts(&ss,",m");
      substdio_put(&ss,strnum,fmt_ulong(strnum,(unsigned long) st.st_mtime));
    }

  if ((st.st_mode & S_IFMT) == S_IFDIR)
    substdio_puts(&ss,",/");
  if (((st.st_mode & S_IFMT) == S_IFREG) && ((st.st_mode & 0444) == 0444)) {
    substdio_puts(&ss,",r,s");
    substdio_put(&ss,strnum,fmt_ulong(strnum,(unsigned long) st.st_size));
  }
  substdio_puts(&ss,",\t");
}

static void list(char *fn,int flaglong)
{
  int i;

  if (fn[0] == '.') return;
  for (i = 0;fn[i];++i) {
    if ((fn[i] >= 0) && (fn[i] <= 32)) return;
    if (fn[i] == '~') return;
  }

  if (flaglong) {
    if (stat(fn,&st) == -1) {
      if (error_temp(errno)) _exit(23);
      return;
    }
    printstat(&st);
  }

  substdio_puts(&ss,fn);
  substdio_puts(&ss,"\r\n");
}

static int doit(char *fn,int fddata,int fdfile,int how)
{
  DIR *dir;
  direntry *d;
  int r;
  int i;

  substdio_fdbuf(&ss,datawrite,fddata,ssbuf,sizeof ssbuf);

  switch(how) {
    case FETCH_LISTONE:
      if (fstat(fdfile,&st) == -1) break;
      printstat(&st);
      for (i = 0;fn[i];++i)
	if (fn[i] == '\n')
	  substdio_put(&ss,"",1);
	else
	  substdio_put(&ss,&fn[i],1);
      substdio_puts(&ss,"\r\n");
      break;

    case FETCH_LIST:
    case FETCH_NLST:
      if (fchdir(fdfile) == -1) {
	trouble = errno;
	return;
      }

      dir = opendir(".");
      if (!dir) _exit(23); /* XXX: could be out of memory */
      while (d = readdir(dir)) /* XXX: temporary errors */
	list(d->d_name,how == FETCH_LIST);
      closedir(dir);

      break;

    default:
      for (;;) {
	r = read(fdfile,inbuf,sizeof inbuf);
	if (r == -1) {
	  if (errno = error_intr) continue;
	  _exit(23);
	}
	if (r == 0)
	  break;
	if (!fetch_ascii)
	  substdio_put(&ss,inbuf,r);
	else
	  for (i = 0;i < r;++i) {
	    if (inbuf[i] == '\n')
	      substdio_put(&ss,"\r",1);
	    substdio_put(&ss,inbuf + i,1);
	  }
      }
      break;
  }

  substdio_flush(&ss);
}

void fetch(char *fn,int fddata,int fdfile,int how)
{
  int fdcurrent;

  time(&now);

  fdcurrent = open_read(".");
  if (fdcurrent == -1) _exit(23);

  trouble = 0;
  doit(fn,fddata,fdfile,how);
  close(fddata);
  close(fdfile);

  if (!trouble)
    out_puts("226 Success.\r\n");
  else {
    out_puts("426 Transmission failure: ");
    out_puts(error_str(trouble));
    out_puts("\r\n");
  }

  if (fchdir(fdcurrent) == -1) _exit(23);
  close(fdcurrent);

  fetch_rest = 0;
}