int snapshot_write_file(struct volume *v, int block, char *file, uint32_t seq, uint32_t type) { uint32_t md5[4] = { 0 }; struct file_header hdr; struct stat s; char buffer[256]; int in = 0, len, offset; int ret = -1; if (stat(file, &s) || md5sum(file, md5)) { ULOG_ERR("stat failed on %s\n", file); goto out; } if ((block * v->block_size) + pad_file_size(v, s.st_size) > v->size) { ULOG_ERR("upgrade is too big for the flash\n"); goto out; } volume_erase(v, block * v->block_size, pad_file_size(v, s.st_size)); volume_erase(v, block * v->block_size + pad_file_size(v, s.st_size), v->block_size); hdr.length = s.st_size; hdr.magic = OWRT; hdr.type = type; hdr.seq = seq; memcpy(hdr.md5, md5, sizeof(md5)); hdr_to_be32(&hdr); if (volume_write(v, &hdr, block * v->block_size, sizeof(struct file_header))) { ULOG_ERR("failed to write header\n"); goto out; } in = open(file, O_RDONLY); if (in < 1) { ULOG_ERR("failed to open %s\n", file); goto out; } offset = (block * v->block_size) + sizeof(struct file_header); while ((len = read(in, buffer, sizeof(buffer))) > 0) { if (volume_write(v, buffer, offset, len) < 0) goto out; offset += len; } ret = 0; out: if (in > 0) close(in); return ret; }
static void change_volume(void) { int lim = volume_get_max(); volume_write( widgets_preferences[0].d.thumbbar.value * lim / VOLUME_SCALE, widgets_preferences[1].d.thumbbar.value * lim / VOLUME_SCALE); }