/* Save the current image to disk. We don't throw any exceptions here because if the 'then-die' argument is t it is not safe to do so. Instead we signal failure by returning false. */ bool factor_vm::save_image(const vm_char* saving_filename, const vm_char* filename) { image_header h; h.magic = image_magic; h.version = image_version; h.data_relocation_base = data->tenured->start; h.data_size = data->tenured->occupied_space(); h.code_relocation_base = code->allocator->start; h.code_size = code->allocator->occupied_space(); for (cell i = 0; i < special_object_count; i++) h.special_objects[i] = (save_special_p(i) ? special_objects[i] : false_object); FILE* file = OPEN_WRITE(saving_filename); if (file == NULL) return false; if (safe_fwrite(&h, sizeof(image_header), 1, file) != 1) return false; if (safe_fwrite((void*)data->tenured->start, h.data_size, 1, file) != 1) return false; if (safe_fwrite((void*)code->allocator->start, h.code_size, 1, file) != 1) return false; if (raw_fclose(file) == -1) return false; if (!move_file(saving_filename, filename)) return false; return true; }
/* NAME:allocate_shot_memory PURPOSE: Create files to store final preconditioner and gradient results. Must be initialized with zeroes. outputfolder (in) folder where snapshot data is store VolumeMemory (in) memory needed to store the domain RETURN none */ void create_output_volumes(char *outputfolder, integer VolumeMemory) { log_info ( "Creating output files in %s", outputfolder); #ifndef DO_NOT_PERFOM_IO char fnamePrecond[300], fnameGradient[300]; sprintf( fnameGradient, "%s/resultGradient.res", outputfolder); sprintf( fnamePrecond , "%s/resultPrecond.res", outputfolder); FILE *fGradient = safe_fopen( fnameGradient, "wb", __FILE__, __LINE__ ); FILE *fPrecond = safe_fopen( fnamePrecond , "wb", __FILE__, __LINE__ ); int numIts = ceil( VolumeMemory / IO_CHUNK_SIZE ); /* create buffer array */ real *tmparray = (real*) __malloc( ALIGN_INT, IO_CHUNK_SIZE ); /* perform the accumulation of the chunks */ for (int i=0; i<numIts; i++) { safe_fwrite(tmparray, 1, IO_CHUNK_SIZE, fGradient, __FILE__, __LINE__ ); safe_fwrite(tmparray, 1, IO_CHUNK_SIZE, fPrecond , __FILE__, __LINE__ ); } __free(tmparray); // close files safe_fclose( fnameGradient, fGradient, __FILE__, __LINE__ ); safe_fclose( fnamePrecond , fPrecond , __FILE__, __LINE__ ); #endif log_info ("Output volumes created correctly"); }
/* Save the current image to disk */ bool factor_vm::save_image(const vm_char *saving_filename, const vm_char *filename) { FILE* file; image_header h; file = OPEN_WRITE(saving_filename); if(file == NULL) { std::cout << "Cannot open image file: " << saving_filename << std::endl; std::cout << strerror(errno) << std::endl; return false; } h.magic = image_magic; h.version = image_version; h.data_relocation_base = data->tenured->start; h.data_size = data->tenured->occupied_space(); h.code_relocation_base = code->seg->start; h.code_size = code->allocator->occupied_space(); h.true_object = true_object; h.bignum_zero = bignum_zero; h.bignum_pos_one = bignum_pos_one; h.bignum_neg_one = bignum_neg_one; for(cell i = 0; i < special_object_count; i++) h.special_objects[i] = (save_special_p(i) ? special_objects[i] : false_object); bool ok = true; if(safe_fwrite(&h,sizeof(image_header),1,file) != 1) ok = false; if(safe_fwrite((void*)data->tenured->start,h.data_size,1,file) != 1) ok = false; if(safe_fwrite(code->allocator->first_block(),h.code_size,1,file) != 1) ok = false; safe_fclose(file); if(!ok) std::cout << "save-image failed: " << strerror(errno) << std::endl; else move_file(saving_filename,filename); return ok; }
void save_to_file (FILE* file) { logger.debug() << "Saving " << Pos::numUsed() << " Obs to file" |0; Logging::IndentBlock block; const size_t FIELD_SIZE = sizeof(Int); for (Pos::iterator i=Pos::begin(); i!=Pos::end(); ++i) { Pos pos = *i; safe_fwrite(&(pos(BOOL_PROPERTIES)), FIELD_SIZE, 1, file); } }
void factor_vm::primitive_fwrite() { FILE* file = pop_file_handle(); cell length = to_cell(ctx->pop()); char* text = alien_offset(ctx->pop()); if (length == 0) return; size_t written = safe_fwrite(text, 1, length, file); if (written != length) io_error_if_not_EINTR(); }
static void file_store(void *f, void *data, long length, long offset) { safe_fseek(f, offset, SEEK_SET); safe_fwrite(data, length, 1, f); }
int do_upgrade(char *url, webs_t stream, int *total) { char upload_file[] = "/tmp/ezp_firmware_XXXXXX"; FILE *fifo = NULL; FILE *tmp_fifo = NULL; char *write_argv[4]; pid_t pid; char *buf = NULL; int count; int ret = 0; long flags = -1; int size = BUFSIZ; struct stat st; int upgrade_cmd_complete = 0; #if defined(PLATFORM_LIS) || defined(PLATFORM_AXA) FILE *pFile; char result[24]={0}; int percentage=0; if ((pFile = popen("ezp-i2c gauge percentage", "r"))) { fgets(result, sizeof(result), pFile); sscanf(result,"Battery: %d %%", &percentage); } // terminate pclose (pFile); if (percentage < 60) { ret=2; goto err; } #endif system("/sbin/stop_services.sh"); start_upgrade_indicator(); /* FIRMWARE upgrade utility */ write_argv[0] = "/usr/sbin/upgraded"; write_argv[1] = upload_file; write_argv[2] = NULL; // system("/tmp/ezp-i2c gauge upgrade start"); // system("/sbin/reserve_link.sh"); mkstemp(upload_file) ; if (!(fifo = fopen(upload_file, "w"))) { ret = errno; goto err; } #ifdef DEBUG printf("Random filename %s\n", upload_file); #endif /* Set nonblock on the socket so we can timeout */ if (!do_ssl) { if ((flags = fcntl(fileno(stream), F_GETFL)) < 0 || fcntl(fileno(stream), F_SETFL, flags | O_NONBLOCK) < 0) { ret = errno; goto err; } } /* ** The buffer must be at least as big as what the stream file is ** using so that it can read all the data that has been buffered ** in the stream file. Otherwise it would be out of sync with fn ** select specially at the end of the data stream in which case ** the select tells there is no more data available but there in ** fact is data buffered in the stream file's buffer. Since no ** one has changed the default stream file's buffer size, let's ** use the constant BUFSIZ until someone changes it. **/ if ((buf = malloc(size)) == NULL) { ret = ENOMEM; goto err; } #ifdef DEBUG printf("Uploading the firmware %s\n", upload_file); #endif /* Upload the file first. */ while (total && *total) { if (do_ssl) { if (size > *total) size = *total; count = wfread(buf, 1, size, stream); } else { if (waitfor(fileno(stream), 30) <= 0) { cprintf("waitfor timeout 30 secs\n"); ret = errno; goto err; } count = safe_fread(buf, 1, size, stream); if (!count && (ferror(stream) || feof(stream))) { ret = errno; goto err; } } safe_fwrite(buf, 1, count, fifo); *total -= count; #ifdef DEBUG printf("."); #endif } fclose(fifo); fifo = NULL; if (!do_ssl) { /* Reset nonblock on the socket */ if (fcntl(fileno(stream), F_SETFL, flags) < 0) { ret = errno; goto err; } } /* Data transfer finished, bring down devices. */ //system("/sbin/ifdown wan0"); //system("/sbin/ifdown wan1"); //system("/sbin/ifdown lan0"); _eval(write_argv, NULL, 0, &pid) ; upgrade_cmd_complete = 1; while((stat("/tmp/fw_incorrect",&st) != 0) && (stat("/tmp/fw_correct",&st) != 0)){ usleep(200000); } if(stat("/tmp/fw_incorrect", &st) == 0) ret = 1; else if(stat("/tmp/fw_correct", &st) == 0) ret = 0; #ifdef DEBUG printf("done with ret:%d\n", ret); #endif err: if (buf) { free(buf); } if (fifo) { fclose(fifo); } if (tmp_fifo) { fclose(tmp_fifo); } #ifdef DEBUG if (ret) { perror(NULL); } #endif if(!upgrade_cmd_complete) { system("/tmp/ezp-i2c gauge upgrade finish"); } /* ret value * 1 : upgrade fail * 2 : low power * other : upgrade success * */ return ret; }
int import_config(char *url, webs_t stream, int *total) { char upload_file[] = "/tmp/ezp_cfg_XXXXXX"; char decoded_file[] = "/tmp/decoded_cfg.cfg"; FILE *fifo = NULL; FILE *tmp_fifo = NULL; char *buf = NULL; int c; int count; int ret = 0; long flags = -1; int size = BUFSIZ; mkstemp(upload_file) ; if (!(fifo = fopen(upload_file, "w"))) { ret = errno; goto err; } #ifdef DEBUG printf("Random filename %s\n", upload_file); #endif /* Set nonblock on the socket so we can timeout */ if (!do_ssl) { if ((flags = fcntl(fileno(stream), F_GETFL)) < 0 || fcntl(fileno(stream), F_SETFL, flags | O_NONBLOCK) < 0) { ret = errno; goto err; } } /* ** The buffer must be at least as big as what the stream file is ** using so that it can read all the data that has been buffered ** in the stream file. Otherwise it would be out of sync with fn ** select specially at the end of the data stream in which case ** the select tells there is no more data available but there in ** fact is data buffered in the stream file's buffer. Since no ** one has changed the default stream file's buffer size, let's ** use the constant BUFSIZ until someone changes it. **/ if ((buf = malloc(size)) == NULL) { ret = ENOMEM; goto err; } #ifdef DEBUG printf("Uploading the configuration file %s\n", upload_file); #endif /* Upload the configuration file first. */ while (total && *total) { if (do_ssl) { if (size > *total) size = *total; count = wfread(buf, 1, size, stream); } else { if (waitfor(fileno(stream), 30) <= 0) { cprintf("waitfor timeout 30 secs\n"); break; } count = safe_fread(buf, 1, size, stream); if (!count && (ferror(stream) || feof(stream))) break; } safe_fwrite(buf, 1, count, fifo); *total -= count; printf("total:%d\n", *total); #ifdef DEBUG printf("."); #endif } fclose(fifo); fifo = NULL; /* cfg file needs to be decoded. */ if (!(fifo = fopen(upload_file, "r"))) { ret = errno; goto err; } if (!(tmp_fifo = fopen(decoded_file, "w"))) { ret = errno; goto err; } /* Decoding procedure */ /* Note: c must be an integer to compare with EOF. */ while ((c = fgetc(fifo)) != EOF) { decode((char *)&c, 1); fputc(c, tmp_fifo); } fclose(fifo); fifo = NULL; fclose(tmp_fifo); tmp_fifo = NULL; if (!do_ssl) { /* Reset nonblock on the socket */ if (fcntl(fileno(stream), F_SETFL, flags) < 0) { ret = errno; goto err; } } ret=nvram_import(decoded_file); #ifdef DEBUG printf("done\n"); #endif err: if (buf) { free(buf); } if (fifo) { fclose(fifo); } if (tmp_fifo) { fclose(tmp_fifo); } #ifdef DEBUG if (ret) { perror(NULL); } #endif return ret; }
int // sys_upgrade(char *url, FILE *stream, int *total) sys_upgrade(char *url, webs_t stream, int *total, int type) // jimmy, // https, // 8/6/2003 { #ifndef ANTI_FLASH char upload_fifo[] = "/tmp/uploadXXXXXX"; FILE *fifo = NULL; char *write_argv[4]; pid_t pid; char *buf = NULL; int count, ret = 0; long flags = -1; int size = BUFSIZ; int i = 0; { write_argv[0] = "write"; write_argv[1] = upload_fifo; write_argv[2] = "linux"; write_argv[3] = NULL; } // diag_led(DIAG, START_LED); // blink the diag led C_led(1); #ifdef HAVE_HTTPS if (do_ssl) ACTION("ACT_WEBS_UPGRADE"); else #endif ACTION("ACT_WEB_UPGRADE"); /* * Feed write from a temporary FIFO */ if (!mktemp(upload_fifo) || !(fifo = fopen(upload_fifo, "w"))) { if (!ret) ret = errno; goto err; } /* * Set nonblock on the socket so we can timeout */ /* ** The buffer must be at least as big as what the stream file is ** using so that it can read all the data that has been buffered ** in the stream file. Otherwise it would be out of sync with fn ** select specially at the end of the data stream in which case ** the select tells there is no more data available but there in ** fact is data buffered in the stream file's buffer. Since no ** one has changed the default stream file's buffer size, let's ** use the constant BUFSIZ until someone changes it. **/ if (size < MIN_BUF_SIZE) size = MIN_BUF_SIZE; if ((buf = safe_malloc(size)) == NULL) { ret = ENOMEM; goto err; } /* * Pipe the rest to the FIFO */ cprintf("Upgrading\n"); // while (total && *total) { wfread(&buf[0], 1, 5, stream); *total -= 5; if (buf[0] != 'R' || buf[1] != 'B' || buf[2] != '5' || buf[3] != '0' || buf[4] != '0') { ret = -1; goto err; } int linuxsize; int fssize; wfread(&linuxsize, 1, 4, stream); wfread(&fssize, 1, 4, stream); *total -= 8; safe_fwrite(&linuxsize, 1, 4, fifo); safe_fwrite(&fssize, 1, 4, fifo); linuxsize += fssize; for (i = 0; i < linuxsize / MIN_BUF_SIZE; i++) { wfread(&buf[0], 1, MIN_BUF_SIZE, stream); fwrite(&buf[0], 1, MIN_BUF_SIZE, fifo); } wfread(&buf[0], 1, linuxsize % MIN_BUF_SIZE, stream); fwrite(&buf[0], 1, linuxsize % MIN_BUF_SIZE, fifo); *total -= linuxsize; } fclose(fifo); fifo = NULL; fifo = fopen(upload_fifo, "rb"); unsigned long linuxsize; unsigned long fssize; linuxsize = 0; linuxsize += getc(fifo); linuxsize += getc(fifo) * 256; linuxsize += getc(fifo) * 256 * 256; linuxsize += getc(fifo) * 256 * 256 * 256; fssize = 0; fssize += getc(fifo); fssize += getc(fifo) * 256; fssize += getc(fifo) * 256 * 256; fssize += getc(fifo) * 256 * 256 * 256; fprintf(stderr, "Write Linux %d\n", linuxsize); FILE *out = fopen("/dev/cf/card0/part1", "wb"); for (i = 0; i < linuxsize; i++) putc(getc(fifo), out); fclose(out); fprintf(stderr, "Write FileSys %d\n", fssize); out = fopen("/dev/cf/card0/part2", "wb"); for (i = 0; i < fssize; i++) putc(getc(fifo), out); fclose(out); /* * Wait for write to terminate */ // waitpid (pid, &ret, 0); cprintf("done\n"); ret = 0; err: if (buf) free(buf); if (fifo) fclose(fifo); unlink(upload_fifo); // diag_led(DIAG, STOP_LED); // C_led (0); fprintf(stderr, "Idle\n"); ACTION("ACT_IDLE"); return ret; #else return 0; #endif }