File: common_bg.c

package info (click to toggle)
taper 6.9rb-5
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,444 kB
  • ctags: 1,604
  • sloc: ansic: 15,921; makefile: 248
file content (407 lines) | stat: -rw-r--r-- 9,127 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
   Time-stamp: <00/01/10 17:31:58 yusuf>

   $Id: common_bg.c,v 1.1.2.1 1999/05/12 13:34:42 yusuf Exp $	

*/

#ifndef lint
static char vcid[] = "$Id: common_bg.c,v 1.1.2.1 1999/05/12 13:34:42 yusuf Exp $";
#endif /* lint */



/* Common routines to background programs and main
   taper program */
   
   
#include "taper.h"   


void mail_finish(char *prog)
/* Prints a message to the mail file about completion */
{
    time_t t;
    char s[100];
    
    time(&t);
    sprintf(s, "\n%s finished at %s\n", prog, ctime(&t));
    write(mf, s, strlen(s));
}


void sendmail()
{
/* Closes mail file, sends mail and deletes mail file */
    char s[MAX_FNAME];
    
    mail_finish("Backup");
    close(mf);
    sprintf(s, "%s %s -s \"Taper Unattended Backup\" < %s", MAIL_PROG, MAIL_TO, mailfn);   /* send mail */
    system(s);
    unlink(mailfn);
}


_errstat do_exit(int c) {
    char err[100];
    _s32  adjc=0;
    
    while (errs[adjc].err_no)			 /* find error in template */
      if (errs[adjc].err_no == c)
        break;
      else adjc++;
    strcpy(err, strerror(errno));
    strcat(err, " while ");
    (errs[adjc].no_append) ? strcpy(err, errs[adjc].mess) :
    (errs[adjc].err_no == 0) ? strcat(err, "Unknown error") :
                               strcat(err, errs[adjc].mess);
    write_log(err);
    (win_main == NULL) ? fprintf(stderr, "%s\n", err) :
                         message_box(err, MB_OK);
    if (errs[adjc].exit || !c) {		 /* need to properly exit */
    	write_log("Exiting with error");
    	if (lf) 
	   write(lf, "\n\n", strlen("\n\n"));
	my_free_all();				 /* free all user memory */
	exit(-1);
    }
    log_errors++;
    if (mf) { 					 /* write to mail file */
	strcat(err, "\n");
	write(mf, err, strlen(err));
	strcpy(err, "Backup ABORTED\n");
	write(mf, err, strlen(err));
	sendmail();				 /* send mail */
    }
    return -1;
}


_errstat exclude_list(char *fn, char *list)
{
/* Determines whether to exclude file 'fn' from archive 
 * 
 * returns 1 if should be excluded (ie. in list)
 * returns 0 if shouldn't be excluded
*/
    char *s, *b;
    char tok[MAX_FNAME];
    int fin=0;
   
    if (!*list) return 0;                         /* empty exclusion list */ 
    strcpy(tok, list);
    s=tok;
    while (!fin) {
	b=s;
	while (*s && (*s != ' ')) s++;		 /* look for space or eos */
	if (!*s) fin=1;				 /* end of string */
	*s = 0; 
	if (!strcasecmp(b, &fn[strlen(fn)-strlen(b)]))
	  return 1;
	s++;
    }
    return 0;
}


_errstat exclude_compression(char *fn)
{
/* Checks to see if file is one of the files not to compress.
 * 
 * returns 1 if should be excluded (ie. in list)
 * returns 0 if shouldn't be excluded
*/
    return exclude_list(fn, exclude_compress);
}

    
_errstat malloc_comp_buffers()
{
    ULONG pci;
    struct compress_identity *ci;
    
    compress(COMPRESS_ACTION_IDENTITY, NULL, NULL, 0, NULL, &pci);   /* get compression buffer */
    ci = (struct compress_identity *) pci;
    cbuf = my_malloc(ci->memory);
    if (cbuf == NULL) return do_exit(ERROR_MEMORY);
    comp_buffer1 = my_malloc(COMPRESS2_BUFFER_SIZE);
    if (comp_buffer1 == NULL) return do_exit(ERROR_MEMORY);
    comp_buffer2 = my_malloc(COMPRESS2_BUFFER_SIZE);
    if (comp_buffer2 == NULL) return do_exit(ERROR_MEMORY);
    return 0;
}


void free_comp_buffers()
{
    if (comp_buffer1) my_free(comp_buffer1);
    if (comp_buffer2) my_free(comp_buffer2);
    if (cbuf) my_free(cbuf);
}


void write_log(char *s)
{
    time_t  t;
    char    s1[500], s2[500];
    
    if (!lf) return;				 /* no log file open */
    t = time(NULL);
    strcpy(s1, ctime(&t));
    s1[strlen(s1)-1] = 0;			 /* remove trailing \n */
    sprintf(s2, "%s:  %s\n", s1, s);
    write(lf, s2, strlen(s2));
}


void write_error_log(char *s)
{
/* Writes an error to the log file. 
 * 
 * Format:   Date:  ERROR: Error while s
*/ 
    char s1[5000];
    
    sprintf(s1, "ERROR:  %s while %s", strerror(errno), s);
    write_log(s1);
    log_errors++;
}


void write_warning_log(char *s)
{
/* Writes a warning to the log file. 
 * 
 * Format:   Date:  WARNING: s
*/ 
    char s1[200];
    
    sprintf(s1, "WARNING:  %s", s);
    log_warnings++;
    write_log(s1);
}


void write_fatal_log(char *s)
{
/* Writes a fatal error to the log file. 
 * 
 * Format:   Date:  FATAL ERROR: Error while s
*/ 
    char s1[5000];
    
    sprintf(s1, "FATAL ERROR:  %s while %s", strerror(errno), s);
    log_errors++;
    write_log(s1);
}


   

void taper_tmpnam(char *s)
{
/* Makes a temporary filename for use by taper.
 * Simply calls tempnam and appends taper to it
 */
    char *x=tempnam(temp_dir, "taper");
    
    strcpy(s, x); 
    free(x);
}


void change_dollar(char *s)
{
/* Reads a string an replaces $ with \$
 * To avoid shell expansions
 */
    
    char  *s1=s, *s2;
    
    while (*s1) {
	if (*s1 == '$') {
	    for (s2=&s[strlen(s)]; s2>=s1; s2--)
	      *(s2+1) = *s2;
	    *s1 = '\\';
	    s1++;
	}
	s1++;
    }
}


_errstat make_path(char *s)
/* Makes a directory path.
 * For example, if s = t/usd/y, it will make directories
 * t, then usd then y. Permission are rwr-xr-x and the owner
 * is the owner of the person running the program.
 * 
 * Returns 0 if error occurred, otherwise 1
*/ 
{
    char *s1;
  int rc;
    
  if (mkdir(s, 0755) == 0 || errno == EEXIST)	/* try to create directory */
    return 1;					/* ok */
    
  if ((s1 = strrchr(s, '/')) != NULL) {		/* filename only */
    *s1 = '\0';					/* remove last name */
    rc = make_path(s);			  	/* try to create */
    *s1 = '/';					/* parent directory */
    if (rc && mkdir(s, 0755) == 0)		/* try again to create
						   directory */
      return 1;					/* ok */
  }

  {
      char et[MAX_FNAME + 20];
      sprintf(et, "Creating directory %s", s);	/* error */
      write_error_log(et);
  }
  
  return 0;
}
	

_errstat make_dirs(char *s)
/* Makes directories for a filename.
 * For example, if s = t/usd/y/f, it will make directories
 * t, then usd then y. Permission are rwr-xr-x and the owner
 * is the owner of the person running the program.
 * 
 * Returns 0 if error occurred, otherwise 1
*/ 
{
  char *s1;
  int rc;

  if((s1 = strrchr(s, '/')) == NULL)		/* filename only */
    return 1;
  if (s1[1] == '\0')				/* directory - should be */
    return 1;					/* created by caller */
    
  *s1 = '\0';					/* remove filename */
  rc = make_path(s);				/* create path */
  *s1 = '/';

  return rc;
}
    

_errstat my_rename(char *oldf, char *newf)
{
/* Renames filename 'old' to 'new'. New is overwritten
 * 
 * If it is a cross-device link, the file is copied.
 * 
 * The tr_buffer is used and assumed to have been initialized
 * and of size max_tr_size
 * 
 * Returns 0 if OK, -1 otherwise
*/
    int oldfd, newfd, e;
    struct stat b;
    
    if (rename(oldf, newf) == 0)
      return 0;
    if (errno != EXDEV)				 /* couldn't rename*/
      return do_exit(ERROR_RENAMING);
    /* cross device - must copy file */
    
    oldfd = open(oldf, O_RDONLY);
    if (oldfd == -1) return do_exit(ERROR_RENAMING);
    fstat(oldfd, &b);
    newfd = creat(newf, b.st_mode);
    if (newfd == -1) {
	close(oldfd);
	return do_exit(ERROR_RENAMING);
    }
    e = my_filecopy(oldfd, newfd);
    close(oldfd);
    close(newfd);
    (e == -1) ?  unlink(newf) : unlink(oldf);	 /* if error, remove new file, else old file */
    if (e == -1) return do_exit(ERROR_RENAMING);
    return 0;
}


_errstat my_filecopy(int oldfd, int newfd)
{
/* Copies the contents of file oldf to newf.
 * 
 * Assumes both files are opened and positioned correctly.
 * Uses tr_buffer and assumes that it is initialised and is
 * of size max_tr_size
 * 
 * Returns 0 if OK, -1 otherwise
*/
    _s32 x;

    while (1) {
	x = read(oldfd, tr_buffer, max_tr_size);
	if (!x) break;
	if (x == -1)
	  return -1;
	write(newfd, tr_buffer, x);
    }				 
    return 0;
}


_errstat setowners(char *s, _errstat ret, struct file_info *fi)
{
    char l[500];
    _s32  sz;
    struct utimbuf ut;

    sprintf(l, "Setting permissions, modes & times for %s", s);
    if (log_level > 2) write_log(l);
    sz = 0;
    if (set_owner)
	sz += chown(s, fi->uid, fi->gid);
    if (!S_ISLNK(fi->mode)) {
      	sz += chmod(s, fi->mode);		 /* directories/links */
	ut.actime = fi->atime;			 /* fix up times */
	ut.modtime = fi->mtime;
	sz += utime(s, &ut);
    }
    if (sz) write_error_log(l);
    if (ret == -4) return ret;
    return (ret == -3) ? -2 : 1;		 /* we passed file */
}


char *trunc_filename(char *org, char *s, int c)
{     
    /* Converts a filename in the form 'the~name' if the filename
       is greater than c characters. 

       Prints 6 leading characters and then the tail
    */
    
    
    strcpy(s, org);
    if (strlen(org) > c) {
	strcpy(s+6, "~");
	strcpy(s+7, s+strlen(org)-(c-7));
    }
    return s;
}


char *pr_filename(char *org, char *s, int c)
{
    /* Prints a filename in the form 'the~name' if the filename
       is greater than c characters. Pads out to the required
       filename length

       Prints 6 leading characters and then the tail
    */
    

    sprintf(s, "%-*s", c, trunc_filename(org, s, c));
    return s;
}