2005-12-28 18:18:37 +03:00
|
|
|
/*
|
|
|
|
** Copyright 1998-2003 University of Illinois Board of Trustees
|
|
|
|
** Copyright 1998-2003 Mark D. Roth
|
|
|
|
** All rights reserved.
|
|
|
|
**
|
|
|
|
** append.c - libtar code to append files to a tar archive
|
|
|
|
**
|
|
|
|
** Mark D. Roth <roth@uiuc.edu>
|
|
|
|
** Campus Information Technologies and Educational Services
|
|
|
|
** University of Illinois at Urbana-Champaign
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libtarint/internal.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2005-12-28 22:58:07 +03:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
2005-12-28 18:18:37 +03:00
|
|
|
# include <libtar/compat.h>
|
|
|
|
#else
|
2009-06-10 19:49:02 +04:00
|
|
|
# ifdef HAVE_SYS_PARAM_H
|
|
|
|
# include <sys/param.h>
|
|
|
|
# endif
|
2005-12-28 18:18:37 +03:00
|
|
|
#endif
|
|
|
|
#include <libtar/compat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#ifdef STDC_HEADERS
|
|
|
|
# include <stdlib.h>
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
2006-05-23 20:38:22 +04:00
|
|
|
|
|
|
|
#ifdef HAVE_IO_H
|
|
|
|
# include <io.h>
|
2005-12-28 18:18:37 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
struct tar_dev
|
|
|
|
{
|
|
|
|
dev_t td_dev;
|
|
|
|
libtar_hash_t *td_h;
|
|
|
|
};
|
|
|
|
typedef struct tar_dev tar_dev_t;
|
|
|
|
|
|
|
|
struct tar_ino
|
|
|
|
{
|
|
|
|
ino_t ti_ino;
|
2005-12-28 22:58:07 +03:00
|
|
|
char ti_name[TAR_MAXPATHLEN];
|
2005-12-28 18:18:37 +03:00
|
|
|
};
|
|
|
|
typedef struct tar_ino tar_ino_t;
|
|
|
|
|
|
|
|
|
|
|
|
/* free memory associated with a tar_dev_t */
|
|
|
|
void
|
|
|
|
tar_dev_free(tar_dev_t *tdp)
|
|
|
|
{
|
|
|
|
libtar_hash_free(tdp->td_h, free);
|
|
|
|
free(tdp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* appends a file to the tar archive */
|
|
|
|
int
|
|
|
|
tar_append_file(TAR *t, char *realname, char *savename)
|
|
|
|
{
|
|
|
|
struct stat s;
|
|
|
|
libtar_hashptr_t hp;
|
2006-01-03 16:39:04 +03:00
|
|
|
tar_dev_t *td;
|
|
|
|
tar_ino_t *ti;
|
2005-12-28 23:03:14 +03:00
|
|
|
#if !defined(_WIN32) || defined(__CYGWIN__)
|
|
|
|
int i;
|
2005-12-30 18:35:23 +03:00
|
|
|
#else
|
|
|
|
size_t plen;
|
2005-12-28 23:03:14 +03:00
|
|
|
#endif
|
2005-12-30 18:35:23 +03:00
|
|
|
char path[TAR_MAXPATHLEN];
|
2005-12-28 18:18:37 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("==> tar_append_file(TAR=0x%lx (\"%s\"), realname=\"%s\", "
|
|
|
|
"savename=\"%s\")\n", t, t->pathname, realname,
|
|
|
|
(savename ? savename : "[NULL]"));
|
|
|
|
#endif
|
|
|
|
|
2005-12-29 20:18:34 +03:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
2005-12-30 18:35:23 +03:00
|
|
|
strncpy(path, realname, sizeof(path)-1);
|
|
|
|
path[sizeof(path)-1] = 0;
|
|
|
|
plen = strlen(path);
|
|
|
|
if (path[plen-1] == '/' )
|
|
|
|
{
|
|
|
|
path[plen-1] = 0;
|
|
|
|
}
|
|
|
|
if (stat(path, &s) != 0)
|
2005-12-28 18:18:37 +03:00
|
|
|
#else
|
|
|
|
if (lstat(realname, &s) != 0)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
perror("lstat()");
|
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set header block */
|
|
|
|
#ifdef DEBUG
|
|
|
|
puts(" tar_append_file(): setting header block...");
|
|
|
|
#endif
|
|
|
|
memset(&(t->th_buf), 0, sizeof(struct tar_header));
|
|
|
|
th_set_from_stat(t, &s);
|
|
|
|
|
|
|
|
/* set the header path */
|
|
|
|
#ifdef DEBUG
|
|
|
|
puts(" tar_append_file(): setting header path...");
|
|
|
|
#endif
|
|
|
|
th_set_path(t, (savename ? savename : realname));
|
|
|
|
|
|
|
|
/* check if it's a hardlink */
|
|
|
|
#ifdef DEBUG
|
|
|
|
puts(" tar_append_file(): checking inode cache for hardlink...");
|
|
|
|
#endif
|
|
|
|
libtar_hashptr_reset(&hp);
|
|
|
|
if (libtar_hash_getkey(t->h, &hp, &(s.st_dev),
|
|
|
|
(libtar_matchfunc_t)dev_match) != 0)
|
|
|
|
td = (tar_dev_t *)libtar_hashptr_data(&hp);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("+++ adding hash for device (0x%lx, 0x%lx)...\n",
|
|
|
|
major(s.st_dev), minor(s.st_dev));
|
|
|
|
#endif
|
|
|
|
td = (tar_dev_t *)calloc(1, sizeof(tar_dev_t));
|
|
|
|
td->td_dev = s.st_dev;
|
|
|
|
td->td_h = libtar_hash_new(256, (libtar_hashfunc_t)ino_hash);
|
|
|
|
if (td->td_h == NULL)
|
|
|
|
return -1;
|
|
|
|
if (libtar_hash_add(t->h, td) == -1)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
libtar_hashptr_reset(&hp);
|
2005-12-30 18:35:23 +03:00
|
|
|
#if !defined(_WIN32) || defined(__CYGWIN__)
|
2005-12-28 18:18:37 +03:00
|
|
|
if (libtar_hash_getkey(td->td_h, &hp, &(s.st_ino),
|
|
|
|
(libtar_matchfunc_t)ino_match) != 0)
|
|
|
|
{
|
|
|
|
ti = (tar_ino_t *)libtar_hashptr_data(&hp);
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf(" tar_append_file(): encoding hard link \"%s\" "
|
|
|
|
"to \"%s\"...\n", realname, ti->ti_name);
|
|
|
|
#endif
|
|
|
|
t->th_buf.typeflag = LNKTYPE;
|
|
|
|
th_set_link(t, ti->ti_name);
|
|
|
|
}
|
|
|
|
else
|
2005-12-30 18:35:23 +03:00
|
|
|
#endif
|
2005-12-28 18:18:37 +03:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("+++ adding entry: device (0x%lx,0x%lx), inode %ld "
|
|
|
|
"(\"%s\")...\n", major(s.st_dev), minor(s.st_dev),
|
|
|
|
s.st_ino, realname);
|
|
|
|
#endif
|
|
|
|
ti = (tar_ino_t *)calloc(1, sizeof(tar_ino_t));
|
|
|
|
if (ti == NULL)
|
|
|
|
return -1;
|
|
|
|
ti->ti_ino = s.st_ino;
|
|
|
|
snprintf(ti->ti_name, sizeof(ti->ti_name), "%s",
|
|
|
|
savename ? savename : realname);
|
|
|
|
libtar_hash_add(td->td_h, ti);
|
|
|
|
}
|
|
|
|
|
2005-12-28 23:03:14 +03:00
|
|
|
#if !defined(_WIN32) || defined(__CYGWIN__)
|
2005-12-28 18:18:37 +03:00
|
|
|
/* check if it's a symlink */
|
|
|
|
if (TH_ISSYM(t))
|
|
|
|
{
|
2005-12-29 20:18:34 +03:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
2005-12-28 18:18:37 +03:00
|
|
|
i = -1;
|
|
|
|
#else
|
|
|
|
i = readlink(realname, path, sizeof(path));
|
|
|
|
#endif
|
|
|
|
if (i == -1)
|
|
|
|
return -1;
|
2005-12-28 22:58:07 +03:00
|
|
|
if (i >= TAR_MAXPATHLEN)
|
|
|
|
i = TAR_MAXPATHLEN - 1;
|
2005-12-28 18:18:37 +03:00
|
|
|
path[i] = '\0';
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf(" tar_append_file(): encoding symlink \"%s\" -> "
|
|
|
|
"\"%s\"...\n", realname, path);
|
|
|
|
#endif
|
|
|
|
th_set_link(t, path);
|
|
|
|
}
|
|
|
|
#endif
|
2005-12-28 23:03:14 +03:00
|
|
|
|
2005-12-28 18:18:37 +03:00
|
|
|
/* print file info */
|
|
|
|
if (t->options & TAR_VERBOSE)
|
|
|
|
th_print_long_ls(t);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
puts(" tar_append_file(): writing header");
|
|
|
|
#endif
|
|
|
|
/* write header */
|
|
|
|
if (th_write(t) != 0)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("t->fd = %d\n", t->fd);
|
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
puts(" tar_append_file(): back from th_write()");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* if it's a regular file, write the contents as well */
|
|
|
|
if (TH_ISREG(t) && tar_append_regfile(t, realname) != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* write EOF indicator */
|
|
|
|
int
|
|
|
|
tar_append_eof(TAR *t)
|
|
|
|
{
|
2006-03-30 22:49:56 +04:00
|
|
|
ssize_t i, j;
|
2005-12-28 18:18:37 +03:00
|
|
|
char block[T_BLOCKSIZE];
|
|
|
|
|
|
|
|
memset(&block, 0, T_BLOCKSIZE);
|
|
|
|
for (j = 0; j < 2; j++)
|
|
|
|
{
|
|
|
|
i = tar_block_write(t, &block);
|
|
|
|
if (i != T_BLOCKSIZE)
|
|
|
|
{
|
|
|
|
if (i != -1)
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* add file contents to a tarchive */
|
|
|
|
int
|
|
|
|
tar_append_regfile(TAR *t, char *realname)
|
|
|
|
{
|
|
|
|
char block[T_BLOCKSIZE];
|
|
|
|
int filefd;
|
2006-03-30 22:49:56 +04:00
|
|
|
ssize_t i, j;
|
2005-12-28 18:18:37 +03:00
|
|
|
size_t size;
|
|
|
|
|
2005-12-29 19:42:31 +03:00
|
|
|
#if defined( _WIN32 ) || defined(__CYGWIN__)
|
2005-12-28 21:33:49 +03:00
|
|
|
filefd = open(realname, O_RDONLY | O_BINARY);
|
2005-12-28 21:35:54 +03:00
|
|
|
#else
|
|
|
|
filefd = open(realname, O_RDONLY);
|
|
|
|
#endif
|
2005-12-28 18:18:37 +03:00
|
|
|
if (filefd == -1)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
perror("open()");
|
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = th_get_size(t);
|
|
|
|
for (i = size; i > T_BLOCKSIZE; i -= T_BLOCKSIZE)
|
|
|
|
{
|
|
|
|
j = read(filefd, &block, T_BLOCKSIZE);
|
|
|
|
if (j != T_BLOCKSIZE)
|
|
|
|
{
|
|
|
|
if (j != -1)
|
2005-12-29 19:42:31 +03:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Unexpected size of read data: %d <> %d for file: %s\n",
|
2006-03-31 08:03:56 +04:00
|
|
|
(int)j, T_BLOCKSIZE, realname);
|
2005-12-28 18:18:37 +03:00
|
|
|
errno = EINVAL;
|
2005-12-29 19:42:31 +03:00
|
|
|
}
|
2005-12-28 18:18:37 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (tar_block_write(t, &block) == -1)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i > 0)
|
|
|
|
{
|
2006-03-30 22:49:56 +04:00
|
|
|
j = (size_t)read(filefd, &block, (unsigned int)i);
|
2005-12-28 18:18:37 +03:00
|
|
|
if (j == -1)
|
|
|
|
return -1;
|
|
|
|
memset(&(block[i]), 0, T_BLOCKSIZE - i);
|
|
|
|
if (tar_block_write(t, &block) == -1)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(filefd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|