MIDAS
Loading...
Searching...
No Matches
mtape.cxx File Reference
#include "midas.h"
#include "msystem.h"
Include dependency graph for mtape.cxx:

Go to the source code of this file.

Functions

INT tape_dir (INT channel, INT count)
 
INT tape_restore (INT channel, INT count, char *file_name)
 
INT tape_backup (INT channel, INT count1, INT count2, char *file_name)
 
int main (int argc, char *argv[])
 

Variables

BOOL verbose = FALSE
 

Function Documentation

◆ main()

int main ( int  argc,
char argv[] 
)

Definition at line 246 of file mtape.cxx.

247{
249 char cmd[100], tape_name[256], file_name[256];
250
251 /* set default tape name */
252
253#ifdef OS_WINNT
254 strcpy(tape_name, "\\\\.\\tape0");
255#elif defined(OS_UNIX)
256 strcpy(tape_name, "/dev/tape");
257#elif defined(OS_VMS)
258 strcpy(tape_name, "mka0:");
259#else
260#error This program cannot be compiled under this operating system
261#endif
262
263 /* default parameters */
264 strcpy(file_name, "run%05d.mid");
265 cmd[0] = 0;
266 count1 = -100;
267 count2 = 0;
268
269 /* if "TAPE" environment variable present, use it */
270 if (getenv("TAPE") != NULL)
271 strcpy(tape_name, getenv("TAPE"));
272
273 /* parse command line parameters */
274 for (i = 1; i < argc; i++) {
275 if (argv[i][0] == '-' && argv[i][1] == 'v')
276 verbose = TRUE;
277 else if (argv[i][0] == '-') {
278 if (i + 1 >= argc || argv[i + 1][0] == '-')
279 goto usage;
280 if (argv[i][1] == 'f')
281 strcpy(tape_name, argv[++i]);
282 else if (argv[i][1] == 'd')
283 strcpy(file_name, argv[++i]);
284 else
285 goto usage;
286 } else if (cmd[0] == 0)
287 strcpy(cmd, argv[i]);
288 else if (count1 == -100)
289 count1 = atoi(argv[i]);
290 else
291 count2 = atoi(argv[i]);
292 }
293
294 if (count1 == -100)
295 count1 = 1;
296
297 /* don't produce system messages */
299
300 if (strcmp(cmd, "status") == 0) {
302 return 0;
303 }
304
305 /* open tape device */
307 printf("Cannot open tape %s.\n", tape_name);
308 return 0;
309 }
310
311 if (strcmp(cmd, "rewind") == 0)
313
314 else if (strcmp(cmd, "online") == 0)
316
317 else if (strcmp(cmd, "offline") == 0)
319
320 else if (strcmp(cmd, "eof") == 0 || strcmp(cmd, "weof") == 0) {
321 /* reopen tape in write mode */
324 printf("Cannot open tape %s.\n", tape_name);
325 return 0;
326 }
327
328 for (i = 0; i < count1; i++)
330 }
331
332 else if (strcmp(cmd, "fsf") == 0 || strcmp(cmd, "ff") == 0)
334
335 else if (strcmp(cmd, "fsr") == 0 || strcmp(cmd, "fr") == 0)
337
338 else if (strcmp(cmd, "bsf") == 0 || strcmp(cmd, "bf") == 0)
340
341 else if (strcmp(cmd, "bsr") == 0 || strcmp(cmd, "br") == 0)
343
344 else if (strcmp(cmd, "seod") == 0)
346
347 else if (strcmp(cmd, "dir") == 0)
349
350 else if (strcmp(cmd, "backup") == 0) {
351 /* reopen tape in write mode */
354 printf("Cannot open tape %s.\n", tape_name);
355 return 0;
356 }
358 }
359
360 else if (strcmp(cmd, "restore") == 0)
362
363 else {
364 usage:
365 printf("usage: mtape [-f tape_device] [-d file] [-v] command [count] [last]\n\n");
366 printf("Following commands are available:\n");
367 printf(" status Print status information about tape\n");
368 printf(" rewind Rewind tape\n");
369 printf
370 (" eof,weof Write <count> End-of-File marks at the current tape position\n");
371 printf(" fsf,ff Forward spaces <count> files\n");
372 printf(" fsr,fr Forward spaces <count> records\n");
373 printf(" bsf,bf Backspaces <count> files\n");
374 printf(" bsr,br Backspaces <count> records\n");
375 printf(" rewind Rewind tape\n");
376 printf(" offline Places the tape offline (unload)\n");
377 printf(" online Places the tape online (load)\n");
378 printf(" seod Space to end of recorded data\n\n");
379 printf("Following commands only work with tapes written in MIDAS format:\n");
380 printf(" dir List next [count] runs on MIDAS tape\n");
381 printf(" backup\n");
382 printf
383 (" Copy runs with numbers [count] to [last] from disk to tape.\n");
384 printf(" If [-d file] is not given, runxxxxx.mid is used where\n");
385 printf(" xxxxx is the run number. If [file] contains %%05d, it\n");
386 printf(" is replaced by the run number.\n");
387 printf(" restore\n");
388 printf(" Copy next [count] files from tape to disk.\n");
389 printf(" If [-d file] is not given, runxxxxx.mid is used where\n");
390 printf(" xxxxx is the run number. If [file] contains %%05d, it\n");
391 printf(" is replaced by the run number.\n");
392 return 0;
393 }
394
395 if (status == -1)
396 printf("Error performing operation\n");
397 else if (status != SS_SUCCESS)
398 printf("Error performing operation, status code = %d\n", status);
399
401
402 return 0;
403}
static void usage()
#define SS_SUCCESS
Definition midas.h:663
#define MT_ALL
Definition midas.h:549
INT ss_tape_rskip(INT channel, INT count)
Definition system.cxx:6292
INT ss_tape_rewind(INT channel)
Definition system.cxx:6348
INT ss_tape_fskip(INT channel, INT count)
Definition system.cxx:6232
INT ss_tape_close(INT channel)
Definition system.cxx:5902
INT ss_tape_status(char *path)
Definition system.cxx:5946
INT ss_tape_mount(INT channel)
Definition system.cxx:6456
INT ss_tape_spool(INT channel)
Definition system.cxx:6400
INT ss_tape_unmount(INT channel)
Definition system.cxx:6512
INT ss_tape_write_eof(INT channel)
Definition system.cxx:6166
INT ss_tape_open(char *path, INT oflag, INT *channel)
Definition system.cxx:5811
INT cm_set_msg_print(INT system_mask, INT user_mask, int(*func)(const char *))
Definition midas.cxx:647
INT channel
INT i
Definition mdump.cxx:32
int INT
Definition midas.h:129
#define TRUE
Definition midas.h:182
INT tape_dir(INT channel, INT count)
Definition mtape.cxx:19
INT tape_backup(INT channel, INT count1, INT count2, char *file_name)
Definition mtape.cxx:191
INT tape_restore(INT channel, INT count, char *file_name)
Definition mtape.cxx:108
BOOL verbose
Definition mtape.cxx:15
char file_name[256]
Definition odbhist.cxx:41
DWORD status
Definition odbhist.cxx:39
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
Definition rmidas.h:24
Here is the call graph for this function:

◆ tape_backup()

INT tape_backup ( INT  channel,
INT  count1,
INT  count2,
char file_name 
)

Definition at line 191 of file mtape.cxx.

192{
193 INT i, n, fh, size, mb;
194 char buffer[TAPE_BUFFER_SIZE], str[256];
195
196 if (count2 < count1)
197 count2 = count1;
198
199 for (i = count1; i <= count2; i++) {
200 if (strchr(file_name, '%'))
202 else
203 strcpy(str, file_name);
204
205 /* open file for read */
206 fh = open(str, O_RDONLY | O_BINARY, 0644);
207 if (fh < 0) {
208 printf("Cannot open file %s\n", str);
209 continue;
210 }
211
212 /* print run info */
213 printf("Copy file %s to tape\n", str);
214 size = mb = 0;
215
216 do {
217 /* read buffer */
218 n = read(fh, buffer, TAPE_BUFFER_SIZE);
219
220 size += n;
221 if (verbose && size > mb + 1024 * 1024) {
222 mb = size;
223 printf("%d MB\r", size / 1024 / 1024);
224 fflush(stdout);
225 }
226
227 /* write buffer */
228 ss_tape_write(channel, buffer, n);
229
230 } while (n > 0);
231
232 if (verbose)
233 printf("%d MB\n", size / 1024 / 1024);
234
235 close(fh);
236
237 /* write EOF */
239 }
240
241 return SS_SUCCESS;
242}
#define O_BINARY
Definition msystem.h:219
INT ss_tape_write(INT channel, void *pdata, INT count)
Definition system.cxx:6032
DWORD n[4]
Definition mana.cxx:247
#define TAPE_BUFFER_SIZE
Definition midas.h:264
#define read(n, a, f)
char str[256]
Definition odbhist.cxx:33
Here is the call graph for this function:
Here is the caller graph for this function:

◆ tape_dir()

INT tape_dir ( INT  channel,
INT  count 
)

Definition at line 19 of file mtape.cxx.

20{
21 EVENT_HEADER *event;
22 char buffer[TAPE_BUFFER_SIZE];
23 INT status, size, index, blockn;
24 time_t t;
25
26 event = (EVENT_HEADER *) buffer;
27 for (index = 0; index < count; index++) {
29 /* read event header at current position */
30 size = sizeof(buffer);
31 status = ss_tape_read(channel, buffer, &size);
32 if (status != SS_SUCCESS) {
33 if (status == SS_END_OF_TAPE) {
34 printf("End of tape reached.\n");
35 return 1;
36 }
37 if (status == SS_END_OF_FILE) {
38 printf("End of File marker found, skipping...\n");
39 goto try_again;
40 }
41 printf("Cannot read from tape, status=%X\n", status);
42 return 1;
43 }
44
45 /* check if data is real MIDAS header */
46 if (event->event_id != EVENTID_BOR || event->trigger_mask != MIDAS_MAGIC) {
47#ifdef OS_UNIX
48 FILE *f;
49
50 /* write tape data in temporary file, let 'file' utility determine type */
51 f = fopen("/tmp/.mt", "w");
52 fwrite(buffer, sizeof(buffer), 1, f);
53 fclose(f);
54 system("file /tmp/.mt > /tmp/.mtf");
55 f = fopen("/tmp/.mtf", "r");
56 if (f != NULL) {
57 memset(buffer, 0, sizeof(buffer));
58 fread(buffer, sizeof(buffer), 1, f);
59
60 /* cut off new line */
61 if (strchr(buffer, '\n'))
62 *strchr(buffer, '\n') = 0;
63 if (strchr(buffer, '\r'))
64 *strchr(buffer, '\r') = 0;
65
66 printf("Found file: ");
67
68 /* start after ':' */
69 if (strchr(buffer, ':'))
70 printf("%s", strchr(buffer, ':') + 2);
71 else
72 printf("%s", buffer);
73 printf("\n");
74 fclose(f);
75 }
76 unlink("/tmp/.mtf");
77 unlink("/tmp/.mt");
78#else
79 printf("File on tape is no MIDAS data\n");
80#endif
81 } else {
83 t = (time_t) event->time_stamp;
84 if (blockn > 0)
85 printf("Found run #%d at block#:%d recorded on %s", event->serial_number,
86 blockn, ctime(&t));
87 else
88 printf("Found run #%d recorded on %s", event->serial_number, ctime(&t));
89 }
90 if (index < count - 1) {
91 printf("Spooling tape...\r");
94 if (status != SS_SUCCESS) {
95 printf("Error spooling tape\n");
96 return -1;
97 }
98 } else
99 /* skip back record */
101 }
102
103 return SS_SUCCESS;
104}
#define SS_END_OF_FILE
Definition midas.h:685
#define SS_END_OF_TAPE
Definition midas.h:684
INT ss_tape_get_blockn(INT channel)
Definition system.cxx:6568
INT ss_tape_read(INT channel, void *pdata, INT *count)
Definition system.cxx:6092
INT blockn
INT index
Definition mana.cxx:271
double count
Definition mdump.cxx:33
#define MIDAS_MAGIC
Definition midas.h:911
#define EVENTID_BOR
Definition midas.h:900
#define ctime
Definition msystem.h:264
short int event_id
Definition midas.h:852
DWORD serial_number
Definition midas.h:854
DWORD time_stamp
Definition midas.h:855
short int trigger_mask
Definition midas.h:853
Here is the call graph for this function:
Here is the caller graph for this function:

◆ tape_restore()

INT tape_restore ( INT  channel,
INT  count,
char file_name 
)

Definition at line 108 of file mtape.cxx.

109{
110 EVENT_HEADER *pevent;
111 INT status, index, n, fh, size, mb;
112 char buffer[TAPE_BUFFER_SIZE], str[80];
113
114 pevent = (EVENT_HEADER *) buffer;
115 for (index = 0; index < count; index++) {
116 /* read event header at current position */
117 try_again:
119 status = ss_tape_read(channel, buffer, &n);
120 if (status != SS_SUCCESS) {
121 if (status == SS_END_OF_TAPE) {
122 printf("End of tape reached.\n");
123 return SS_SUCCESS;
124 } else if (status == SS_END_OF_FILE) {
125 printf("End of File marker found, skipping...\n");
126 goto try_again;
127 }
128 return -1;
129 }
130
131 /* check if data is real MIDAS header */
132 if (pevent->event_id != EVENTID_BOR || pevent->trigger_mask != MIDAS_MAGIC) {
133 printf("Data on tape is no MIDAS data\n");
134 if (count > 1) {
135 printf("Skipping file...\r");
136 fflush(stdout);
138 continue;
139 }
140 return -1;
141 }
142
143 /* make file name */
144 if (strchr(file_name, '%'))
146 else
147 strcpy(str, file_name);
148
149 /* print run info */
150 printf("Copy run to file %s\n", str);
151
152 /* open file */
153 fh = open(str, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0644);
154 if (fh < 0) {
155 printf("Cannot open file %s\n", str);
156 return -1;
157 }
158
159 size = mb = 0;
160 do {
161 /* write buffer */
162 write(fh, buffer, n);
163
164 size += n;
165 if (verbose && size > mb + 1024 * 1024) {
166 mb = size;
167 printf("%d MB\r", size / 1024 / 1024);
168 fflush(stdout);
169 }
170
171 /* read next buffer */
173 ss_tape_read(channel, buffer, &n);
174 } while (n == TAPE_BUFFER_SIZE);
175
176 if (verbose)
177 printf("%d MB\n", size / 1024 / 1024);
178
179 write(fh, buffer, n);
180 close(fh);
181
182 /* skip to next file */
184 }
185
186 return SS_SUCCESS;
187}
#define write(n, a, f, d)
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ verbose

BOOL verbose = FALSE

Definition at line 15 of file mtape.cxx.