File: tar.c

package info (click to toggle)
epm 3.0-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,020 kB
  • ctags: 371
  • sloc: ansic: 6,199; cpp: 1,188; makefile: 201; perl: 134
file content (425 lines) | stat: -rw-r--r-- 9,298 bytes parent folder | download
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
 * "$Id: tar.c,v 1.15 2001/06/26 16:22:22 mike Exp $"
 *
 *   TAR file functions for the ESP Package Manager (EPM).
 *
 *   Copyright 1999-2001 by Easy Software Products.
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2, or (at your option)
 *   any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 * Contents:
 *
 *   tar_close()     - Close a tar file, padding as needed.
 *   tar_directory() - Archive a directory.
 *   tar_file()      - Write the contents of a file...
 *   tar_header()    - Write a TAR header for the specified file...
 *   tar_open()      - Open a TAR file for writing.
 */

/*
 * Include necessary headers...
 */

#include "epm.h"


/*
 * 'tar_close()' - Close a tar file, padding as needed.
 */

int			/* O - -1 on error, 0 on success */
tar_close(tarf_t *fp)	/* I - File to write to */
{
  int	blocks;		/* Number of blocks to write */
  int	status;		/* Return status */
  char	padding[TAR_BLOCKS * TAR_BLOCK];
			/* Padding for tar blocks */


 /*
  * Zero the padding record...
  */

  memset(padding, 0, sizeof(padding));

 /*
  * Write a single 0 block to signal the end of the archive...
  */

  if (fwrite(padding, TAR_BLOCK, 1, fp->file) < 1)
    return (-1);

  fp->blocks ++;

 /*
  * Pad the tar files to TAR_BLOCKS blocks...  This is bullshit of course,
  * but old versions of tar need it...
  */

  status = 0;

  if ((blocks = fp->blocks % TAR_BLOCKS) > 0)
  {
    blocks = TAR_BLOCKS - blocks;

    if (fwrite(padding, TAR_BLOCK, blocks, fp->file) < blocks)
      status = -1;
  }
  else
  {
   /*
    * Sun tar needs at least 2 0 blocks...
    */

    if (fwrite(padding, TAR_BLOCK, blocks, fp->file) < blocks)
      status = -1;
  }

 /*
  * Close the file and free memory...
  */

  if (fp->compressed)
  {
    if (pclose(fp->file))
      status = -1;
  }
  else if (fclose(fp->file))
    status = -1;

  free(fp);

  return (status);
}


/*
 * 'tar_directory()' - Archive a directory.
 */

int					/* O - 0 on success, -1 on error */
tar_directory(tarf_t     *tar,		/* I - Tar file to write to */
              const char *srcpath,	/* I - Source directory */
              const char *dstpath)	/* I - Destination directory */
{
  DIR		*dir;			/* Directory */
  DIRENT	*dent;			/* Directory entry */
  char		src[1024],		/* Source file */
		dst[1024],		/* Destination file */
		srclink[1024];		/* Symlink value */
  struct stat	srcinfo;		/* Information on the source file */


 /*
  * Range check input...
  */

  if (tar == NULL || srcpath == NULL || dstpath == NULL)
    return (-1);

 /*
  * Try opening the source directory...
  */

  if ((dir = opendir(srcpath)) == NULL)
  {
    fprintf(stderr, "epm: Unable to open directory \"%s\" - %s.\n", srcpath,
            strerror(errno));

    return (-1);
  }

 /*
  * Read from the directory...
  */

  while ((dent = readdir(dir)) != NULL)
  {
   /*
    * Skip "." and ".."...
    */

    if (strcmp(dent->d_name, ".") == 0 ||
        strcmp(dent->d_name, "..") == 0)
      continue;

   /*
    * Get source file info...
    */

    snprintf(src, sizeof(src), "%s/%s", srcpath, dent->d_name);
    if (dstpath[0])
      snprintf(dst, sizeof(dst), "%s/%s", dstpath, dent->d_name);
    else
    {
      strncpy(dst, dent->d_name, sizeof(dst) - 1);
      dst[sizeof(dst) - 1] = '\0';
    }

    if (stat(src, &srcinfo))
    {
      fprintf(stderr, "epm: Unable to stat \"%s\" - %s.\n", src,
              strerror(errno));
      continue;
    }

   /*
    * Process accordingly...
    */

    if (Verbosity)
      puts(dst);

    if (S_ISDIR(srcinfo.st_mode))
    {
     /*
      * Directory...
      */

      if (tar_header(tar, TAR_DIR, srcinfo.st_mode, 0,
                     srcinfo.st_mtime, "root", "sys", dst, NULL))
        return (-1);

      if (tar_directory(tar, src, dst))
        return (-1);
    }
    else if (S_ISLNK(srcinfo.st_mode))
    {
     /*
      * Symlink...
      */

      if (readlink(src, srclink, sizeof(srclink)) < 0)
        return (-1);

      if (tar_header(tar, TAR_SYMLINK, srcinfo.st_mode, 0,
                     srcinfo.st_mtime, "root", "sys", dst, srclink))
        return (-1);
    }
    else
    {
     /*
      * Regular file...
      */

      if (tar_header(tar, TAR_NORMAL, srcinfo.st_mode, srcinfo.st_size,
                     srcinfo.st_mtime, "root", "sys", dst, NULL))
        return (-1);

      if (tar_file(tar, src))
        return (-1);
    }
  }

  closedir(dir);
  return (0);
}


/*
 * 'tar_file()' - Write the contents of a file...
 */

int				/* O - 0 on success, -1 on error */
tar_file(tarf_t     *fp,	/* I - Tar file to write to */
         const char *filename)	/* I - File to write */
{
  FILE	*file;			/* File to write */
  int	nbytes,			/* Number of bytes read */
	tbytes,			/* Total bytes read/written */
	fill;			/* Number of fill bytes needed */
  char	buffer[8192];		/* Copy buffer */


 /*
  * Try opening the file...
  */

  if ((file = fopen(filename, "rb")) == NULL)
    return (-1);

 /*
  * Copy the file to the tar file...
  */

  tbytes = 0;

  while ((nbytes = fread(buffer, 1, sizeof(buffer), file)) > 0)
  {
   /*
    * Zero fill the file to a 512 byte record as needed.
    */

    if (nbytes < sizeof(buffer))
    {
      fill = TAR_BLOCK - (nbytes & (TAR_BLOCK - 1));

      if (fill < TAR_BLOCK)
      {
        memset(buffer + nbytes, 0, fill);
        nbytes += fill;
      }
    }

   /*
    * Write the buffer to the file...
    */

    if (fwrite(buffer, 1, nbytes, fp->file) < nbytes)
    {
      fclose(file);
      return (-1);
    }

    tbytes     += nbytes;
    fp->blocks += nbytes / TAR_BLOCK;
  }

 /*
  * Close the file and return...
  */

  fclose(file);
  return (0);
}


/*
 * 'tar_header()' - Write a TAR header for the specified file...
 */

int				/* O - 0 on success, -1 on error */
tar_header(tarf_t     *fp,	/* I - Tar file to write to */
           char       type,	/* I - File type */
	   int        mode,	/* I - File permissions */
	   int        size,	/* I - File size */
           time_t     mtime,	/* I - File modification time */
	   const char *user,	/* I - File owner */
	   const char *group,	/* I - File group */
	   const char *pathname,/* I - File name */
	   const char *linkname)/* I - File link name (for links only) */
{
  tar_t		record;		/* TAR header record */
  int		i,		/* Looping var... */
		sum;		/* Checksum */
  unsigned char	*sumptr;	/* Pointer into header record */
  struct passwd	*pwd;		/* Pointer to user record */
  struct group	*grp;		/* Pointer to group record */


 /*
  * Find the username and groupname IDs...
  */

  pwd = getpwnam(user);
  grp = getgrnam(group);

  endpwent();
  endgrent();

 /*
  * Format the header...
  */

  memset(&record, 0, sizeof(record));

  strncpy(record.header.pathname, pathname, sizeof(record.header.pathname) - 1);
  if (type == TAR_DIR)
    strncat(record.header.pathname, "/", sizeof(record.header.pathname) - 1);
  sprintf(record.header.mode, "%-6o ", (unsigned)mode);
  sprintf(record.header.uid, "%o ", pwd == NULL ? 0 : (unsigned)pwd->pw_uid);
  sprintf(record.header.gid, "%o ", grp == NULL ? 0 : (unsigned)grp->gr_gid);
  sprintf(record.header.size, "%011o", (unsigned)size);
  sprintf(record.header.mtime, "%011o", (unsigned)mtime);
  memset(&(record.header.chksum), ' ', sizeof(record.header.chksum));
  record.header.linkflag = type;
  if (type == TAR_SYMLINK)
    strncpy(record.header.linkname, linkname,
            sizeof(record.header.linkname) - 1);
  strcpy(record.header.magic, TAR_MAGIC);
  strcpy(record.header.uname, user);
  strcpy(record.header.gname, group);

 /*
  * Compute the checksum of the header...
  */

  for (i = sizeof(record), sumptr = record.all, sum = 0; i > 0; i --)
    sum += *sumptr++;

 /*
  * Put the correct checksum in place and write the header...
  */

  sprintf(record.header.chksum, "%6o", sum);

  if (fwrite(&record, 1, sizeof(record), fp->file) < sizeof(record))
    return (-1);

  fp->blocks ++;
  return (0);
}


/*
 * 'tar_open()' - Open a TAR file for writing.
 */

tarf_t *			/* O - New tar file */
tar_open(const char *filename,	/* I - File to create */
         int        compress)	/* I - Compress with gzip? */
{
  tarf_t	*fp;		/* New tar file */
  char		command[1024];	/* Compression command */


 /*
  * Allocate memory for the tar file state...
  */

  if ((fp = calloc(sizeof(tarf_t), 1)) == NULL)
    return (NULL);

 /*
  * Open the output file or pipe into gzip for compressed output...
  */

  if (compress)
  {
    snprintf(command, sizeof(command), EPM_GZIP " > %s", filename);
    fp->file = popen(command, "w");
  }
  else
    fp->file = fopen(filename, "wb");

 /*
  * If we couldn't open the output file, abort...
  */

  if (fp->file == NULL)
  {
    free(fp);
    return (NULL);
  }

 /*
  * Save the compression state and return...
  */

  fp->compressed = compress;

  return (fp);
}


/*
 * End of "$Id: tar.c,v 1.15 2001/06/26 16:22:22 mike Exp $".
 */