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