File: openfile.c

package info (click to toggle)
ifmail 2.14tx8.10-22
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,816 kB
  • ctags: 4,126
  • sloc: ansic: 30,317; perl: 4,955; yacc: 835; makefile: 732; sh: 426; cpp: 235; lex: 206; awk: 24
file content (234 lines) | stat: -rw-r--r-- 4,958 bytes parent folder | download | duplicates (15)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <utime.h>
#include <fcntl.h>
#include "xutil.h"
#include "lutil.h"
#include "config.h"
#include "needed.h"

extern unsigned INT32 sequencer(void);

static FILE *infp=NULL;
static char *infpath=NULL;
static time_t intime;
static int isfreq;
char *freqname=NULL;

/*
   Try to find present (probably incomplete) file with the same timestamp
   (it might be renamed), open it for append and store resync offset.
   Store 0 in resync offset if the file is new. Return FILE* or NULL.
   resync() must accept offset in bytes and return 0 on success, nonzero
   otherwise (and then the file will be open for write).  For Zmodem,
   resync is always possible, but for SEAlink it is not.  Do not try
   any resyncs if remsize == 0.
*/

FILE *openfile(char*,time_t,off_t,off_t*,int(*)(off_t));
FILE *openfile(fname,remtime,remsize,resofs,resync)
char *fname;
time_t remtime;
off_t remsize;
off_t *resofs;
int (*resync)(off_t);
{
	char *opentype;
	char *p,x;
	char ctt[32];
	int rc,ncount;
	struct stat st;
	struct flock fl;
	char tmpfname[16];

	fl.l_type=F_WRLCK;
	fl.l_whence=0;
	fl.l_start=0L;
	fl.l_len=0L;
	strcpy(ctt,date(remtime));
	debug(11,"openfile(\"%s\",%s,%lu,...)",
		S(fname),S(ctt),(unsigned long)remsize);

	if ((fname == NULL) || (fname[0] == '\0'))
	{
		sprintf(tmpfname,"%08lx.pkt",(unsigned long)sequencer());
		fname=tmpfname;
	}
	if (infpath) free(infpath);
	infpath=xstrcpy(inbound);
	infpath=xstrcat(infpath,"/tmp/");
	infpath=xstrcat(infpath,fname);

	if ((strlen(fname) == 12) &&
	    (strspn(fname,"0123456789abcdefABCDEF") == 8) &&
	    (strcasecmp(fname+8,".req") == 0))
	{
		debug(12,"received wazoo freq file");
		isfreq=1;
	}
	else isfreq=0;

/*
	Renaming algorythm is as follows: start with the present name,
	increase the last character of the file name, jumping from 
	'9' to 'a', from 'z' to 'A', from 'Z' to '0'. If _all_ these 
	names are occupied, 
*/

	p=infpath+strlen(infpath)-1;
	x=*p;
	ncount=0;
	while (((rc=stat(infpath,&st)) == 0) &&
	       (remtime != st.st_mtime) &&
	       (ncount++ < 62))
	{
		if (x == '9') x='a';
		else if (x == 'z') x='A';
		else if (x == 'Z') x='0';
		else x++;
		*p=x;
	}
	if (isfreq || (ncount >= 62)) /* names exhausted */
	{
		rc=1;
		p=strrchr(infpath,'/');
		*p='\0';
		p=tempnam(infpath,",");
		free(infpath);
		infpath=xstrcpy(p); /* tempnam() returns pointer to a static
					buffer, right? */
	}
	*resofs=0L;
	opentype="w";
	if ((rc == 0) && (remsize != 0))
	{
		loginf("resyncing at offset %lu of \"%s\"",
			(unsigned long)st.st_size,infpath);
		if (resync(st.st_size) == 0)
		{
			opentype="a";
			*resofs=st.st_size;
		}
	}
	debug(11,"try fopen(\"%s\",\"%s\")",infpath,opentype);
	if ((infp=fopen(infpath,opentype)) == NULL)
	{
		logerr("$cannot open local file \"%s\" for \"%s\"",
			infpath,opentype);
		free(infpath);
		infpath=NULL;
		return NULL;
	}
	fl.l_pid=getpid();
	if (fcntl(fileno(infp),F_SETLK,&fl) != 0)
	{
		loginf("$cannot lock local file \"%s\"",infpath);
		fclose(infp);
		infp=NULL;
		free(infpath);
		infpath=NULL;
		return NULL;
	}
	intime=remtime;

	if (isfreq)
	{
		if (freqname) free(freqname);
		freqname=xstrcpy(infpath);
	}

	debug(11,"opened file \"%s\" for \"%s\", restart at %lu",
		infpath,opentype,(unsigned long)*resofs);
	return infp;
}

/*
   close file and if (success) { move it to the final location }
*/

int closefile(int);
int closefile(success)
int success;
{
	char *newpath,*p;
	int rc=0,ncount;
	char x;
	struct stat st;
	struct utimbuf ut;

	debug(11,"closefile(%d), for file \"%s\"",success,S(infpath));

	if ((infp == NULL) || (infpath == NULL))
	{
		logerr("internal error: try close unopened file!");
		return 1;
	}

	rc=fclose(infp);
	infp=NULL;

	if (rc == 0)
	{
		ut.actime=intime;
		ut.modtime=intime;
		if ((rc=utime(infpath,&ut)))
		{
			logerr("$utime failed");
		}
	}

	if (isfreq)
	{
		if ((rc != 0) || (!success))
		{
			loginf("removing unsuccessfuly received wazoo freq");
			unlink(freqname);
			free(freqname);
			freqname=NULL;
		}
		isfreq=0;
	}
	else if ((rc == 0) && success)
	{
		newpath=xstrcpy(inbound);
		newpath=xstrcat(newpath,strrchr(infpath,'/'));
		
		p=newpath+strlen(newpath)-1;
		x=*p;
		ncount=0;
		while (((rc=stat(newpath,&st)) == 0) &&
		       (ncount++ < 62))
		{
			if (x == '9') x='a';
			else if (x == 'z') x='A';
			else if (x == 'Z') x='0';
			else x++;
			*p=x;
		}
		if (ncount >= 62) /* names exhausted */
		{
			rc=1;
			p=strrchr(newpath,'/');
			*p='\0';
			p=tempnam(newpath,",");
			free(newpath);
			newpath=xstrcpy(p); /* tempnam() returns pointer to 
						a static buffer, right? */
		}

		debug(11,"moving \"%s\" -> \"%s\"",S(infpath),S(newpath));
		rc=rename(infpath,newpath);
		if (rc) logerr("$error renaming \"%s\" -> \"%s\"",
				S(infpath),S(newpath));

		free(newpath);
	}
	free(infpath);
	infpath=NULL;
	return rc;
}