Exemplo n.º 1
0
static int getNextHeader(struct ourfd * fd, struct cpioHeader * chPtr,
                         struct cpioCrcPhysicalHeader * physHeaderPtr) {
    struct cpioCrcPhysicalHeader physHeader;
    int nameSize;
    char * end;
    int major, minor;

    if (ourread(fd, &physHeader, PHYS_HDR_SIZE) != PHYS_HDR_SIZE)
        return CPIOERR_READ_FAILED;

    if (physHeaderPtr)
        memcpy(physHeaderPtr, &physHeader, PHYS_HDR_SIZE);

    if (strncmp(CPIO_CRC_MAGIC, physHeader.magic, strlen(CPIO_CRC_MAGIC)) &&
            strncmp(CPIO_NEWC_MAGIC, physHeader.magic, strlen(CPIO_NEWC_MAGIC)))
        return CPIOERR_BAD_MAGIC;

    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);
    chPtr->dev = makedev(major, minor);

    GET_NUM_FIELD(physHeader.rdevMajor, major);
    GET_NUM_FIELD(physHeader.rdevMinor, minor);
    chPtr->rdev = makedev(major, minor);

    GET_NUM_FIELD(physHeader.namesize, nameSize);

    chPtr->path = malloc(nameSize + 1);
    if (ourread(fd, chPtr->path, nameSize) != nameSize) {
        free(chPtr->path);
        return CPIOERR_BAD_HEADER;
    }

    /* this is unecessary chPtr->path[nameSize] = '\0'; */

    padinfd(fd, 4);

    return 0;
}
Exemplo n.º 2
0
Arquivo: cpio.c Projeto: akozumpl/rpm
int cpioHeaderRead(FSM_t fsm, struct stat * st)
{
    struct cpioCrcPhysicalHeader hdr;
    int nameSize;
    char * end;
    unsigned int major, minor;
    int rc = 0;
    char * path = NULL;

    fsm->wrlen = PHYS_HDR_SIZE;
    rc = fsmNext(fsm, FSM_DREAD);
    if (!rc && fsm->rdnb != fsm->wrlen)
	rc = CPIOERR_READ_FAILED;
    if (rc) return rc;
    memcpy(&hdr, fsm->wrbuf, fsm->rdnb);

    if (strncmp(CPIO_CRC_MAGIC, hdr.magic, sizeof(CPIO_CRC_MAGIC)-1) &&
	strncmp(CPIO_NEWC_MAGIC, hdr.magic, sizeof(CPIO_NEWC_MAGIC)-1))
	return CPIOERR_BAD_MAGIC;

    GET_NUM_FIELD(hdr.inode, st->st_ino);
    GET_NUM_FIELD(hdr.mode, st->st_mode);
    GET_NUM_FIELD(hdr.uid, st->st_uid);
    GET_NUM_FIELD(hdr.gid, st->st_gid);
    GET_NUM_FIELD(hdr.nlink, st->st_nlink);
    GET_NUM_FIELD(hdr.mtime, st->st_mtime);
    GET_NUM_FIELD(hdr.filesize, st->st_size);

    GET_NUM_FIELD(hdr.devMajor, major);
    GET_NUM_FIELD(hdr.devMinor, minor);
    st->st_dev = makedev(major, minor);

    GET_NUM_FIELD(hdr.rdevMajor, major);
    GET_NUM_FIELD(hdr.rdevMinor, minor);
    st->st_rdev = makedev(major, minor);

    GET_NUM_FIELD(hdr.namesize, nameSize);
    if (nameSize >= fsm->wrsize)
	return CPIOERR_BAD_HEADER;

    fsm->wrlen = nameSize;
    rc = fsmNext(fsm, FSM_DREAD);
    if (!rc && fsm->rdnb != fsm->wrlen)
	rc = CPIOERR_BAD_HEADER;

    if (!rc) {
	path = xmalloc(nameSize + 1);
	memcpy(path, fsm->wrbuf, fsm->rdnb);
	path[nameSize] = '\0';
    }
    fsm->path = path;
    return rc;
}
Exemplo n.º 3
0
Arquivo: cpio.c Projeto: junaruga/rpm
int rpmcpioHeaderRead(rpmcpio_t cpio, char ** path, int * fx)
{
    struct cpioCrcPhysicalHeader hdr;
    int nameSize;
    char * end;
    int rc = 0;
    ssize_t read;
    char magic[6];
    rpm_loff_t fsize;

    if ((cpio->mode & O_ACCMODE) != O_RDONLY) {
        return RPMERR_READ_FAILED;
    }

    /* Move to next file */
    if (cpio->fileend != cpio->offset) {
        /* XXX try using Fseek() - which is currently broken */
        char buf[8*BUFSIZ];
        while (cpio->fileend != cpio->offset) {
            read = cpio->fileend - cpio->offset > 8*BUFSIZ ? 8*BUFSIZ : cpio->fileend - cpio->offset;
            if (rpmcpioRead(cpio, &buf, read) != read) {
                return RPMERR_READ_FAILED;
            }
        }
    }

    rc = rpmcpioReadPad(cpio);
    if (rc) return rc;

    read = Fread(&magic, 6, 1, cpio->fd);
    cpio->offset += read;
    if (read != 6)
	return RPMERR_BAD_MAGIC;

    /* read stripped header */
    if (!strncmp(CPIO_STRIPPED_MAGIC, magic,
                 sizeof(CPIO_STRIPPED_MAGIC)-1)) {
        struct cpioStrippedPhysicalHeader shdr;
        read = Fread(&shdr, STRIPPED_PHYS_HDR_SIZE, 1, cpio->fd);
        cpio->offset += read;
        if (read != STRIPPED_PHYS_HDR_SIZE)
	    return RPMERR_BAD_HEADER;

        GET_NUM_FIELD(shdr.fx, *fx);
        rc = rpmcpioReadPad(cpio);

        if (!rc && *fx == -1)
            rc = RPMERR_ITER_END;
        return rc;
    }

    if (strncmp(CPIO_CRC_MAGIC, magic, sizeof(CPIO_CRC_MAGIC)-1) &&
	strncmp(CPIO_NEWC_MAGIC, magic, sizeof(CPIO_NEWC_MAGIC)-1)) {
	return RPMERR_BAD_MAGIC;
    }

    read = Fread(&hdr, PHYS_HDR_SIZE, 1, cpio->fd);
    cpio->offset += read;
    if (read != PHYS_HDR_SIZE)
        return RPMERR_BAD_HEADER;

    GET_NUM_FIELD(hdr.filesize, fsize);
    GET_NUM_FIELD(hdr.namesize, nameSize);
    if (nameSize <= 0 || nameSize > 4096) {
        return RPMERR_BAD_HEADER;
    }

    char name[nameSize + 1];
    read = Fread(name, nameSize, 1, cpio->fd);
    name[nameSize] = '\0';
    cpio->offset += read;
    if (read != nameSize ) {
        return RPMERR_BAD_HEADER;
    }

    rc = rpmcpioReadPad(cpio);
    cpio->fileend = cpio->offset + fsize;

    if (!rc && rstreq(name, CPIO_TRAILER))
	rc = RPMERR_ITER_END;

    if (!rc && path)
	*path = xstrdup(name);

    return rc;
}