File: open.c

package info (click to toggle)
zlibc 0.9k-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 624 kB
  • ctags: 461
  • sloc: ansic: 3,073; sh: 3,023; csh: 128; makefile: 58; sed: 4
file content (401 lines) | stat: -rw-r--r-- 8,332 bytes parent folder | download | duplicates (5)
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
/*
 * open.c
 *
 * Copyright (C) 1993 Alain Knaff
 */

#define _LARGEFILE64_SOURCE
#define _GNU_SOURCE

#include "sysincludes.h"

static char *zlib_compressor[]= { COMPR, 0 };


#ifdef HAVE___LIBC_OPEN
int __libc_open(__const char *pathname, int flags, mode_t mode);
#define open __libc_open
/*ALIASF2(open, __libc_open, int, mode_t);*/
#endif


#include <stdarg.h>

int open(__const char *name, int flags, mode_t mode);

int open64 (__const char *name, int flags, ...)
{
  va_list ap;
  va_start(ap, flags);
  if(flags & O_CREAT) {
    mode_t mode = va_arg(ap, mode_t);
    return open(name, flags | O_LARGEFILE, mode);
  } else {
    return open(name, flags | O_LARGEFILE, 0);
  }
}

int open(__const char *name, int flags, mode_t mode)
{
  int fd;
  int zfd;
  int pid;
  int filetype;
  int zflags;
  int do_append;
  int do_uncompress;
  int do_create;
  int have_filetype;
  int tmp;
  int stat_result;
  int status;
  struct stat buf;
#ifdef SYS_UTIME
  struct utimbuf buf2;
#else
# ifdef SYS_UTIMES
  struct timeval buf2[2];
# endif
#endif
  int pipeout[2];

  char newname[MAXPATHLEN + MAXEXTLEN + 1];
  char _pname[MAXPATHLEN + MAXEXTLEN + 1];
  __const char *pname;
  _zlibc_init();
  filetype=have_filetype=do_append=do_uncompress=do_create=0;

  zflags = flags;
  if (flags & O_CREAT){
    zlib_initialise();
    have_filetype=1;
    if ( ! (zlib_mode & CM_DISAB )){
      filetype = zlib_getfiletype(name,-1);

      if ((flags & O_ACCMODE) == O_WRONLY &&
	  (flags & (O_TRUNC | O_EXCL)) &&
	  (filetype & PM_CREATE_COMPR))
	do_create = 1;
      else if ((flags & O_ACCMODE) == O_WRONLY &&
	       (flags & O_APPEND) &&
	       (filetype & PM_APPEND_COMPR))
	do_append=1;
      else if ((flags & O_ACCMODE ) != O_RDONLY &&
	       (filetype & PM_UNCOMPR_BEFORE_WRITE))
	do_uncompress=1;
      if (do_create | do_append | do_uncompress)
	zflags &= ~O_CREAT;
    }
  }

  fd = zlib_real_open(name,zflags,mode);

#ifdef DEBUG
  fprintf(stderr,"file opened *: %s %d %x %x %d\n", name,fd, flags, zflags,
	  errno);
#endif

  /* success to open uncompressed file */
  if ( fd >= 0 || errno != ENOENT )
    return fd;

  if (!have_filetype)
    zlib_initialise();

  if ( zlib_mode & CM_DISAB )
    return fd;

  if (!have_filetype)
    filetype = zlib_getfiletype(name,-1);

  if ( zlib_mode & CM_VERBOSE )
    fprintf(stderr,"opening %s %o\n", name,flags);

  strncpy(newname,name,MAXPATHLEN);
  strcat(newname,zlib_ext);

  /* open compressed file */

  zflags = flags;
  if ((flags & O_ACCMODE) == O_WRONLY &&
      (flags & O_TRUNC) &&
      (filetype & PM_CREATE_COMPR))
    do_create = 1;
  else if ((flags & O_ACCMODE) == O_WRONLY &&
	   (flags & O_APPEND) &&
	   (filetype & PM_APPEND_COMPR))
    do_append=1;
  else if ((flags & O_ACCMODE ) != O_RDONLY &&
	   (filetype & PM_UNCOMPR_BEFORE_WRITE)){
    zflags = O_RDONLY;
    do_uncompress=1;
  } else if ((flags & O_ACCMODE) != O_RDONLY)
    return fd;

  zfd = zlib_real_open(newname,zflags,mode);

  if ( zfd < 0 ){
    if ( flags & O_CREAT )
      return zlib_real_open(name,flags,mode);
    if ( errno == EINVAL ) /* don't want to replace perm errors */
      errno = ENOENT;
    return zfd;
  }

  if (do_append || do_create){
    /* make a pipe */
    if(pipe(pipeout) < 0 )
      return -1;
    
    /* double fork */
    switch(pid=fork()){
    case 0: /* son */
      switch(fork()){
      case 0: /* son of son */
	if (zfd == 0){
	  /* shit happens ... */
	  tmp = dup(zfd);
	  close(zfd);
	  zfd = tmp;
	}
	if (pipeout[0] != 0){
	  close(0);
	  dup(pipeout[0]);
	}
	if ( zfd != 1 ){
	  close(1);
	  dup(zfd); /* compressed file, input */
	}

	close(pipeout[0]);
	close(pipeout[1]);

	if(! (zlib_mode & CM_VERBOSE))
	  close(2);

	execvp (zlib_compressor[0], zlib_compressor);
	
	if ( zlib_mode & CM_VERBOSE ){
	  perror("exec compressor");
	  exit(1);
	}
      case -1: /* error */
	if ( zlib_mode & CM_VERBOSE )
	  perror("fork error");
	exit(1);
      default: /* father */
	exit(0); /* son of son will be taken over by init */
      }
    case -1: /* error */
      errno = ENOENT;
      return -1;
    }
    close(pipeout[0]); /* close read end of pipe */

    close(zfd);        /* close raw compressed file */
    fd=dup(pipeout[1]);
    close(pipeout[1]);
    /*wait4(pid,&status,0,0);  wait for son */
    wait(&status);
    return fd;    
  }

  if (!do_uncompress &&  (filetype & PM_READ_MASK) >= PM_HIDE_PIPE ){
    /* make a pipe */
    if(pipe(pipeout) < 0 )
      return -1;
    
    /* double fork */
    switch(pid=fork()){
    case 0: /* son */
      switch(fork()){
      case 0: /* son of son */
	if ( zfd != 0 ){
	  close(0);
	  dup(zfd); /* compressed file, input */
	}
	
	if ( pipeout[1] != 1 ){
	  close(1);
	  dup(pipeout[1]);
	}

	close(pipeout[0]);
	close(pipeout[1]);

	if(! (zlib_mode & CM_VERBOSE))
	  close(2);

	execvp ( zlib_uncompressor[0], zlib_uncompressor );
	
	if ( zlib_mode & CM_VERBOSE ){
	  perror("exec uncompressor");
	  exit(1);
	}
      case -1: /* error */
	if ( zlib_mode & CM_VERBOSE )
	  perror("fork error");
	exit(1);
      default: /* father */
	exit(0); /* son of son will be taken over by init */
      }
    case -1: /* error */
      errno = ENOENT;
      return -1;
    }
    close(pipeout[1]); /* close write end of pipe */


    close(zfd);        /* close raw compressed file */
    fd=dup(pipeout[0]);
    close(pipeout[0]);
    wait(&status);
  } else {
    stat_result=fstat(zfd, &buf);

    mode = 0400;
    if ( !do_uncompress){
      sprintf(_pname,"%s/pipe.%d",zlib_tmp,(int)getpid());
      pname = _pname;
    } else {
      mode = 0;
      pname = name;
    }
    zlib_real_unlink(pname);
    pipeout[0]=zlib_real_open(pname, O_RDWR | O_CREAT | O_EXCL, mode);
    /* temporary output file */
    if ( pipeout[0] < 0 ){
      if ( zlib_mode & CM_VERBOSE )
	perror("could not create uncompressed file");
      errno = ENOENT;
      return -1;
    }

    switch(pid=fork()){
    case 0: /* son  */
      if ( zfd != 0 ){
	close(0);
	dup(zfd); /* compressed file, input */
      }

      if ( pipeout[0] != 1 ){
	close(1);
	dup(pipeout[0]);
      }

      if(! (zlib_mode & CM_VERBOSE))
	close(2);

      execvp ( zlib_uncompressor[0], zlib_uncompressor ) ;
      if ( zlib_mode & CM_VERBOSE )
	perror("exec uncompressor");
      exit(1);
    case -1: /* error */
      errno = ENOENT;
      return -1;
    }

    wait(&status);
       
    /* touch it */
    close(pipeout[0]);

    if(do_uncompress){
      /* restore the original mode */
      if ( stat_result >= 0)
	mode = buf.st_mode;
      else
	mode = 0600;
      zlib_real_chmod(pname, mode);
    }

    if ( stat_result >= 0 ){
#ifdef SYS_UTIME
      buf2.actime = buf.st_atime;
      buf2.modtime = buf.st_mtime;
      zlib_real_utime(pname, &buf2);
#else
# ifdef SYS_UTIMES
      buf2[0].tv_sec =  buf.st_atime;
      buf2[0].tv_usec = 0;
      buf2[1].tv_sec =  buf.st_mtime;
      buf2[1].tv_usec = 0;
      zlib_real_utimes(pname, buf2);
# endif
#endif
    }
    close(zfd);        /* close raw compressed file */
    
    /* re-open it */
    fd=zlib_real_open(pname, flags, mode);

    if ( !do_uncompress)
      zlib_real_unlink(pname);  
    else {
      if ( fd>=0 && WIFEXITED(status) && WEXITSTATUS(status)==0 )
	/* only erase the original file if nothing looks fishy... */
	zlib_real_unlink(newname);
    }
  }
  return fd;
}

#undef creat
int creat(__const char *name, mode_t mode)
{
  return open(name, O_CREAT | O_WRONLY | O_TRUNC, mode);
}

#ifdef linux

FILE *fopen(const char *path, const char *mode)
{
  FILE *f;
  int fd;

  _zlibc_init();

  f=zlib_real_fopen(path, mode);
  if(f != NULL)
    return f;
  if ( zlib_mode & CM_DISAB )
    /* ZLIBC disabled */
    return NULL;
  
  if(strcmp(mode, "r"))
    /* If mode is anything else than read, don't bother */
    return NULL;

  fd = open(path, O_RDONLY, 0);
  if(fd < 0)
    return NULL;

  return fdopen(fd, mode);
}

FILE *fopen64(const char *path, const char *mode)
{
  FILE *f;
  int fd;

  _zlibc_init();

  f=zlib_real_fopen64(path, mode);
  if(f != NULL)
    return f;
  if ( zlib_mode & CM_DISAB )
    /* ZLIBC disabled */
    return NULL;
  
  if(strcmp(mode, "r"))
    /* If mode is anything else than read, don't bother */
    return NULL;

  fd = open(path, O_RDONLY, 0);
  if(fd < 0)
    return NULL;

  return fdopen(fd, mode);
}

#endif