1998-07-26 05:00:26 +08:00
|
|
|
#include "system.h"
|
1997-05-06 04:46:58 +08:00
|
|
|
|
1997-01-04 10:48:55 +08:00
|
|
|
#include "cpio.h"
|
1997-05-06 04:46:58 +08:00
|
|
|
|
1997-07-24 02:07:46 +08:00
|
|
|
#define CPIO_NEWC_MAGIC "070701"
|
1997-01-04 10:48:55 +08:00
|
|
|
#define CPIO_CRC_MAGIC "070702"
|
|
|
|
#define TRAILER "TRAILER!!!"
|
|
|
|
|
|
|
|
/* FIXME: We don't translate between cpio and system mode bits! These
|
|
|
|
should both be the same, but really odd things are going to happen if
|
|
|
|
that's not true! */
|
|
|
|
|
1997-05-07 01:38:05 +08:00
|
|
|
struct hardLink {
|
1997-07-24 02:07:46 +08:00
|
|
|
struct hardLink * next;
|
1999-09-18 05:31:38 +08:00
|
|
|
const char ** files; /* nlink of these, used by install */
|
1997-07-24 02:07:46 +08:00
|
|
|
int * fileMaps; /* used by build */
|
1997-05-07 01:38:05 +08:00
|
|
|
dev_t dev;
|
|
|
|
ino_t inode;
|
|
|
|
int nlink;
|
|
|
|
int linksLeft;
|
|
|
|
int createdPath;
|
1997-07-24 02:07:46 +08:00
|
|
|
struct stat sb;
|
1997-05-07 01:38:05 +08:00
|
|
|
};
|
|
|
|
|
1997-01-04 10:48:55 +08:00
|
|
|
struct cpioCrcPhysicalHeader {
|
|
|
|
char magic[6];
|
|
|
|
char inode[8];
|
|
|
|
char mode[8];
|
|
|
|
char uid[8];
|
|
|
|
char gid[8];
|
|
|
|
char nlink[8];
|
|
|
|
char mtime[8];
|
|
|
|
char filesize[8];
|
|
|
|
char devMajor[8];
|
|
|
|
char devMinor[8];
|
|
|
|
char rdevMajor[8];
|
|
|
|
char rdevMinor[8];
|
|
|
|
char namesize[8];
|
|
|
|
char checksum[8]; /* ignored !! */
|
|
|
|
};
|
|
|
|
|
1998-12-04 03:30:32 +08:00
|
|
|
#define PHYS_HDR_SIZE 110 /* don't depend on sizeof(struct) */
|
|
|
|
|
1997-01-04 10:48:55 +08:00
|
|
|
struct cpioHeader {
|
|
|
|
ino_t inode;
|
|
|
|
mode_t mode;
|
|
|
|
uid_t uid;
|
|
|
|
gid_t gid;
|
|
|
|
int nlink;
|
|
|
|
time_t mtime;
|
|
|
|
long size;
|
|
|
|
dev_t dev, rdev;
|
1999-09-18 05:31:38 +08:00
|
|
|
/*@owned@*/char * path;
|
1997-01-04 10:48:55 +08:00
|
|
|
};
|
|
|
|
|
1999-09-18 05:31:38 +08:00
|
|
|
static inline off_t saferead(CFD_t *cfd, /*@out@*/void * vbuf, size_t amount)
|
|
|
|
{
|
1998-07-25 23:33:15 +08:00
|
|
|
off_t rc = 0;
|
|
|
|
char * buf = vbuf;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
1998-07-25 23:33:15 +08:00
|
|
|
while (amount > 0) {
|
|
|
|
size_t nb;
|
|
|
|
switch (cfd->cpioIoType) {
|
|
|
|
default:
|
|
|
|
#ifdef PARANOID
|
1998-11-17 05:40:28 +08:00
|
|
|
fprintf(stderr, "\tsaferead(%p,%p,%x)\n", cfd, vbuf, (unsigned)amount);
|
1998-07-25 23:33:15 +08:00
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case cpioIoTypeDebug:
|
|
|
|
nb = amount;
|
1998-11-17 05:40:28 +08:00
|
|
|
fprintf(stderr, "\tsaferead(%p,%p,%x)\n", cfd, vbuf, (unsigned)amount);
|
1998-07-25 23:33:15 +08:00
|
|
|
break;
|
|
|
|
case cpioIoTypeFd:
|
1998-11-19 05:41:05 +08:00
|
|
|
nb = fdRead(cfd->cpioFd, buf, amount);
|
1998-07-25 23:33:15 +08:00
|
|
|
break;
|
|
|
|
case cpioIoTypeFp:
|
|
|
|
nb = fread(buf, amount, 1, cfd->cpioFp);
|
|
|
|
if (nb)
|
|
|
|
nb *= amount;
|
|
|
|
break;
|
|
|
|
case cpioIoTypeGzFd:
|
1998-11-23 03:48:48 +08:00
|
|
|
nb = gzdRead(cfd->cpioGzFd, buf, amount);
|
1998-07-25 23:33:15 +08:00
|
|
|
break;
|
1999-09-20 21:34:47 +08:00
|
|
|
#if ENABLE_BZIP2_PAYLOAD
|
|
|
|
case cpioIoTypeBzFd:
|
|
|
|
nb = bzdRead(cfd->cpioBzFd, buf, amount);
|
|
|
|
break;
|
|
|
|
#endif
|
1998-07-25 23:33:15 +08:00
|
|
|
}
|
|
|
|
if (nb <= 0)
|
|
|
|
return nb;
|
|
|
|
rc += nb;
|
|
|
|
if (rc >= amount)
|
|
|
|
break;
|
|
|
|
buf += nb;
|
|
|
|
amount -= nb;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
1999-09-18 05:31:38 +08:00
|
|
|
static inline off_t ourread(CFD_t * cfd, /*@out@*/void * buf, size_t size)
|
|
|
|
{
|
1998-07-25 23:33:15 +08:00
|
|
|
off_t i = saferead(cfd, buf, size);
|
|
|
|
if (i > 0)
|
|
|
|
cfd->cpioPos += i;
|
1997-01-04 10:48:55 +08:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
1999-09-18 05:31:38 +08:00
|
|
|
static inline void padinfd(CFD_t * cfd, int modulo)
|
|
|
|
{
|
1997-01-04 10:48:55 +08:00
|
|
|
int buf[10];
|
|
|
|
int amount;
|
|
|
|
|
1998-07-25 23:33:15 +08:00
|
|
|
amount = (modulo - cfd->cpioPos % modulo) % modulo;
|
1998-11-17 05:40:28 +08:00
|
|
|
(void)ourread(cfd, buf, amount);
|
1997-01-04 10:48:55 +08:00
|
|
|
}
|
|
|
|
|
1999-09-18 05:31:38 +08:00
|
|
|
static inline off_t safewrite(CFD_t *cfd, const void * vbuf, size_t amount)
|
|
|
|
{
|
1998-07-25 23:33:15 +08:00
|
|
|
off_t rc = 0;
|
1999-09-18 05:31:38 +08:00
|
|
|
const char * buf = vbuf;
|
1997-07-24 02:07:46 +08:00
|
|
|
|
1998-07-25 23:33:15 +08:00
|
|
|
while (amount > 0) {
|
|
|
|
size_t nb;
|
|
|
|
switch (cfd->cpioIoType) {
|
|
|
|
default:
|
|
|
|
#ifdef PARANOID
|
1998-11-17 05:40:28 +08:00
|
|
|
fprintf(stderr, "\tsafewrite(%p,%p,%x)\n", cfd, vbuf, (unsigned)amount);
|
1998-07-25 23:33:15 +08:00
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case cpioIoTypeDebug:
|
|
|
|
nb = amount;
|
1998-11-17 05:40:28 +08:00
|
|
|
fprintf(stderr, "\tsafewrite(%p,%p,%x)\n", cfd, vbuf, (unsigned)amount);
|
1998-07-25 23:33:15 +08:00
|
|
|
break;
|
|
|
|
case cpioIoTypeFd:
|
1998-11-19 05:41:05 +08:00
|
|
|
nb = fdWrite(cfd->cpioFd, buf, amount);
|
1998-07-25 23:33:15 +08:00
|
|
|
break;
|
|
|
|
case cpioIoTypeFp:
|
|
|
|
nb = fwrite(buf, amount, 1, cfd->cpioFp);
|
|
|
|
if (nb)
|
|
|
|
nb *= amount;
|
|
|
|
break;
|
|
|
|
case cpioIoTypeGzFd:
|
1998-11-23 03:48:48 +08:00
|
|
|
nb = gzdWrite(cfd->cpioGzFd, buf, amount);
|
1998-07-25 23:33:15 +08:00
|
|
|
break;
|
1999-09-20 21:34:47 +08:00
|
|
|
#if ENABLE_BZIP2_PAYLOAD
|
|
|
|
case cpioIoTypeBzFd:
|
|
|
|
nb = bzdWrite(cfd->cpioBzFd, buf, amount);
|
|
|
|
break;
|
|
|
|
#endif
|
1998-07-25 23:33:15 +08:00
|
|
|
}
|
|
|
|
if (nb <= 0)
|
|
|
|
return nb;
|
|
|
|
rc += nb;
|
|
|
|
if (rc >= amount)
|
|
|
|
break;
|
|
|
|
buf += nb;
|
|
|
|
amount -= nb;
|
1997-07-24 02:07:46 +08:00
|
|
|
}
|
|
|
|
|
1998-07-25 23:33:15 +08:00
|
|
|
return rc;
|
1997-07-24 02:07:46 +08:00
|
|
|
}
|
|
|
|
|
1999-09-18 05:31:38 +08:00
|
|
|
static inline int padoutfd(CFD_t * cfd, size_t * where, int modulo)
|
|
|
|
{
|
1998-07-25 23:33:15 +08:00
|
|
|
static int buf[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
|
1997-07-24 02:07:46 +08:00
|
|
|
int amount;
|
|
|
|
|
|
|
|
amount = (modulo - *where % modulo) % modulo;
|
|
|
|
*where += amount;
|
|
|
|
|
1998-07-25 23:33:15 +08:00
|
|
|
if (safewrite(cfd, buf, amount) != amount)
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_WRITE_FAILED;
|
1997-07-24 02:07:46 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-09-18 05:31:38 +08:00
|
|
|
static int strntoul(const char *str, /*@out@*/char **endptr, int base, int num)
|
|
|
|
{
|
1998-03-28 01:23:20 +08:00
|
|
|
char * buf, * end;
|
1998-03-28 01:41:19 +08:00
|
|
|
unsigned long ret;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
|
|
|
buf = alloca(num + 1);
|
|
|
|
strncpy(buf, str, num);
|
|
|
|
buf[num] = '\0';
|
|
|
|
|
1998-03-28 01:23:20 +08:00
|
|
|
ret = strtoul(buf, &end, base);
|
|
|
|
if (*end)
|
1998-12-06 07:22:41 +08:00
|
|
|
*endptr = ((char *)str) + (end - buf); /* XXX discards const */
|
1998-03-28 01:23:20 +08:00
|
|
|
else
|
1999-09-19 08:29:44 +08:00
|
|
|
*endptr = ((char *)str) + strlen(str);
|
1998-03-28 01:23:20 +08:00
|
|
|
|
1997-01-04 10:48:55 +08:00
|
|
|
return strtoul(buf, endptr, base);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define GET_NUM_FIELD(phys, log) \
|
|
|
|
log = strntoul(phys, &end, 16, sizeof(phys)); \
|
1998-12-06 07:22:41 +08:00
|
|
|
if (*end) return CPIOERR_BAD_HEADER;
|
1997-07-24 02:07:46 +08:00
|
|
|
#define SET_NUM_FIELD(phys, val, space) \
|
|
|
|
sprintf(space, "%8.8lx", (unsigned long) (val)); \
|
|
|
|
memcpy(phys, space, 8);
|
1997-01-04 10:48:55 +08:00
|
|
|
|
1999-09-18 05:31:38 +08:00
|
|
|
static int getNextHeader(CFD_t * cfd, struct cpioHeader * chPtr)
|
|
|
|
{
|
1997-01-04 10:48:55 +08:00
|
|
|
struct cpioCrcPhysicalHeader physHeader;
|
|
|
|
int nameSize;
|
|
|
|
char * end;
|
|
|
|
int major, minor;
|
|
|
|
|
1998-12-04 03:30:32 +08:00
|
|
|
if (ourread(cfd, &physHeader, PHYS_HDR_SIZE) != PHYS_HDR_SIZE)
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_READ_FAILED;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
1999-09-08 06:49:45 +08:00
|
|
|
if (strncmp(CPIO_CRC_MAGIC, physHeader.magic, sizeof(CPIO_CRC_MAGIC)-1) &&
|
|
|
|
strncmp(CPIO_NEWC_MAGIC, physHeader.magic, sizeof(CPIO_NEWC_MAGIC)-1))
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_BAD_MAGIC;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
|
|
|
GET_NUM_FIELD(physHeader.inode, chPtr->inode);
|
|
|
|
GET_NUM_FIELD(physHeader.mode, chPtr->mode);
|
|
|
|
GET_NUM_FIELD(physHeader.uid, chPtr->uid);
|
|
|
|
GET_NUM_FIELD(physHeader.gid, chPtr->gid);
|
|
|
|
GET_NUM_FIELD(physHeader.nlink, chPtr->nlink);
|
|
|
|
GET_NUM_FIELD(physHeader.mtime, chPtr->mtime);
|
|
|
|
GET_NUM_FIELD(physHeader.filesize, chPtr->size);
|
|
|
|
|
|
|
|
GET_NUM_FIELD(physHeader.devMajor, major);
|
|
|
|
GET_NUM_FIELD(physHeader.devMinor, minor);
|
1998-11-19 05:41:05 +08:00
|
|
|
#ifndef __LCLINT__
|
1997-05-06 04:46:58 +08:00
|
|
|
chPtr->dev = makedev(major, minor);
|
1998-11-19 05:41:05 +08:00
|
|
|
#endif
|
1997-01-04 10:48:55 +08:00
|
|
|
|
|
|
|
GET_NUM_FIELD(physHeader.rdevMajor, major);
|
|
|
|
GET_NUM_FIELD(physHeader.rdevMinor, minor);
|
1998-11-19 05:41:05 +08:00
|
|
|
#ifndef __LCLINT__
|
1997-05-06 04:46:58 +08:00
|
|
|
chPtr->rdev = makedev(major, minor);
|
1998-11-19 05:41:05 +08:00
|
|
|
#endif
|
1997-01-04 10:48:55 +08:00
|
|
|
|
|
|
|
GET_NUM_FIELD(physHeader.namesize, nameSize);
|
|
|
|
|
1999-09-21 11:22:53 +08:00
|
|
|
chPtr->path = xmalloc(nameSize + 1);
|
1998-07-25 23:33:15 +08:00
|
|
|
if (ourread(cfd, chPtr->path, nameSize) != nameSize) {
|
1997-01-04 10:48:55 +08:00
|
|
|
free(chPtr->path);
|
1999-09-18 05:31:38 +08:00
|
|
|
chPtr->path = NULL;
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_BAD_HEADER;
|
1997-01-04 10:48:55 +08:00
|
|
|
}
|
|
|
|
|
1997-07-24 02:07:46 +08:00
|
|
|
/* this is unecessary chPtr->path[nameSize] = '\0'; */
|
1997-01-04 10:48:55 +08:00
|
|
|
|
1998-07-25 23:33:15 +08:00
|
|
|
padinfd(cfd, 4);
|
1997-01-04 10:48:55 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-09-18 05:31:38 +08:00
|
|
|
int cpioFileMapCmp(const void * a, const void * b)
|
|
|
|
{
|
1997-01-04 10:48:55 +08:00
|
|
|
const struct cpioFileMapping * first = a;
|
|
|
|
const struct cpioFileMapping * second = b;
|
|
|
|
|
|
|
|
return (strcmp(first->archivePath, second->archivePath));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This could trash files in the path! I'm not sure that's a good thing */
|
1999-09-18 05:31:38 +08:00
|
|
|
static int createDirectory(char * path, mode_t perms)
|
|
|
|
{
|
1997-01-04 10:48:55 +08:00
|
|
|
struct stat sb;
|
1997-05-06 04:46:58 +08:00
|
|
|
|
1997-05-06 23:27:46 +08:00
|
|
|
if (!lstat(path, &sb)) {
|
1998-11-19 05:41:05 +08:00
|
|
|
int dounlink = 0; /* XXX eliminate, dounlink==1 on all paths */
|
1997-01-04 10:48:55 +08:00
|
|
|
if (S_ISDIR(sb.st_mode)) {
|
|
|
|
return 0;
|
|
|
|
} else if (S_ISLNK(sb.st_mode)) {
|
|
|
|
if (stat(path, &sb)) {
|
|
|
|
if (errno != ENOENT)
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_STAT_FAILED;
|
1997-01-04 10:48:55 +08:00
|
|
|
dounlink = 1;
|
|
|
|
} else {
|
|
|
|
if (S_ISDIR(sb.st_mode))
|
|
|
|
return 0;
|
|
|
|
dounlink = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dounlink = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dounlink && unlink(path)) {
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_UNLINK_FAILED;
|
1997-01-04 10:48:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mkdir(path, 000))
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_MKDIR_FAILED;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
1997-05-15 02:32:18 +08:00
|
|
|
if (chmod(path, perms))
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_CHMOD_FAILED;
|
1997-05-15 02:32:18 +08:00
|
|
|
|
1997-01-04 10:48:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-09-18 05:31:38 +08:00
|
|
|
static int setInfo(struct cpioHeader * hdr)
|
|
|
|
{
|
1997-01-04 10:48:55 +08:00
|
|
|
int rc = 0;
|
1998-03-05 00:52:59 +08:00
|
|
|
struct utimbuf stamp;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
1998-03-05 00:52:59 +08:00
|
|
|
stamp.actime = hdr->mtime;
|
|
|
|
stamp.modtime = hdr->mtime;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
|
|
|
if (!S_ISLNK(hdr->mode)) {
|
1997-07-16 09:56:14 +08:00
|
|
|
if (!getuid() && chown(hdr->path, hdr->uid, hdr->gid))
|
1998-12-06 07:22:41 +08:00
|
|
|
rc = CPIOERR_CHOWN_FAILED;
|
1997-01-04 10:48:55 +08:00
|
|
|
if (!rc && chmod(hdr->path, hdr->mode & 07777))
|
1998-12-06 07:22:41 +08:00
|
|
|
rc = CPIOERR_CHMOD_FAILED;
|
1997-01-04 10:48:55 +08:00
|
|
|
if (!rc && utime(hdr->path, &stamp))
|
1998-12-06 07:22:41 +08:00
|
|
|
rc = CPIOERR_UTIME_FAILED;
|
1997-05-15 23:19:56 +08:00
|
|
|
} else {
|
1998-03-05 00:52:59 +08:00
|
|
|
# if ! CHOWN_FOLLOWS_SYMLINK
|
1997-05-20 23:28:25 +08:00
|
|
|
if (!getuid() && !rc && lchown(hdr->path, hdr->uid, hdr->gid))
|
1998-12-06 07:22:41 +08:00
|
|
|
rc = CPIOERR_CHOWN_FAILED;
|
1998-03-05 00:52:59 +08:00
|
|
|
# endif
|
1997-01-04 10:48:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
1999-10-21 05:40:10 +08:00
|
|
|
static int checkDirectory(const char * filename)
|
1999-09-18 05:31:38 +08:00
|
|
|
{
|
1999-10-21 05:40:10 +08:00
|
|
|
/*@only@*/ static char * lastDir = NULL; /* XXX memory leak */
|
1997-01-04 10:48:55 +08:00
|
|
|
static int lastDirLength = 0;
|
|
|
|
static int lastDirAlloced = 0;
|
|
|
|
int length = strlen(filename);
|
|
|
|
char * buf;
|
|
|
|
char * chptr;
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
buf = alloca(length + 1);
|
|
|
|
strcpy(buf, filename);
|
|
|
|
|
|
|
|
for (chptr = buf + length - 1; chptr > buf; chptr--) {
|
|
|
|
if (*chptr == '/') break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chptr == buf) return 0; /* /filename - no directories */
|
|
|
|
|
|
|
|
*chptr = '\0'; /* buffer is now just directories */
|
|
|
|
|
|
|
|
length = strlen(buf);
|
|
|
|
if (lastDirLength == length && !strcmp(buf, lastDir)) return 0;
|
|
|
|
|
|
|
|
if (lastDirAlloced < (length + 1)) {
|
|
|
|
lastDirAlloced = length + 100;
|
1999-09-22 01:21:57 +08:00
|
|
|
lastDir = xrealloc(lastDir, lastDirAlloced); /* XXX memory leak */
|
1997-01-04 10:48:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(lastDir, buf);
|
|
|
|
lastDirLength = length;
|
|
|
|
|
|
|
|
for (chptr = buf + 1; *chptr; chptr++) {
|
|
|
|
if (*chptr == '/') {
|
|
|
|
*chptr = '\0';
|
1997-05-15 02:32:18 +08:00
|
|
|
rc = createDirectory(buf, 0755);
|
1997-01-04 10:48:55 +08:00
|
|
|
*chptr = '/';
|
|
|
|
if (rc) return rc;
|
|
|
|
}
|
|
|
|
}
|
1997-05-15 02:32:18 +08:00
|
|
|
rc = createDirectory(buf, 0755);
|
1997-01-04 10:48:55 +08:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
1998-07-25 23:33:15 +08:00
|
|
|
static int expandRegular(CFD_t * cfd, struct cpioHeader * hdr,
|
1999-09-18 05:31:38 +08:00
|
|
|
cpioCallback cb, void * cbData)
|
|
|
|
{
|
1998-11-19 05:41:05 +08:00
|
|
|
FD_t out;
|
1997-05-06 23:27:46 +08:00
|
|
|
char buf[8192];
|
1997-01-04 10:48:55 +08:00
|
|
|
int bytesRead;
|
|
|
|
int left = hdr->size;
|
|
|
|
int rc = 0;
|
1997-05-06 23:27:46 +08:00
|
|
|
struct cpioCallbackInfo cbInfo;
|
|
|
|
struct stat sb;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
1999-02-19 01:21:50 +08:00
|
|
|
/* Rename the old file before attempting unlink to avoid EBUSY errors */
|
|
|
|
if (!lstat(hdr->path, &sb)) {
|
|
|
|
strcpy(buf, hdr->path);
|
1999-02-19 23:23:34 +08:00
|
|
|
strcat(buf, "-RPMDELETE");
|
1999-02-19 01:21:50 +08:00
|
|
|
if (rename(hdr->path, buf)) {
|
1999-03-23 01:31:53 +08:00
|
|
|
fprintf(stderr, _("can't rename %s to %s: %s\n"),
|
1999-02-19 01:21:50 +08:00
|
|
|
hdr->path, buf, strerror(errno));
|
|
|
|
return CPIOERR_UNLINK_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unlink(buf)) {
|
|
|
|
fprintf(stderr, _("can't unlink %s: %s\n"),
|
|
|
|
buf, strerror(errno));
|
|
|
|
#if 0
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_UNLINK_FAILED;
|
1999-02-19 01:21:50 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
1997-01-04 10:48:55 +08:00
|
|
|
|
1998-11-19 05:41:05 +08:00
|
|
|
out = fdOpen(hdr->path, O_CREAT | O_WRONLY, 0);
|
|
|
|
if (fdFileno(out) < 0)
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_OPEN_FAILED;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
1997-05-06 23:27:46 +08:00
|
|
|
cbInfo.file = hdr->path;
|
|
|
|
cbInfo.fileSize = hdr->size;
|
|
|
|
|
1997-01-04 10:48:55 +08:00
|
|
|
while (left) {
|
1998-07-25 23:33:15 +08:00
|
|
|
bytesRead = ourread(cfd, buf, left < sizeof(buf) ? left : sizeof(buf));
|
1997-01-04 10:48:55 +08:00
|
|
|
if (bytesRead <= 0) {
|
1998-12-06 07:22:41 +08:00
|
|
|
rc = CPIOERR_READ_FAILED;
|
1997-01-04 10:48:55 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1998-11-19 05:41:05 +08:00
|
|
|
if (fdWrite(out, buf, bytesRead) != bytesRead) {
|
1998-12-06 07:22:41 +08:00
|
|
|
rc = CPIOERR_COPY_FAILED;
|
1997-01-04 10:48:55 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
left -= bytesRead;
|
1997-05-06 23:27:46 +08:00
|
|
|
|
|
|
|
/* don't call this with fileSize == fileComplete */
|
|
|
|
if (!rc && cb && left) {
|
|
|
|
cbInfo.fileComplete = hdr->size - left;
|
1998-07-25 23:33:15 +08:00
|
|
|
cbInfo.bytesProcessed = cfd->cpioPos;
|
1997-05-06 23:27:46 +08:00
|
|
|
cb(&cbInfo, cbData);
|
|
|
|
}
|
1997-01-04 10:48:55 +08:00
|
|
|
}
|
|
|
|
|
1998-11-19 05:41:05 +08:00
|
|
|
fdClose(out);
|
1997-01-04 10:48:55 +08:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
1999-09-18 05:31:38 +08:00
|
|
|
static int expandSymlink(CFD_t * cfd, struct cpioHeader * hdr)
|
|
|
|
{
|
1997-07-16 09:56:14 +08:00
|
|
|
char buf[2048], buf2[2048];
|
1997-05-06 23:27:46 +08:00
|
|
|
struct stat sb;
|
1997-07-16 09:56:14 +08:00
|
|
|
int len;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
|
|
|
if ((hdr->size + 1)> sizeof(buf))
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_HDR_SIZE;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
1998-07-25 23:33:15 +08:00
|
|
|
if (ourread(cfd, buf, hdr->size) != hdr->size)
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_READ_FAILED;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
|
|
|
buf[hdr->size] = '\0';
|
|
|
|
|
1997-07-16 09:56:14 +08:00
|
|
|
if (!lstat(hdr->path, &sb)) {
|
|
|
|
if (S_ISLNK(sb.st_mode)) {
|
|
|
|
len = readlink(hdr->path, buf2, sizeof(buf2) - 1);
|
|
|
|
if (len > 0) {
|
|
|
|
buf2[len] = '\0';
|
|
|
|
if (!strcmp(buf, buf2)) return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unlink(hdr->path))
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_UNLINK_FAILED;
|
1997-07-16 09:56:14 +08:00
|
|
|
}
|
|
|
|
|
1997-07-16 09:38:55 +08:00
|
|
|
if (symlink(buf, hdr->path) < 0)
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_SYMLINK_FAILED;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-10-05 04:18:48 +08:00
|
|
|
static int expandFifo( /*@unused@*/ CFD_t * cfd, struct cpioHeader * hdr)
|
1999-09-18 05:31:38 +08:00
|
|
|
{
|
1997-01-04 10:48:55 +08:00
|
|
|
struct stat sb;
|
|
|
|
|
1997-05-06 23:27:46 +08:00
|
|
|
if (!lstat(hdr->path, &sb)) {
|
1997-01-04 10:48:55 +08:00
|
|
|
if (S_ISFIFO(sb.st_mode)) return 0;
|
|
|
|
|
|
|
|
if (unlink(hdr->path))
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_UNLINK_FAILED;
|
1997-01-04 10:48:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mkfifo(hdr->path, 0))
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_MKFIFO_FAILED;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-10-05 04:18:48 +08:00
|
|
|
static int expandDevice( /*@unused@*/ CFD_t * cfd, struct cpioHeader * hdr)
|
1999-09-18 05:31:38 +08:00
|
|
|
{
|
1997-05-06 23:27:46 +08:00
|
|
|
struct stat sb;
|
|
|
|
|
1997-07-16 09:56:14 +08:00
|
|
|
if (!lstat(hdr->path, &sb)) {
|
|
|
|
if ((S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) &&
|
|
|
|
(sb.st_rdev == hdr->rdev))
|
|
|
|
return 0;
|
1997-01-04 10:48:55 +08:00
|
|
|
if (unlink(hdr->path))
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_UNLINK_FAILED;
|
1997-07-16 09:56:14 +08:00
|
|
|
}
|
1997-01-04 10:48:55 +08:00
|
|
|
|
1999-10-05 04:18:48 +08:00
|
|
|
if ( /*@-unrecog@*/ mknod(hdr->path, hdr->mode & (~0777), hdr->rdev) /*@=unrecog@*/ )
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_MKNOD_FAILED;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-10-05 04:18:48 +08:00
|
|
|
static void freeLink( /*@only@*/ struct hardLink * li)
|
1999-09-18 05:31:38 +08:00
|
|
|
{
|
1997-05-07 01:38:05 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < li->nlink; i++) {
|
1999-09-18 05:31:38 +08:00
|
|
|
if (li->files[i] == NULL) continue;
|
|
|
|
free((void *)li->files[i]);
|
|
|
|
li->files[i] = NULL;
|
1997-05-07 01:38:05 +08:00
|
|
|
}
|
|
|
|
free(li->files);
|
|
|
|
}
|
|
|
|
|
1999-09-18 05:31:38 +08:00
|
|
|
static int createLinks(struct hardLink * li, /*@out@*/const char ** failedFile)
|
|
|
|
{
|
1997-05-07 01:38:05 +08:00
|
|
|
int i;
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
for (i = 0; i < li->nlink; i++) {
|
|
|
|
if (i == li->createdPath) continue;
|
|
|
|
if (!li->files[i]) continue;
|
|
|
|
|
|
|
|
if (!lstat(li->files[i], &sb)) {
|
|
|
|
if (unlink(li->files[i])) {
|
1999-09-18 05:31:38 +08:00
|
|
|
if (failedFile)
|
1999-09-21 11:22:53 +08:00
|
|
|
*failedFile = xstrdup(li->files[i]);
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_UNLINK_FAILED;
|
1997-05-07 01:38:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (link(li->files[li->createdPath], li->files[i])) {
|
1999-09-18 05:31:38 +08:00
|
|
|
if (failedFile)
|
1999-09-21 11:22:53 +08:00
|
|
|
*failedFile = xstrdup(li->files[i]);
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_LINK_FAILED;
|
1997-05-07 01:38:05 +08:00
|
|
|
}
|
|
|
|
|
1999-09-18 05:31:38 +08:00
|
|
|
free((void *)li->files[i]);
|
1997-05-07 01:38:05 +08:00
|
|
|
li->files[i] = NULL;
|
|
|
|
li->linksLeft--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-09-18 05:31:38 +08:00
|
|
|
static int eatBytes(CFD_t * cfd, int amount)
|
|
|
|
{
|
1997-05-23 23:18:15 +08:00
|
|
|
char buf[4096];
|
|
|
|
int bite;
|
|
|
|
|
|
|
|
while (amount) {
|
|
|
|
bite = (amount > sizeof(buf)) ? sizeof(buf) : amount;
|
1998-07-25 23:33:15 +08:00
|
|
|
if (ourread(cfd, buf, bite) != bite)
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_READ_FAILED;
|
1997-05-23 23:18:15 +08:00
|
|
|
amount -= bite;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1998-07-25 23:33:15 +08:00
|
|
|
int cpioInstallArchive(CFD_t *cfd, struct cpioFileMapping * mappings,
|
1997-05-06 23:27:46 +08:00
|
|
|
int numMappings, cpioCallback cb, void * cbData,
|
1999-09-18 05:31:38 +08:00
|
|
|
const char ** failedFile)
|
|
|
|
{
|
1997-01-04 10:48:55 +08:00
|
|
|
struct cpioHeader ch;
|
|
|
|
int rc = 0;
|
1997-05-07 01:38:05 +08:00
|
|
|
int linkNum = 0;
|
1997-01-04 10:48:55 +08:00
|
|
|
struct cpioFileMapping * map = NULL;
|
|
|
|
struct cpioFileMapping needle;
|
|
|
|
mode_t cpioMode;
|
|
|
|
int olderr;
|
1997-05-06 23:27:46 +08:00
|
|
|
struct cpioCallbackInfo cbInfo;
|
1997-05-07 01:38:05 +08:00
|
|
|
struct hardLink * links = NULL;
|
|
|
|
struct hardLink * li = NULL;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
1998-07-25 23:33:15 +08:00
|
|
|
cfd->cpioPos = 0;
|
1999-09-18 05:31:38 +08:00
|
|
|
if (failedFile)
|
|
|
|
*failedFile = NULL;
|
1997-05-07 01:38:05 +08:00
|
|
|
|
1998-11-19 05:41:05 +08:00
|
|
|
ch.path = NULL;
|
1997-01-04 10:48:55 +08:00
|
|
|
do {
|
1998-07-25 23:33:15 +08:00
|
|
|
if ((rc = getNextHeader(cfd, &ch))) {
|
1999-01-22 01:18:38 +08:00
|
|
|
#if 0 /* XXX this is the failure point for an unreadable rpm */
|
1999-01-09 00:44:30 +08:00
|
|
|
fprintf(stderr, _("getNextHeader: %s\n"), cpioStrerror(rc));
|
1999-01-22 01:18:38 +08:00
|
|
|
#endif
|
|
|
|
return rc;
|
1997-01-04 10:48:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(ch.path, TRAILER)) {
|
1997-05-06 04:46:58 +08:00
|
|
|
free(ch.path);
|
1997-01-04 10:48:55 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mappings) {
|
|
|
|
needle.archivePath = ch.path;
|
|
|
|
map = bsearch(&needle, mappings, numMappings, sizeof(needle),
|
|
|
|
cpioFileMapCmp);
|
|
|
|
}
|
|
|
|
|
1997-05-23 23:18:15 +08:00
|
|
|
if (mappings && !map) {
|
1998-07-25 23:33:15 +08:00
|
|
|
eatBytes(cfd, ch.size);
|
1997-05-23 23:18:15 +08:00
|
|
|
} else {
|
1997-01-04 10:48:55 +08:00
|
|
|
cpioMode = ch.mode;
|
|
|
|
|
|
|
|
if (map) {
|
|
|
|
if (map->mapFlags & CPIO_MAP_PATH) {
|
1997-05-06 04:46:58 +08:00
|
|
|
free(ch.path);
|
1999-09-21 11:22:53 +08:00
|
|
|
ch.path = xstrdup(map->fsPath);
|
1997-01-04 10:48:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (map->mapFlags & CPIO_MAP_MODE)
|
|
|
|
ch.mode = map->finalMode;
|
|
|
|
if (map->mapFlags & CPIO_MAP_UID)
|
|
|
|
ch.uid = map->finalUid;
|
|
|
|
if (map->mapFlags & CPIO_MAP_GID)
|
|
|
|
ch.gid = map->finalGid;
|
|
|
|
}
|
|
|
|
|
1997-05-07 01:38:05 +08:00
|
|
|
/* This won't get hard linked symlinks right, but I can't seem
|
|
|
|
to create those anyway */
|
|
|
|
|
1997-08-25 22:38:48 +08:00
|
|
|
if (S_ISREG(ch.mode) && ch.nlink > 1) {
|
1997-05-07 01:38:05 +08:00
|
|
|
for (li = links; li; li = li->next) {
|
|
|
|
if (li->inode == ch.inode && li->dev == ch.dev) break;
|
|
|
|
}
|
|
|
|
|
1999-09-18 05:31:38 +08:00
|
|
|
if (li == NULL) {
|
1999-09-21 11:22:53 +08:00
|
|
|
li = xmalloc(sizeof(*li));
|
1997-05-07 01:38:05 +08:00
|
|
|
li->inode = ch.inode;
|
|
|
|
li->dev = ch.dev;
|
|
|
|
li->nlink = ch.nlink;
|
|
|
|
li->linksLeft = ch.nlink;
|
|
|
|
li->createdPath = -1;
|
1999-09-21 11:22:53 +08:00
|
|
|
li->files = xcalloc(li->nlink,(sizeof(*li->files)));
|
1997-05-07 01:38:05 +08:00
|
|
|
li->next = links;
|
|
|
|
links = li;
|
1997-01-04 10:48:55 +08:00
|
|
|
}
|
1997-05-07 01:38:05 +08:00
|
|
|
|
|
|
|
for (linkNum = 0; linkNum < li->nlink; linkNum++)
|
|
|
|
if (!li->files[linkNum]) break;
|
1999-09-21 11:22:53 +08:00
|
|
|
li->files[linkNum] = xstrdup(ch.path);
|
1997-01-04 10:48:55 +08:00
|
|
|
}
|
1997-05-07 01:38:05 +08:00
|
|
|
|
|
|
|
if ((ch.nlink > 1) && S_ISREG(ch.mode) && !ch.size &&
|
|
|
|
li->createdPath == -1) {
|
|
|
|
/* defer file creation */
|
1997-08-26 01:45:30 +08:00
|
|
|
} else if ((ch.nlink > 1) && S_ISREG(ch.mode) &&
|
|
|
|
(li->createdPath != -1)) {
|
1997-05-07 01:38:05 +08:00
|
|
|
createLinks(li, failedFile);
|
1997-07-24 02:07:46 +08:00
|
|
|
|
|
|
|
/* this only happens for cpio archives which contain
|
|
|
|
hardlinks w/ the contents of each hardlink being
|
|
|
|
listed (intead of the data being given just once. This
|
|
|
|
shouldn't happen, but I've made it happen w/ buggy
|
|
|
|
code, so what the heck? GNU cpio handles this well fwiw */
|
1998-07-25 23:33:15 +08:00
|
|
|
if (ch.size) eatBytes(cfd, ch.size);
|
1997-05-07 01:38:05 +08:00
|
|
|
} else {
|
|
|
|
rc = checkDirectory(ch.path);
|
|
|
|
|
|
|
|
if (!rc) {
|
|
|
|
if (S_ISREG(ch.mode))
|
1998-07-25 23:33:15 +08:00
|
|
|
rc = expandRegular(cfd, &ch, cb, cbData);
|
1997-05-07 01:38:05 +08:00
|
|
|
else if (S_ISDIR(ch.mode))
|
1997-05-15 02:32:18 +08:00
|
|
|
rc = createDirectory(ch.path, 000);
|
1997-05-07 01:38:05 +08:00
|
|
|
else if (S_ISLNK(ch.mode))
|
1998-07-25 23:33:15 +08:00
|
|
|
rc = expandSymlink(cfd, &ch);
|
1997-05-07 01:38:05 +08:00
|
|
|
else if (S_ISFIFO(ch.mode))
|
1998-07-25 23:33:15 +08:00
|
|
|
rc = expandFifo(cfd, &ch);
|
1997-05-07 01:38:05 +08:00
|
|
|
else if (S_ISCHR(ch.mode) || S_ISBLK(ch.mode))
|
1998-07-25 23:33:15 +08:00
|
|
|
rc = expandDevice(cfd, &ch);
|
1997-05-07 01:38:05 +08:00
|
|
|
else if (S_ISSOCK(ch.mode)) {
|
|
|
|
/* this mimicks cpio but probably isnt' right */
|
1998-07-25 23:33:15 +08:00
|
|
|
rc = expandFifo(cfd, &ch);
|
1997-05-07 01:38:05 +08:00
|
|
|
} else {
|
1999-05-12 04:05:43 +08:00
|
|
|
rc = CPIOERR_UNKNOWN_FILETYPE;
|
1997-05-07 01:38:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rc)
|
|
|
|
rc = setInfo(&ch);
|
1997-01-04 10:48:55 +08:00
|
|
|
|
1997-08-25 22:38:48 +08:00
|
|
|
if (S_ISREG(ch.mode) && ch.nlink > 1) {
|
1997-05-07 01:38:05 +08:00
|
|
|
li->createdPath = linkNum;
|
|
|
|
li->linksLeft--;
|
|
|
|
rc = createLinks(li, failedFile);
|
|
|
|
}
|
|
|
|
}
|
1997-01-04 10:48:55 +08:00
|
|
|
|
1999-09-18 05:31:38 +08:00
|
|
|
if (rc && failedFile && *failedFile == NULL) {
|
1999-09-21 11:22:53 +08:00
|
|
|
*failedFile = xstrdup(ch.path);
|
1997-01-04 10:48:55 +08:00
|
|
|
|
|
|
|
olderr = errno;
|
|
|
|
unlink(ch.path);
|
|
|
|
errno = olderr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-07-25 23:33:15 +08:00
|
|
|
padinfd(cfd, 4);
|
1997-05-06 23:27:46 +08:00
|
|
|
|
|
|
|
if (!rc && cb) {
|
|
|
|
cbInfo.file = ch.path;
|
|
|
|
cbInfo.fileSize = ch.size;
|
|
|
|
cbInfo.fileComplete = ch.size;
|
1998-07-25 23:33:15 +08:00
|
|
|
cbInfo.bytesProcessed = cfd->cpioPos;
|
1997-05-06 23:27:46 +08:00
|
|
|
cb(&cbInfo, cbData);
|
|
|
|
}
|
|
|
|
|
1997-05-06 04:46:58 +08:00
|
|
|
free(ch.path);
|
1998-11-19 05:41:05 +08:00
|
|
|
ch.path = NULL;
|
1997-01-04 10:48:55 +08:00
|
|
|
} while (1 && !rc);
|
|
|
|
|
1997-05-07 01:38:05 +08:00
|
|
|
li = links;
|
|
|
|
while (li && !rc) {
|
|
|
|
if (li->linksLeft) {
|
|
|
|
if (li->createdPath == -1)
|
1999-05-12 04:05:43 +08:00
|
|
|
rc = CPIOERR_MISSING_HARDLINK;
|
1997-05-07 01:38:05 +08:00
|
|
|
else
|
|
|
|
rc = createLinks(li, failedFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
freeLink(li);
|
|
|
|
|
|
|
|
links = li;
|
|
|
|
li = li->next;
|
|
|
|
free(links);
|
|
|
|
links = li;
|
|
|
|
}
|
|
|
|
|
|
|
|
li = links;
|
|
|
|
/* if an error got us here links will still be eating some memory */
|
|
|
|
while (li) {
|
|
|
|
freeLink(li);
|
|
|
|
links = li;
|
|
|
|
li = li->next;
|
|
|
|
free(links);
|
|
|
|
}
|
|
|
|
|
1997-01-04 10:48:55 +08:00
|
|
|
return rc;
|
|
|
|
}
|
1997-07-24 02:07:46 +08:00
|
|
|
|
1998-07-25 23:33:15 +08:00
|
|
|
static int writeFile(CFD_t *cfd, struct stat sb, struct cpioFileMapping * map,
|
1999-09-18 05:31:38 +08:00
|
|
|
/*@out@*/size_t * sizep, int writeData)
|
|
|
|
{
|
1997-07-24 02:07:46 +08:00
|
|
|
struct cpioCrcPhysicalHeader hdr;
|
|
|
|
char buf[8192], symbuf[2048];
|
|
|
|
dev_t num;
|
1998-11-19 05:41:05 +08:00
|
|
|
FD_t datafd;
|
1997-07-25 21:57:17 +08:00
|
|
|
size_t size, amount = 0;
|
1997-07-24 02:07:46 +08:00
|
|
|
int rc, olderrno;
|
|
|
|
|
|
|
|
if (!(map->mapFlags & CPIO_MAP_PATH))
|
|
|
|
map->archivePath = map->fsPath;
|
|
|
|
if (map->mapFlags & CPIO_MAP_MODE)
|
|
|
|
sb.st_mode = (sb.st_mode & S_IFMT) | map->finalMode;
|
|
|
|
if (map->mapFlags & CPIO_MAP_UID)
|
|
|
|
sb.st_uid = map->finalUid;
|
|
|
|
if (map->mapFlags & CPIO_MAP_GID)
|
|
|
|
sb.st_gid = map->finalGid;
|
|
|
|
|
1997-07-31 22:04:56 +08:00
|
|
|
if (!writeData || S_ISDIR(sb.st_mode)) {
|
1997-07-24 02:07:46 +08:00
|
|
|
sb.st_size = 0;
|
|
|
|
} else if (S_ISLNK(sb.st_mode)) {
|
|
|
|
/* While linux puts the size of a symlink in the st_size field,
|
|
|
|
I don't think that's a specified standard */
|
|
|
|
|
|
|
|
amount = readlink(map->fsPath, symbuf, sizeof(symbuf));
|
|
|
|
if (amount <= 0) {
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_READLINK_FAILED;
|
1997-07-24 02:07:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sb.st_size = amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(hdr.magic, CPIO_NEWC_MAGIC, sizeof(hdr.magic));
|
|
|
|
SET_NUM_FIELD(hdr.inode, sb.st_ino, buf);
|
|
|
|
SET_NUM_FIELD(hdr.mode, sb.st_mode, buf);
|
|
|
|
SET_NUM_FIELD(hdr.uid, sb.st_uid, buf);
|
|
|
|
SET_NUM_FIELD(hdr.gid, sb.st_gid, buf);
|
|
|
|
SET_NUM_FIELD(hdr.nlink, sb.st_nlink, buf);
|
|
|
|
SET_NUM_FIELD(hdr.mtime, sb.st_mtime, buf);
|
|
|
|
SET_NUM_FIELD(hdr.filesize, sb.st_size, buf);
|
|
|
|
|
|
|
|
num = major(sb.st_dev); SET_NUM_FIELD(hdr.devMajor, num, buf);
|
|
|
|
num = minor(sb.st_dev); SET_NUM_FIELD(hdr.devMinor, num, buf);
|
|
|
|
num = major(sb.st_rdev); SET_NUM_FIELD(hdr.rdevMajor, num, buf);
|
|
|
|
num = minor(sb.st_rdev); SET_NUM_FIELD(hdr.rdevMinor, num, buf);
|
|
|
|
|
|
|
|
num = strlen(map->archivePath) + 1; SET_NUM_FIELD(hdr.namesize, num, buf);
|
|
|
|
memcpy(hdr.checksum, "00000000", 8);
|
|
|
|
|
1998-12-06 03:18:43 +08:00
|
|
|
if ((rc = safewrite(cfd, &hdr, PHYS_HDR_SIZE)) != PHYS_HDR_SIZE)
|
1997-07-24 02:07:46 +08:00
|
|
|
return rc;
|
1998-07-25 23:33:15 +08:00
|
|
|
if ((rc = safewrite(cfd, map->archivePath, num)) != num)
|
1997-07-24 02:07:46 +08:00
|
|
|
return rc;
|
1998-12-04 03:30:32 +08:00
|
|
|
size = PHYS_HDR_SIZE + num;
|
1998-07-25 23:33:15 +08:00
|
|
|
if ((rc = padoutfd(cfd, &size, 4)))
|
1997-07-24 02:07:46 +08:00
|
|
|
return rc;
|
|
|
|
|
|
|
|
if (writeData && S_ISREG(sb.st_mode)) {
|
1999-01-14 06:10:00 +08:00
|
|
|
char *b;
|
|
|
|
#if HAVE_MMAP
|
|
|
|
void *mapped;
|
|
|
|
size_t nmapped;
|
|
|
|
#endif
|
|
|
|
|
1998-11-19 05:41:05 +08:00
|
|
|
if (fdFileno(datafd = fdOpen(map->fsPath, O_RDONLY, 0)) < 0)
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_OPEN_FAILED;
|
1997-07-24 02:07:46 +08:00
|
|
|
|
1999-01-14 06:10:00 +08:00
|
|
|
#if HAVE_MMAP
|
|
|
|
nmapped = 0;
|
|
|
|
mapped = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fdFileno(datafd), 0);
|
|
|
|
if (mapped != (void *)-1) {
|
|
|
|
b = (char *)mapped;
|
|
|
|
nmapped = sb.st_size;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
b = buf;
|
|
|
|
}
|
|
|
|
|
1997-07-24 02:07:46 +08:00
|
|
|
size += sb.st_size;
|
|
|
|
|
|
|
|
while (sb.st_size) {
|
1999-01-14 06:10:00 +08:00
|
|
|
#if HAVE_MMAP
|
|
|
|
if (mapped != (void *)-1) {
|
|
|
|
amount = nmapped;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
amount = fdRead(datafd, b,
|
1997-07-24 02:07:46 +08:00
|
|
|
sb.st_size > sizeof(buf) ? sizeof(buf) : sb.st_size);
|
|
|
|
if (amount <= 0) {
|
|
|
|
olderrno = errno;
|
1998-11-19 05:41:05 +08:00
|
|
|
fdClose(datafd);
|
1997-07-24 02:07:46 +08:00
|
|
|
errno = olderrno;
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_READ_FAILED;
|
1997-07-24 02:07:46 +08:00
|
|
|
}
|
1999-01-14 06:10:00 +08:00
|
|
|
}
|
1997-07-24 02:07:46 +08:00
|
|
|
|
1999-01-14 06:10:00 +08:00
|
|
|
if ((rc = safewrite(cfd, b, amount)) != amount) {
|
1997-07-24 02:07:46 +08:00
|
|
|
olderrno = errno;
|
1998-11-19 05:41:05 +08:00
|
|
|
fdClose(datafd);
|
1997-07-24 02:07:46 +08:00
|
|
|
errno = olderrno;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
sb.st_size -= amount;
|
|
|
|
}
|
1997-08-01 00:02:19 +08:00
|
|
|
|
1999-01-14 06:10:00 +08:00
|
|
|
#if HAVE_MMAP
|
|
|
|
if (mapped != (void *)-1) {
|
|
|
|
munmap(mapped, nmapped);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1998-11-19 05:41:05 +08:00
|
|
|
fdClose(datafd);
|
1997-07-24 02:07:46 +08:00
|
|
|
} else if (writeData && S_ISLNK(sb.st_mode)) {
|
1998-07-25 23:33:15 +08:00
|
|
|
if ((rc = safewrite(cfd, symbuf, amount)) != amount)
|
1997-07-24 02:07:46 +08:00
|
|
|
return rc;
|
|
|
|
size += amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this is a noop for most file types */
|
1998-07-25 23:33:15 +08:00
|
|
|
if ((rc = padoutfd(cfd, &size, 4)))
|
1997-07-24 02:07:46 +08:00
|
|
|
return rc;
|
|
|
|
|
1999-09-18 05:31:38 +08:00
|
|
|
if (sizep)
|
|
|
|
*sizep = size;
|
1997-07-24 02:07:46 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1998-07-25 23:33:15 +08:00
|
|
|
static int writeLinkedFile(CFD_t *cfd, struct hardLink * hlink,
|
1997-07-24 02:07:46 +08:00
|
|
|
struct cpioFileMapping * mappings,
|
|
|
|
cpioCallback cb, void * cbData,
|
1999-09-18 05:31:38 +08:00
|
|
|
/*@out@*/size_t * sizep,
|
|
|
|
/*@out@*/const char ** failedFile)
|
|
|
|
{
|
1997-07-24 02:07:46 +08:00
|
|
|
int i, rc;
|
1999-09-18 05:31:38 +08:00
|
|
|
size_t size, total;
|
1997-07-24 02:07:46 +08:00
|
|
|
struct cpioCallbackInfo cbinfo;
|
|
|
|
|
1999-09-18 05:31:38 +08:00
|
|
|
total = 0;
|
1997-07-24 02:07:46 +08:00
|
|
|
|
|
|
|
for (i = hlink->nlink - 1; i > hlink->linksLeft; i--) {
|
1998-07-25 23:33:15 +08:00
|
|
|
if ((rc = writeFile(cfd, hlink->sb, mappings + hlink->fileMaps[i],
|
1997-07-24 02:07:46 +08:00
|
|
|
&size, 0))) {
|
1999-09-18 05:31:38 +08:00
|
|
|
if (failedFile)
|
1999-09-21 11:22:53 +08:00
|
|
|
*failedFile = xstrdup(mappings[hlink->fileMaps[i]].fsPath);
|
1997-07-24 02:07:46 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
1999-09-18 05:31:38 +08:00
|
|
|
total += size;
|
1997-07-24 02:07:46 +08:00
|
|
|
|
|
|
|
if (cb) {
|
|
|
|
cbinfo.file = mappings[i].archivePath;
|
|
|
|
cb(&cbinfo, cbData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-07-25 23:33:15 +08:00
|
|
|
if ((rc = writeFile(cfd, hlink->sb,
|
1997-07-24 02:07:46 +08:00
|
|
|
mappings + hlink->fileMaps[hlink->linksLeft],
|
|
|
|
&size, 1))) {
|
1999-09-18 05:31:38 +08:00
|
|
|
if (sizep)
|
|
|
|
*sizep = total;
|
1997-07-24 02:07:46 +08:00
|
|
|
if (failedFile)
|
1999-09-21 11:22:53 +08:00
|
|
|
*failedFile = xstrdup(mappings[hlink->fileMaps[hlink->linksLeft]].fsPath);
|
1997-07-24 02:07:46 +08:00
|
|
|
return rc;
|
|
|
|
}
|
1999-09-18 05:31:38 +08:00
|
|
|
total += size;
|
1997-07-24 02:07:46 +08:00
|
|
|
|
1999-09-18 05:31:38 +08:00
|
|
|
if (sizep)
|
|
|
|
*sizep = total;
|
1997-07-24 02:07:46 +08:00
|
|
|
|
|
|
|
if (cb) {
|
|
|
|
cbinfo.file = mappings[i].archivePath;
|
|
|
|
cb(&cbinfo, cbData);
|
|
|
|
}
|
|
|
|
|
1997-08-01 00:25:34 +08:00
|
|
|
return 0;
|
1997-07-24 02:07:46 +08:00
|
|
|
}
|
|
|
|
|
1998-07-25 23:33:15 +08:00
|
|
|
int cpioBuildArchive(CFD_t *cfd, struct cpioFileMapping * mappings,
|
1997-07-24 02:07:46 +08:00
|
|
|
int numMappings, cpioCallback cb, void * cbData,
|
1999-09-18 05:31:38 +08:00
|
|
|
unsigned int * archiveSize, const char ** failedFile)
|
|
|
|
{
|
1997-07-24 02:07:46 +08:00
|
|
|
size_t size, totalsize = 0;
|
|
|
|
int rc;
|
|
|
|
int i;
|
|
|
|
struct cpioCallbackInfo cbinfo;
|
|
|
|
struct cpioCrcPhysicalHeader hdr;
|
|
|
|
struct stat sb;
|
1998-11-20 03:10:23 +08:00
|
|
|
/*@-fullinitblock@*/
|
1997-07-24 02:07:46 +08:00
|
|
|
struct hardLink hlinkList = { NULL };
|
1998-11-20 03:10:23 +08:00
|
|
|
/*@=fullinitblock@*/
|
1997-07-24 02:07:46 +08:00
|
|
|
struct hardLink * hlink, * parent;
|
|
|
|
|
1997-07-31 22:04:56 +08:00
|
|
|
hlinkList.next = NULL;
|
|
|
|
|
1997-07-24 02:07:46 +08:00
|
|
|
for (i = 0; i < numMappings; i++) {
|
1997-07-31 22:04:56 +08:00
|
|
|
if (mappings[i].mapFlags & CPIO_FOLLOW_SYMLINKS)
|
|
|
|
rc = stat(mappings[i].fsPath, &sb);
|
|
|
|
else
|
|
|
|
rc = lstat(mappings[i].fsPath, &sb);
|
|
|
|
|
1997-08-01 00:02:19 +08:00
|
|
|
if (rc) {
|
1999-09-18 05:31:38 +08:00
|
|
|
if (failedFile)
|
1999-09-21 11:22:53 +08:00
|
|
|
*failedFile = xstrdup(mappings[i].fsPath);
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_STAT_FAILED;
|
1997-08-01 00:02:19 +08:00
|
|
|
}
|
1997-07-24 02:07:46 +08:00
|
|
|
|
1997-07-31 22:04:56 +08:00
|
|
|
if (!S_ISDIR(sb.st_mode) && sb.st_nlink > 1) {
|
1997-07-24 02:07:46 +08:00
|
|
|
hlink = hlinkList.next;
|
|
|
|
while (hlink &&
|
|
|
|
(hlink->dev != sb.st_dev || hlink->inode != sb.st_ino))
|
|
|
|
hlink = hlink->next;
|
|
|
|
if (!hlink) {
|
1999-09-21 11:22:53 +08:00
|
|
|
hlink = xmalloc(sizeof(*hlink));
|
1997-07-24 02:07:46 +08:00
|
|
|
hlink->next = hlinkList.next;
|
|
|
|
hlinkList.next = hlink;
|
1999-01-22 01:18:38 +08:00
|
|
|
hlink->sb = sb; /* structure assignment */
|
1997-07-24 02:07:46 +08:00
|
|
|
hlink->dev = sb.st_dev;
|
|
|
|
hlink->inode = sb.st_ino;
|
|
|
|
hlink->nlink = sb.st_nlink;
|
|
|
|
hlink->linksLeft = sb.st_nlink;
|
1999-09-21 11:22:53 +08:00
|
|
|
hlink->fileMaps = xmalloc(sizeof(*hlink->fileMaps) *
|
1997-07-24 02:07:46 +08:00
|
|
|
sb.st_nlink);
|
|
|
|
}
|
|
|
|
|
|
|
|
hlink->fileMaps[--hlink->linksLeft] = i;
|
|
|
|
|
|
|
|
if (!hlink->linksLeft) {
|
1998-07-25 23:33:15 +08:00
|
|
|
if ((rc = writeLinkedFile(cfd, hlink, mappings, cb, cbData,
|
1997-07-24 02:07:46 +08:00
|
|
|
&size, failedFile)))
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
totalsize += size;
|
|
|
|
|
|
|
|
free(hlink->fileMaps);
|
|
|
|
|
|
|
|
parent = &hlinkList;
|
|
|
|
while (parent->next != hlink) parent = parent->next;
|
|
|
|
parent->next = parent->next->next;
|
|
|
|
free(hlink);
|
|
|
|
}
|
|
|
|
} else {
|
1998-07-25 23:33:15 +08:00
|
|
|
if ((rc = writeFile(cfd, sb, mappings + i, &size, 1))) {
|
1999-09-18 05:31:38 +08:00
|
|
|
if (failedFile)
|
1999-09-21 11:22:53 +08:00
|
|
|
*failedFile = xstrdup(mappings[i].fsPath);
|
1997-07-24 02:07:46 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cb) {
|
|
|
|
cbinfo.file = mappings[i].archivePath;
|
|
|
|
cb(&cbinfo, cbData);
|
|
|
|
}
|
|
|
|
|
|
|
|
totalsize += size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hlink = hlinkList.next;
|
|
|
|
while (hlink) {
|
1998-07-25 23:33:15 +08:00
|
|
|
if ((rc = writeLinkedFile(cfd, hlink, mappings, cb, cbData,
|
1997-07-24 02:07:46 +08:00
|
|
|
&size, failedFile)))
|
|
|
|
return rc;
|
|
|
|
free(hlink->fileMaps);
|
|
|
|
parent = hlink;
|
|
|
|
hlink = hlink->next;
|
|
|
|
free(parent);
|
1997-10-04 00:08:20 +08:00
|
|
|
|
|
|
|
totalsize += size;
|
1997-07-24 02:07:46 +08:00
|
|
|
}
|
|
|
|
|
1998-12-06 03:18:43 +08:00
|
|
|
memset(&hdr, '0', PHYS_HDR_SIZE);
|
1997-07-24 02:07:46 +08:00
|
|
|
memcpy(hdr.magic, CPIO_NEWC_MAGIC, sizeof(hdr.magic));
|
|
|
|
memcpy(hdr.nlink, "00000001", 8);
|
|
|
|
memcpy(hdr.namesize, "0000000b", 8);
|
1998-12-06 03:18:43 +08:00
|
|
|
if ((rc = safewrite(cfd, &hdr, PHYS_HDR_SIZE)) != PHYS_HDR_SIZE)
|
1997-07-24 02:07:46 +08:00
|
|
|
return rc;
|
1998-07-25 23:33:15 +08:00
|
|
|
if ((rc = safewrite(cfd, "TRAILER!!!", 11)) != 11)
|
1997-07-24 02:07:46 +08:00
|
|
|
return rc;
|
1998-12-04 03:30:32 +08:00
|
|
|
totalsize += PHYS_HDR_SIZE + 11;
|
1997-07-24 02:07:46 +08:00
|
|
|
|
|
|
|
/* GNU cpio pads to 512 bytes here, but we don't. I'm not sure if
|
|
|
|
it matters or not */
|
|
|
|
|
1998-07-25 23:33:15 +08:00
|
|
|
if ((rc = padoutfd(cfd, &totalsize, 4)))
|
1997-07-24 02:07:46 +08:00
|
|
|
return rc;
|
|
|
|
|
1997-08-25 22:38:48 +08:00
|
|
|
if (archiveSize) *archiveSize = totalsize;
|
|
|
|
|
1997-07-24 02:07:46 +08:00
|
|
|
return 0;
|
|
|
|
}
|
1998-12-06 07:22:41 +08:00
|
|
|
|
|
|
|
const char * cpioStrerror(int rc)
|
|
|
|
{
|
|
|
|
static char msg[256];
|
|
|
|
char *s;
|
|
|
|
int l, myerrno = errno;
|
|
|
|
|
|
|
|
strcpy(msg, "cpio: ");
|
|
|
|
switch (rc) {
|
|
|
|
default:
|
|
|
|
s = msg + strlen(msg);
|
1999-09-18 05:31:38 +08:00
|
|
|
sprintf(s, _("(error 0x%x)"), (unsigned)rc);
|
1998-12-06 07:22:41 +08:00
|
|
|
s = NULL;
|
|
|
|
break;
|
|
|
|
case CPIOERR_BAD_MAGIC: s = _("Bad magic"); break;
|
1999-01-22 01:18:38 +08:00
|
|
|
case CPIOERR_BAD_HEADER: s = _("Bad/unreadable header");break;
|
1998-12-06 07:22:41 +08:00
|
|
|
|
|
|
|
case CPIOERR_OPEN_FAILED: s = "open"; break;
|
|
|
|
case CPIOERR_CHMOD_FAILED: s = "chmod"; break;
|
|
|
|
case CPIOERR_CHOWN_FAILED: s = "chown"; break;
|
|
|
|
case CPIOERR_WRITE_FAILED: s = "write"; break;
|
|
|
|
case CPIOERR_UTIME_FAILED: s = "utime"; break;
|
|
|
|
case CPIOERR_UNLINK_FAILED: s = "unlink"; break;
|
|
|
|
case CPIOERR_SYMLINK_FAILED: s = "symlink"; break;
|
|
|
|
case CPIOERR_STAT_FAILED: s = "stat"; break;
|
|
|
|
case CPIOERR_MKDIR_FAILED: s = "mkdir"; break;
|
|
|
|
case CPIOERR_MKNOD_FAILED: s = "mknod"; break;
|
|
|
|
case CPIOERR_MKFIFO_FAILED: s = "mkfifo"; break;
|
|
|
|
case CPIOERR_LINK_FAILED: s = "link"; break;
|
|
|
|
case CPIOERR_READLINK_FAILED: s = "readlink"; break;
|
|
|
|
case CPIOERR_READ_FAILED: s = "read"; break;
|
|
|
|
case CPIOERR_COPY_FAILED: s = "copy"; break;
|
|
|
|
|
|
|
|
case CPIOERR_HDR_SIZE: s = _("Header size too big"); break;
|
|
|
|
case CPIOERR_UNKNOWN_FILETYPE: s = _("Unknown file type"); break;
|
1999-05-12 04:05:43 +08:00
|
|
|
case CPIOERR_MISSING_HARDLINK: s = _("Missing hard link"); break;
|
|
|
|
case CPIOERR_INTERNAL: s = _("Internal error"); break;
|
1998-12-06 07:22:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
l = sizeof(msg) - strlen(msg) - 1;
|
|
|
|
if (s != NULL) {
|
|
|
|
if (l > 0) strncat(msg, s, l);
|
|
|
|
l -= strlen(s);
|
|
|
|
}
|
|
|
|
if (rc & CPIOERR_CHECK_ERRNO) {
|
|
|
|
s = _(" failed - ");
|
|
|
|
if (l > 0) strncat(msg, s, l);
|
|
|
|
l -= strlen(s);
|
|
|
|
if (l > 0) strncat(msg, strerror(myerrno), l);
|
|
|
|
}
|
|
|
|
return msg;
|
|
|
|
}
|