[1] | 1 | /* SAPfs |
---|
| 2 | * Version 0.9.4 |
---|
| 3 | * Copyright (C) 2001-2003 Eric Botcazou |
---|
| 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; either version 2 of the License, or |
---|
| 8 | * (at your option) any later version. |
---|
| 9 | * |
---|
| 10 | * This program is distributed in the hope that it will be useful, |
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | * GNU General Public License for more details. |
---|
| 14 | * |
---|
| 15 | * You should have received a copy of the GNU General Public License |
---|
| 16 | * along with this program; if not, write to the Free Software |
---|
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | |
---|
| 21 | #include <stdio.h> |
---|
| 22 | #include <stdlib.h> |
---|
| 23 | #include <string.h> |
---|
| 24 | #include <time.h> |
---|
| 25 | #include <dirent.h> |
---|
| 26 | #include <sys/stat.h> |
---|
| 27 | #include "libsap.h" |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | #define SAPFS_VERSION_STR "0.9.4" |
---|
| 31 | |
---|
| 32 | #ifdef linux |
---|
| 33 | #define SAPFS_PLATFORM_STR "Linux" |
---|
| 34 | #else |
---|
| 35 | #define SAPFS_PLATFORM_STR "MSDOS" |
---|
| 36 | #endif |
---|
| 37 | |
---|
| 38 | #define FILENAME_LENGTH 512 |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | /* ugly hack to support French accents */ |
---|
| 42 | #ifdef linux |
---|
| 43 | static char eacute[] = "é"; |
---|
| 44 | static char egrave[] = "è"; |
---|
| 45 | static char agrave[] = "à"; |
---|
| 46 | static char ugrave[] = "ù"; |
---|
| 47 | #else |
---|
| 48 | static char eacute[] = ""; |
---|
| 49 | static char egrave[] = ""; |
---|
| 50 | static char agrave[] = "
"; |
---|
| 51 | static char ugrave[] = ""; |
---|
| 52 | #endif |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | /* PrintErrorMessage: |
---|
| 57 | * Prints the error message corresponding to the specified error. |
---|
| 58 | */ |
---|
| 59 | static void PrintErrorMessage(int errno, const char str[]) |
---|
| 60 | { |
---|
| 61 | switch (errno) { |
---|
| 62 | |
---|
| 63 | case SAP_EBADF: |
---|
| 64 | fprintf(stderr, "Erreur: le fichier %s n'est pas une archive SAP valide.\n", str); |
---|
| 65 | break; |
---|
| 66 | |
---|
| 67 | case SAP_EFBIG: |
---|
| 68 | fprintf(stderr, "Erreur: le fichier %s est de taille trop importante.\n", str); |
---|
| 69 | break; |
---|
| 70 | |
---|
| 71 | case SAP_ENFILE: |
---|
| 72 | fprintf(stderr, "Erreur: le fichier %s est vide.\n", str); |
---|
| 73 | break; |
---|
| 74 | |
---|
| 75 | case SAP_ENOENT: |
---|
| 76 | fprintf(stderr, "Erreur: le fichier %s est introuvable.\n", str); |
---|
| 77 | break; |
---|
| 78 | |
---|
| 79 | case SAP_ENOSPC: |
---|
| 80 | fprintf(stderr, "Erreur: le r%spertoire de l'archive est plein.\n", eacute); |
---|
| 81 | break; |
---|
| 82 | |
---|
| 83 | case SAP_EPERM: |
---|
| 84 | fprintf(stderr, "Erreur: impossible de cr%ser le fichier %s.\n", eacute, str); |
---|
| 85 | break; |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | /* put_separator: |
---|
| 92 | * Helper function to add a missing directory separator. |
---|
| 93 | */ |
---|
| 94 | static void put_separator(char filename[]) |
---|
| 95 | { |
---|
| 96 | int last; |
---|
| 97 | |
---|
| 98 | last = strlen(filename) - 1; |
---|
| 99 | |
---|
| 100 | if (filename[last] != '/') |
---|
| 101 | filename[last + 1] = '/'; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | /* CreateEmptyArchive: |
---|
| 107 | * Creates a new archive using the specified name. |
---|
| 108 | */ |
---|
| 109 | static int CreateEmptyArchive(const char sap_name[], int format, int capacity) |
---|
| 110 | { |
---|
| 111 | sapID sap_file; |
---|
| 112 | |
---|
| 113 | if ((sap_file=sap_CreateArchive(sap_name, format)) == SAP_ERROR) { |
---|
| 114 | PrintErrorMessage(sap_errno, sap_name); |
---|
| 115 | return 1; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | sap_FormatArchive(sap_file, capacity); |
---|
| 119 | sap_CloseArchive(sap_file); |
---|
| 120 | |
---|
| 121 | return 0; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | /* FormatArchive: |
---|
| 127 | * Formats the specified already existing archive. |
---|
| 128 | */ |
---|
| 129 | static int FormatArchive(const char sap_name[], int capacity) |
---|
| 130 | { |
---|
| 131 | sapID sap_file; |
---|
| 132 | int format; |
---|
| 133 | |
---|
| 134 | if ((sap_file=sap_OpenArchive(sap_name, &format)) == SAP_ERROR) { |
---|
| 135 | PrintErrorMessage(sap_errno, sap_name); |
---|
| 136 | return 1; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | sap_FormatArchive(sap_file, capacity); |
---|
| 140 | sap_CloseArchive(sap_file); |
---|
| 141 | |
---|
| 142 | return 0; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | /* VerifyArchive: |
---|
| 148 | * Verifies one or more sectors from the specified archive. |
---|
| 149 | */ |
---|
| 150 | static int VerifyArchive(const char sap_name[], int track, int sect) |
---|
| 151 | { |
---|
| 152 | sapsector_t sapsector; |
---|
| 153 | sapID sap_file; |
---|
| 154 | int format, ntracks, s, t, flag; |
---|
| 155 | |
---|
| 156 | if ((sap_file=sap_OpenArchive(sap_name, &format)) == SAP_ERROR) { |
---|
| 157 | PrintErrorMessage(sap_errno, sap_name); |
---|
| 158 | return 1; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | ntracks = (format == SAP_FORMAT1 ? SAP_NTRACKS1 : SAP_NTRACKS2); |
---|
| 162 | |
---|
| 163 | /* check track number */ |
---|
| 164 | if (track<0) { |
---|
| 165 | t = 0; |
---|
| 166 | track = ntracks-1; |
---|
| 167 | } |
---|
| 168 | else { |
---|
| 169 | if (track>=ntracks) { |
---|
| 170 | fprintf(stderr, "Erreur: num%sro de piste invalide.\n", eacute); |
---|
| 171 | sap_CloseArchive(sap_file); |
---|
| 172 | return 1; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | t = track; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | /* check sector number */ |
---|
| 179 | if (sect<0) { |
---|
| 180 | s = 1; |
---|
| 181 | sect = SAP_NSECTS; |
---|
| 182 | } |
---|
| 183 | else { |
---|
| 184 | if ((sect<1) || (sect>SAP_NSECTS)) { |
---|
| 185 | fprintf(stderr, "Erreur: num%sro de secteur invalide.\n", eacute); |
---|
| 186 | sap_CloseArchive(sap_file); |
---|
| 187 | return 1; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | s = sect; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | while (t <= track) { |
---|
| 194 | while (s <= sect) { |
---|
| 195 | flag = sap_ReadSector(sap_file, t, s, &sapsector); |
---|
| 196 | |
---|
| 197 | if (flag != SAP_OK) { |
---|
| 198 | printf("track %d sector %02d: ", t, s); |
---|
| 199 | |
---|
| 200 | if (flag & SAP_NO_STD_FMT) |
---|
| 201 | printf("<format=%d> ", sapsector.format); |
---|
| 202 | |
---|
| 203 | if (flag & SAP_PROTECTED) |
---|
| 204 | printf("<protection=%d> ", sapsector.protection); |
---|
| 205 | |
---|
| 206 | if (flag & SAP_BAD_SECTOR) |
---|
| 207 | printf("<track=%d sector=%d> ", sapsector.track, sapsector.sector); |
---|
| 208 | |
---|
| 209 | if (flag & SAP_CRC_ERROR) |
---|
| 210 | printf("<CRC error>"); |
---|
| 211 | |
---|
| 212 | printf("\n"); |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | s++; |
---|
| 216 | } /* end of while (s <= sect) */ |
---|
| 217 | |
---|
| 218 | s = 1; |
---|
| 219 | t++; |
---|
| 220 | } /* end of while (t <= track ) */ |
---|
| 221 | |
---|
| 222 | sap_CloseArchive(sap_file); |
---|
| 223 | |
---|
| 224 | return 0; |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | |
---|
| 228 | |
---|
| 229 | /* DumpArchive: |
---|
| 230 | * Dumps one or more sectors from the specified archive. |
---|
| 231 | */ |
---|
| 232 | static int DumpArchive(const char sap_name[], int track, int sect) |
---|
| 233 | { |
---|
| 234 | sapsector_t sapsector; |
---|
| 235 | unsigned char c; |
---|
| 236 | sapID sap_file; |
---|
| 237 | int format, ntracks, i,j,s,t; |
---|
| 238 | |
---|
| 239 | if ((sap_file=sap_OpenArchive(sap_name, &format)) == SAP_ERROR) { |
---|
| 240 | PrintErrorMessage(sap_errno, sap_name); |
---|
| 241 | return 1; |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | ntracks = (format == SAP_FORMAT1 ? SAP_NTRACKS1 : SAP_NTRACKS2); |
---|
| 245 | |
---|
| 246 | /* check track number */ |
---|
| 247 | if (track<0) { |
---|
| 248 | t = 0; |
---|
| 249 | track = ntracks-1; |
---|
| 250 | } |
---|
| 251 | else { |
---|
| 252 | if (track>=ntracks) { |
---|
| 253 | fprintf(stderr, "Erreur: num%sro de piste invalide.\n", eacute); |
---|
| 254 | sap_CloseArchive(sap_file); |
---|
| 255 | return 1; |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | t = track; |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | /* check sector number */ |
---|
| 262 | if (sect<0) { |
---|
| 263 | s = 1; |
---|
| 264 | sect = SAP_NSECTS; |
---|
| 265 | } |
---|
| 266 | else { |
---|
| 267 | if ((sect<1) || (sect>SAP_NSECTS)) { |
---|
| 268 | fprintf(stderr, "Erreur: num%sro de secteur invalide.\n", eacute); |
---|
| 269 | sap_CloseArchive(sap_file); |
---|
| 270 | return 1; |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | s = sect; |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | while (t <= track) { |
---|
| 277 | while (s <= sect) { |
---|
| 278 | sap_ReadSector(sap_file, t, s, &sapsector); |
---|
| 279 | |
---|
| 280 | printf("track id: %02d\n", sapsector.track); |
---|
| 281 | printf("sector id: %02d\n", sapsector.sector); |
---|
| 282 | printf("format: %d\n", sapsector.format); |
---|
| 283 | printf("protection: %d\n", sapsector.protection); |
---|
| 284 | printf("data:\n"); |
---|
| 285 | |
---|
| 286 | for (i=0; i< (format == SAP_FORMAT1 ? 16 : 8); i++) { |
---|
| 287 | for (j=0; j<16; j++) { |
---|
| 288 | c = sapsector.data[i*16+j]; |
---|
| 289 | printf("%02X ", c); |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | for (j=0; j<16; j++) { |
---|
| 293 | c = sapsector.data[i*16+j]; |
---|
| 294 | |
---|
| 295 | if ((c>=32) && (c<=125)) |
---|
| 296 | putchar(c); |
---|
| 297 | else |
---|
| 298 | putchar('.'); |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | printf("\n"); |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | s++; |
---|
| 305 | } /* end of while (s <= sect) */ |
---|
| 306 | |
---|
| 307 | s = 1; |
---|
| 308 | t++; |
---|
| 309 | } /* end of while (t <= track ) */ |
---|
| 310 | |
---|
| 311 | sap_CloseArchive(sap_file); |
---|
| 312 | |
---|
| 313 | return 0; |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | |
---|
| 317 | |
---|
| 318 | /* ListArchive: |
---|
| 319 | * Lists the files contained in the specified archive. |
---|
| 320 | */ |
---|
| 321 | static int ListArchive(const char sap_name[]) |
---|
| 322 | { |
---|
| 323 | char buffer[4096]; |
---|
| 324 | sapID sap_file; |
---|
| 325 | int format; |
---|
| 326 | |
---|
| 327 | if ((sap_file=sap_OpenArchive(sap_name, &format)) == SAP_ERROR) { |
---|
| 328 | PrintErrorMessage(sap_errno, sap_name); |
---|
| 329 | return 1; |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | sap_ListArchive(sap_file, buffer, sizeof(buffer)); |
---|
| 333 | puts(buffer); |
---|
| 334 | sap_CloseArchive(sap_file); |
---|
| 335 | |
---|
| 336 | return 0; |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | |
---|
| 340 | |
---|
| 341 | /* PrintFileInfo: |
---|
| 342 | * Prints the info for the specified file. |
---|
| 343 | */ |
---|
| 344 | static int PrintFileInfo(const char sap_name[], char filename[]) |
---|
| 345 | { |
---|
| 346 | int format, i, ret = 0; |
---|
| 347 | sapID sap_file; |
---|
| 348 | sapfileinfo_t info; |
---|
| 349 | char tmp[64]; |
---|
| 350 | |
---|
| 351 | if ((sap_file=sap_OpenArchive(sap_name, &format)) == SAP_ERROR) { |
---|
| 352 | PrintErrorMessage(sap_errno, sap_name); |
---|
| 353 | return 1; |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | if (sap_GetFileInfo(sap_file, filename, &info) == SAP_ERROR) { |
---|
| 357 | PrintErrorMessage(sap_errno, filename); |
---|
| 358 | ret = 1; |
---|
| 359 | } |
---|
| 360 | else { |
---|
| 361 | printf("name: %s\n", filename); |
---|
| 362 | printf("size: %d bytes\n", info.size); |
---|
| 363 | printf("file type: %d\n", info.file_type); |
---|
| 364 | printf("data type: %d\n", info.data_type); |
---|
| 365 | |
---|
| 366 | if (info.date) { |
---|
| 367 | strftime(tmp, sizeof(tmp), "%m/%d/%Y %H:%M ", localtime(&info.date)); |
---|
| 368 | printf("date: %s\n", tmp); |
---|
| 369 | } |
---|
| 370 | |
---|
| 371 | if (info.comment[0]) |
---|
| 372 | printf("comment: %s\n", info.comment); |
---|
| 373 | |
---|
| 374 | printf("blocks: "); |
---|
| 375 | for (i=0; i<info.nblocks; i++) |
---|
| 376 | printf("%d ", info.block[i]); |
---|
| 377 | printf("\n"); |
---|
| 378 | |
---|
| 379 | free(info.block); |
---|
| 380 | } |
---|
| 381 | |
---|
| 382 | sap_CloseArchive(sap_file); |
---|
| 383 | |
---|
| 384 | return ret; |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | |
---|
| 388 | |
---|
| 389 | /* ExtractFile: |
---|
| 390 | * Extracts one or more files from the specified archive. |
---|
| 391 | */ |
---|
| 392 | static int ExtractFile(const char sap_name[], char *filename[], int nfiles) |
---|
| 393 | { |
---|
| 394 | int format, i, len, ret = 0; |
---|
| 395 | sapID sap_file; |
---|
| 396 | |
---|
| 397 | if ((sap_file=sap_OpenArchive(sap_name, &format)) == SAP_ERROR) { |
---|
| 398 | PrintErrorMessage(sap_errno, sap_name); |
---|
| 399 | return 1; |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | for (i=0; i<nfiles; i++) { |
---|
| 403 | len = sap_ExtractFile(sap_file, filename[i]); |
---|
| 404 | |
---|
| 405 | if (len == 0) { |
---|
| 406 | PrintErrorMessage(sap_errno, filename[i]); |
---|
| 407 | ret = 1; |
---|
| 408 | break; |
---|
| 409 | } |
---|
| 410 | } |
---|
| 411 | |
---|
| 412 | sap_CloseArchive(sap_file); |
---|
| 413 | |
---|
| 414 | return ret; |
---|
| 415 | } |
---|
| 416 | |
---|
| 417 | |
---|
| 418 | |
---|
| 419 | /* AddFile: |
---|
| 420 | * Adds one or more files to the specified archive. |
---|
| 421 | */ |
---|
| 422 | static int AddFile(const char sap_name[], char *filename[], int nfiles) |
---|
| 423 | { |
---|
| 424 | char pathname[FILENAME_LENGTH]; |
---|
| 425 | int format, i, len = 0, ret = 0; |
---|
| 426 | struct stat s; |
---|
| 427 | struct dirent *entry; |
---|
| 428 | sapID sap_file; |
---|
| 429 | DIR *dir; |
---|
| 430 | |
---|
| 431 | if ((sap_file=sap_OpenArchive(sap_name, &format)) == SAP_ERROR) { |
---|
| 432 | PrintErrorMessage(sap_errno, sap_name); |
---|
| 433 | return 1; |
---|
| 434 | } |
---|
| 435 | |
---|
| 436 | for (i=0; i<nfiles; i++) { |
---|
| 437 | /* get file statistics */ |
---|
| 438 | if (stat(filename[i], &s) != 0) { |
---|
| 439 | fprintf(stderr, "Erreur: le fichier %s est introuvable.\n", filename[i]); |
---|
| 440 | ret = 1; |
---|
| 441 | break; |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | if (S_ISDIR(s.st_mode)) { /* directory? */ |
---|
| 445 | if ((dir=opendir(filename[i])) == NULL) { |
---|
| 446 | fprintf(stderr, "Erreur: le r%spertoire %s est inaccessible en lecture.\n", eacute, filename[i]); |
---|
| 447 | ret = 1; |
---|
| 448 | break; |
---|
| 449 | } |
---|
| 450 | |
---|
| 451 | /* add every entry in turn */ |
---|
| 452 | while ((entry = readdir(dir)) != NULL) { |
---|
| 453 | if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) { |
---|
| 454 | strncpy(pathname, filename[i], FILENAME_LENGTH - 1); |
---|
| 455 | put_separator(pathname); |
---|
| 456 | strncat(pathname, entry->d_name, FILENAME_LENGTH - strlen(pathname) - 1); |
---|
| 457 | len = sap_AddFile(sap_file, pathname); |
---|
| 458 | |
---|
| 459 | if (len == 0) { |
---|
| 460 | PrintErrorMessage(sap_errno, pathname); |
---|
| 461 | ret = 1; |
---|
| 462 | break; |
---|
| 463 | } |
---|
| 464 | } |
---|
| 465 | } |
---|
| 466 | |
---|
| 467 | closedir(dir); |
---|
| 468 | } |
---|
| 469 | else { |
---|
| 470 | len = sap_AddFile(sap_file, filename[i]); |
---|
| 471 | } |
---|
| 472 | |
---|
| 473 | if (len == 0) { |
---|
| 474 | if (ret == 0) { |
---|
| 475 | PrintErrorMessage(sap_errno, filename[i]); |
---|
| 476 | ret = 1; |
---|
| 477 | } |
---|
| 478 | break; |
---|
| 479 | } |
---|
| 480 | } |
---|
| 481 | |
---|
| 482 | sap_CloseArchive(sap_file); |
---|
| 483 | |
---|
| 484 | return ret; |
---|
| 485 | } |
---|
| 486 | |
---|
| 487 | |
---|
| 488 | |
---|
| 489 | /* DeleteFile: |
---|
| 490 | * Deletes one or more files from the specified archive. |
---|
| 491 | */ |
---|
| 492 | static int DeleteFile(const char sap_name[], char *filename[], int nfiles) |
---|
| 493 | { |
---|
| 494 | int format, i, len, ret = 0; |
---|
| 495 | sapID sap_file; |
---|
| 496 | |
---|
| 497 | if ((sap_file=sap_OpenArchive(sap_name, &format)) == SAP_ERROR) { |
---|
| 498 | PrintErrorMessage(sap_errno, sap_name); |
---|
| 499 | return 1; |
---|
| 500 | } |
---|
| 501 | |
---|
| 502 | for (i=0; i<nfiles; i++) { |
---|
| 503 | len = sap_DeleteFile(sap_file, filename[i]); |
---|
| 504 | |
---|
| 505 | if (len == 0) { |
---|
| 506 | PrintErrorMessage(sap_errno, filename[i]); |
---|
| 507 | ret = 1; |
---|
| 508 | break; |
---|
| 509 | } |
---|
| 510 | } |
---|
| 511 | |
---|
| 512 | sap_CloseArchive(sap_file); |
---|
| 513 | |
---|
| 514 | return ret; |
---|
| 515 | } |
---|
| 516 | |
---|
| 517 | |
---|
| 518 | |
---|
| 519 | /* CopyArchive: |
---|
| 520 | * Copies one or more sectors from the source archive to the dest archive. |
---|
| 521 | */ |
---|
| 522 | static int CopyArchive(const char src_name[], const char dest_name[], int track, int sect) |
---|
| 523 | { |
---|
| 524 | sapsector_t sapsector; |
---|
| 525 | sapID src_file, dest_file; |
---|
| 526 | int src_format, dest_format, ntracks, s, t; |
---|
| 527 | |
---|
| 528 | if ((src_file=sap_OpenArchive(src_name, &src_format)) == SAP_ERROR) { |
---|
| 529 | PrintErrorMessage(sap_errno, src_name); |
---|
| 530 | return 1; |
---|
| 531 | } |
---|
| 532 | |
---|
| 533 | if ((dest_file=sap_OpenArchive(dest_name, &dest_format)) == SAP_ERROR) { |
---|
| 534 | PrintErrorMessage(sap_errno, dest_name); |
---|
| 535 | sap_CloseArchive(src_file); |
---|
| 536 | return 1; |
---|
| 537 | } |
---|
| 538 | |
---|
| 539 | if (src_format != dest_format) { |
---|
| 540 | fprintf(stderr, "Erreur: archives de format diff%srent.\n", eacute); |
---|
| 541 | goto Error; |
---|
| 542 | } |
---|
| 543 | |
---|
| 544 | ntracks = (src_format == SAP_FORMAT1 ? SAP_NTRACKS1 : SAP_NTRACKS2); |
---|
| 545 | |
---|
| 546 | /* check track number */ |
---|
| 547 | if (track<0) { |
---|
| 548 | t = 0; |
---|
| 549 | track = ntracks-1; |
---|
| 550 | } |
---|
| 551 | else { |
---|
| 552 | if (track>=ntracks) { |
---|
| 553 | fprintf(stderr, "Erreur: num%sro de piste invalide.\n", eacute); |
---|
| 554 | goto Error; |
---|
| 555 | } |
---|
| 556 | |
---|
| 557 | t = track; |
---|
| 558 | } |
---|
| 559 | |
---|
| 560 | /* check sector number */ |
---|
| 561 | if (sect<0) { |
---|
| 562 | s = 1; |
---|
| 563 | sect = SAP_NSECTS; |
---|
| 564 | } |
---|
| 565 | else { |
---|
| 566 | if ((sect<1) || (sect>SAP_NSECTS)) { |
---|
| 567 | fprintf(stderr, "Erreur: num%sro de secteur invalide.\n", eacute); |
---|
| 568 | goto Error; |
---|
| 569 | } |
---|
| 570 | |
---|
| 571 | s = sect; |
---|
| 572 | } |
---|
| 573 | |
---|
| 574 | while (t <= track) { |
---|
| 575 | while (s <= sect) { |
---|
| 576 | sap_ReadSector(src_file, t, s, &sapsector); |
---|
| 577 | sap_WriteSector(dest_file, t, s, &sapsector); |
---|
| 578 | |
---|
| 579 | s++; |
---|
| 580 | } /* end of while (s <= sect) */ |
---|
| 581 | |
---|
| 582 | s = 1; |
---|
| 583 | t++; |
---|
| 584 | } /* end of while (t <= track ) */ |
---|
| 585 | |
---|
| 586 | sap_CloseArchive(dest_file); |
---|
| 587 | sap_CloseArchive(src_file); |
---|
| 588 | return 0; |
---|
| 589 | |
---|
| 590 | Error: |
---|
| 591 | sap_CloseArchive(src_file); |
---|
| 592 | sap_CloseArchive(dest_file); |
---|
| 593 | return 1; |
---|
| 594 | } |
---|
| 595 | |
---|
| 596 | |
---|
| 597 | |
---|
| 598 | /* MoveSector: |
---|
| 599 | * Copies with deplacement one sector from the source archive to the dest archive. |
---|
| 600 | */ |
---|
| 601 | static int MoveSector(const char src_name[], int src_track, int src_sect, |
---|
| 602 | const char dest_name[], int dest_track, int dest_sect) |
---|
| 603 | { |
---|
| 604 | sapsector_t sapsector; |
---|
| 605 | sapID src_file, dest_file; |
---|
| 606 | int src_format, dest_format, ntracks; |
---|
| 607 | |
---|
| 608 | if ((src_file=sap_OpenArchive(src_name, &src_format)) == SAP_ERROR) { |
---|
| 609 | PrintErrorMessage(sap_errno, src_name); |
---|
| 610 | return 1; |
---|
| 611 | } |
---|
| 612 | |
---|
| 613 | if ((dest_file=sap_OpenArchive(dest_name, &dest_format)) == SAP_ERROR) { |
---|
| 614 | PrintErrorMessage(sap_errno, dest_name); |
---|
| 615 | sap_CloseArchive(src_file); |
---|
| 616 | return 1; |
---|
| 617 | } |
---|
| 618 | |
---|
| 619 | if (src_format != dest_format) { |
---|
| 620 | fprintf(stderr, "Erreur: archives de format diff%srent.\n", eacute); |
---|
| 621 | goto Error; |
---|
| 622 | } |
---|
| 623 | |
---|
| 624 | ntracks = (src_format == SAP_FORMAT1 ? SAP_NTRACKS1 : SAP_NTRACKS2); |
---|
| 625 | |
---|
| 626 | /* check track number */ |
---|
| 627 | if ((src_track<0) || (src_track>=ntracks)) { |
---|
| 628 | fprintf(stderr, "Erreur: num%sro de piste invalide.\n", eacute); |
---|
| 629 | goto Error; |
---|
| 630 | } |
---|
| 631 | |
---|
| 632 | /* check sector number */ |
---|
| 633 | if ((src_sect<1) || (src_sect>SAP_NSECTS)) { |
---|
| 634 | fprintf(stderr, "Erreur: num%sro de secteur invalide.\n", eacute); |
---|
| 635 | goto Error; |
---|
| 636 | } |
---|
| 637 | |
---|
| 638 | /* check track number */ |
---|
| 639 | if ((dest_track<0) || (dest_track>=ntracks)) { |
---|
| 640 | fprintf(stderr, "Erreur: num%sro de piste invalide.\n", eacute); |
---|
| 641 | goto Error; |
---|
| 642 | } |
---|
| 643 | |
---|
| 644 | /* check sector number */ |
---|
| 645 | if ((dest_sect<1) || (dest_sect>SAP_NSECTS)) { |
---|
| 646 | fprintf(stderr, "Erreur: num%sro de secteur invalide.\n", eacute); |
---|
| 647 | goto Error; |
---|
| 648 | } |
---|
| 649 | |
---|
| 650 | sap_ReadSector(src_file, src_track, src_sect, &sapsector); |
---|
| 651 | |
---|
| 652 | sapsector.track = dest_track; |
---|
| 653 | sapsector.sector = dest_sect; |
---|
| 654 | |
---|
| 655 | sap_WriteSector(dest_file, dest_track, dest_sect, &sapsector); |
---|
| 656 | |
---|
| 657 | sap_CloseArchive(src_file); |
---|
| 658 | sap_CloseArchive(dest_file); |
---|
| 659 | return 0; |
---|
| 660 | |
---|
| 661 | Error: |
---|
| 662 | sap_CloseArchive(src_file); |
---|
| 663 | sap_CloseArchive(dest_file); |
---|
| 664 | return 1; |
---|
| 665 | } |
---|
| 666 | |
---|
| 667 | |
---|
| 668 | #define COMMAND_MAX 14 |
---|
| 669 | |
---|
| 670 | static char *short_command[] = { "-h", "-v", "-c", "-f", "-w", "-u", |
---|
| 671 | "-t", "-i", "-x", "-y", "-a", "-d", "-k", "-m" }; |
---|
| 672 | static char *long_command[] = { "--help", "--version", "--create", "--format", "--verify", "--dump", |
---|
| 673 | "--list", "--info", "--extract", "--extract-all", "--add", "--delete", |
---|
| 674 | "--copy", "--move" }; |
---|
| 675 | |
---|
| 676 | |
---|
| 677 | /* usage: |
---|
| 678 | * Displays the commands and quits. |
---|
| 679 | */ |
---|
| 680 | static void usage(const char prog_name[]) |
---|
| 681 | { |
---|
| 682 | fprintf(stderr, "Usage: %s -h --help | -v --version | -c --create | -f --format\n", prog_name); |
---|
| 683 | fprintf(stderr, " -w --verify | -d --dump | -t --list | -i --info\n"); |
---|
| 684 | fprintf(stderr, " -x --extract[-all] | -a --add | -d --delete\n"); |
---|
| 685 | exit(EXIT_FAILURE); |
---|
| 686 | } |
---|
| 687 | |
---|
| 688 | |
---|
| 689 | |
---|
| 690 | /* main: |
---|
| 691 | * Entry point for the program. |
---|
| 692 | */ |
---|
| 693 | int main(int argc, char *argv[]) |
---|
| 694 | { |
---|
| 695 | int i, ret = 0; |
---|
| 696 | char *star = "*"; |
---|
| 697 | |
---|
| 698 | if (argc < 2) /* no argument? */ |
---|
| 699 | usage(argv[0]); |
---|
| 700 | |
---|
| 701 | if (argv[1][0] == '-') { |
---|
| 702 | |
---|
| 703 | switch (argv[1][1]) { |
---|
| 704 | |
---|
| 705 | case '-': /* long commands */ |
---|
| 706 | for (i=0; i<COMMAND_MAX; i++) { |
---|
| 707 | if (strcmp(argv[1], long_command[i]) == 0) { |
---|
| 708 | argv[1] = short_command[i]; |
---|
| 709 | return main(argc, argv); |
---|
| 710 | } |
---|
| 711 | } |
---|
| 712 | |
---|
| 713 | usage(argv[0]); |
---|
| 714 | |
---|
| 715 | case 'h': /* help */ |
---|
| 716 | printf("SAPfs est un outil de manipulation des archives SAP qui permet de r%saliser\n", eacute); |
---|
| 717 | printf("sur ces archives les op%srations naturelles d'un syst%sme de fichiers.\n\n", eacute, egrave); |
---|
| 718 | printf("Usage:\n"); |
---|
| 719 | printf(" %s commande1 archive.sap [fichier...] [piste] [sect]\n", argv[0]); |
---|
| 720 | printf(" %s commande2 archive.sap [nb pistes] [densit%s]\n", argv[0], eacute); |
---|
| 721 | printf(" %s commande3 archive.sap archive2.sap [piste] [sect]\n", argv[0]); |
---|
| 722 | printf(" %s commande4 archive1.sap piste sect archive2.sap piste sect\n", argv[0]); |
---|
| 723 | printf("o%s la commande1 est prise parmi les suivantes:\n", ugrave); |
---|
| 724 | printf(" -h, --help affiche cette aide\n"); |
---|
| 725 | printf(" -v, --version affiche la version du programme\n"); |
---|
| 726 | printf(" -w, --verify effectue une v%srification d'un ou plusieurs secteurs\n", eacute); |
---|
| 727 | printf(" -u, --dump affiche le contenu d'un ou plusieurs secteurs\n"); |
---|
| 728 | printf(" -t, --list affiche la liste des fichiers de l'archive SAP\n"); |
---|
| 729 | printf(" -i, --info affiche les informations relatives %s un fichier\n", agrave); |
---|
| 730 | printf(" -x, --extract extrait un ou plusieurs fichiers de l'achive SAP\n"); |
---|
| 731 | printf(" --extract-all extrait tous les fichiers de l'archive SAP\n"); |
---|
| 732 | printf(" -a, --add ajoute un ou plusieurs fichiers %s l'archive SAP\n", agrave); |
---|
| 733 | printf(" -d, --delete d%struit un ou plusieurs fichiers de l'archive SAP\n", eacute); |
---|
| 734 | printf("et o%s la commande2 est prise parmi les suivantes:\n", ugrave); |
---|
| 735 | printf(" -c, --create cr%se une archive SAP vide\n", eacute); |
---|
| 736 | printf(" -f, --format formate une archive SAP\n"); |
---|
| 737 | printf("et o%s la commande3 est prise parmi les suivantes:\n", ugrave); |
---|
| 738 | printf(" -k, --copy copie un ou plusieurs secteurs\n"); |
---|
| 739 | printf("et o%s la commande4 est prise parmi les suivantes:\n", ugrave); |
---|
| 740 | printf(" -m, --move copie un secteur avec d%splacement\n", eacute); |
---|
| 741 | break; |
---|
| 742 | |
---|
| 743 | case 'v': /* version */ |
---|
| 744 | printf("SAPfs version "SAPFS_VERSION_STR" pour "SAPFS_PLATFORM_STR", copyright (C) 2001-2003 Eric Botcazou.\n"); |
---|
| 745 | break; |
---|
| 746 | |
---|
| 747 | case 'c': /* create */ |
---|
| 748 | if (argc < 3) |
---|
| 749 | usage(argv[0]); |
---|
| 750 | |
---|
| 751 | if (argc < 4) { |
---|
| 752 | ret = CreateEmptyArchive(argv[2], SAP_FORMAT1, SAP_TRK80); |
---|
| 753 | } |
---|
| 754 | else if (argc == 4){ |
---|
| 755 | if (atoi(argv[3]) == 40) |
---|
| 756 | ret = CreateEmptyArchive(argv[2], SAP_FORMAT1, SAP_TRK40); |
---|
| 757 | else if (atoi(argv[3]) == 80) |
---|
| 758 | ret = CreateEmptyArchive(argv[2], SAP_FORMAT1, SAP_TRK80); |
---|
| 759 | else |
---|
| 760 | ret = 1; |
---|
| 761 | } |
---|
| 762 | else { |
---|
| 763 | if (atoi(argv[3]) == 40) |
---|
| 764 | ret = CreateEmptyArchive(argv[2], (atoi(argv[4]) == 1 ? SAP_FORMAT2 : SAP_FORMAT1), SAP_TRK40); |
---|
| 765 | else if (atoi(argv[3]) == 80) |
---|
| 766 | ret = CreateEmptyArchive(argv[2], (atoi(argv[4]) == 1 ? SAP_FORMAT2 : SAP_FORMAT1), SAP_TRK80); |
---|
| 767 | else |
---|
| 768 | ret = 1; |
---|
| 769 | } |
---|
| 770 | break; |
---|
| 771 | |
---|
| 772 | case 'f': /* format */ |
---|
| 773 | if (argc < 3) |
---|
| 774 | usage(argv[0]); |
---|
| 775 | |
---|
| 776 | if (argc < 4) { |
---|
| 777 | ret = FormatArchive(argv[2], SAP_TRK80); |
---|
| 778 | } |
---|
| 779 | else { |
---|
| 780 | if (atoi(argv[3]) == 80) |
---|
| 781 | ret = FormatArchive(argv[2], SAP_TRK80); |
---|
| 782 | else if (atoi(argv[3]) == 40) |
---|
| 783 | ret = FormatArchive(argv[2], SAP_TRK40); |
---|
| 784 | else |
---|
| 785 | ret = 1; |
---|
| 786 | } |
---|
| 787 | break; |
---|
| 788 | |
---|
| 789 | case 'w': /* verify */ |
---|
| 790 | if (argc < 3) |
---|
| 791 | usage(argv[0]); |
---|
| 792 | |
---|
| 793 | if (argc < 4) |
---|
| 794 | ret = VerifyArchive(argv[2], -1, -1); |
---|
| 795 | else if (argc < 5) |
---|
| 796 | ret = VerifyArchive(argv[2], atoi(argv[3]), -1); |
---|
| 797 | else |
---|
| 798 | ret = VerifyArchive(argv[2], atoi(argv[3]), atoi(argv[4])); |
---|
| 799 | break; |
---|
| 800 | |
---|
| 801 | |
---|
| 802 | case 'u': /* dump */ |
---|
| 803 | if (argc < 3) |
---|
| 804 | usage(argv[0]); |
---|
| 805 | |
---|
| 806 | if (argc < 4) |
---|
| 807 | ret = DumpArchive(argv[2], -1, -1); |
---|
| 808 | else if (argc < 5) |
---|
| 809 | ret = DumpArchive(argv[2], atoi(argv[3]), -1); |
---|
| 810 | else |
---|
| 811 | ret = DumpArchive(argv[2], atoi(argv[3]), atoi(argv[4])); |
---|
| 812 | break; |
---|
| 813 | |
---|
| 814 | case 't': /* list */ |
---|
| 815 | if (argc < 3) |
---|
| 816 | usage(argv[0]); |
---|
| 817 | |
---|
| 818 | ret = ListArchive(argv[2]); |
---|
| 819 | break; |
---|
| 820 | |
---|
| 821 | case 'i': /* info */ |
---|
| 822 | if (argc < 4) |
---|
| 823 | usage(argv[0]); |
---|
| 824 | |
---|
| 825 | ret = PrintFileInfo(argv[2], argv[3]); |
---|
| 826 | break; |
---|
| 827 | |
---|
| 828 | case 'x': /* extract */ |
---|
| 829 | if (argc < 4) |
---|
| 830 | usage(argv[0]); |
---|
| 831 | |
---|
| 832 | ret = ExtractFile(argv[2], argv+3, argc-3); |
---|
| 833 | break; |
---|
| 834 | |
---|
| 835 | case 'y': /* extract all */ |
---|
| 836 | if (argc < 3) |
---|
| 837 | usage(argv[0]); |
---|
| 838 | |
---|
| 839 | ret = ExtractFile(argv[2], &star, 1); |
---|
| 840 | break; |
---|
| 841 | |
---|
| 842 | case 'a': /* add */ |
---|
| 843 | if (argc < 4) |
---|
| 844 | usage(argv[0]); |
---|
| 845 | |
---|
| 846 | ret = AddFile(argv[2], argv+3, argc-3); |
---|
| 847 | break; |
---|
| 848 | |
---|
| 849 | case 'd': /* delete */ |
---|
| 850 | if (argc < 4) |
---|
| 851 | usage(argv[0]); |
---|
| 852 | |
---|
| 853 | ret = DeleteFile(argv[2], argv+3, argc-3); |
---|
| 854 | break; |
---|
| 855 | |
---|
| 856 | case 'k': /* copy */ |
---|
| 857 | if (argc < 4) |
---|
| 858 | usage(argv[0]); |
---|
| 859 | |
---|
| 860 | if (argc < 5) |
---|
| 861 | ret = CopyArchive(argv[2], argv[3], -1, -1); |
---|
| 862 | else if (argc < 6) |
---|
| 863 | ret = CopyArchive(argv[2], argv[3], atoi(argv[4]), -1); |
---|
| 864 | else |
---|
| 865 | ret = CopyArchive(argv[2], argv[3], atoi(argv[4]), atoi(argv[5])); |
---|
| 866 | break; |
---|
| 867 | |
---|
| 868 | case 'm': /* move */ |
---|
| 869 | if (argc != 8) |
---|
| 870 | usage(argv[0]); |
---|
| 871 | |
---|
| 872 | ret = MoveSector(argv[2], atoi(argv[3]), atoi(argv[4]), argv[5], atoi(argv[6]), atoi(argv[7])); |
---|
| 873 | break; |
---|
| 874 | |
---|
| 875 | default: |
---|
| 876 | usage(argv[0]); |
---|
| 877 | } /* end of switch */ |
---|
| 878 | } |
---|
| 879 | |
---|
| 880 | return ret; |
---|
| 881 | } |
---|
| 882 | |
---|