static imgtoolerr_t os9_diskimage_createdir(imgtool_partition *partition, const char *path) { imgtoolerr_t err; imgtool_image *image = imgtool_partition_image(partition); struct os9_fileinfo file_info; UINT8 dir_data[64]; UINT32 parent_lsn; err = os9_lookup_path(image, path, CREATE_DIR, &file_info, &parent_lsn, NULL, NULL); if (err) goto done; err = os9_set_file_size(image, &file_info, 64); if (err) goto done; /* create intial directories */ memset(dir_data, 0, sizeof(dir_data)); place_string(dir_data, 0, 32, ".."); place_integer_be(dir_data, 29, 3, parent_lsn); place_string(dir_data, 32, 32, "."); place_integer_be(dir_data, 61, 3, file_info.lsn); err = os9_write_lsn(image, file_info.sector_map[0].lsn, 0, dir_data, sizeof(dir_data)); if (err) goto done; done: return err; }
static imgtoolerr_t os9_diskimage_writefile(imgtool_partition *partition, const char *path, const char *fork, imgtool_stream *sourcef, option_resolution *opts) { imgtoolerr_t err; imgtool_image *image = imgtool_partition_image(partition); struct os9_fileinfo file_info; size_t write_size; void *buf = NULL; int i = -1; UINT32 lsn = 0; UINT32 count = 0; UINT32 sz; const os9_diskinfo *disk_info; disk_info = os9_get_diskinfo(image); buf = malloc(disk_info->sector_size); if (!buf) { err = IMGTOOLERR_OUTOFMEMORY; goto done; } err = os9_lookup_path(image, path, CREATE_FILE, &file_info, NULL, NULL, NULL); if (err) goto done; sz = (UINT32) stream_size(sourcef); err = os9_set_file_size(image, &file_info, sz); if (err) goto done; while(sz > 0) { write_size = (size_t) MIN(sz, (UINT64) disk_info->sector_size); stream_read(sourcef, buf, write_size); while(count == 0) { i++; lsn = file_info.sector_map[i].lsn; count = file_info.sector_map[i].count; } err = os9_write_lsn(image, lsn, 0, buf, write_size); if (err) goto done; lsn++; count--; sz -= write_size; } done: if (buf) free(buf); return err; }
static imgtoolerr_t os9_deallocate_lsn(imgtool_image *image, UINT32 lsn) { UINT8 mask; os9_diskinfo *disk_info; disk_info = os9_get_diskinfo(image); mask = 1 << (7 - (lsn % 8)); disk_info->allocation_bitmap[lsn / 8] &= ~mask; return os9_write_lsn(image, 1, 0, disk_info->allocation_bitmap, disk_info->allocation_bitmap_bytes); }
static imgtoolerr_t os9_allocate_lsn(imgtool_image *image, UINT32 *lsn) { UINT32 i; os9_diskinfo *disk_info; UINT8 b, mask; disk_info = os9_get_diskinfo(image); for (i = 0; i < disk_info->total_sectors; i++) { b = disk_info->allocation_bitmap[i / 8]; mask = 1 << (7 - (i % 8)); if ((b & mask) == 0) { disk_info->allocation_bitmap[i / 8] |= mask; *lsn = i; return os9_write_lsn(image, 1, 0, disk_info->allocation_bitmap, disk_info->allocation_bitmap_bytes); } } return IMGTOOLERR_NOSPACE; }
static imgtoolerr_t os9_lookup_path(imgtool_image *img, const char *path, creation_policy_t create, struct os9_fileinfo *file_info, UINT32 *parent_lsn, UINT32 *dirent_lsn, UINT32 *dirent_index) { imgtoolerr_t err = IMGTOOLERR_SUCCESS; struct os9_fileinfo dir_info; UINT32 index, current_lsn, dir_size; UINT32 entry_index = 0; UINT32 free_entry_index = 0xffffffff; UINT32 entry_lsn = 0; UINT32 allocated_lsn = 0; UINT8 entry[32]; UINT8 block[64]; char *filename; const os9_diskinfo *disk_info; disk_info = os9_get_diskinfo(img); current_lsn = disk_info->root_dir_lsn; if (parent_lsn) *parent_lsn = 0; /* we have to transverse each path element */ while(*path) { if (parent_lsn) *parent_lsn = current_lsn; /* decode the directory header of this directory */ err = os9_decode_file_header(img, current_lsn, &dir_info); if (err) goto done; dir_size = dir_info.file_size; /* sanity check directory */ if (!dir_info.directory) { err = (current_lsn == disk_info->root_dir_lsn) ? IMGTOOLERR_CORRUPTIMAGE : IMGTOOLERR_INVALIDPATH; goto done; } /* walk the directory */ for (index = 0; index < dir_size; index += 32) { entry_index = index; entry_lsn = os9_lookup_lsn(img, &dir_info, &entry_index); err = os9_read_lsn(img, entry_lsn, entry_index, entry, sizeof(entry)); if (err) goto done; /* remember first free entry found */ if( free_entry_index == 0xffffffff ) { if( entry[0] == 0 ) free_entry_index = index; } if (os9_interpret_dirent(entry, &filename, ¤t_lsn, NULL)) { if (!strcmp(path, filename)) break; } } /* at the end of the file? */ if (index >= dir_size) { /* if we're not creating, or we are creating but we have not fully * transversed the directory, error out */ if (!create || path[strlen(path) + 1]) { err = IMGTOOLERR_PATHNOTFOUND; goto done; } /* allocate a new LSN */ err = os9_allocate_lsn(img, &allocated_lsn); if (err) goto done; /* write the file */ memset(block, 0, sizeof(block)); place_integer_be(block, 0, 1, 0x1B | ((create == CREATE_DIR) ? 0x80 : 0x00)); err = os9_write_lsn(img, allocated_lsn, 0, block, sizeof(block)); if (err) goto done; if( free_entry_index == 0xffffffff ) { /* expand the directory to hold the new entry */ err = os9_set_file_size(img, &dir_info, dir_size + 32); if (err) goto done; } else /* use first unused entry in directory */ dir_size = free_entry_index; /* now prepare the directory entry */ memset(entry, 0, sizeof(entry)); place_string(entry, 0, 28, path); place_integer_be(entry, 29, 3, allocated_lsn); /* now write the directory entry */ entry_index = dir_size; entry_lsn = os9_lookup_lsn(img, &dir_info, &entry_index); err = os9_write_lsn(img, entry_lsn, entry_index, entry, 32); if (err) goto done; /* directory entry append complete; no need to hold this lsn */ current_lsn = allocated_lsn; allocated_lsn = 0; } path += strlen(path) + 1; } if (file_info) { err = os9_decode_file_header(img, current_lsn, file_info); if (err) goto done; } if (dirent_lsn) *dirent_lsn = entry_lsn; if (dirent_index) *dirent_index = entry_index; done: if (allocated_lsn != 0) os9_deallocate_lsn(img, allocated_lsn); return err; }
static imgtoolerr_t os9_set_file_size(imgtool_image *image, struct os9_fileinfo *file_info, UINT32 new_size) { imgtoolerr_t err; const os9_diskinfo *disk_info; UINT32 new_lsn_count, current_lsn_count; UINT32 free_lsns, lsn, i; int sector_map_length = -1; UINT8 header[256]; /* easy way out? */ if (file_info->file_size == new_size) return IMGTOOLERR_SUCCESS; disk_info = os9_get_diskinfo(image); free_lsns = os9_get_free_lsns(image); current_lsn_count = (file_info->file_size + disk_info->sector_size - 1) / disk_info->sector_size; new_lsn_count = (new_size + disk_info->sector_size - 1) / disk_info->sector_size; /* check to see if the file is growing and we do not have enough space */ if ((new_lsn_count > current_lsn_count) && (new_lsn_count - current_lsn_count > free_lsns)) return IMGTOOLERR_NOSPACE; if (current_lsn_count != new_lsn_count) { /* first find out the size of our sector map */ sector_map_length = 0; lsn = 0; while((lsn < current_lsn_count) && (sector_map_length < ARRAY_LENGTH(file_info->sector_map))) { if (file_info->sector_map[sector_map_length].count == 0) return os9_corrupt_file_error(file_info); lsn += file_info->sector_map[sector_map_length].count; sector_map_length++; } /* keep in mind that the sector_map might not parallel our expected * current LSN count; we should tolerate this abnormality */ current_lsn_count = lsn; while(current_lsn_count > new_lsn_count) { /* shrink this file */ lsn = file_info->sector_map[sector_map_length - 1].lsn + file_info->sector_map[sector_map_length - 1].count - 1; err = os9_deallocate_lsn(image, lsn); if (err) return err; file_info->sector_map[sector_map_length - 1].count--; while(sector_map_length > 0 && (file_info->sector_map[sector_map_length - 1].count == 0)) sector_map_length--; current_lsn_count--; } while(current_lsn_count < new_lsn_count) { /* grow this file */ err = os9_allocate_lsn(image, &lsn); if (err) return err; if ((sector_map_length > 0) && ((file_info->sector_map[sector_map_length - 1].lsn + file_info->sector_map[sector_map_length - 1].count) == lsn)) { file_info->sector_map[sector_map_length - 1].count++; } else if (sector_map_length >= ARRAY_LENGTH(file_info->sector_map)) { return IMGTOOLERR_NOSPACE; } else { file_info->sector_map[sector_map_length].lsn = lsn; file_info->sector_map[sector_map_length].count = 1; sector_map_length++; file_info->sector_map[sector_map_length].lsn = 0; file_info->sector_map[sector_map_length].count = 0; } current_lsn_count++; } } /* now lets write back the sector */ err = os9_read_lsn(image, file_info->lsn, 0, header, sizeof(header)); if (err) return err; file_info->file_size = new_size; place_integer_be(header, 9, 4, file_info->file_size); /* do we have to write the sector map? */ if (sector_map_length >= 0) { for (i = 0; i < MIN(sector_map_length + 1, ARRAY_LENGTH(file_info->sector_map)); i++) { place_integer_be(header, 16 + (i * 5) + 0, 3, file_info->sector_map[i].lsn); place_integer_be(header, 16 + (i * 5) + 3, 2, file_info->sector_map[i].count); } } err = os9_write_lsn(image, file_info->lsn, 0, header, disk_info->sector_size); if (err) return err; return IMGTOOLERR_SUCCESS; }
static imgtoolerr_t os9_diskimage_delete(imgtool_partition *partition, const char *path, unsigned int delete_directory) { imgtoolerr_t err; imgtool_image *image = imgtool_partition_image(partition); //const os9_diskinfo *disk_info; struct os9_fileinfo file_info; UINT32 dirent_lsn, dirent_index; UINT32 entry_lsn, entry_index; UINT32 i, j, lsn; UINT8 b; //disk_info = os9_get_diskinfo(image); err = os9_lookup_path(image, path, CREATE_NONE, &file_info, NULL, &dirent_lsn, &dirent_index); if (err) return err; if (file_info.directory != delete_directory) return IMGTOOLERR_FILENOTFOUND; /* make sure that if we are deleting a directory, it is empty */ if (delete_directory) { for (i = 64; i < file_info.file_size; i += 32) { entry_index = i; entry_lsn = os9_lookup_lsn(image, &file_info, &entry_index); err = os9_read_lsn(image, entry_lsn, entry_index, &b, 1); if (err) return err; /* this had better be a deleted file, if not we can't delete */ if (b != 0) return IMGTOOLERR_DIRNOTEMPTY; } } /* zero out the file entry */ b = '\0'; err = os9_write_lsn(image, dirent_lsn, dirent_index, &b, 1); if (err) return err; /* get the link count */ err = os9_read_lsn(image, file_info.lsn, 8, &b, 1); if (err) return err; if (b > 0) b--; if (b > 0) { /* link count is greater than zero */ err = os9_write_lsn(image, file_info.lsn, 8, &b, 1); if (err) return err; } else { /* no more links; outright delete the file */ err = os9_deallocate_lsn(image, file_info.lsn); if (err) return err; for (i = 0; (i < ARRAY_LENGTH(file_info.sector_map)) && file_info.sector_map[i].count; i++) { lsn = file_info.sector_map[i].lsn; for (j = 0; j < file_info.sector_map[i].count; j++) { err = os9_deallocate_lsn(image, lsn + j); if (err) return err; } } } return IMGTOOLERR_SUCCESS; }