Пример #1
0
void merge(head *startMemory, head *maxAdress){
	
	head *history = startMemory, *parser = getNextHeader(startMemory, maxAdress);
	
	while(history != NULL && parser != NULL){
		if(parser->alloc == 0 && history->alloc == 0){
			history->size = (parser->size)+(history->size);
		}
		else {
			history = parser;
		}
		parser =  getNextHeader(startMemory, maxAdress);
	}
}
Пример #2
0
int myCpioFilterArchive(gzFile inStream, gzFile outStream, char ** patterns) {
    struct ourfd inFd, outFd;
    char ** aPattern;
    struct cpioHeader ch;
    int rc;
    struct cpioCrcPhysicalHeader pHeader;

    inFd.fd = inStream;
    inFd.pos = 0;
    outFd.fd = outStream;
    outFd.pos = 0;

    do {
        if ((rc = getNextHeader(&inFd, &ch, &pHeader))) {
            fprintf(stderr, _("error %d reading header: %s\n"), rc,
                    myCpioStrerror(rc));
            return CPIOERR_BAD_HEADER;
        }

        if (!strcmp(ch.path, TRAILER)) {
            free(ch.path);
            break;
        }

        for (aPattern = patterns; *aPattern; aPattern++)
            if (!fnmatch(*aPattern, ch.path, FNM_PATHNAME | FNM_PERIOD))
                break;

        if (!*aPattern)
            eatBytes(&inFd, ch.size);
        else
            copyFile(&inFd, &outFd, &ch, &pHeader);

        padinfd(&inFd, 4);

        free(ch.path);
    } while (1 && !rc);

    memset(&pHeader, '0', sizeof(pHeader));
    memcpy(pHeader.magic, CPIO_NEWC_MAGIC, sizeof(pHeader.magic));
    memcpy(pHeader.nlink, "00000001", 8);
    memcpy(pHeader.namesize, "0000000b", 8);
    gzip_write(outFd.fd, &pHeader, PHYS_HDR_SIZE);
    gzip_write(outFd.fd, "TRAILER!!!", 11);

    outFd.pos += PHYS_HDR_SIZE + 11;

    if ((rc = padoutfd(&outFd, &outFd.pos, 4)))
        return rc;

    if ((rc = padoutfd(&outFd, &outFd.pos, 512)))
        return rc;

    return 0;
}
Пример #3
0
int *insert(int elem, int * startofmemory, int heapsize){ //elem est en bits !!!!
	int* current = startofmemory;
	while(current != NULL){
		if(elem<=getSize(*current)){
			if(getSize(*current)-(elem/4)-8 > 0){
				int newHeader = getHeader((unsigned int)getSize(*current)-(elem/4)-8,0);
				*(current+(elem/4)) = newHeader;
				*current = getHeader((unsigned int)elem,1);
			}
			current += 8;
			return current;
		}
		current = getNextHeader(current,heapsize,startofmemory); // last step
	}
	return NULL; //allocation impossible 
}
Пример #4
0
head *insert(int elem, head *startMem,head *maxAdress){		//heapsize et elem sont en BYTES !!
	//printf("\n%i\t%p\t%p\n",elem, startMem, maxAdress);	//debug
	if(startMem == NULL || startMem + elem/4 > maxAdress)
		return NULL;
	if(startMem->size-4 >= elem){
															//il y a la place
		if(startMem->size >= elem+8){						//on doit insérer un nouveau header
			head newOne = {startMem->size-4-elem,0,0};
			*(startMem+4+elem) = newOne;
			startMem->size = elem+4;
		}
		startMem->alloc = 1;
		return startMem+1;
	}
															//il n'y a pas de place ici, on va chercher la suivante
	return insert(elem, getNextHeader(startMem, maxAdress), maxAdress);
}
Пример #5
0
int myCpioInstallArchive(gzFile stream, struct cpioFileMapping * mappings,
                         int numMappings, cpioCallback cb, void * cbData,
                         const char ** failedFile) {
    struct cpioHeader ch;
    struct ourfd fd;
    int rc = 0;
    int linkNum = 0;
    struct cpioFileMapping * map = NULL;
    struct cpioFileMapping needle;
    mode_t cpioMode;
    int olderr;
    struct cpioCallbackInfo cbInfo;
    struct hardLink * links = NULL;
    struct hardLink * li = NULL;

    fd.fd = stream;
    fd.pos = 0;

    *failedFile = NULL;

    do {
        if ((rc = getNextHeader(&fd, &ch, NULL))) {
            fprintf(stderr, _("error %d reading header: %s\n"), rc,
                    myCpioStrerror(rc));
            return CPIOERR_BAD_HEADER;
        }

        if (!strcmp(ch.path, TRAILER)) {
            free(ch.path);
            break;
        }

        if (mappings) {
            needle.archivePath = ch.path;
            map = bsearch(&needle, mappings, numMappings, sizeof(needle),
                          myCpioFileMapCmp);
        }

        if (mappings && !map) {
            eatBytes(&fd, ch.size);
        } else {
            cpioMode = ch.mode;

            if (map) {
                if (map->mapFlags & CPIO_MAP_PATH) {
                    free(ch.path);
                    ch.path = strdup(map->fsPath);
                }

                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;
            }

            /* This won't get hard linked symlinks right, but I can't seem
               to create those anyway */

            if (S_ISREG(ch.mode) && ch.nlink > 1) {
                li = links;
                for (li = links; li; li = li->next) {
                    if (li->inode == ch.inode && li->dev == ch.dev) break;
                }

                if (!li) {
                    li = malloc(sizeof(*li));
                    li->inode = ch.inode;
                    li->dev = ch.dev;
                    li->nlink = ch.nlink;
                    li->linksLeft = ch.nlink;
                    li->createdPath = -1;
                    li->files = calloc(sizeof(char *), li->nlink);
                    li->next = links;
                    links = li;
                }

                for (linkNum = 0; linkNum < li->nlink; linkNum++)
                    if (!li->files[linkNum]) break;
                li->files[linkNum] = strdup(ch.path);
            }

            if ((ch.nlink > 1) && S_ISREG(ch.mode) && !ch.size &&
                    li->createdPath == -1) {
                /* defer file creation */
            } else if ((ch.nlink > 1) && S_ISREG(ch.mode) &&
                       (li->createdPath != -1)) {
                createLinks(li, failedFile);

                /* 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 */
                if (ch.size) eatBytes(&fd, ch.size);
            } else {
                rc = checkDirectory(ch.path);

                if (!rc) {
                    if (S_ISREG(ch.mode))
                        rc = expandRegular(&fd, &ch, cb, cbData);
                    else if (S_ISDIR(ch.mode))
                        rc = createDirectory(ch.path, 000);
                    else if (S_ISLNK(ch.mode))
                        rc = expandSymlink(&fd, &ch);
                    else if (S_ISFIFO(ch.mode))
                        rc = expandFifo(&fd, &ch);
                    else if (S_ISCHR(ch.mode) || S_ISBLK(ch.mode))
                        rc = expandDevice(&fd, &ch);
                    else if (S_ISSOCK(ch.mode)) {
                        /* this mimicks cpio but probably isnt' right */
                        rc = expandFifo(&fd, &ch);
                    } else {
                        rc = CPIOERR_INTERNAL;
                    }
                }

                if (!rc)
                    rc = setInfo(&ch);

                if (S_ISREG(ch.mode) && ch.nlink > 1) {
                    li->createdPath = linkNum;
                    li->linksLeft--;
                    rc = createLinks(li, failedFile);
                }
            }

            if (rc && !*failedFile) {
                *failedFile = strdup(ch.path);

                olderr = errno;
                unlink(ch.path);
                errno = olderr;
            }
        }

        padinfd(&fd, 4);

        if (!rc && cb) {
            cbInfo.file = ch.path;
            cbInfo.fileSize = ch.size;
            cbInfo.fileComplete = ch.size;
            cbInfo.bytesProcessed = fd.pos;
            cb(&cbInfo, cbData);
        }

        free(ch.path);
    } while (1 && !rc);

    li = links;
    while (li && !rc) {
        if (li->linksLeft) {
            if (li->createdPath == -1)
                rc = CPIOERR_INTERNAL;
            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);
    }

    return rc;
}