2000-08-28 03:18:25 +08:00
|
|
|
/** \ingroup payload rpmio
|
|
|
|
* \file lib/cpio.c
|
2000-03-23 23:49:50 +08:00
|
|
|
* Handle cpio payloads within rpm packages.
|
|
|
|
*
|
2000-08-28 03:18:25 +08:00
|
|
|
* \warning FIXME: We don't translate between cpio and system mode bits! These
|
2000-03-23 23:49:50 +08:00
|
|
|
* should both be the same, but really odd things are going to happen if
|
|
|
|
* that's not true!
|
|
|
|
*/
|
|
|
|
|
1998-07-26 05:00:26 +08:00
|
|
|
#include "system.h"
|
1997-01-04 10:48:55 +08:00
|
|
|
#include "cpio.h"
|
2000-12-13 04:03:45 +08:00
|
|
|
#include "debug.h"
|
1997-05-06 04:46:58 +08:00
|
|
|
|
2000-12-13 04:03:45 +08:00
|
|
|
/*@access FD_t@*/
|
2000-03-23 23:49:50 +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!!!"
|
|
|
|
|
2000-08-28 03:18:25 +08:00
|
|
|
/** \ingroup payload
|
2000-08-23 20:39:49 +08:00
|
|
|
* Keeps track of set of all hard linked files in archive.
|
|
|
|
*/
|
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;
|
2000-03-23 23:49:50 +08:00
|
|
|
int nlink;
|
1997-05-07 01:38:05 +08:00
|
|
|
int linksLeft;
|
|
|
|
int createdPath;
|
2000-05-18 20:11:51 +08:00
|
|
|
const struct stat sb;
|
1997-05-07 01:38:05 +08:00
|
|
|
};
|
|
|
|
|
2000-08-28 09:08:57 +08:00
|
|
|
/** \ingroup payload
|
|
|
|
*/
|
|
|
|
enum hardLinkType {
|
|
|
|
HARDLINK_INSTALL=1,
|
|
|
|
HARDLINK_BUILD
|
|
|
|
};
|
2000-03-23 23:49:50 +08:00
|
|
|
|
2000-08-28 03:18:25 +08:00
|
|
|
/** \ingroup payload
|
2000-08-23 20:39:49 +08:00
|
|
|
* Cpio archive header information.
|
2000-08-28 09:08:57 +08:00
|
|
|
* @todo Add support for tar (soon) and ar (eventually) archive formats.
|
2000-08-23 20:39:49 +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 !! */
|
|
|
|
};
|
|
|
|
|
2000-08-28 09:08:57 +08:00
|
|
|
#define PHYS_HDR_SIZE 110 /*!< Don't depend on sizeof(struct) */
|
1998-12-04 03:30:32 +08:00
|
|
|
|
2000-08-28 03:18:25 +08:00
|
|
|
/** \ingroup payload
|
2000-08-23 20:39:49 +08:00
|
|
|
* File name and stat information.
|
|
|
|
*/
|
1997-01-04 10:48:55 +08:00
|
|
|
struct cpioHeader {
|
2000-03-23 23:49:50 +08:00
|
|
|
/*@owned@*/ const char * path;
|
|
|
|
struct stat sb;
|
1997-01-04 10:48:55 +08:00
|
|
|
};
|
|
|
|
|
2000-05-18 20:11:51 +08:00
|
|
|
#if 0
|
|
|
|
static void prtli(const char *msg, struct hardLink * li)
|
|
|
|
{
|
|
|
|
if (msg) fprintf(stderr, "%s", msg);
|
|
|
|
fprintf(stderr, " next %p files %p fileMaps %p dev %x ino %x nlink %d left %d createdPath %d size %d\n", li->next, li->files, li->fileMaps, (unsigned)li->dev, (unsigned)li->inode, li->nlink, li->linksLeft, li->createdPath, li->sb.st_size);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-10-24 21:46:51 +08:00
|
|
|
/**
|
|
|
|
* Read data from payload.
|
|
|
|
* @param cfd payload file handle
|
|
|
|
* @retval vbuf data from read
|
|
|
|
* @param amount no. bytes to read
|
|
|
|
* @return no. bytes read
|
|
|
|
*/
|
1999-10-28 07:18:10 +08:00
|
|
|
static inline off_t saferead(FD_t cfd, /*@out@*/void * vbuf, size_t amount)
|
2000-08-23 20:39:49 +08:00
|
|
|
/*@modifies cfd, *vbuf @*/
|
1999-09-18 05:31:38 +08:00
|
|
|
{
|
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;
|
1999-10-28 07:18:10 +08:00
|
|
|
|
1999-11-11 06:09:49 +08:00
|
|
|
nb = Fread(buf, sizeof(buf[0]), amount, cfd);
|
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;
|
|
|
|
}
|
|
|
|
|
2000-10-24 21:46:51 +08:00
|
|
|
/**
|
|
|
|
* Read data from payload and update number of bytes read.
|
|
|
|
* @param cfd payload file handle
|
|
|
|
* @retval buf data from read
|
|
|
|
* @param size no. bytes to read
|
|
|
|
* @return no. bytes read
|
|
|
|
*/
|
1999-10-28 07:18:10 +08:00
|
|
|
static inline off_t ourread(FD_t cfd, /*@out@*/void * buf, size_t size)
|
2000-08-23 20:39:49 +08:00
|
|
|
/*@modifies cfd, *buf @*/
|
1999-09-18 05:31:38 +08:00
|
|
|
{
|
1998-07-25 23:33:15 +08:00
|
|
|
off_t i = saferead(cfd, buf, size);
|
|
|
|
if (i > 0)
|
1999-11-02 22:33:14 +08:00
|
|
|
fdSetCpioPos(cfd, fdGetCpioPos(cfd) + i);
|
1997-01-04 10:48:55 +08:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2000-10-24 21:46:51 +08:00
|
|
|
/**
|
|
|
|
* Align input payload handle, skipping input bytes.
|
|
|
|
* @param cfd payload file handle
|
|
|
|
* @param modulo data alignment
|
|
|
|
*/
|
1999-10-28 07:18:10 +08:00
|
|
|
static inline void padinfd(FD_t cfd, int modulo)
|
2000-08-23 20:39:49 +08:00
|
|
|
/*@modifies cfd @*/
|
1999-09-18 05:31:38 +08:00
|
|
|
{
|
1997-01-04 10:48:55 +08:00
|
|
|
int buf[10];
|
|
|
|
int amount;
|
2000-03-23 23:49:50 +08:00
|
|
|
|
1999-11-02 22:33:14 +08:00
|
|
|
amount = (modulo - fdGetCpioPos(cfd) % modulo) % modulo;
|
1998-11-17 05:40:28 +08:00
|
|
|
(void)ourread(cfd, buf, amount);
|
1997-01-04 10:48:55 +08:00
|
|
|
}
|
|
|
|
|
2000-10-24 21:46:51 +08:00
|
|
|
/**
|
|
|
|
* Write data to payload.
|
|
|
|
* @param cfd payload file handle
|
|
|
|
* @param vbuf data to write
|
|
|
|
* @param amount no. bytes to write
|
|
|
|
* @return no. bytes written
|
|
|
|
*/
|
1999-10-28 07:18:10 +08:00
|
|
|
static inline off_t safewrite(FD_t cfd, const void * vbuf, size_t amount)
|
2000-08-23 20:39:49 +08:00
|
|
|
/*@modifies cfd @*/
|
1999-09-18 05:31:38 +08:00
|
|
|
{
|
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;
|
1999-10-28 07:18:10 +08:00
|
|
|
|
1999-11-11 06:09:49 +08:00
|
|
|
nb = Fwrite(buf, sizeof(buf[0]), amount, cfd);
|
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
|
|
|
}
|
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
return rc;
|
1997-07-24 02:07:46 +08:00
|
|
|
}
|
|
|
|
|
2000-10-24 21:46:51 +08:00
|
|
|
/**
|
|
|
|
* Align output payload handle, padding with zeroes.
|
|
|
|
* @param cfd payload file handle
|
|
|
|
* @param modulo data alignment
|
|
|
|
* @return 0 on success, CPIOERR_WRITE_FAILED
|
|
|
|
*/
|
1999-10-28 07:18:10 +08:00
|
|
|
static inline int padoutfd(FD_t cfd, size_t * where, int modulo)
|
2000-08-23 20:39:49 +08:00
|
|
|
/*@modifies cfd, *where @*/
|
1999-09-18 05:31:38 +08:00
|
|
|
{
|
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;
|
2000-03-23 23:49:50 +08:00
|
|
|
|
1997-07-24 02:07:46 +08:00
|
|
|
amount = (modulo - *where % modulo) % modulo;
|
|
|
|
*where += amount;
|
|
|
|
|
2000-03-23 23:49:50 +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;
|
|
|
|
}
|
|
|
|
|
2000-10-24 21:46:51 +08:00
|
|
|
/**
|
|
|
|
* Convert string to unsigned integer (with buffer size check).
|
|
|
|
* @param input string
|
|
|
|
* @retval address of 1st character not processed
|
|
|
|
* @param base numerical conversion base
|
|
|
|
* @param num max no. of bytes to read
|
|
|
|
* @return converted integer
|
|
|
|
*/
|
1999-09-18 05:31:38 +08:00
|
|
|
static int strntoul(const char *str, /*@out@*/char **endptr, int base, int num)
|
2000-08-23 20:39:49 +08:00
|
|
|
/*@modifies *endptr @*/
|
1999-09-18 05:31:38 +08:00
|
|
|
{
|
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
|
|
|
|
2000-10-24 21:46:51 +08:00
|
|
|
return ret;
|
1997-01-04 10:48:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
|
|
|
2000-10-24 21:46:51 +08:00
|
|
|
/**
|
|
|
|
* Process next cpio heasder.
|
|
|
|
* @param cfd payload file handle
|
|
|
|
* @retval hdr file name and stat info
|
|
|
|
* @return 0 on success
|
|
|
|
*/
|
2000-03-23 23:49:50 +08:00
|
|
|
static int getNextHeader(FD_t cfd, struct cpioHeader * hdr)
|
2000-08-23 20:39:49 +08:00
|
|
|
/*@modifies cfd, hdr->path, hdr->sb @*/
|
1999-09-18 05:31:38 +08:00
|
|
|
{
|
1997-01-04 10:48:55 +08:00
|
|
|
struct cpioCrcPhysicalHeader physHeader;
|
2000-03-23 23:49:50 +08:00
|
|
|
struct stat * st = &hdr->sb;
|
1997-01-04 10:48:55 +08:00
|
|
|
int nameSize;
|
|
|
|
char * end;
|
|
|
|
int major, minor;
|
|
|
|
|
2000-03-23 23:49:50 +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
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
GET_NUM_FIELD(physHeader.inode, st->st_ino);
|
|
|
|
GET_NUM_FIELD(physHeader.mode, st->st_mode);
|
|
|
|
GET_NUM_FIELD(physHeader.uid, st->st_uid);
|
|
|
|
GET_NUM_FIELD(physHeader.gid, st->st_gid);
|
|
|
|
GET_NUM_FIELD(physHeader.nlink, st->st_nlink);
|
|
|
|
GET_NUM_FIELD(physHeader.mtime, st->st_mtime);
|
|
|
|
GET_NUM_FIELD(physHeader.filesize, st->st_size);
|
1997-01-04 10:48:55 +08:00
|
|
|
|
|
|
|
GET_NUM_FIELD(physHeader.devMajor, major);
|
|
|
|
GET_NUM_FIELD(physHeader.devMinor, minor);
|
2000-03-23 23:49:50 +08:00
|
|
|
st->st_dev = /*@-shiftsigned@*/ makedev(major, minor) /*@=shiftsigned@*/ ;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
|
|
|
GET_NUM_FIELD(physHeader.rdevMajor, major);
|
|
|
|
GET_NUM_FIELD(physHeader.rdevMinor, minor);
|
2000-03-23 23:49:50 +08:00
|
|
|
st->st_rdev = /*@-shiftsigned@*/ makedev(major, minor) /*@=shiftsigned@*/ ;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
|
|
|
GET_NUM_FIELD(physHeader.namesize, nameSize);
|
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
{ char * t = xmalloc(nameSize + 1);
|
|
|
|
if (ourread(cfd, t, nameSize) != nameSize) {
|
|
|
|
free(t);
|
|
|
|
hdr->path = NULL;
|
|
|
|
return CPIOERR_BAD_HEADER;
|
|
|
|
}
|
|
|
|
hdr->path = t;
|
1997-01-04 10:48:55 +08:00
|
|
|
}
|
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
/* this is unecessary hdr->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)
|
|
|
|
{
|
2000-07-10 07:10:25 +08:00
|
|
|
const char * afn = ((const struct cpioFileMapping *)a)->archivePath;
|
|
|
|
const char * bfn = ((const struct cpioFileMapping *)b)->archivePath;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
2000-07-15 22:53:54 +08:00
|
|
|
/* Match payloads with ./ prefixes as well. */
|
2000-07-10 07:10:25 +08:00
|
|
|
if (afn[0] == '.' && afn[1] == '/') afn += 2;
|
|
|
|
if (bfn[0] == '.' && bfn[1] == '/') bfn += 2;
|
|
|
|
|
|
|
|
return strcmp(afn, bfn);
|
1997-01-04 10:48:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This could trash files in the path! I'm not sure that's a good thing */
|
2000-10-24 21:46:51 +08:00
|
|
|
/**
|
|
|
|
* @param path directory path
|
|
|
|
* @param perms directory permissions
|
|
|
|
* @return 0 on success
|
|
|
|
*/
|
2000-03-23 23:49:50 +08:00
|
|
|
static int createDirectory(const char * path, mode_t perms)
|
2000-08-23 20:39:49 +08:00
|
|
|
/*@modifies fileSystem @*/
|
1999-09-18 05:31:38 +08:00
|
|
|
{
|
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)) {
|
2000-03-23 23:49:50 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2000-10-24 21:46:51 +08:00
|
|
|
/**
|
|
|
|
* Set owner, group, and modify/access times.
|
|
|
|
* @param hdr file name and stat info
|
|
|
|
* @return 0 on success
|
|
|
|
*/
|
1999-09-18 05:31:38 +08:00
|
|
|
static int setInfo(struct cpioHeader * hdr)
|
2000-08-23 20:39:49 +08:00
|
|
|
/*@modifies fileSystem @*/
|
1999-09-18 05:31:38 +08:00
|
|
|
{
|
1997-01-04 10:48:55 +08:00
|
|
|
int rc = 0;
|
1998-03-05 00:52:59 +08:00
|
|
|
struct utimbuf stamp;
|
2000-03-23 23:49:50 +08:00
|
|
|
struct stat * st = &hdr->sb;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
stamp.actime = st->st_mtime;
|
|
|
|
stamp.modtime = st->st_mtime;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
if (!S_ISLNK(st->st_mode)) {
|
|
|
|
if (!getuid() && chown(hdr->path, st->st_uid, st->st_gid))
|
1998-12-06 07:22:41 +08:00
|
|
|
rc = CPIOERR_CHOWN_FAILED;
|
2000-03-23 23:49:50 +08:00
|
|
|
if (!rc && chmod(hdr->path, st->st_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
|
2000-03-23 23:49:50 +08:00
|
|
|
if (!getuid() && !rc && lchown(hdr->path, st->st_uid, st->st_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;
|
|
|
|
}
|
|
|
|
|
2000-10-24 21:46:51 +08:00
|
|
|
/**
|
|
|
|
* Create directories in file path (like "mkdir -p").
|
|
|
|
* @param filename file path
|
|
|
|
* @return 0 on success
|
|
|
|
*/
|
2000-08-23 20:39:49 +08:00
|
|
|
static int inline 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;
|
|
|
|
}
|
|
|
|
|
2000-10-24 21:46:51 +08:00
|
|
|
/**
|
|
|
|
* Create file from payload stream.
|
2000-12-03 05:53:44 +08:00
|
|
|
* @todo Legacy: support brokenEndian MD5 checks?
|
2000-10-24 21:46:51 +08:00
|
|
|
* @param cfd payload file handle
|
|
|
|
* @param hdr file name and stat info
|
|
|
|
* @param filemd5 file md5 sum
|
|
|
|
* @param cb callback function
|
|
|
|
* @param cbData callback private data
|
|
|
|
* @return 0 on success
|
|
|
|
*/
|
2000-07-10 07:10:25 +08:00
|
|
|
static int expandRegular(FD_t cfd, const struct cpioHeader * hdr,
|
2000-10-24 21:46:51 +08:00
|
|
|
const char * filemd5, cpioCallback cb, void * cbData)
|
2000-08-23 20:39:49 +08:00
|
|
|
/*@modifies fileSystem, cfd @*/
|
1999-09-18 05:31:38 +08:00
|
|
|
{
|
1999-10-28 07:18:10 +08:00
|
|
|
FD_t ofd;
|
1999-11-11 06:09:49 +08:00
|
|
|
char buf[BUFSIZ];
|
1997-01-04 10:48:55 +08:00
|
|
|
int bytesRead;
|
2000-05-18 20:11:51 +08:00
|
|
|
const struct stat * st = &hdr->sb;
|
2000-03-23 23:49:50 +08:00
|
|
|
int left = st->st_size;
|
1997-01-04 10:48:55 +08:00
|
|
|
int rc = 0;
|
1999-10-28 07:18:10 +08:00
|
|
|
struct cpioCallbackInfo cbInfo = { NULL, 0, 0, 0 };
|
1997-05-06 23:27:46 +08:00
|
|
|
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
|
|
|
|
1999-11-25 00:16:17 +08:00
|
|
|
ofd = Fopen(hdr->path, "w.ufdio");
|
|
|
|
if (ofd == NULL || Ferror(ofd))
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_OPEN_FAILED;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
2000-12-03 05:53:44 +08:00
|
|
|
/* XXX This doesn't support brokenEndian checks. */
|
2000-10-24 21:46:51 +08:00
|
|
|
if (filemd5)
|
2000-12-03 05:53:44 +08:00
|
|
|
fdInitMD5(ofd, 0);
|
2000-10-24 21:46:51 +08:00
|
|
|
|
1997-05-06 23:27:46 +08:00
|
|
|
cbInfo.file = hdr->path;
|
2000-03-23 23:49:50 +08:00
|
|
|
cbInfo.fileSize = st->st_size;
|
1997-05-06 23:27:46 +08:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
1999-11-11 06:09:49 +08:00
|
|
|
if (Fwrite(buf, sizeof(buf[0]), bytesRead, ofd) != 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) {
|
2000-03-23 23:49:50 +08:00
|
|
|
cbInfo.fileComplete = st->st_size - left;
|
1999-11-02 22:33:14 +08:00
|
|
|
cbInfo.bytesProcessed = fdGetCpioPos(cfd);
|
1997-05-06 23:27:46 +08:00
|
|
|
cb(&cbInfo, cbData);
|
|
|
|
}
|
1997-01-04 10:48:55 +08:00
|
|
|
}
|
|
|
|
|
2000-10-24 21:46:51 +08:00
|
|
|
if (filemd5) {
|
2000-10-29 01:16:25 +08:00
|
|
|
const char * md5sum = NULL;
|
2000-10-24 21:46:51 +08:00
|
|
|
|
|
|
|
Fflush(ofd);
|
|
|
|
fdFiniMD5(ofd, (void **)&md5sum, NULL, 1);
|
|
|
|
|
2000-10-29 01:16:25 +08:00
|
|
|
if (md5sum == NULL) {
|
2000-10-24 21:46:51 +08:00
|
|
|
rc = CPIOERR_MD5SUM_MISMATCH;
|
2000-10-29 01:16:25 +08:00
|
|
|
} else {
|
|
|
|
if (strcmp(md5sum, filemd5))
|
|
|
|
rc = CPIOERR_MD5SUM_MISMATCH;
|
2000-12-13 04:03:45 +08:00
|
|
|
free((void *)md5sum);
|
2000-10-29 01:16:25 +08:00
|
|
|
}
|
2000-10-24 21:46:51 +08:00
|
|
|
}
|
|
|
|
|
1999-10-28 07:18:10 +08:00
|
|
|
Fclose(ofd);
|
2000-03-23 23:49:50 +08:00
|
|
|
|
1997-01-04 10:48:55 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2000-10-24 21:46:51 +08:00
|
|
|
/**
|
|
|
|
* Create symlink from payload stream.
|
|
|
|
* @param cfd payload file handle
|
|
|
|
* @param hdr file name and stat info
|
|
|
|
* @return 0 on success
|
|
|
|
*/
|
2000-07-10 07:10:25 +08:00
|
|
|
static int expandSymlink(FD_t cfd, const struct cpioHeader * hdr)
|
2000-08-23 20:39:49 +08:00
|
|
|
/*@modifies fileSystem, cfd @*/
|
1999-09-18 05:31:38 +08:00
|
|
|
{
|
1997-07-16 09:56:14 +08:00
|
|
|
char buf[2048], buf2[2048];
|
1997-05-06 23:27:46 +08:00
|
|
|
struct stat sb;
|
2000-05-18 20:11:51 +08:00
|
|
|
const struct stat * st = &hdr->sb;
|
1997-07-16 09:56:14 +08:00
|
|
|
int len;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
if ((st->st_size + 1)> sizeof(buf))
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_HDR_SIZE;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
if (ourread(cfd, buf, st->st_size) != st->st_size)
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_READ_FAILED;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
buf[st->st_size] = '\0';
|
1997-01-04 10:48:55 +08:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2000-10-24 21:46:51 +08:00
|
|
|
/**
|
|
|
|
* Create fifo from payload stream.
|
|
|
|
* @param cfd payload file handle
|
|
|
|
* @param hdr file name and stat info
|
|
|
|
* @return 0 on success
|
|
|
|
*/
|
2000-07-10 07:10:25 +08:00
|
|
|
static int expandFifo( /*@unused@*/ FD_t cfd, const struct cpioHeader * hdr)
|
2000-08-23 20:39:49 +08:00
|
|
|
/*@modifies fileSystem @*/
|
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
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
return 0;
|
1997-01-04 10:48:55 +08:00
|
|
|
}
|
|
|
|
|
2000-10-24 21:46:51 +08:00
|
|
|
/**
|
|
|
|
* Create fifo from payload stream.
|
|
|
|
* @param cfd payload file handle
|
|
|
|
* @param hdr file name and stat info
|
|
|
|
* @return 0 on success
|
|
|
|
*/
|
2000-07-10 07:10:25 +08:00
|
|
|
static int expandDevice( /*@unused@*/ FD_t cfd, const struct cpioHeader * hdr)
|
2000-08-23 20:39:49 +08:00
|
|
|
/*@modifies fileSystem @*/
|
1999-09-18 05:31:38 +08:00
|
|
|
{
|
2000-05-18 20:11:51 +08:00
|
|
|
const struct stat * st = &hdr->sb;
|
1997-05-06 23:27:46 +08:00
|
|
|
struct stat sb;
|
|
|
|
|
1997-07-16 09:56:14 +08:00
|
|
|
if (!lstat(hdr->path, &sb)) {
|
2000-03-23 23:49:50 +08:00
|
|
|
if ((S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) &&
|
|
|
|
(sb.st_rdev == st->st_rdev))
|
1997-07-16 09:56:14 +08:00
|
|
|
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
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
if ( /*@-unrecog@*/ mknod(hdr->path, st->st_mode & (~0777), st->st_rdev) /*@=unrecog@*/ )
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_MKNOD_FAILED;
|
2000-03-23 23:49:50 +08:00
|
|
|
|
1997-01-04 10:48:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-10-24 21:46:51 +08:00
|
|
|
/**
|
|
|
|
* Create and initialize set of hard links.
|
|
|
|
* @param st link stat info
|
|
|
|
* @param hltype type of hard link set to create
|
|
|
|
* @return pointer to set of hard links
|
|
|
|
*/
|
2000-03-23 23:49:50 +08:00
|
|
|
static /*@only@*/ struct hardLink * newHardLink(const struct stat * st,
|
2000-08-23 20:39:49 +08:00
|
|
|
enum hardLinkType hltype) /*@*/
|
1999-09-18 05:31:38 +08:00
|
|
|
{
|
2000-03-23 23:49:50 +08:00
|
|
|
struct hardLink * li = xmalloc(sizeof(*li));
|
|
|
|
|
|
|
|
li->next = NULL;
|
|
|
|
li->nlink = st->st_nlink;
|
|
|
|
li->dev = st->st_dev;
|
|
|
|
li->inode = st->st_ino;
|
|
|
|
li->createdPath = -1;
|
|
|
|
|
|
|
|
switch (hltype) {
|
|
|
|
case HARDLINK_INSTALL:
|
|
|
|
li->linksLeft = st->st_nlink;
|
|
|
|
li->fileMaps = xmalloc(sizeof(li->fileMaps[0]) * st->st_nlink);
|
|
|
|
li->files = NULL;
|
|
|
|
break;
|
|
|
|
case HARDLINK_BUILD:
|
|
|
|
li->linksLeft = 0;
|
|
|
|
li->fileMaps = NULL;
|
|
|
|
li->files = xcalloc(st->st_nlink, sizeof(*li->files));
|
|
|
|
break;
|
|
|
|
}
|
2000-05-18 20:11:51 +08:00
|
|
|
|
|
|
|
{ struct stat * myst = (struct stat *) &li->sb;
|
|
|
|
*myst = *st; /* structure assignment */
|
|
|
|
}
|
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
return li;
|
|
|
|
}
|
1997-05-07 01:38:05 +08:00
|
|
|
|
2000-10-24 21:46:51 +08:00
|
|
|
/**
|
|
|
|
* Destroy set of hard links.
|
|
|
|
* @param li set of hard links
|
|
|
|
*/
|
2000-03-23 23:49:50 +08:00
|
|
|
static void freeHardLink( /*@only@*/ struct hardLink * li)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (li->files) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < li->nlink; i++) {
|
|
|
|
if (li->files[i] == NULL) continue;
|
|
|
|
/*@-unqualifiedtrans@*/ free((void *)li->files[i]) /*@=unqualifiedtrans@*/ ;
|
|
|
|
li->files[i] = NULL;
|
|
|
|
}
|
|
|
|
free(li->files);
|
|
|
|
li->files = NULL;
|
|
|
|
}
|
|
|
|
if (li->fileMaps) {
|
|
|
|
free(li->fileMaps);
|
|
|
|
li->fileMaps = NULL;
|
1997-05-07 01:38:05 +08:00
|
|
|
}
|
2000-03-23 23:49:50 +08:00
|
|
|
free(li);
|
1997-05-07 01:38:05 +08:00
|
|
|
}
|
|
|
|
|
2000-10-24 21:46:51 +08:00
|
|
|
/**
|
|
|
|
* Create hard links to existing file.
|
|
|
|
* @param li set of hard links
|
|
|
|
* @retval failedFile on error, file name that failed
|
|
|
|
* @return 0 on success
|
|
|
|
*/
|
2000-03-23 23:49:50 +08:00
|
|
|
static int createLinks(struct hardLink * li, /*@out@*/ const char ** failedFile)
|
2000-08-23 20:39:49 +08:00
|
|
|
/*@modifies fileSystem, *failedFile, li->files, li->linksLeft @*/
|
1999-09-18 05:31:38 +08:00
|
|
|
{
|
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;
|
2000-03-23 23:49:50 +08:00
|
|
|
if (li->files[i] == NULL) continue;
|
1997-05-07 01:38:05 +08:00
|
|
|
|
|
|
|
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-10-30 07:03:12 +08:00
|
|
|
/*@-unqualifiedtrans@*/ free((void *)li->files[i]) /*@=unqualifiedtrans@*/ ;
|
1997-05-07 01:38:05 +08:00
|
|
|
li->files[i] = NULL;
|
|
|
|
li->linksLeft--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-10-24 21:46:51 +08:00
|
|
|
/**
|
|
|
|
* Skip amount bytes on input payload stream.
|
|
|
|
* @param cfd payload file handle
|
|
|
|
* @param amount no. bytes to skip
|
|
|
|
* @return 0 on success
|
|
|
|
*/
|
1999-10-28 07:18:10 +08:00
|
|
|
static int eatBytes(FD_t cfd, int amount)
|
2000-08-23 20:39:49 +08:00
|
|
|
/*@modifies cfd @*/
|
1999-09-18 05:31:38 +08:00
|
|
|
{
|
1997-05-23 23:18:15 +08:00
|
|
|
char buf[4096];
|
|
|
|
int bite;
|
2000-03-23 23:49:50 +08:00
|
|
|
|
1997-05-23 23:18:15 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2000-10-24 21:46:51 +08:00
|
|
|
/** @todo Verify payload MD5 sum. */
|
2000-05-18 20:11:51 +08:00
|
|
|
int cpioInstallArchive(FD_t cfd, const 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)
|
|
|
|
{
|
2000-03-23 23:49:50 +08:00
|
|
|
struct cpioHeader ch, *hdr = &ch;
|
1997-01-04 10:48:55 +08:00
|
|
|
struct cpioFileMapping * map = NULL;
|
|
|
|
struct cpioFileMapping needle;
|
1999-10-28 07:18:10 +08:00
|
|
|
struct cpioCallbackInfo cbInfo = { NULL, 0, 0, 0 };
|
1997-05-07 01:38:05 +08:00
|
|
|
struct hardLink * links = NULL;
|
|
|
|
struct hardLink * li = NULL;
|
2000-04-16 04:21:15 +08:00
|
|
|
int rc = 0;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
2000-10-24 21:46:51 +08:00
|
|
|
#ifdef NOTYET
|
|
|
|
char * md5sum = NULL;
|
|
|
|
|
2000-12-03 05:53:44 +08:00
|
|
|
fdInitMD5(cfd, 0);
|
2000-10-24 21:46:51 +08:00
|
|
|
#endif
|
|
|
|
|
1999-11-02 22:33:14 +08:00
|
|
|
fdSetCpioPos(cfd, 0);
|
1999-09-18 05:31:38 +08:00
|
|
|
if (failedFile)
|
|
|
|
*failedFile = NULL;
|
1997-05-07 01:38:05 +08:00
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
memset(hdr, 0, sizeof(*hdr));
|
|
|
|
hdr->path = NULL;
|
2000-04-16 04:21:15 +08:00
|
|
|
do {
|
2000-03-23 23:49:50 +08:00
|
|
|
struct stat * st;
|
|
|
|
|
|
|
|
if (hdr->path) {
|
2000-12-13 04:03:45 +08:00
|
|
|
free((void *)hdr->path);
|
2000-03-23 23:49:50 +08:00
|
|
|
hdr->path = NULL;
|
|
|
|
}
|
|
|
|
if ((rc = getNextHeader(cfd, hdr))) {
|
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
|
|
|
}
|
2000-03-23 23:49:50 +08:00
|
|
|
st = &hdr->sb;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
if (!strcmp(hdr->path, TRAILER))
|
1997-01-04 10:48:55 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (mappings) {
|
2000-03-23 23:49:50 +08:00
|
|
|
needle.archivePath = hdr->path;
|
1997-01-04 10:48:55 +08:00
|
|
|
map = bsearch(&needle, mappings, numMappings, sizeof(needle),
|
|
|
|
cpioFileMapCmp);
|
|
|
|
}
|
|
|
|
|
1997-05-23 23:18:15 +08:00
|
|
|
if (mappings && !map) {
|
2000-03-23 23:49:50 +08:00
|
|
|
eatBytes(cfd, st->st_size);
|
1997-05-23 23:18:15 +08:00
|
|
|
} else {
|
1997-01-04 10:48:55 +08:00
|
|
|
if (map) {
|
|
|
|
if (map->mapFlags & CPIO_MAP_PATH) {
|
2000-12-13 04:03:45 +08:00
|
|
|
if (hdr->path) free((void *)hdr->path);
|
2000-03-23 23:49:50 +08:00
|
|
|
hdr->path = xstrdup(map->fsPath);
|
|
|
|
}
|
1997-01-04 10:48:55 +08:00
|
|
|
|
|
|
|
if (map->mapFlags & CPIO_MAP_MODE)
|
2000-03-23 23:49:50 +08:00
|
|
|
st->st_mode = map->finalMode;
|
1997-01-04 10:48:55 +08:00
|
|
|
if (map->mapFlags & CPIO_MAP_UID)
|
2000-03-23 23:49:50 +08:00
|
|
|
st->st_uid = map->finalUid;
|
1997-01-04 10:48:55 +08:00
|
|
|
if (map->mapFlags & CPIO_MAP_GID)
|
2000-03-23 23:49:50 +08:00
|
|
|
st->st_gid = map->finalGid;
|
1997-01-04 10:48:55 +08:00
|
|
|
}
|
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
/* This won't get hard linked symlinks right, but I can't seem
|
1997-05-07 01:38:05 +08:00
|
|
|
to create those anyway */
|
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
if (S_ISREG(st->st_mode) && st->st_nlink > 1) {
|
1997-05-07 01:38:05 +08:00
|
|
|
for (li = links; li; li = li->next) {
|
2000-03-23 23:49:50 +08:00
|
|
|
if (li->inode == st->st_ino && li->dev == st->st_dev) break;
|
1997-05-07 01:38:05 +08:00
|
|
|
}
|
|
|
|
|
1999-09-18 05:31:38 +08:00
|
|
|
if (li == NULL) {
|
2000-03-23 23:49:50 +08:00
|
|
|
li = newHardLink(st, HARDLINK_BUILD);
|
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
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
li->files[li->linksLeft++] = xstrdup(hdr->path);
|
1997-01-04 10:48:55 +08:00
|
|
|
}
|
2000-03-23 23:49:50 +08:00
|
|
|
|
|
|
|
if ((st->st_nlink > 1) && S_ISREG(st->st_mode) && !st->st_size &&
|
1997-05-07 01:38:05 +08:00
|
|
|
li->createdPath == -1) {
|
|
|
|
/* defer file creation */
|
2000-03-23 23:49:50 +08:00
|
|
|
} else if ((st->st_nlink > 1) && S_ISREG(st->st_mode) &&
|
1997-08-26 01:45:30 +08:00
|
|
|
(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 */
|
2000-03-23 23:49:50 +08:00
|
|
|
if (st->st_size) eatBytes(cfd, st->st_size);
|
1997-05-07 01:38:05 +08:00
|
|
|
} else {
|
2000-03-23 23:49:50 +08:00
|
|
|
rc = checkDirectory(hdr->path);
|
1997-05-07 01:38:05 +08:00
|
|
|
|
|
|
|
if (!rc) {
|
2000-03-23 23:49:50 +08:00
|
|
|
if (S_ISREG(st->st_mode))
|
2000-10-24 21:46:51 +08:00
|
|
|
rc = expandRegular(cfd, hdr, map->md5sum, cb, cbData);
|
2000-03-23 23:49:50 +08:00
|
|
|
else if (S_ISDIR(st->st_mode))
|
|
|
|
rc = createDirectory(hdr->path, 000);
|
|
|
|
else if (S_ISLNK(st->st_mode))
|
|
|
|
rc = expandSymlink(cfd, hdr);
|
|
|
|
else if (S_ISFIFO(st->st_mode))
|
|
|
|
rc = expandFifo(cfd, hdr);
|
|
|
|
else if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode))
|
|
|
|
rc = expandDevice(cfd, hdr);
|
|
|
|
else if (S_ISSOCK(st->st_mode)) {
|
1997-05-07 01:38:05 +08:00
|
|
|
/* this mimicks cpio but probably isnt' right */
|
2000-03-23 23:49:50 +08:00
|
|
|
rc = expandFifo(cfd, hdr);
|
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)
|
2000-03-23 23:49:50 +08:00
|
|
|
rc = setInfo(hdr);
|
1997-01-04 10:48:55 +08:00
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
if (S_ISREG(st->st_mode) && st->st_nlink > 1) {
|
2000-04-16 04:21:15 +08:00
|
|
|
li->createdPath = --li->linksLeft;
|
1997-05-07 01:38:05 +08:00
|
|
|
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) {
|
2000-04-16 04:21:15 +08:00
|
|
|
int olderrno;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
2000-04-16 04:21:15 +08:00
|
|
|
*failedFile = xstrdup(hdr->path);
|
|
|
|
olderrno = errno;
|
2000-03-23 23:49:50 +08:00
|
|
|
unlink(hdr->path);
|
2000-04-16 04:21:15 +08:00
|
|
|
errno = olderrno;
|
1997-01-04 10:48:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-07-25 23:33:15 +08:00
|
|
|
padinfd(cfd, 4);
|
1997-05-06 23:27:46 +08:00
|
|
|
|
|
|
|
if (!rc && cb) {
|
2000-03-23 23:49:50 +08:00
|
|
|
cbInfo.file = hdr->path;
|
|
|
|
cbInfo.fileSize = st->st_size;
|
|
|
|
cbInfo.fileComplete = st->st_size;
|
1999-11-02 22:33:14 +08:00
|
|
|
cbInfo.bytesProcessed = fdGetCpioPos(cfd);
|
1997-05-06 23:27:46 +08:00
|
|
|
cb(&cbInfo, cbData);
|
|
|
|
}
|
|
|
|
|
2000-04-16 04:21:15 +08:00
|
|
|
} while (rc == 0);
|
2000-03-23 23:49:50 +08:00
|
|
|
|
|
|
|
if (hdr->path) {
|
2000-12-13 04:03:45 +08:00
|
|
|
free((void *)hdr->path);
|
2000-03-23 23:49:50 +08:00
|
|
|
hdr->path = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create any remaining links (if no error), and clean up. */
|
|
|
|
while ((li = links) != NULL) {
|
|
|
|
links = li->next;
|
|
|
|
li->next = NULL;
|
1997-01-04 10:48:55 +08:00
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
if (rc == 0 && li->linksLeft) {
|
1997-05-07 01:38:05 +08:00
|
|
|
if (li->createdPath == -1)
|
1999-05-12 04:05:43 +08:00
|
|
|
rc = CPIOERR_MISSING_HARDLINK;
|
2000-03-23 23:49:50 +08:00
|
|
|
else
|
1997-05-07 01:38:05 +08:00
|
|
|
rc = createLinks(li, failedFile);
|
|
|
|
}
|
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
freeHardLink(li);
|
1997-05-07 01:38:05 +08:00
|
|
|
}
|
|
|
|
|
2000-10-24 21:46:51 +08:00
|
|
|
#ifdef NOTYET
|
|
|
|
fdFiniMD5(cfd, (void **)&md5sum, NULL, 1);
|
|
|
|
|
|
|
|
if (md5sum)
|
|
|
|
free(md5sum);
|
|
|
|
#endif
|
|
|
|
|
1997-01-04 10:48:55 +08:00
|
|
|
return rc;
|
|
|
|
}
|
1997-07-24 02:07:46 +08:00
|
|
|
|
2000-10-24 21:46:51 +08:00
|
|
|
/**
|
|
|
|
* Write next item to payload stream.
|
|
|
|
* @param cfd payload file handle
|
|
|
|
* @param st stat info for item
|
|
|
|
* @param map mapping name and flags for item
|
|
|
|
* @retval sizep address of no. bytes written
|
|
|
|
* @param writeData should data be written?
|
|
|
|
* @return 0 on success
|
|
|
|
*/
|
2000-05-18 20:11:51 +08:00
|
|
|
static int writeFile(FD_t cfd, const struct stat * st,
|
|
|
|
const struct cpioFileMapping * map, /*@out@*/ size_t * sizep,
|
|
|
|
int writeData)
|
2000-08-23 20:39:49 +08:00
|
|
|
/*@modifies cfd, *sizep @*/
|
1999-09-18 05:31:38 +08:00
|
|
|
{
|
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;
|
2000-05-18 20:11:51 +08:00
|
|
|
size_t st_size = st->st_size; /* XXX hard links need size preserved */
|
|
|
|
const char * archivePath;
|
|
|
|
mode_t st_mode = st->st_mode;
|
|
|
|
uid_t st_uid = st->st_uid;
|
|
|
|
gid_t st_gid = st->st_gid;
|
1997-07-25 21:57:17 +08:00
|
|
|
size_t size, amount = 0;
|
2000-04-16 04:21:15 +08:00
|
|
|
int rc;
|
1997-07-24 02:07:46 +08:00
|
|
|
|
2000-05-18 20:11:51 +08:00
|
|
|
archivePath = (!(map->mapFlags & CPIO_MAP_PATH))
|
|
|
|
? map->fsPath : map->archivePath;
|
|
|
|
|
1997-07-24 02:07:46 +08:00
|
|
|
if (map->mapFlags & CPIO_MAP_MODE)
|
2000-05-18 20:11:51 +08:00
|
|
|
st_mode = (st_mode & S_IFMT) | map->finalMode;
|
1997-07-24 02:07:46 +08:00
|
|
|
if (map->mapFlags & CPIO_MAP_UID)
|
2000-05-18 20:11:51 +08:00
|
|
|
st_uid = map->finalUid;
|
1997-07-24 02:07:46 +08:00
|
|
|
if (map->mapFlags & CPIO_MAP_GID)
|
2000-05-18 20:11:51 +08:00
|
|
|
st_gid = map->finalGid;
|
1997-07-24 02:07:46 +08:00
|
|
|
|
2000-05-18 20:11:51 +08:00
|
|
|
if (!writeData || S_ISDIR(st_mode)) {
|
|
|
|
st_size = 0;
|
|
|
|
} else if (S_ISLNK(st_mode)) {
|
1997-07-24 02:07:46 +08:00
|
|
|
/* While linux puts the size of a symlink in the st_size field,
|
|
|
|
I don't think that's a specified standard */
|
|
|
|
|
1999-11-24 08:03:54 +08:00
|
|
|
amount = Readlink(map->fsPath, symbuf, sizeof(symbuf));
|
1997-07-24 02:07:46 +08:00
|
|
|
if (amount <= 0) {
|
1998-12-06 07:22:41 +08:00
|
|
|
return CPIOERR_READLINK_FAILED;
|
1997-07-24 02:07:46 +08:00
|
|
|
}
|
|
|
|
|
2000-05-18 20:11:51 +08:00
|
|
|
st_size = amount;
|
1997-07-24 02:07:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(hdr.magic, CPIO_NEWC_MAGIC, sizeof(hdr.magic));
|
2000-03-23 23:49:50 +08:00
|
|
|
SET_NUM_FIELD(hdr.inode, st->st_ino, buf);
|
2000-05-18 20:11:51 +08:00
|
|
|
SET_NUM_FIELD(hdr.mode, st_mode, buf);
|
|
|
|
SET_NUM_FIELD(hdr.uid, st_uid, buf);
|
|
|
|
SET_NUM_FIELD(hdr.gid, st_gid, buf);
|
2000-03-23 23:49:50 +08:00
|
|
|
SET_NUM_FIELD(hdr.nlink, st->st_nlink, buf);
|
|
|
|
SET_NUM_FIELD(hdr.mtime, st->st_mtime, buf);
|
2000-05-18 20:11:51 +08:00
|
|
|
SET_NUM_FIELD(hdr.filesize, st_size, buf);
|
2000-03-23 23:49:50 +08:00
|
|
|
|
|
|
|
num = major((unsigned)st->st_dev); SET_NUM_FIELD(hdr.devMajor, num, buf);
|
|
|
|
num = minor((unsigned)st->st_dev); SET_NUM_FIELD(hdr.devMinor, num, buf);
|
|
|
|
num = major((unsigned)st->st_rdev); SET_NUM_FIELD(hdr.rdevMajor, num, buf);
|
|
|
|
num = minor((unsigned)st->st_rdev); SET_NUM_FIELD(hdr.rdevMinor, num, buf);
|
1997-07-24 02:07:46 +08:00
|
|
|
|
2000-05-18 20:11:51 +08:00
|
|
|
num = strlen(archivePath) + 1; SET_NUM_FIELD(hdr.namesize, num, buf);
|
1997-07-24 02:07:46 +08:00
|
|
|
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;
|
2000-05-18 20:11:51 +08:00
|
|
|
if ((rc = safewrite(cfd, 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;
|
2000-03-23 23:49:50 +08:00
|
|
|
|
2000-05-18 20:11:51 +08:00
|
|
|
if (writeData && S_ISREG(st_mode)) {
|
1999-01-14 06:10:00 +08:00
|
|
|
char *b;
|
|
|
|
#if HAVE_MMAP
|
|
|
|
void *mapped;
|
|
|
|
size_t nmapped;
|
|
|
|
#endif
|
|
|
|
|
1999-11-05 05:26:08 +08:00
|
|
|
/* XXX unbuffered mmap generates *lots* of fdio debugging */
|
1999-11-24 08:03:54 +08:00
|
|
|
datafd = Fopen(map->fsPath, "r.ufdio");
|
|
|
|
if (datafd == NULL || Ferror(datafd))
|
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;
|
2000-05-18 20:11:51 +08:00
|
|
|
mapped = mmap(NULL, st_size, PROT_READ, MAP_SHARED, Fileno(datafd), 0);
|
1999-01-14 06:10:00 +08:00
|
|
|
if (mapped != (void *)-1) {
|
|
|
|
b = (char *)mapped;
|
2000-05-18 20:11:51 +08:00
|
|
|
nmapped = st_size;
|
1999-01-14 06:10:00 +08:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
b = buf;
|
|
|
|
}
|
|
|
|
|
2000-05-18 20:11:51 +08:00
|
|
|
size += st_size;
|
1997-07-24 02:07:46 +08:00
|
|
|
|
2000-05-18 20:11:51 +08:00
|
|
|
while (st_size) {
|
1999-01-14 06:10:00 +08:00
|
|
|
#if HAVE_MMAP
|
|
|
|
if (mapped != (void *)-1) {
|
|
|
|
amount = nmapped;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
1999-11-11 06:09:49 +08:00
|
|
|
amount = Fread(b, sizeof(buf[0]),
|
2000-05-18 20:11:51 +08:00
|
|
|
(st_size > sizeof(buf) ? sizeof(buf) : st_size),
|
1999-11-11 06:09:49 +08:00
|
|
|
datafd);
|
1997-07-24 02:07:46 +08:00
|
|
|
if (amount <= 0) {
|
2000-04-16 04:21:15 +08:00
|
|
|
int olderrno = errno;
|
1999-10-28 07:18:10 +08:00
|
|
|
Fclose(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) {
|
2000-04-16 04:21:15 +08:00
|
|
|
int olderrno = errno;
|
1999-10-28 07:18:10 +08:00
|
|
|
Fclose(datafd);
|
1997-07-24 02:07:46 +08:00
|
|
|
errno = olderrno;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2000-05-18 20:11:51 +08:00
|
|
|
st_size -= amount;
|
1997-07-24 02:07:46 +08:00
|
|
|
}
|
1997-08-01 00:02:19 +08:00
|
|
|
|
1999-01-14 06:10:00 +08:00
|
|
|
#if HAVE_MMAP
|
|
|
|
if (mapped != (void *)-1) {
|
2000-03-23 23:49:50 +08:00
|
|
|
/*@-noeffect@*/ munmap(mapped, nmapped) /*@=noeffect@*/;
|
1999-01-14 06:10:00 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-10-28 07:18:10 +08:00
|
|
|
Fclose(datafd);
|
2000-05-18 20:11:51 +08:00
|
|
|
} else if (writeData && S_ISLNK(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;
|
|
|
|
}
|
|
|
|
|
2000-10-24 21:46:51 +08:00
|
|
|
/**
|
|
|
|
* Write set of linked files to payload stream.
|
|
|
|
* @param cfd payload file handle
|
|
|
|
* @param hlink set of linked files
|
|
|
|
* @param mappings mapping name and flags for linked files
|
|
|
|
* @param cb callback function
|
|
|
|
* @param cbData callback private data
|
|
|
|
* @retval sizep address of no. bytes written
|
|
|
|
* @retval failedFile on error, file name that failed
|
|
|
|
* @return 0 on success
|
|
|
|
*/
|
2000-05-18 20:11:51 +08:00
|
|
|
static int writeLinkedFile(FD_t cfd, const struct hardLink * hlink,
|
|
|
|
const struct cpioFileMapping * mappings,
|
1997-07-24 02:07:46 +08:00
|
|
|
cpioCallback cb, void * cbData,
|
1999-09-18 05:31:38 +08:00
|
|
|
/*@out@*/size_t * sizep,
|
|
|
|
/*@out@*/const char ** failedFile)
|
2000-08-23 20:39:49 +08:00
|
|
|
/*@modifies cfd, *sizep, *failedFile @*/
|
1999-09-18 05:31:38 +08:00
|
|
|
{
|
1997-07-24 02:07:46 +08:00
|
|
|
int i, rc;
|
1999-09-18 05:31:38 +08:00
|
|
|
size_t size, total;
|
1999-10-28 07:18:10 +08:00
|
|
|
struct cpioCallbackInfo cbInfo = { NULL, 0, 0, 0 };
|
1997-07-24 02:07:46 +08:00
|
|
|
|
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--) {
|
2000-03-23 23:49:50 +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) {
|
1999-10-28 07:18:10 +08:00
|
|
|
cbInfo.file = mappings[i].archivePath;
|
|
|
|
cb(&cbInfo, cbData);
|
1997-07-24 02:07:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
if ((rc = writeFile(cfd, &hlink->sb,
|
|
|
|
mappings + hlink->fileMaps[hlink->linksLeft],
|
1997-07-24 02:07:46 +08:00
|
|
|
&size, 1))) {
|
1999-09-18 05:31:38 +08:00
|
|
|
if (sizep)
|
|
|
|
*sizep = total;
|
2000-03-23 23:49:50 +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) {
|
1999-10-28 07:18:10 +08:00
|
|
|
cbInfo.file = mappings[i].archivePath;
|
|
|
|
cb(&cbInfo, cbData);
|
1997-07-24 02:07:46 +08:00
|
|
|
}
|
|
|
|
|
1997-08-01 00:25:34 +08:00
|
|
|
return 0;
|
1997-07-24 02:07:46 +08:00
|
|
|
}
|
|
|
|
|
2000-05-18 20:11:51 +08:00
|
|
|
int cpioBuildArchive(FD_t cfd, const 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;
|
1999-10-28 07:18:10 +08:00
|
|
|
struct cpioCallbackInfo cbInfo = { NULL, 0, 0, 0 };
|
1997-07-24 02:07:46 +08:00
|
|
|
struct cpioCrcPhysicalHeader hdr;
|
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@*/
|
2000-05-18 20:11:51 +08:00
|
|
|
struct stat * st = (struct stat *) &hlinkList.sb;
|
2000-03-23 23:49:50 +08:00
|
|
|
struct hardLink * hlink;
|
1997-07-24 02:07:46 +08:00
|
|
|
|
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++) {
|
2000-05-18 20:11:51 +08:00
|
|
|
const struct cpioFileMapping * map;
|
|
|
|
|
|
|
|
map = mappings + i;
|
|
|
|
|
|
|
|
if (map->mapFlags & CPIO_FOLLOW_SYMLINKS)
|
|
|
|
rc = Stat(map->fsPath, st);
|
1997-07-31 22:04:56 +08:00
|
|
|
else
|
2000-05-18 20:11:51 +08:00
|
|
|
rc = Lstat(map->fsPath, st);
|
1997-07-31 22:04:56 +08:00
|
|
|
|
1997-08-01 00:02:19 +08:00
|
|
|
if (rc) {
|
1999-09-18 05:31:38 +08:00
|
|
|
if (failedFile)
|
2000-05-18 20:11:51 +08:00
|
|
|
*failedFile = xstrdup(map->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
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
|
1997-07-24 02:07:46 +08:00
|
|
|
hlink = hlinkList.next;
|
2000-03-23 23:49:50 +08:00
|
|
|
while (hlink &&
|
|
|
|
(hlink->dev != st->st_dev || hlink->inode != st->st_ino))
|
|
|
|
hlink = hlink->next;
|
|
|
|
if (hlink == NULL) {
|
|
|
|
hlink = newHardLink(st, HARDLINK_INSTALL);
|
1997-07-24 02:07:46 +08:00
|
|
|
hlink->next = hlinkList.next;
|
|
|
|
hlinkList.next = hlink;
|
|
|
|
}
|
|
|
|
|
|
|
|
hlink->fileMaps[--hlink->linksLeft] = i;
|
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
if (hlink->linksLeft == 0) {
|
|
|
|
struct hardLink * prev;
|
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;
|
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
prev = &hlinkList;
|
|
|
|
do {
|
|
|
|
if (prev->next != hlink)
|
|
|
|
continue;
|
|
|
|
prev->next = hlink->next;
|
|
|
|
hlink->next = NULL;
|
|
|
|
freeHardLink(hlink);
|
|
|
|
hlink = NULL;
|
|
|
|
break;
|
|
|
|
} while ((prev = prev->next) != NULL);
|
1997-07-24 02:07:46 +08:00
|
|
|
}
|
|
|
|
} else {
|
2000-05-18 20:11:51 +08:00
|
|
|
if ((rc = writeFile(cfd, st, map, &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) {
|
2000-05-18 20:11:51 +08:00
|
|
|
cbInfo.file = map->archivePath;
|
1999-10-28 07:18:10 +08:00
|
|
|
cb(&cbInfo, cbData);
|
1997-07-24 02:07:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
totalsize += size;
|
|
|
|
}
|
2000-03-23 23:49:50 +08:00
|
|
|
}
|
1997-07-24 02:07:46 +08:00
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
rc = 0;
|
|
|
|
while ((hlink = hlinkList.next) != NULL) {
|
|
|
|
hlinkList.next = hlink->next;
|
|
|
|
hlink->next = NULL;
|
1997-10-04 00:08:20 +08:00
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
if (rc == 0) {
|
|
|
|
rc = writeLinkedFile(cfd, hlink, mappings, cb, cbData,
|
|
|
|
&size, failedFile);
|
|
|
|
totalsize += size;
|
|
|
|
}
|
|
|
|
freeHardLink(hlink);
|
1997-07-24 02:07:46 +08:00
|
|
|
}
|
2000-03-23 23:49:50 +08:00
|
|
|
if (rc)
|
|
|
|
return rc;
|
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 */
|
2000-03-23 23:49:50 +08:00
|
|
|
|
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;
|
2000-10-24 21:46:51 +08:00
|
|
|
case CPIOERR_MD5SUM_MISMATCH: s = _("MD5 sum mismatch"); break;
|
1999-05-12 04:05:43 +08:00
|
|
|
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);
|
|
|
|
}
|
2000-01-25 06:13:45 +08:00
|
|
|
if ((rc & CPIOERR_CHECK_ERRNO) && myerrno) {
|
1998-12-06 07:22:41 +08:00
|
|
|
s = _(" failed - ");
|
|
|
|
if (l > 0) strncat(msg, s, l);
|
|
|
|
l -= strlen(s);
|
|
|
|
if (l > 0) strncat(msg, strerror(myerrno), l);
|
|
|
|
}
|
|
|
|
return msg;
|
|
|
|
}
|