Daisuke Nojiri | a5ae62e | 2015-11-03 15:04:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The ChromiumOS Authors. All rights reserved. |
| 3 | * written by Daisuke Nojiri <dnojiri@chromium.org> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; version 2 of the License. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | |
| 15 | #include "archive.h" |
| 16 | #include <endian.h> |
| 17 | #include <errno.h> |
| 18 | #include <libgen.h> |
| 19 | #include <stdint.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | |
| 24 | static struct directory *archive; |
| 25 | |
| 26 | static void usage(void) |
| 27 | { |
| 28 | printf("Name:\n"); |
| 29 | printf(" archive - concatenate files and create an archive\n"); |
| 30 | printf("Usage:\n"); |
| 31 | printf(" archive archive_name create file0 file1 ...\n"); |
| 32 | } |
| 33 | |
| 34 | static int get_file_size(const char *file) |
| 35 | { |
| 36 | FILE *fp = fopen(file, "rb"); |
| 37 | int size; |
| 38 | |
| 39 | if (!fp) { |
| 40 | fprintf(stderr, "Error: failed to open %s\n", file); |
| 41 | return -1; |
| 42 | } |
| 43 | fseek(fp, 0, SEEK_END); |
| 44 | size = ftell(fp); |
| 45 | fclose(fp); |
| 46 | if (size < 0) { |
| 47 | fprintf(stderr, "Error: failed to get file size\n"); |
| 48 | return -1; |
| 49 | } |
| 50 | |
| 51 | return size; |
| 52 | } |
| 53 | |
| 54 | static int set_file_name(const char *path, struct dentry *dest) |
| 55 | { |
| 56 | struct dentry *entry; |
| 57 | char *name, *copy; |
| 58 | int i; |
| 59 | |
| 60 | copy = strdup(path); |
| 61 | name = basename(copy); |
| 62 | |
| 63 | /* check name length */ |
| 64 | if (strlen(name) > NAME_LENGTH) { |
| 65 | fprintf(stderr, "Error: file name '%s' exceeds %d chars\n", |
| 66 | name, NAME_LENGTH); |
| 67 | free(copy); |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | /* check if there is a duplicate name */ |
| 72 | entry = get_first_dentry(archive); |
| 73 | for (i = 0; i < archive->count && &entry[i] != dest; i++) { |
| 74 | if (!strncmp(entry[i].name, name, NAME_LENGTH)) { |
| 75 | fprintf(stderr, "Error: duplicate name '%s'\n", name); |
| 76 | free(copy); |
| 77 | return -1; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /* copy the name to the entry */ |
| 82 | strncpy(dest->name, name, NAME_LENGTH); |
| 83 | free(copy); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * Add a file to the archive in RAM |
| 90 | * |
| 91 | * path: path to the file to be added |
| 92 | * entry: pointer to struct dentry where file header is created |
| 93 | * offset: offset of the file contents from the archive header |
| 94 | * |
| 95 | * return: 0 on success or -1 on error |
| 96 | */ |
| 97 | static int add_file(const char *path, struct dentry *entry, uint32_t offset) |
| 98 | { |
| 99 | FILE *fp; |
| 100 | int size; |
| 101 | |
| 102 | if (!path || !*path || !entry) { |
| 103 | fprintf(stderr, "Error: invalid path or entry\n"); |
| 104 | return -1; |
| 105 | } |
| 106 | |
| 107 | size = get_file_size(path); |
| 108 | if (size < 0) |
| 109 | return -1; |
| 110 | if (offset + size > archive->size) { |
| 111 | fprintf(stderr, "Error: invalid offset or size\n"); |
| 112 | return -1; |
| 113 | } |
| 114 | |
| 115 | fp = fopen(path, "rb"); |
| 116 | if (!fp) { |
| 117 | fprintf(stderr, "Error: failed to open %s (%d: %s)\n", |
| 118 | path, errno, strerror(errno)); |
| 119 | return -1; |
| 120 | } |
| 121 | if (fread((char *)archive + offset, sizeof(char), size, fp) != size) { |
| 122 | fprintf(stderr, "Error: failed to read %s\n", path); |
| 123 | fclose(fp); |
| 124 | return -1; |
| 125 | } |
| 126 | fclose(fp); |
| 127 | |
| 128 | /* set file name*/ |
| 129 | if (set_file_name(path, entry)) |
| 130 | return -1; |
| 131 | |
| 132 | entry->offset = offset; |
| 133 | entry->size = size; |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Allocate memory for archive |
| 140 | * |
| 141 | * count: number of files to add |
| 142 | * files: pointer to the array of file names |
| 143 | * |
| 144 | * return: 0 on success or -1 on error |
| 145 | */ |
| 146 | static int setup_archive(int count, const char **files) |
| 147 | { |
| 148 | uint32_t size; |
| 149 | int i, s; |
| 150 | |
| 151 | size = sizeof(*archive); |
| 152 | for (i = 0; i < count; i++) { |
| 153 | s = get_file_size(files[i]); |
| 154 | if (s < 0) |
| 155 | return -1; |
| 156 | size += sizeof(struct dentry); |
| 157 | size += s; |
| 158 | } |
| 159 | |
| 160 | archive = calloc(size, 1); |
| 161 | if (!archive) { |
| 162 | fprintf(stderr, "Error: failed to allocate memory\n"); |
| 163 | return -1; |
| 164 | } |
| 165 | |
| 166 | /* install magic string */ |
| 167 | memcpy(archive->magic, CBAR_MAGIC, sizeof(archive->magic)); |
| 168 | archive->version = VERSION; |
| 169 | archive->size = size; |
| 170 | archive->count = count; |
| 171 | |
| 172 | printf("Set up archive: size=%d count=%d\n", size, count); |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * Store files in archive |
| 179 | */ |
| 180 | static int archive_files(const char **files) |
| 181 | { |
| 182 | struct dentry *entry; |
| 183 | uint32_t offset; |
| 184 | int i; |
| 185 | |
| 186 | entry = get_first_dentry(archive); |
| 187 | offset = get_first_offset(archive); |
| 188 | for (i = 0; i < archive->count; i++) { |
| 189 | if (add_file(files[i], entry, offset)) |
| 190 | return -1; |
| 191 | offset += entry->size; |
| 192 | entry++; |
| 193 | } |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | static void convert_endian(void) |
| 199 | { |
| 200 | struct dentry *entry; |
| 201 | int i; |
| 202 | |
| 203 | entry = get_first_dentry(archive); |
| 204 | for (i = 0; i < archive->count; i++) { |
| 205 | entry[i].offset = htole32(entry[i].offset); |
| 206 | entry[i].size = htole32(entry[i].size); |
| 207 | } |
| 208 | |
| 209 | archive->version = htole32(archive->version); |
| 210 | archive->size = htole32(archive->size); |
| 211 | archive->count = htole32(archive->count); |
| 212 | } |
| 213 | |
| 214 | /* |
| 215 | * Write archive to file |
| 216 | */ |
| 217 | static int output_archive(const char *path) |
| 218 | { |
| 219 | FILE *fp; |
| 220 | |
| 221 | convert_endian(); |
| 222 | |
| 223 | fp = fopen(path, "wb"); |
| 224 | if (!fp) { |
| 225 | fprintf(stderr, "Error: failed to open %s\n", path); |
| 226 | fclose(fp); |
| 227 | return -1; |
| 228 | } |
| 229 | if (fwrite(archive, sizeof(char), archive->size, fp) != archive->size) { |
| 230 | fprintf(stderr, "Error: failed to write to %s\n", path); |
| 231 | fclose(fp); |
| 232 | return -1; |
| 233 | } |
| 234 | fclose(fp); |
| 235 | printf("Wrote archive to %s\n", path); |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | static int cmd_create(const char *archive_path, int count, const char **files) |
| 241 | { |
| 242 | if (count < 1 || !files) { |
| 243 | fprintf(stderr, "Error: no input files specified\n"); |
| 244 | return -1; |
| 245 | } |
| 246 | |
| 247 | if (setup_archive(count, files)) |
| 248 | return -1; |
| 249 | |
| 250 | if (archive_files(files)) |
| 251 | return -1; |
| 252 | |
| 253 | if (output_archive(archive_path)) |
| 254 | return -1; |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | int main(int argc, const char *argv[]) |
| 260 | { |
| 261 | const char *command; |
| 262 | |
| 263 | if (argc < 3) { |
| 264 | fprintf(stderr, "Error: invalid number of arguments\n"); |
| 265 | usage(); |
| 266 | return -1; |
| 267 | } |
| 268 | |
| 269 | command = argv[2]; |
| 270 | |
| 271 | /* branch by command name */ |
| 272 | if (!strncmp(command, "create", sizeof("create"))) { |
| 273 | if (cmd_create(argv[1], argc - 3, &argv[3])) |
| 274 | return -1; |
| 275 | } else { |
| 276 | fprintf(stderr, "Error: invalid command: %s\n", command); |
| 277 | return -1; |
| 278 | } |
| 279 | |
| 280 | return 0; |
| 281 | } |