static HANDLE zlib_file_open(const char *path, const char *mode) { gzFile ret = gzopen(path, mode); if (ret) { gzsetparams(ret, Z_DEFAULT_COMPRESSION, Z_DEFAULT_STRATEGY); } return ret; }
int gzstreambuf::set_gzparams(int level, int strategy) { if (file) { return gzsetparams(file,level,strategy); } else { return Z_BUF_ERROR; } }
value mlgz_gzsetparams(value chan, value comp, value strat) { static const int c_strat[] = { Z_DEFAULT_STRATEGY, Z_FILTERED, Z_HUFFMAN_ONLY } ; gzFile str = Gzfile_val(chan) ; int res ; res = gzsetparams(str, Int_val(comp), c_strat[ Int_val(strat) ]); if(res<0) mlgz_error(str); return Val_unit; }
static TACommandVerdict gzsetparams_cmd(TAThread thread,TAInputStream stream) { void* file; int res, level, strategy; file = readPointer(&stream); level = readInt(&stream); strategy = readInt(&stream); START_TARGET_OPERATION(thread); res = gzsetparams(file, level, strategy); END_TARGET_OPERATION(thread); writeInt(thread, res); sendResponse(thread); return taDefaultVerdict; }
/* * Receive a tar format file from the connection to the server, and write * the data from this file directly into a tar file. If compression is * enabled, the data will be compressed while written to the file. * * The file will be named base.tar[.gz] if it's for the main data directory * or <tablespaceoid>.tar[.gz] if it's for another tablespace. * * No attempt to inspect or validate the contents of the file is done. */ static void ReceiveTarFile(PGconn *conn, PGresult *res, int rownum) { char filename[MAXPGPATH]; char *copybuf = NULL; FILE *tarfile = NULL; #ifdef HAVE_LIBZ gzFile ztarfile = NULL; #endif if (PQgetisnull(res, rownum, 0)) { /* * Base tablespaces */ if (strcmp(basedir, "-") == 0) { #ifdef HAVE_LIBZ if (compresslevel != 0) { ztarfile = gzdopen(dup(fileno(stdout)), "wb"); if (gzsetparams(ztarfile, compresslevel, Z_DEFAULT_STRATEGY) != Z_OK) { fprintf(stderr, _("%s: could not set compression level %d: %s\n"), progname, compresslevel, get_gz_error(ztarfile)); disconnect_and_exit(1); } } else #endif tarfile = stdout; } else { #ifdef HAVE_LIBZ if (compresslevel != 0) { snprintf(filename, sizeof(filename), "%s/base.tar.gz", basedir); ztarfile = gzopen(filename, "wb"); if (gzsetparams(ztarfile, compresslevel, Z_DEFAULT_STRATEGY) != Z_OK) { fprintf(stderr, _("%s: could not set compression level %d: %s\n"), progname, compresslevel, get_gz_error(ztarfile)); disconnect_and_exit(1); } } else #endif { snprintf(filename, sizeof(filename), "%s/base.tar", basedir); tarfile = fopen(filename, "wb"); } } } else { /* * Specific tablespace */ #ifdef HAVE_LIBZ if (compresslevel != 0) { snprintf(filename, sizeof(filename), "%s/%s.tar.gz", basedir, PQgetvalue(res, rownum, 0)); ztarfile = gzopen(filename, "wb"); if (gzsetparams(ztarfile, compresslevel, Z_DEFAULT_STRATEGY) != Z_OK) { fprintf(stderr, _("%s: could not set compression level %d: %s\n"), progname, compresslevel, get_gz_error(ztarfile)); disconnect_and_exit(1); } } else #endif { snprintf(filename, sizeof(filename), "%s/%s.tar", basedir, PQgetvalue(res, rownum, 0)); tarfile = fopen(filename, "wb"); } } #ifdef HAVE_LIBZ if (compresslevel != 0) { if (!ztarfile) { /* Compression is in use */ fprintf(stderr, _("%s: could not create compressed file \"%s\": %s\n"), progname, filename, get_gz_error(ztarfile)); disconnect_and_exit(1); } } else #endif { /* Either no zlib support, or zlib support but compresslevel = 0 */ if (!tarfile) { fprintf(stderr, _("%s: could not create file \"%s\": %s\n"), progname, filename, strerror(errno)); disconnect_and_exit(1); } } /* * Get the COPY data stream */ res = PQgetResult(conn); if (PQresultStatus(res) != PGRES_COPY_OUT) { fprintf(stderr, _("%s: could not get COPY data stream: %s"), progname, PQerrorMessage(conn)); disconnect_and_exit(1); } while (1) { int r; if (copybuf != NULL) { PQfreemem(copybuf); copybuf = NULL; } r = PQgetCopyData(conn, ©buf, 0); if (r == -1) { /* * End of chunk. Close file (but not stdout). * * Also, write two completely empty blocks at the end of the tar * file, as required by some tar programs. */ char zerobuf[1024]; MemSet(zerobuf, 0, sizeof(zerobuf)); #ifdef HAVE_LIBZ if (ztarfile != NULL) { if (gzwrite(ztarfile, zerobuf, sizeof(zerobuf)) != sizeof(zerobuf)) { fprintf(stderr, _("%s: could not write to compressed file \"%s\": %s\n"), progname, filename, get_gz_error(ztarfile)); disconnect_and_exit(1); } } else #endif { if (fwrite(zerobuf, sizeof(zerobuf), 1, tarfile) != 1) { fprintf(stderr, _("%s: could not write to file \"%s\": %s\n"), progname, filename, strerror(errno)); disconnect_and_exit(1); } } #ifdef HAVE_LIBZ if (ztarfile != NULL) { if (gzclose(ztarfile) != 0) { fprintf(stderr, _("%s: could not close compressed file \"%s\": %s\n"), progname, filename, get_gz_error(ztarfile)); disconnect_and_exit(1); } } else #endif { if (strcmp(basedir, "-") != 0) { if (fclose(tarfile) != 0) { fprintf(stderr, _("%s: could not close file \"%s\": %s\n"), progname, filename, strerror(errno)); disconnect_and_exit(1); } } } break; } else if (r == -2) { fprintf(stderr, _("%s: could not read COPY data: %s"), progname, PQerrorMessage(conn)); disconnect_and_exit(1); } #ifdef HAVE_LIBZ if (ztarfile != NULL) { if (gzwrite(ztarfile, copybuf, r) != r) { fprintf(stderr, _("%s: could not write to compressed file \"%s\": %s\n"), progname, filename, get_gz_error(ztarfile)); disconnect_and_exit(1); } } else #endif { if (fwrite(copybuf, r, 1, tarfile) != 1) { fprintf(stderr, _("%s: could not write to file \"%s\": %s\n"), progname, filename, strerror(errno)); disconnect_and_exit(1); } } totaldone += r; if (showprogress) progress_report(rownum, filename); } /* while (1) */ if (copybuf != NULL) PQfreemem(copybuf); }
// Set compression level and strategy int gzfilebuf::setcompression(int comp_level, int comp_strategy) { return gzsetparams(file, comp_level, comp_strategy); }
int emu_SaveLoadGame(int load, int sram) { int ret = 0; char *saveFname; // make save filename saveFname = emu_GetSaveFName(load, sram, state_slot); if (saveFname == NULL) { if (!sram) { strcpy(noticeMsg, load ? "LOAD FAILED (missing file)" : "SAVE FAILED "); emu_noticeMsgUpdated(); } return -1; } lprintf("saveLoad (%i, %i): %s\n", load, sram, saveFname); if (sram) { FILE *sramFile; int sram_size; unsigned char *sram_data; int truncate = 1; if (PicoMCD&1) { if (PicoOpt&0x8000) { // MCD RAM cart? sram_size = 0x12000; sram_data = SRam.data; if (sram_data) memcpy32((int *)sram_data, (int *)Pico_mcd->bram, 0x2000/4); } else { sram_size = 0x2000; sram_data = Pico_mcd->bram; truncate = 0; // the .brm may contain RAM cart data after normal brm } } else { sram_size = SRam.end-SRam.start+1; if(Pico.m.sram_reg & 4) sram_size=0x2000; sram_data = SRam.data; } if (!sram_data) return 0; // SRam forcefully disabled for this game if (load) { sramFile = fopen(saveFname, "rb"); if(!sramFile) return -1; fread(sram_data, 1, sram_size, sramFile); fclose(sramFile); if ((PicoMCD&1) && (PicoOpt&0x8000)) memcpy32((int *)Pico_mcd->bram, (int *)sram_data, 0x2000/4); } else { // sram save needs some special processing // see if we have anything to save for (; sram_size > 0; sram_size--) if (sram_data[sram_size-1]) break; if (sram_size) { sramFile = fopen(saveFname, truncate ? "wb" : "r+b"); if (!sramFile) sramFile = fopen(saveFname, "wb"); // retry if (!sramFile) return -1; ret = fwrite(sram_data, 1, sram_size, sramFile); ret = (ret != sram_size) ? -1 : 0; fclose(sramFile); #ifndef NO_SYNC sync(); #endif } } return ret; } else { void *PmovFile = NULL; if (strcmp(saveFname + strlen(saveFname) - 3, ".gz") == 0) { if( (PmovFile = gzopen(saveFname, load ? "rb" : "wb")) ) { emu_setSaveStateCbs(1); if (!load) gzsetparams(PmovFile, 9, Z_DEFAULT_STRATEGY); } } else { if( (PmovFile = fopen(saveFname, load ? "rb" : "wb")) ) { emu_setSaveStateCbs(0); } } if(PmovFile) { ret = PmovState(load ? 6 : 5, PmovFile); areaClose(PmovFile); PmovFile = 0; if (load) Pico.m.dirtyPal=1; #ifndef NO_SYNC else sync(); #endif } else ret = -1; if (!ret) strcpy(noticeMsg, load ? "GAME LOADED " : "GAME SAVED "); else { strcpy(noticeMsg, load ? "LOAD FAILED " : "SAVE FAILED "); ret = -1; } emu_noticeMsgUpdated(); return ret; } }
int gzfilebuf::setcompressionstrategy( short comp_strategy ) { return gzsetparams(file, -2, comp_strategy); }
int gzfilebuf::setcompressionlevel( short comp_level ) { return gzsetparams(file, comp_level, -2); }
/* * _prop_object_externalize_write_file -- * Write an externalized dictionary to the specified file. * The file is written atomically from the caller's perspective, * and the mode set to 0666 modified by the caller's umask. * * The 'compress' argument enables gzip (via zlib) compression * for the file to be written. */ bool _prop_object_externalize_write_file(const char *fname, const char *xml, size_t len, bool do_compress) { gzFile gzf = NULL; char tname[PATH_MAX]; int fd; int save_errno; mode_t myumask; if (len > SSIZE_MAX) { errno = EFBIG; return (false); } /* * Get the directory name where the file is to be written * and create the temporary file. */ _prop_object_externalize_file_dirname(fname, tname); #define PLISTTMP "/.plistXXXXXX" if (strlen(tname) + strlen(PLISTTMP) >= sizeof(tname)) { errno = ENAMETOOLONG; return (false); } strcat(tname, PLISTTMP); #undef PLISTTMP if ((fd = mkstemp(tname)) == -1) return (false); if (do_compress) { if ((gzf = gzdopen(fd, "a")) == NULL) goto bad; if (gzsetparams(gzf, Z_BEST_COMPRESSION, Z_DEFAULT_STRATEGY)) goto bad; if (gzwrite(gzf, xml, len) != (ssize_t)len) goto bad; } else { if (write(fd, xml, len) != (ssize_t)len) goto bad; } #ifdef HAVE_FDATASYNC if (fdatasync(fd) == -1) #else if (fsync(fd) == -1) #endif goto bad; myumask = umask(0); (void)umask(myumask); if (fchmod(fd, 0666 & ~myumask) == -1) goto bad; if (do_compress) (void)gzclose(gzf); else (void)close(fd); fd = -1; if (rename(tname, fname) == -1) goto bad; return (true); bad: save_errno = errno; if (do_compress && gzf != NULL) (void)gzclose(gzf); else if (fd != -1) (void)close(fd); (void) unlink(tname); errno = save_errno; return (false); }
static int bebox_build_image(char *kernel, char *boot, char *rawdev, char *outname) { unsigned char *elf_img = NULL, *kern_img = NULL, *header_img = NULL; int i, ch, tmp, kgzlen, err, hsize = BEBOX_HEADER_SIZE; int elf_fd, bebox_fd, kern_fd, elf_img_len = 0; off_t lenpos, kstart, kend, toff, endoff, flength; uint32_t swapped[128]; int32_t *offset; gzFile gzf; struct stat kern_stat; struct bebox_image_block *p; struct timeval tp; Elf32_External_Phdr phdr; elf_fd = open_file("bootloader", boot, &hdr, &elf_stat); if (inkernflag) { kern_fd = open_file("kernel", kernel, &khdr, &kern_stat); kern_len = kern_stat.st_size + BEBOX_MAGICSIZE + KERNLENSIZE; } else kern_len = BEBOX_MAGICSIZE + KERNLENSIZE; for (i = 0; i < ELFGET16(hdr.e_phnum); i++) { lseek(elf_fd, ELFGET32(hdr.e_phoff) + sizeof(phdr) * i, SEEK_SET); if (read(elf_fd, &phdr, sizeof(phdr)) != sizeof(phdr)) errx(3, "Can't read input '%s' phdr : %s", boot, strerror(errno)); if ((ELFGET32(phdr.p_type) != PT_LOAD) || !(ELFGET32(phdr.p_flags) & PF_X)) continue; fstat(elf_fd, &elf_stat); elf_img_len = ELFGET32(phdr.p_filesz); lseek(elf_fd, ELFGET32(phdr.p_offset), SEEK_SET); break; } if ((bebox_fd = open(outname, O_RDWR|O_TRUNC, 0)) < 0) { /* we couldn't open it, it must be new */ bebox_fd = creat(outname, 0644); if (bebox_fd < 0) errx(2, "Can't open output '%s': %s", outname, strerror(errno)); } lseek(bebox_fd, hsize, SEEK_SET); if (inkernflag) { /* * write the header with the wrong values to get the offset * right */ bebox_write_header(bebox_fd, elf_img_len, kern_stat.st_size); /* Copy kernel */ kern_img = malloc(kern_stat.st_size); if (kern_img == NULL) errx(3, "Can't malloc: %s", strerror(errno)); /* we need to jump back after having read the headers */ lseek(kern_fd, 0, SEEK_SET); if (read(kern_fd, (void *)kern_img, kern_stat.st_size) != kern_stat.st_size) errx(3, "Can't read kernel '%s' : %s", kernel, strerror(errno)); gzf = gzdopen(dup(bebox_fd), "a"); if (gzf == NULL) errx(3, "Can't init compression: %s", strerror(errno)); if (gzsetparams(gzf, Z_BEST_COMPRESSION, Z_DEFAULT_STRATEGY) != Z_OK) errx(3, "%s", gzerror(gzf, &err)); } else bebox_write_header(bebox_fd, elf_img_len, 0); /* write a magic number and size before the kernel */ write(bebox_fd, (void *)bebox_magic, BEBOX_MAGICSIZE); lenpos = lseek(bebox_fd, 0, SEEK_CUR); tmp = sa_htobe32(0); write(bebox_fd, (void *)&tmp, KERNLENSIZE); if (inkernflag) { /* write in the compressed kernel */ kstart = lseek(bebox_fd, 0, SEEK_CUR); kgzlen = gzwrite(gzf, kern_img, kern_stat.st_size); gzclose(gzf); kend = lseek(bebox_fd, 0, SEEK_CUR); free(kern_img); } else { kstart = kend = lseek(bebox_fd, 0, SEEK_CUR); kgzlen = 0; } /* jump back to the length position now that we know the length */ lseek(bebox_fd, lenpos, SEEK_SET); kgzlen = kend - kstart; tmp = sa_htobe32(kgzlen); write(bebox_fd, (void *)&tmp, KERNLENSIZE); /* now rewrite the header correctly */ lseek(bebox_fd, hsize, SEEK_SET); tmp = kgzlen + BEBOX_MAGICSIZE + KERNLENSIZE; toff = bebox_write_header(bebox_fd, elf_img_len, tmp); /* Copy boot image */ elf_img = malloc(elf_img_len); if (!elf_img) errx(3, "Can't malloc: %s", strerror(errno)); if (read(elf_fd, elf_img, elf_img_len) != elf_img_len) errx(3, "Can't read file '%s' : %s", boot, strerror(errno)); lseek(bebox_fd, toff + hsize, SEEK_SET); write(bebox_fd, elf_img, elf_img_len); free(elf_img); if (inkernflag) close(kern_fd); close(elf_fd); /* Now go back and write in the block header */ endoff = lseek(bebox_fd, 0, SEEK_END); lseek(bebox_fd, 0, SEEK_SET); header_img = malloc(BEBOX_HEADER_SIZE); if (!header_img) errx(3, "Can't malloc: %s", strerror(errno)); memset(header_img, 0, BEBOX_HEADER_SIZE); /* copy the boot image into the buffer */ for (p = bebox_image_block; p->offset != -1; p++) memcpy(header_img + p->offset, p->data, p->size); /* fill used block bitmap */ memset(header_img + BEBOX_FILE_BLOCK_MAP_START, 0xff, BEBOX_FILE_BLOCK_MAP_END - BEBOX_FILE_BLOCK_MAP_START); /* fix the file size in the header */ tmp = endoff - BEBOX_HEADER_SIZE; *(int32_t *)(header_img + BEBOX_FILE_SIZE_OFFSET) = (int32_t)sa_htobe32(tmp); *(int32_t *)(header_img + BEBOX_FILE_SIZE_ALIGN_OFFSET) = (int32_t)sa_htobe32(roundup(tmp, BEBOX_FILE_BLOCK_SIZE)); gettimeofday(&tp, 0); for (offset = bebox_mtime_offset; *offset != -1; offset++) *(int32_t *)(header_img + *offset) = (int32_t)sa_htobe32(tp.tv_sec); write(bebox_fd, header_img, BEBOX_HEADER_SIZE); /* now pad the end */ flength = roundup(endoff, BEBOX_BLOCK_SIZE); /* refill the header_img with zeros */ memset(header_img, 0, BEBOX_BLOCK_SIZE * 2); lseek(bebox_fd, 0, SEEK_END); write(bebox_fd, header_img, flength - endoff); close(bebox_fd); free(header_img); return 0; }
static int rs6000_build_image(char *kernel, char *boot, char *rawdev, char *outname) { unsigned char *elf_img = NULL, *kern_img = NULL; int i, ch, tmp, kgzlen, err; int elf_fd, rs6000_fd, kern_fd, elf_img_len = 0, elf_pad; uint32_t swapped[128]; off_t lenpos, kstart, kend; unsigned long length; long flength; gzFile gzf; struct stat kern_stat; Elf32_External_Phdr phdr; elf_fd = open_file("bootloader", boot, &hdr, &elf_stat); kern_fd = open_file("kernel", kernel, &khdr, &kern_stat); kern_len = kern_stat.st_size + RS6000_MAGICSIZE + KERNLENSIZE; for (i = 0; i < ELFGET16(hdr.e_phnum); i++) { lseek(elf_fd, ELFGET32(hdr.e_phoff) + sizeof(phdr) * i, SEEK_SET); if (read(elf_fd, &phdr, sizeof(phdr)) != sizeof(phdr)) errx(3, "Can't read input '%s' phdr : %s", boot, strerror(errno)); if ((ELFGET32(phdr.p_type) != PT_LOAD) || !(ELFGET32(phdr.p_flags) & PF_X)) continue; fstat(elf_fd, &elf_stat); elf_img_len = elf_stat.st_size - ELFGET32(phdr.p_offset); elf_pad = ELFGET32(phdr.p_memsz) - ELFGET32(phdr.p_filesz); if (verboseflag) printf("Padding %d\n", elf_pad); lseek(elf_fd, ELFGET32(phdr.p_offset), SEEK_SET); break; } if ((rs6000_fd = open(outname, O_RDWR|O_TRUNC, 0)) < 0) { /* we couldn't open it, it must be new */ rs6000_fd = creat(outname, 0644); if (rs6000_fd < 0) errx(2, "Can't open output '%s': %s", outname, strerror(errno)); } /* Set file pos. to 2nd sector where image will be written */ lseek(rs6000_fd, 0x400, SEEK_SET); /* Copy boot image */ elf_img = malloc(elf_img_len); if (!elf_img) errx(3, "Can't malloc: %s", strerror(errno)); if (read(elf_fd, elf_img, elf_img_len) != elf_img_len) errx(3, "Can't read file '%s' : %s", boot, strerror(errno)); write(rs6000_fd, elf_img, elf_img_len); free(elf_img); /* now dump in the padding space for the BSS */ elf_pad += 100; /* just a little extra for good luck */ lseek(rs6000_fd, elf_pad, SEEK_CUR); /* Copy kernel */ kern_img = malloc(kern_stat.st_size); if (kern_img == NULL) errx(3, "Can't malloc: %s", strerror(errno)); /* we need to jump back after having read the headers */ lseek(kern_fd, 0, SEEK_SET); if (read(kern_fd, (void *)kern_img, kern_stat.st_size) != kern_stat.st_size) errx(3, "Can't read kernel '%s' : %s", kernel, strerror(errno)); gzf = gzdopen(dup(rs6000_fd), "a"); if (gzf == NULL) errx(3, "Can't init compression: %s", strerror(errno)); if (gzsetparams(gzf, Z_BEST_COMPRESSION, Z_DEFAULT_STRATEGY) != Z_OK) errx(3, "%s", gzerror(gzf, &err)); /* write a magic number and size before the kernel */ write(rs6000_fd, (void *)rs6000_magic, RS6000_MAGICSIZE); lenpos = lseek(rs6000_fd, 0, SEEK_CUR); if (verboseflag) printf("wrote magic at pos 0x%lx\n", (unsigned long)lenpos); tmp = sa_htobe32(0); write(rs6000_fd, (void *)&tmp, KERNLENSIZE); /* write in the compressed kernel */ kstart = lseek(rs6000_fd, 0, SEEK_CUR); if (verboseflag) printf("kernel start at pos 0x%lx\n", (unsigned long)kstart); kgzlen = gzwrite(gzf, kern_img, kern_stat.st_size); gzclose(gzf); kend = lseek(rs6000_fd, 0, SEEK_CUR); if (verboseflag) printf("kernel end at pos 0x%lx\n", (unsigned long)kend); /* jump back to the length position now that we know the length */ lseek(rs6000_fd, lenpos, SEEK_SET); kgzlen = kend - kstart; tmp = sa_htobe32(kgzlen); if (verboseflag) printf("kernel len = 0x%x tmp = 0x%x\n", kgzlen, tmp); write(rs6000_fd, (void *)&tmp, KERNLENSIZE); #if 0 lseek(rs6000_fd, sizeof(boot_record_t) + sizeof(config_record_t), SEEK_SET); /* set entry and length */ length = sa_htole32(0x400); write(rs6000_fd, &length, sizeof(length)); length = sa_htole32(0x400 + elf_img_len + 8 + kgzlen); write(rs6000_fd, &length, sizeof(length)); #endif /* generate the header now that we know the kernel length */ if (verboseflag) printf("building records\n"); rs6000_build_records(elf_img_len + 8 + kgzlen); lseek(rs6000_fd, 0, SEEK_SET); /* ROM wants it byteswapped in 32bit chunks */ if (verboseflag) printf("writing records\n"); memcpy(swapped, &bootrec, sizeof(rs6000_boot_record_t)); for (i=0; i < 128; i++) swapped[i] = htonl(swapped[i]); write(rs6000_fd, swapped, sizeof(rs6000_boot_record_t)); memcpy(swapped, &confrec, sizeof(rs6000_config_record_t)); for (i=0; i < 128; i++) swapped[i] = htonl(swapped[i]); write(rs6000_fd, swapped, sizeof(rs6000_config_record_t)); free(kern_img); close(kern_fd); close(rs6000_fd); close(elf_fd); return 0; }
static int prep_build_image(char *kernel, char *boot, char *rawdev, char *outname) { unsigned char *elf_img = NULL, *kern_img = NULL; int i, ch, tmp, kgzlen, err; int elf_fd, prep_fd, kern_fd, elf_img_len = 0; off_t lenpos, kstart, kend; unsigned long length; long flength; gzFile gzf; struct stat kern_stat; Elf32_External_Phdr phdr; elf_fd = open_file("bootloader", boot, &hdr, &elf_stat); if (inkernflag) { kern_fd = open_file("kernel", kernel, &khdr, &kern_stat); kern_len = kern_stat.st_size + PREP_MAGICSIZE + KERNLENSIZE; } else kern_len = PREP_MAGICSIZE + KERNLENSIZE; for (i = 0; i < ELFGET16(hdr.e_phnum); i++) { lseek(elf_fd, ELFGET32(hdr.e_phoff) + sizeof(phdr) * i, SEEK_SET); if (read(elf_fd, &phdr, sizeof(phdr)) != sizeof(phdr)) errx(3, "Can't read input '%s' phdr : %s", boot, strerror(errno)); if ((ELFGET32(phdr.p_type) != PT_LOAD) || !(ELFGET32(phdr.p_flags) & PF_X)) continue; fstat(elf_fd, &elf_stat); elf_img_len = elf_stat.st_size - ELFGET32(phdr.p_offset); lseek(elf_fd, ELFGET32(phdr.p_offset), SEEK_SET); break; } if ((prep_fd = open(outname, O_RDWR|O_TRUNC, 0)) < 0) { /* we couldn't open it, it must be new */ prep_fd = creat(outname, 0644); if (prep_fd < 0) errx(2, "Can't open output '%s': %s", outname, strerror(errno)); } prep_check_mbr(prep_fd, rawdev); /* Set file pos. to 2nd sector where image will be written */ lseek(prep_fd, 0x400, SEEK_SET); /* Copy boot image */ elf_img = malloc(elf_img_len); if (!elf_img) errx(3, "Can't malloc: %s", strerror(errno)); if (read(elf_fd, elf_img, elf_img_len) != elf_img_len) errx(3, "Can't read file '%s' : %s", boot, strerror(errno)); write(prep_fd, elf_img, elf_img_len); free(elf_img); if (inkernflag) { /* Copy kernel */ kern_img = malloc(kern_stat.st_size); if (kern_img == NULL) errx(3, "Can't malloc: %s", strerror(errno)); /* we need to jump back after having read the headers */ lseek(kern_fd, 0, SEEK_SET); if (read(kern_fd, (void *)kern_img, kern_stat.st_size) != kern_stat.st_size) errx(3, "Can't read kernel '%s' : %s", kernel, strerror(errno)); } gzf = gzdopen(dup(prep_fd), "a"); if (gzf == NULL) errx(3, "Can't init compression: %s", strerror(errno)); if (gzsetparams(gzf, Z_BEST_COMPRESSION, Z_DEFAULT_STRATEGY) != Z_OK) errx(3, "%s", gzerror(gzf, &err)); /* write a magic number and size before the kernel */ write(prep_fd, (void *)prep_magic, PREP_MAGICSIZE); lenpos = lseek(prep_fd, 0, SEEK_CUR); tmp = sa_htobe32(0); write(prep_fd, (void *)&tmp, KERNLENSIZE); /* write in the compressed kernel */ kstart = lseek(prep_fd, 0, SEEK_CUR); if (inkernflag) { kgzlen = gzwrite(gzf, kern_img, kern_stat.st_size); gzclose(gzf); } kend = lseek(prep_fd, 0, SEEK_CUR); /* jump back to the length position now that we know the length */ lseek(prep_fd, lenpos, SEEK_SET); kgzlen = kend - kstart; tmp = sa_htobe32(kgzlen); write(prep_fd, (void *)&tmp, KERNLENSIZE); length = sa_htole32(0x400 + elf_img_len + 8 + kgzlen); lseek(prep_fd, sizeof(mbr) + 4, SEEK_SET); write(prep_fd, &length, sizeof(length)); flength = 0x400 + elf_img_len + 8 + kgzlen; if (lfloppyflag) flength -= (5760 * 512); else flength -= (2880 * 512); if (flength > 0 && !saloneflag) fprintf(stderr, "%s: Image %s is %ld bytes larger than single" " floppy. Can only be used for netboot.\n", getprogname(), outname, flength); if (inkernflag) { free(kern_img); close(kern_fd); } close(prep_fd); close(elf_fd); return 0; }
int dump(uint32_t nd, pid_t pid, long size ) { int fd; int ret; FILE *fp; char path[PATH_MAX + 1]; char buff[PATH_MAX + 1]; struct _procfs_debug_info *map = (struct _procfs_debug_info *)buff; uid_t uid = 0; gid_t gid = 0; procfs_info info; if(pid == 0) { return ENOENT; } if(ND_NODE_CMP(nd, ND_LOCAL_NODE) && match_pid != -1 && match_pid != pid) { return ENXIO; } sprintf(buff, "/proc/%d/as", pid); if ( requested ) { if((fd = open(buff, O_RDWR|O_NONBLOCK)) == -1) { fprintf(stderr,"dumper: error attaching to process %d - %s\n", pid, strerror(errno) ); return errno; } if ((ret = devctl(fd, DCMD_PROC_STOP, NULL, 0, 0)) != EOK) { vprintf(("failed PROC_STOP %s\n", strerror(ret) )); close(fd); return ret; } } else { if((fd = open(buff, O_RDONLY|O_NONBLOCK)) == -1) { return errno; } } if((ret = devctl(fd, DCMD_PROC_MAPDEBUG_BASE, map, sizeof buff, 0)) != EOK) { close(fd); return ret; } if((ret = devctl(fd, DCMD_PROC_INFO, &info, sizeof info, 0)) == EOK) { uid = geteuid(); if ( uid != 0 && uid != info.uid ) { vprintf(("Permission Denied\n")); close(fd); return EPERM; } uid = info.euid; if(uid != info.uid) /* suid binary */ gid=0; else gid = info.egid; } path[0] = '\0'; if(nd != -1) { if(dump_dir) { strcpy(path, dump_dir); } else { struct passwd *pwp; if((ret = devctl(fd, DCMD_PROC_INFO, &info, sizeof info, 0)) == EOK && (pwp = getpwuid(info.uid))) { strcpy(path, pwp->pw_dir); } else { strcpy(path, "/tmp"); } } strcat(path, "/"); } strcat(path, basename(map->path)); dump_path = gen_dump_path(path); if (gzlevel == -1) { fp = fopen(dump_path, "w"); } else { fp = (FILE *)gzopen(dump_path, "w"); if (fp) gzsetparams((gzFile)fp, gzlevel, 0); } if(!fp) { perror(dump_path); close(fd); return errno; } chown( dump_path, uid, gid ); chmod( dump_path, world_readable ? 0644 : S_IRUSR|S_IWUSR ); ret = elfcore(fd, fp, map->path, size); if (gzlevel == -1) { fclose(fp); } else { gzclose((gzFile)fp); } close(fd); return ret; }
/* * Receive a tar format file from the connection to the server, and write * the data from this file directly into a tar file. If compression is * enabled, the data will be compressed while written to the file. * * The file will be named base.tar[.gz] if it's for the main data directory * or <tablespaceoid>.tar[.gz] if it's for another tablespace. * * No attempt to inspect or validate the contents of the file is done. */ static void ReceiveTarFile(PGconn *conn, PGresult *res, int rownum) { char filename[MAXPGPATH]; char *copybuf = NULL; FILE *tarfile = NULL; char tarhdr[512]; bool basetablespace = PQgetisnull(res, rownum, 0); bool in_tarhdr = true; bool skip_file = false; size_t tarhdrsz = 0; size_t filesz = 0; #ifdef HAVE_LIBZ gzFile ztarfile = NULL; #endif if (basetablespace) { /* * Base tablespaces */ if (strcmp(basedir, "-") == 0) { #ifdef HAVE_LIBZ if (compresslevel != 0) { ztarfile = gzdopen(dup(fileno(stdout)), "wb"); if (gzsetparams(ztarfile, compresslevel, Z_DEFAULT_STRATEGY) != Z_OK) { fprintf(stderr, _("%s: could not set compression level %d: %s\n"), progname, compresslevel, get_gz_error(ztarfile)); disconnect_and_exit(1); } } else #endif tarfile = stdout; } else { #ifdef HAVE_LIBZ if (compresslevel != 0) { snprintf(filename, sizeof(filename), "%s/base.tar.gz", basedir); ztarfile = gzopen(filename, "wb"); if (gzsetparams(ztarfile, compresslevel, Z_DEFAULT_STRATEGY) != Z_OK) { fprintf(stderr, _("%s: could not set compression level %d: %s\n"), progname, compresslevel, get_gz_error(ztarfile)); disconnect_and_exit(1); } } else #endif { snprintf(filename, sizeof(filename), "%s/base.tar", basedir); tarfile = fopen(filename, "wb"); } } } else { /* * Specific tablespace */ #ifdef HAVE_LIBZ if (compresslevel != 0) { snprintf(filename, sizeof(filename), "%s/%s.tar.gz", basedir, PQgetvalue(res, rownum, 0)); ztarfile = gzopen(filename, "wb"); if (gzsetparams(ztarfile, compresslevel, Z_DEFAULT_STRATEGY) != Z_OK) { fprintf(stderr, _("%s: could not set compression level %d: %s\n"), progname, compresslevel, get_gz_error(ztarfile)); disconnect_and_exit(1); } } else #endif { snprintf(filename, sizeof(filename), "%s/%s.tar", basedir, PQgetvalue(res, rownum, 0)); tarfile = fopen(filename, "wb"); } } #ifdef HAVE_LIBZ if (compresslevel != 0) { if (!ztarfile) { /* Compression is in use */ fprintf(stderr, _("%s: could not create compressed file \"%s\": %s\n"), progname, filename, get_gz_error(ztarfile)); disconnect_and_exit(1); } } else #endif { /* Either no zlib support, or zlib support but compresslevel = 0 */ if (!tarfile) { fprintf(stderr, _("%s: could not create file \"%s\": %s\n"), progname, filename, strerror(errno)); disconnect_and_exit(1); } } /* * Get the COPY data stream */ res = PQgetResult(conn); if (PQresultStatus(res) != PGRES_COPY_OUT) { fprintf(stderr, _("%s: could not get COPY data stream: %s"), progname, PQerrorMessage(conn)); disconnect_and_exit(1); } while (1) { int r; if (copybuf != NULL) { PQfreemem(copybuf); copybuf = NULL; } r = PQgetCopyData(conn, ©buf, 0); if (r == -1) { /* * End of chunk. If requested, and this is the base tablespace, * write recovery.conf into the tarfile. When done, close the file * (but not stdout). * * Also, write two completely empty blocks at the end of the tar * file, as required by some tar programs. */ char zerobuf[1024]; MemSet(zerobuf, 0, sizeof(zerobuf)); if (basetablespace && writerecoveryconf) { char header[512]; int padding; tarCreateHeader(header, "recovery.conf", NULL, recoveryconfcontents->len, 0600, 04000, 02000, time(NULL)); padding = ((recoveryconfcontents->len + 511) & ~511) - recoveryconfcontents->len; WRITE_TAR_DATA(header, sizeof(header)); WRITE_TAR_DATA(recoveryconfcontents->data, recoveryconfcontents->len); if (padding) WRITE_TAR_DATA(zerobuf, padding); } /* 2 * 512 bytes empty data at end of file */ WRITE_TAR_DATA(zerobuf, sizeof(zerobuf)); #ifdef HAVE_LIBZ if (ztarfile != NULL) { if (gzclose(ztarfile) != 0) { fprintf(stderr, _("%s: could not close compressed file \"%s\": %s\n"), progname, filename, get_gz_error(ztarfile)); disconnect_and_exit(1); } } else #endif { if (strcmp(basedir, "-") != 0) { if (fclose(tarfile) != 0) { fprintf(stderr, _("%s: could not close file \"%s\": %s\n"), progname, filename, strerror(errno)); disconnect_and_exit(1); } } } break; } else if (r == -2) { fprintf(stderr, _("%s: could not read COPY data: %s"), progname, PQerrorMessage(conn)); disconnect_and_exit(1); } if (!writerecoveryconf || !basetablespace) { /* * When not writing recovery.conf, or when not working on the base * tablespace, we never have to look for an existing recovery.conf * file in the stream. */ WRITE_TAR_DATA(copybuf, r); } else { /* * Look for a recovery.conf in the existing tar stream. If it's * there, we must skip it so we can later overwrite it with our * own version of the file. * * To do this, we have to process the individual files inside the * TAR stream. The stream consists of a header and zero or more * chunks, all 512 bytes long. The stream from the server is * broken up into smaller pieces, so we have to track the size of * the files to find the next header structure. */ int rr = r; int pos = 0; while (rr > 0) { if (in_tarhdr) { /* * We're currently reading a header structure inside the * TAR stream, i.e. the file metadata. */ if (tarhdrsz < 512) { /* * Copy the header structure into tarhdr in case the * header is not aligned to 512 bytes or it's not * returned in whole by the last PQgetCopyData call. */ int hdrleft; int bytes2copy; hdrleft = 512 - tarhdrsz; bytes2copy = (rr > hdrleft ? hdrleft : rr); memcpy(&tarhdr[tarhdrsz], copybuf + pos, bytes2copy); rr -= bytes2copy; pos += bytes2copy; tarhdrsz += bytes2copy; } else { /* * We have the complete header structure in tarhdr, * look at the file metadata: - the subsequent file * contents have to be skipped if the filename is * recovery.conf - find out the size of the file * padded to the next multiple of 512 */ int padding; skip_file = (strcmp(&tarhdr[0], "recovery.conf") == 0); sscanf(&tarhdr[124], "%11o", (unsigned int *) &filesz); padding = ((filesz + 511) & ~511) - filesz; filesz += padding; /* Next part is the file, not the header */ in_tarhdr = false; /* * If we're not skipping the file, write the tar * header unmodified. */ if (!skip_file) WRITE_TAR_DATA(tarhdr, 512); } } else { /* * We're processing a file's contents. */ if (filesz > 0) { /* * We still have data to read (and possibly write). */ int bytes2write; bytes2write = (filesz > rr ? rr : filesz); if (!skip_file) WRITE_TAR_DATA(copybuf + pos, bytes2write); rr -= bytes2write; pos += bytes2write; filesz -= bytes2write; } else { /* * No more data in the current file, the next piece of * data (if any) will be a new file header structure. */ in_tarhdr = true; skip_file = false; tarhdrsz = 0; filesz = 0; } } } } totaldone += r; if (showprogress) progress_report(rownum, filename); } /* while (1) */ if (copybuf != NULL) PQfreemem(copybuf); }
static void start_recording (Viewer *self) { if (self->is_recording) { err ("viewer: already recording!!\n"); return; } #ifndef USE_ZMOV assert (self->fb_area == NULL); #endif assert (self->movie_buffer == NULL); int window_width = GTK_WIDGET (self->gl_area)->allocation.width; int window_height = GTK_WIDGET (self->gl_area)->allocation.height; self->movie_width = window_width - (window_width % 4); self->movie_height = window_height; self->movie_stride = self->movie_width * 3; self->movie_buffer = (uint8_t*) malloc (self->movie_stride * self->movie_height); self->movie_desired_fps = 1000.0 / gtk_spin_button_get_value (GTK_SPIN_BUTTON (self->fps_spin)); self->movie_actual_fps = self->movie_desired_fps; self->movie_frame_last_utime = 0; /* int wwidth, wheight; gtk_window_get_size(GTK_WINDOW(self->window), &wwidth, &wheight); printf("%5d, %5d\n", self->movie_width, self->movie_height); gtk_widget_set_size_request(GTK_WIDGET(self->gl_area), self->movie_width, self->movie_height); gtk_window_set_default_size(GTK_WINDOW(self->window), wwidth, wheight); gtk_widget_set_size_request(GTK_WIDGET(self->gl_area), self->movie_width, self->movie_height); gtk_window_set_resizable(GTK_WINDOW(self->window), FALSE); */ #ifdef USE_ZMOV self->movie_path = get_unique_filename(NULL, "viewer", 1, "ppms.gz"); self->movie_gzf = gzopen(self->movie_path, "w"); gzsetparams(self->movie_gzf, Z_BEST_SPEED, Z_DEFAULT_STRATEGY); if (self->movie_gzf == NULL) goto abort_recording; viewer_set_status_bar_message (self, "Recording to: %s", self->movie_path); #else self->fb_area = fb_gl_drawing_area_new (FALSE, mov_width, mov_height, GL_BGR); if (!self->fb_area) { err ("Couldn't create FramebufferObject offscreen plugin\n"); gtk_toggle_tool_button_set_active ( GTK_TOGGLE_TOOL_BUTTON (self->record_button), FALSE); free (self->mov_bgr_buf); return; } assert(self->ezavi == NULL); ezavi_params_t avi_params = { .path = NULL, .file_prefix = "viewer", .date_in_file = 1, .codec = "raw", .width = mov_width, .height = mov_height, .src_stride = mov_width * 3, .frame_rate = 30, .split_point = 4000000000UL }; self->ezavi = ezavi_new (&avi_params); if (!self->ezavi) { err ("Couldn't create AVI file\n"); goto abort_recording; } g_signal_connect (G_OBJECT(self->fb_area), "buffer-ready", G_CALLBACK (on_fb_ready), self->ezavi); viewer_set_status_bar_message (self, "Recording to: %s", ezavi_get_filename (self->ezavi)); #endif self->render_timer_id = g_timeout_add (1000 / gtk_spin_button_get_value (GTK_SPIN_BUTTON (self->fps_spin)), (GSourceFunc) on_render_timer, self); self->is_recording = 1; return; abort_recording: #ifndef USE_ZMOV g_object_unref (self->fb_area); #endif gtk_toggle_tool_button_set_active ( GTK_TOGGLE_TOOL_BUTTON (self->record_button), FALSE); free (self->mov_bgr_buf); } static void stop_recording (Viewer *self) { #ifndef USE_ZMOV if (!self->fb_area) return; #endif free(self->movie_buffer); self->movie_buffer = NULL; dbg ("\nRecording stopped\n"); viewer_set_status_bar_message (self, "Recording stopped"); #ifdef USE_ZMOV gzclose(self->movie_gzf); self->movie_gzf = NULL; free(self->movie_path); self->movie_draw_pending = 0; #else fb_gl_drawing_area_flush (self->fb_area); ezavi_finish (self->ezavi); ezavi_destroy (self->ezavi); self->ezavi = NULL; g_object_unref (self->fb_area); self->fb_area = NULL; #endif g_source_remove (self->render_timer_id); self->is_recording = 0; gtk_toggle_tool_button_set_active (GTK_TOGGLE_TOOL_BUTTON (self->record_button), FALSE); // gtk_window_set_resizable(GTK_WINDOW(self->window), TRUE); } static void update_status_bar(Viewer *viewer) { char buf[1024]; if (viewer->picking_handler && !viewer->picking_handler->picking) viewer->picking_handler = NULL; int vp[4] = {0,0,0,0}; if (viewer->gl_area && gtku_gl_drawing_area_set_context (viewer->gl_area) == 0) { glGetIntegerv(GL_VIEWPORT, vp); } int width = vp[2], height = vp[3]; if (viewer->picking_handler) snprintf(buf, sizeof (buf), "%s%d x %d [%s] %s", (viewer->simulation_flag?"[SIM ENABLED] ":""), width, height, viewer->picking_handler->name, viewer->status_bar_message); else snprintf(buf, sizeof (buf), "%s%d x %d [Idle] %s", (viewer->simulation_flag?"[SIM ENABLED] ":""), width, height, viewer->status_bar_message); gtk_statusbar_push(GTK_STATUSBAR(viewer->status_bar), gtk_statusbar_get_context_id( GTK_STATUSBAR(viewer->status_bar),"info"), buf); }