static void test(const char *rda, const char *mnt, const char *file) { static const msdos_format_request_param_t rqdata = { .quick_format = true, .sync_device = true }; rtems_status_code sc; int disk_fd; int rv; sc = rtems_disk_io_initialize(); rtems_test_assert(sc == RTEMS_SUCCESSFUL); disk_fd = open(rda, O_RDWR); rtems_test_assert(disk_fd >= 0); rv = msdos_format(rda, &rqdata); rtems_test_assert(rv == 0); rv = mount_and_make_target_path( rda, mnt, RTEMS_FILESYSTEM_TYPE_DOSFS, RTEMS_FILESYSTEM_READ_WRITE, NULL ); rtems_test_assert(rv == 0); create_file(file); rv = rtems_disk_fd_purge(disk_fd); rtems_test_assert(rv == 0); write_to_file(file, false); rv = rtems_disk_fd_purge(disk_fd); rtems_test_assert(rv == 0); check_file_size(file, 0); write_to_file(file, true); rv = rtems_disk_fd_purge(disk_fd); rtems_test_assert(rv == 0); check_file_size(file, 1); rv = unmount(mnt); rtems_test_assert(rv == 0); rv = close(disk_fd); rtems_test_assert(rv == 0); } static void Init(rtems_task_argument arg) { puts("\n\n*** TEST FSDOSFSSYNC 1 ***"); test("/dev/rda", "/mnt", "/mnt/file"); puts("*** END OF TEST FSDOSFSSYNC 1 ***"); rtems_test_exit(0); }
void log_lprint(const int level, const char *fmt, ...) { char *buf = NULL; static int log_index = 0; va_list ap; char systime[32]; if (level > global_level) { return; } va_start(ap, fmt); vasprintf(&buf, fmt, ap); va_end(ap); if (log_fd > 0) { char sbuf[16]; check_file_size(); get_system_time(systime); sprintf(sbuf, "[%d]: ", log_index++); write(log_fd, sbuf, strlen(sbuf)); write(log_fd, systime, strlen(systime)); write(log_fd, buf, strlen(buf)); write(log_fd, "\n", 1); fsync(log_fd); } else { fprintf(stdout, "%s", buf); fflush(stdout); } if (buf) { free(buf); } }
int op_testpattern(const char *file, bool op_write) { int fd = -1; mode_t mode = O_LARGEFILE; unsigned long long pos, len, i, op_blocks; double op_elapsed; struct timeval start_time; mode |= op_write ? O_RDWR : O_RDONLY; fd = open(file, mode); if (fd == -1) { fprintf(stderr, "Unable to open %s, errno %d\n", file, errno); return 1; } if (!check_file_size(fd)) { close(fd); return 1; } gettimeofday(&start_time, NULL); for(i = 0; i < max_blocks; i++) { pos = i * block_size; if (lseek(fd, pos, SEEK_SET) == (off_t)-1) { fprintf(stderr, "Unable to seek to offset %llx\n", pos); close(fd); return 1; } if (op_write) { update_block(); len = atomicio(vwrite, fd, block_buf, block_size); if (len < block_size) { fprintf(stderr, "Write block %llx failed\n", i); close(fd); return 1; } } else { len = atomicio(read, fd, block_buf, block_size); if (len < block_size) { fprintf(stderr, "Read block %llx failed\n", i); close(fd); return 1; } verify_block(); } op_blocks = i + 1; op_elapsed = get_op_elapsed(&start_time); if (max_time > 0 && op_elapsed >= max_time) break; } printf("%llu %llu %f %llu\n", max_blocks, op_blocks, op_elapsed, sect_errors); close(fd); return 0; }
void KSGLogBackend::log2file(const ACE_TCHAR *msg,int msglen) { check_file_size(msglen); if(_logfile && _logfile->is_open()) { (*_logfile)<<msg<<std::flush; } }
/* * truncate(2) checks. */ int t51(char *name) { char file[MAX_PATH_LENGTH] = ""; int fd; long long size; int result; ENTER("truncate() should truncate file to proper length"); snprintf(file, MAX_PATH_LENGTH, "%s/test_t51_file", lustre_path); for (size = 0; size < T51_NR * T51_STEP; size += T51_STEP) { t_echo_create(file, ""); if (truncate(file, size) != 0) { printf("\nerror truncating file: %s\n",strerror(errno)); return(-1); } result = check_file_size(file, size); if (result != 0) return result; t_unlink(file); t_echo_create(file, ""); fd = open(file, O_RDWR|O_CREAT, (mode_t)0666); if (fd < 0) { printf("\nerror open file: %s\n", strerror(errno)); return(-1); } if (ftruncate(fd, size) != 0) { printf("\nerror ftruncating file:%s\n",strerror(errno)); return(-1); } close(fd); result = check_file_size(file, size); if (result != 0) return result; t_unlink(file); if (size % (T51_STEP * (T51_NR / 75)) == 0) { printf("."); fflush(stdout); } } printf("\n"); LEAVE(); }
static void test(void) { int rv = 0; const char file_a [] = "/FTP/127.0.0.1/a.txt"; const char file_b [] = "/FTP/127.0.0.1/b.txt"; rv = rtems_bsdnet_initialize_network(); rtems_test_assert(rv == 0); rv = rtems_initialize_ftpd(); rtems_test_assert(rv == 0); initialize_ftpfs(); change_self_priority(); create_file(file_a, &content [0], sizeof(content)); copy_file(file_a, file_b); check_file(file_b); check_file_size(file_a, sizeof(content)); check_file_size(file_b, sizeof(content)); }
gboolean gst_validate_media_info_inspect_uri (GstValidateMediaInfo * mi, const gchar * uri, gboolean discover_only, GError ** err) { GstDiscovererInfo *info; GstDiscoverer *discoverer = gst_discoverer_new (GST_SECOND * 60, err); gboolean ret = TRUE; g_return_val_if_fail (uri != NULL, FALSE); g_free (mi->uri); mi->uri = g_strdup (uri); if (!discoverer) { return FALSE; } info = gst_discoverer_discover_uri (discoverer, uri, err); if (gst_discoverer_info_get_result (info) != GST_DISCOVERER_OK) { gst_object_unref (discoverer); return FALSE; } mi->is_image = check_is_image (info); ret = check_file_size (mi) & ret; ret = check_encoding_profile (mi, info) & ret; ret = check_file_duration (mi, info) & ret; if (mi->is_image) goto done; check_seekable (mi, info); if (discover_only) goto done; ret = check_playback (mi, &mi->playback_error) & ret; ret = check_reverse_playback (mi, &mi->reverse_playback_error) & ret; ret = check_track_selection (mi, &mi->track_switch_error) & ret; done: gst_object_unref (discoverer); return ret; }
static char * fixfrag_ii(char *pathname, char *gfarm_file, char *sec) { char *hostname, *e; struct gfarm_file_section_copy_info sc_info; /* * XXX - Gfarm v1 uses a special lock file to avoid race * condition during on-demand replication. This file should * not be registered. */ if (strstr(sec, ":::lock")) return ("lock file"); if (check_all == 0 && gfs_file_section_check_busy(gfarm_file, sec) != GFARM_ERR_TEXT_FILE_BUSY) { /* check file size */ e = check_file_size(pathname, gfarm_file, sec); if (e != GFARM_ERR_NO_FRAGMENT_INFORMATION && e != NULL) return (e); /* check whether the fragment is already registered. */ e = gfarm_host_get_canonical_self_name(&hostname); if (e == NULL) { e = gfarm_file_section_copy_info_get( gfarm_file, sec, hostname, &sc_info); if (e == NULL) { /* already exist */ gfarm_file_section_copy_info_free(&sc_info); return (GFARM_ERR_ALREADY_EXISTS); } if (e != GFARM_ERR_NO_SUCH_OBJECT) return (e); } } /* * If the corresponding metadata does not exist, register it. * Otherwise, check file size and checksum. */ return (gfs_pio_set_fragment_info_local(pathname, gfarm_file, sec)); }
int t19(char *name) { char file[MAX_PATH_LENGTH] = ""; int fd; int result; ENTER("open(O_TRUNC) should truncate file to 0-length"); snprintf(file, MAX_PATH_LENGTH, "%s/test_t19_file", lustre_path); t_echo_create(file, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); fd = open(file, O_RDWR|O_CREAT|O_TRUNC, (mode_t)0666); if (fd < 0) { printf("error open file: %s\n", strerror(errno)); return(-1); } close(fd); result = check_file_size(file, 0); if (result != 0) return result; t_unlink(file); LEAVE(); }
/** * Handle the SIGXFSZ signal. A SIGXFSZ signal is received when a file exceeds * the maximum allowable size either as dictated by the fzise paramater in * /etc/security/limits.conf (ulimit -f) or by the maximum size allowed by * the filesystem */ void handle_sigxfsz() { /* TODO: This doesn't really do anything worthwhile in most cases. The "right" thing * to do would probably be to rotate out the offending file, if feasible. Just ignoring * the problem is not likely to work. */ static time_t lastlog_time = (time_t)0; /* Save the last log time so we don't log too often. */ unsigned long log_interval = 300; /* How frequently to log messages about receiving the signal */ struct rlimit rlim; time_t now; char *files[] = { log_file, debug_file, host_perfdata_file, service_perfdata_file, object_cache_file, object_precache_file, status_file, retention_file, }; int x; char **filep; long long size; long long max_size = 0LL; char *max_name = NULL; /* Check the current time and if less time has passed since the last time the signal was received, ignore it */ time(&now); if ((unsigned long)(now - lastlog_time) < log_interval) return; /* Get the current file size limit */ if (getrlimit(RLIMIT_FSIZE, &rlim) != 0) { /* Attempt to log the error, realizing that the logging may fail if it is the log file that is over the size limit. */ nm_log(NSLOG_RUNTIME_ERROR, "Unable to determine current resource limits: %s\n", strerror(errno)); lastlog_time = now; return; } /* Try to figure out which file caused the signal and react appropriately */ for (x = 0, filep = files; (size_t)x < (sizeof(files) / sizeof(files[0])); x++, filep++) { if (*filep == NULL) continue; if ((size = check_file_size(*filep, 1024, rlim)) == -1) { lastlog_time = now; return; } else if (size > max_size) { max_size = size; max_name = *filep; } } if ((max_size > 0) && (max_name != NULL)) { nm_log(NSLOG_RUNTIME_ERROR, "SIGXFSZ received because a " "file's size may have exceeded the file size limits of " "the filesystem. The largest file checked, '%s', has a " "size of %lld bytes", max_name, max_size); } else { nm_log(NSLOG_RUNTIME_ERROR, "SIGXFSZ received but unable to " "determine which file may have caused it."); } }
/**************************************************************************** compress ISO ****************************************************************************/ int comp_ciso(int level, int no_comp_diff) { unsigned long long file_size; unsigned long long write_pos; int total_sectors; int index_size; int block; unsigned char buf4[64]; int cmp_size; int status; int percent_period; int percent_cnt; int align,align_b,align_m; lzo_voidp wrkmem; unsigned long lzolen; file_size = check_file_size(fin); if(file_size<0) { printf("Can't get file size\n"); return 1; } /* allocate index block */ index_size = (ciso_total_block + 1 ) * sizeof(unsigned long); index_buf = malloc(index_size); crc_buf = malloc(index_size); block_buf1 = malloc(ciso.block_size*2); block_buf2 = malloc(ciso.block_size*2); if( !index_buf || !crc_buf || !block_buf1 || !block_buf2 ) { printf("Can't allocate memory\n"); return 1; } memset(index_buf,0,index_size); memset(crc_buf,0,index_size); memset(buf4,0,sizeof(buf4)); if(is_ziso) { if(lzo_init() != LZO_E_OK) { printf("lzo_init() failed\n"); return 1; } //wrkmem = (lzo_voidp) malloc(LZO1X_1_MEM_COMPRESS); wrkmem = (lzo_voidp) malloc(LZO1X_999_MEM_COMPRESS); } else { /* init zlib */ z.zalloc = Z_NULL; z.zfree = Z_NULL; z.opaque = Z_NULL; } /* show info */ printf("Compress '%s' to '%s'\n",fname_in,fname_out); printf("Total File Size %ld bytes\n",ciso.total_bytes); printf("block size %d bytes\n",ciso.block_size); printf("index align %d\n",1<<ciso.align); printf("compress level %d\n",level); printf("type %s\n", is_ziso ? "ZISO" : "CISO"); /* write header block */ fwrite(&ciso,1,sizeof(ciso),fout); /* dummy write index block */ fwrite(index_buf,1,index_size,fout); write_pos = sizeof(ciso) + index_size; /* compress data */ percent_period = ciso_total_block/100; percent_cnt = ciso_total_block/100; align_b = 1<<(ciso.align); align_m = align_b -1; for(block = 0;block < ciso_total_block ; block++) { if(--percent_cnt<=0) { percent_cnt = percent_period; printf("compress %3d%% avarage rate %3d%%\r" ,block / percent_period ,block==0 ? 0 : 100*write_pos/(block*0x800)); } if(!is_ziso) { if (deflateInit2(&z, level , Z_DEFLATED, -15,8,Z_DEFAULT_STRATEGY) != Z_OK) { printf("deflateInit : %s\n", (z.msg) ? z.msg : "???"); return 1; } } /* write align */ align = (int)write_pos & align_m; if(align) { align = align_b - align; if(fwrite(buf4,1,align, fout) != align) { printf("block %d : Write error\n",block); return 1; } write_pos += align; } /* mark offset index */ index_buf[block] = write_pos>>(ciso.align); /* read buffer */ z.next_out = block_buf2; z.avail_out = ciso.block_size*2; z.next_in = block_buf1; z.avail_in = fread(block_buf1, 1, ciso.block_size , fin); if(z.avail_in != ciso.block_size) { printf("block=%d : read error\n",block); return 1; } if(is_ziso) { //status = lzo1x_1_compress(block_buf1, z.avail_in, block_buf2, &lzolen, wrkmem); status = lzo1x_999_compress(block_buf1, z.avail_in, block_buf2, &lzolen, wrkmem); //printf("in: %d out: %d\n", z.avail_in, lzolen); if (status != LZO_E_OK) { /* this should NEVER happen */ printf("compression failed: lzo1x_1_compress: %d\n", status); return 1; } cmp_size = lzolen; } else { /* compress block status = deflate(&z, Z_FULL_FLUSH);*/ status = deflate(&z, Z_FINISH); if (status != Z_STREAM_END) /* if (status != Z_OK) */ { printf("block %d:deflate : %s[%d]\n", block,(z.msg) ? z.msg : "error",status); return 1; } cmp_size = ciso.block_size*2 - z.avail_out; } /* choise plain / compress */ if(cmp_size >= (ciso.block_size - no_comp_diff)) { cmp_size = ciso.block_size; memcpy(block_buf2,block_buf1,cmp_size); /* plain block mark */ index_buf[block] |= 0x80000000; } /* write compressed block */ if(fwrite(block_buf2, 1,cmp_size , fout) != cmp_size) { printf("block %d : Write error\n",block); return 1; } /* mark next index */ write_pos += cmp_size; if(!is_ziso) { /* term zlib */ if (deflateEnd(&z) != Z_OK) { printf("deflateEnd : %s\n", (z.msg) ? z.msg : "error"); return 1; } } } /* last position (total size)*/ index_buf[block] = write_pos>>(ciso.align); /* write header & index block */ fseek(fout,sizeof(ciso),SEEK_SET); fwrite(index_buf,1,index_size,fout); printf("ciso compress completed , total size = %8d bytes , rate %d%%\n" ,(int)write_pos,(int)(write_pos*100/ciso.total_bytes)); return 0; }
/* Loop though files in a package and perform full file property checking. */ int check_pkg_full(alpm_pkg_t *pkg) { const char *root, *pkgname; size_t errors = 0; size_t rootlen; char filepath[PATH_MAX]; struct archive *mtree; struct archive_entry *entry = NULL; size_t file_count = 0; const alpm_list_t *lp; root = alpm_option_get_root(config->handle); rootlen = strlen(root); if(rootlen + 1 > PATH_MAX) { /* we are in trouble here */ pm_printf(ALPM_LOG_ERROR, _("path too long: %s%s\n"), root, ""); return 1; } strcpy(filepath, root); pkgname = alpm_pkg_get_name(pkg); mtree = alpm_pkg_mtree_open(pkg); if(mtree == NULL) { /* TODO: check error to confirm failure due to no mtree file */ if(!config->quiet) { printf(_("%s: no mtree file\n"), pkgname); } return 0; } while(alpm_pkg_mtree_next(pkg, mtree, &entry) == ARCHIVE_OK) { struct stat st; const char *path = archive_entry_pathname(entry); mode_t type; size_t file_errors = 0; int backup = 0; /* strip leading "./" from path entries */ if(path[0] == '.' && path[1] == '/') { path += 2; } if(strcmp(path, ".INSTALL") == 0) { char filename[PATH_MAX]; snprintf(filename, PATH_MAX, "%slocal/%s-%s/install", alpm_option_get_dbpath(config->handle) + 1, pkgname, alpm_pkg_get_version(pkg)); archive_entry_set_pathname(entry, filename); path = archive_entry_pathname(entry); } else if(strcmp(path, ".CHANGELOG") == 0) { char filename[PATH_MAX]; snprintf(filename, PATH_MAX, "%slocal/%s-%s/changelog", alpm_option_get_dbpath(config->handle) + 1, pkgname, alpm_pkg_get_version(pkg)); archive_entry_set_pathname(entry, filename); path = archive_entry_pathname(entry); } else if(*path == '.') { continue; } file_count++; if(rootlen + 1 + strlen(path) > PATH_MAX) { pm_printf(ALPM_LOG_WARNING, _("path too long: %s%s\n"), root, path); continue; } strcpy(filepath + rootlen, path); if(check_file_exists(pkgname, filepath, &st) == 1) { errors++; continue; } type = archive_entry_filetype(entry); if(type != AE_IFDIR && type != AE_IFREG && type != AE_IFLNK) { pm_printf(ALPM_LOG_WARNING, _("file type not recognized: %s%s\n"), root, path); continue; } if(check_file_type(pkgname, filepath, &st, entry) == 1) { errors++; continue; } file_errors += check_file_permissions(pkgname, filepath, &st, entry); if(type == AE_IFLNK) { file_errors += check_file_link(pkgname, filepath, &st, entry); } /* the following checks are expected to fail if a backup file has been modified */ for(lp = alpm_pkg_get_backup(pkg); lp; lp = lp->next) { alpm_backup_t *bl = lp->data; if(strcmp(path, bl->name) == 0) { backup = 1; break; } } if(type != AE_IFDIR) { /* file or symbolic link */ file_errors += check_file_time(pkgname, filepath, &st, entry, backup); } if(type == AE_IFREG) { /* TODO: these are expected to be changed with backup files */ file_errors += check_file_size(pkgname, filepath, &st, entry, backup); /* file_errors += check_file_md5sum(pkgname, filepath, &st, entry, backup); */ } if(config->quiet && file_errors) { printf("%s %s\n", pkgname, filepath); } errors += (file_errors != 0 ? 1 : 0); } alpm_pkg_mtree_close(pkg, mtree); if(!config->quiet) { printf(_n("%s: %jd total file, ", "%s: %jd total files, ", (unsigned long)file_count), pkgname, (intmax_t)file_count); printf(_n("%jd altered file\n", "%jd altered files\n", (unsigned long)errors), (intmax_t)errors); } return (errors != 0 ? 1 : 0); }
void output_dump( const struct Field fldi, const double t) { FILE *ht; int dump_version,i; int size_x, size_y, size_z; int marker, included_field; int nfield; long int filesize; ht=NULL; DEBUG_START_FUNC; size_x = NX; size_y = NY; size_z = NZ; // This is a check when we try to read a dump file dump_version = OUTPUT_DUMP_VERSION; // This is a hard coded marker to check that we have correctly read the file marker = DUMP_MARKER; if(rank==0) { ht=fopen(OUTPUT_DUMP_WRITE,"w"); if(ht==NULL) ERROR_HANDLER( ERROR_CRITICAL, "Error opening dump file."); fwrite(&dump_version, sizeof(int), 1, ht); fwrite(&size_x , sizeof(int), 1, ht); fwrite(&size_y , sizeof(int), 1, ht); fwrite(&size_z , sizeof(int), 1, ht); // Included fields // First bit is Boussinesq fields // Second bit is MHD fields // Other fields can be added from that stage... included_field=0; #ifdef BOUSSINESQ included_field+=1; #endif #ifdef MHD included_field+=2; #endif fwrite(&included_field, sizeof(int), 1, ht); } write_field(ht, fldi.vx); write_field(ht, fldi.vy); write_field(ht, fldi.vz); #ifdef BOUSSINESQ write_field(ht, fldi.th); #endif #ifdef MHD write_field(ht, fldi.bx); write_field(ht, fldi.by); write_field(ht, fldi.bz); #endif if(rank==0) { fwrite(&t , sizeof(double) , 1 , ht); fwrite(&noutput_flow , sizeof(int) , 1 , ht); fwrite(&lastoutput_time , sizeof(double) , 1 , ht); fwrite(&lastoutput_flow , sizeof(double) , 1 , ht); fwrite(&lastoutput_dump , sizeof(double) , 1 , ht); // Any extra information should be put here. if(param.numericalkevol){//AJB 09/03/12 for(i=0;i<9;i++){ fwrite(&kbasis[i],sizeof(double),1,ht); } } fwrite(&marker , sizeof(int) , 1 , ht); // Check everything was fine with the file if(ferror(ht)) ERROR_HANDLER( ERROR_CRITICAL, "Error writing dump file"); fclose(ht); } // predict the file size: nfield = 3; // Velocity fields #ifdef BOUSSINESQ nfield += 1; #endif #ifdef MHD nfield += 3; #endif filesize = nfield * sizeof(double complex) * NTOTAL_COMPLEX * NPROC + 4 * sizeof( double) + 7 * sizeof( int ); if(param.numericalkevol) {//AJB 09/03/12 filesize = nfield * sizeof(double complex) * NTOTAL_COMPLEX * NPROC + 13 * sizeof( double) + 7 * sizeof( int ); } if( check_file_size( OUTPUT_DUMP_WRITE, filesize ) ) { MPI_Printf("Error checking the dump size, got %d instead of %d\n", (int) check_file_size( OUTPUT_DUMP_WRITE, filesize), (int) filesize); ERROR_HANDLER( ERROR_CRITICAL, "Error writing dump file, check your quotas"); } // This bit prevents the code from losing all the dump files (this kind of thing happens sometimes...) // With this routine, one will always have a valid restart dump, either in OUTPUT_DUMP_WRITE, OUTPUT_DUMP or OUTPUT_DUMP_SAV // (it should normally be in OUTPUT_DUMP) if(rank==0) { remove(OUTPUT_DUMP_SAV); // Delete the previously saved output dump rename(OUTPUT_DUMP, OUTPUT_DUMP_SAV); // Save the current dump file rename(OUTPUT_DUMP_WRITE, OUTPUT_DUMP); // Move the new dump file to its final location } if( check_file_size( OUTPUT_DUMP, filesize ) ) { MPI_Printf("Error checking the dump size, got %d instead of %d\n", (int) check_file_size( OUTPUT_DUMP, filesize), (int) filesize); ERROR_HANDLER( ERROR_CRITICAL, "Error writing dump file, check your quotas"); } #ifdef MPI_SUPPORT MPI_Barrier(MPI_COMM_WORLD); #endif DEBUG_END_FUNC; return; }
KSGLogBackend::KSGLogBackend(const std::string& level ,const std::string& appender ,const std::string& filepath) :_level(level),_appender(appender) ,_filepath(filepath),_logfile(NULL) ,_file_max_count(1),_file_max_size(_MAX_SIZE) ,_file_curr_size(0) #ifndef WIN32 ,_syslog(NULL) #endif { if(_level == "debug") _priority = LM_DEBUG; else if(_level == "info" ) _priority = LM_INFO; else if(_level == "error" ) _priority = LM_ERROR; else if(_level == "notice") _priority = LM_NOTICE; else if(_level == "alert") _priority = LM_ALERT; else _priority = LM_INFO; std::list<std::string> alist; _log_app = 0; std::back_insert_iterator<std::list<std::string> > iter(alist); xutil::StringUtil::SpliteString(_appender,",",iter); for(std::list<std::string>::iterator i = alist.begin();i != alist.end(); ++i) { if("stdout" == *i) _log_app |= laStdout; else if("file" == *i) _log_app |= laFile; else if("syslog" == *i) _log_app |= laSyslog; } // 默认为 stdout if(!_log_app) _log_app = laStdout; if((_log_app & laFile) && (_filepath.length() != 0)) { // open log file _logfile = new ofstream(_filepath.c_str(),std::ios_base::out|std::ios_base::app); if(_logfile->bad()) { ACE_DEBUG((LM_ERROR,"打开日志文件失败")); } else { _file_curr_size = ACE_OS::filesize(_filepath.c_str()); check_file_size(0); } } #ifndef WIN32 if(_log_app & laSyslog) { _syslog = new ACE_Log_Msg_UNIX_Syslog; _syslog->open("gwmain"); } #endif }
/**************************************************************************** compress ISO ****************************************************************************/ int comp_ciso(int level) { unsigned long long file_size; unsigned long long write_pos; int index_size; int block; unsigned char buf4[64]; int cmp_size; int status; int percent_period; int percent_cnt; int align,align_b,align_m; file_size = check_file_size(fin); if(file_size<0) { fprintf(stderr, "Can't get file size\n"); return 1; } // allocate index block index_size = (ciso_total_block + 1 ) * sizeof(unsigned int); index_buf = malloc(index_size); crc_buf = malloc(index_size); block_buf1 = malloc(ciso.block_size); block_buf2 = malloc(ciso.block_size*2); if( !index_buf || !crc_buf || !block_buf1 || !block_buf2 ) { perror("can't allocate memory\n"); return 1; } memset(index_buf,0,index_size); memset(crc_buf,0,index_size); memset(buf4,0,sizeof(buf4)); // init zlib z.zalloc = Z_NULL; z.zfree = Z_NULL; z.opaque = Z_NULL; // show info fprintf(stderr, "Compress '%s' to '%s'\n",fname_in,fname_out); fprintf(stderr, "Total File Size %lld bytes\n",ciso.total_bytes); fprintf(stderr, "block size %d bytes\n",ciso.block_size); fprintf(stderr, "index align %d\n",1<<ciso.align); fprintf(stderr, "compress level %d\n",level); // write header block fwrite(&ciso,1,sizeof(ciso),fout); // dummy write index block fwrite(index_buf,1,index_size,fout); write_pos = sizeof(ciso) + index_size; // compress data percent_period = ciso_total_block/100; percent_cnt = 0; align_b = 1<<(ciso.align); align_m = align_b -1; for(block = 0;block < ciso_total_block ; block++) { if(--percent_cnt<=0) { percent_cnt = percent_period; fprintf(stderr, "compress %3d%% average rate %3lld%%\r" ,block / percent_period ,block==0 ? 0 : 100*write_pos/(block*2352)); } if (deflateInit2(&z, level , Z_DEFLATED, -15,8,Z_DEFAULT_STRATEGY) != Z_OK) { fprintf(stderr, "deflateInit : %s\n", (z.msg) ? z.msg : "???"); return 1; } // write align align = (int)write_pos & align_m; if(align) { align = align_b - align; if(fwrite(buf4,1,align, fout) != align) { fprintf(stderr, "block %d : Write error\n",block); return 1; } write_pos += align; } // mark offset index index_buf[block] = write_pos>>(ciso.align); // read buffer z.next_out = block_buf2; z.avail_out = ciso.block_size*2; z.next_in = block_buf1; z.avail_in = fread(block_buf1, 1, ciso.block_size , fin); if(z.avail_in != ciso.block_size) { fprintf(stderr, "block=%d : read error\n",block); return 1; } // compress block // status = deflate(&z, Z_FULL_FLUSH); status = deflate(&z, Z_FINISH); if (status != Z_STREAM_END) // if (status != Z_OK) { fprintf(stderr, "block %d:deflate : %s[%d]\n", block,(z.msg) ? z.msg : "error",status); return 1; } cmp_size = ciso.block_size*2 - z.avail_out; // choise plain / compress if(cmp_size >= ciso.block_size) { cmp_size = ciso.block_size; memcpy(block_buf2,block_buf1,cmp_size); // plain block mark index_buf[block] |= 0x80000000; } // write compressed block if(fwrite(block_buf2, 1,cmp_size , fout) != cmp_size) { fprintf(stderr, "block %d : Write error\n",block); return 1; } // mark next index write_pos += cmp_size; // term zlib if (deflateEnd(&z) != Z_OK) { fprintf(stderr, "deflateEnd : %s\n", (z.msg) ? z.msg : "error"); return 1; } } // last position (total size) index_buf[block] = write_pos>>(ciso.align); // write header & index block fseek(fout,sizeof(ciso),SEEK_SET); fwrite(index_buf,1,index_size,fout); fprintf(stderr, "ciso compress completed , total size = %8d bytes , rate %d%%\n" ,(int)write_pos,(int)(write_pos*100/ciso.total_bytes)); return 0; }