Debian Codegraph @

faketape/file-to-tape.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2024
3 * Tim Woodall. All rights reserved
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * file-to-tape. This reads a file created by dump and writes it to a faketape
31 * recreating the blocking as if it had been written by dump
32 */
33
34 #include "config.h" // IWYU pragma: keep
35 #include "faketape-lib.h"
36
37 #include <err.h> // errx
38 #include <fcntl.h> // O_WRONLY
39 #include <cstring>
40 #include <errno.h>
41 #include <stdint.h>
42 #include <stdlib.h>
43 #include <sys/types.h>
44 #include <unistd.h>
45 #include <iostream>
46 #include <string>
47 #include <vector>
48
49
50 // Messy. Ideally we'd use readblock_tape from restore/readtape.c but there are
51 // too many issues with global variables that we don't use but need to link in.
52
53
54 static FakeTape tape;
55
56 static std::vector<uint8_t> databuffer;
57
58 void usage(const char* prog) {
59 std::cerr << "Usage " << prog << " [ -b max block size ] [-r] faketape-img dump-img" << std::endl;
60 std::cerr << " -r write as max block size blocks ignoring original block sizes." << std::endl;
61 exit(1);
62 }
63
64 int main(int argc, char* argv[]) {
65
66 const char* prog = argv[0];
67
68 int ch;
69 ssize_t block_size = 0;
70 bool reblock = false;
71 while (( ch = getopt(argc, argv, "b:r")) != -1)
72 switch(ch) {
73 case 'b': {
74 block_size = atoi(optarg);
75 if (block_size < 1024 || block_size > 16*1024*1024)
76 errx(1, "Invalid blocksize, must be between 1024 and %d", 16*1024*1024);
77 tape.setMaxBlockSize(block_size);
78 break;
79 }
80 case 'r':
81 reblock = true;
82 break;
83 default:
84 usage(prog);
85 }
86 argc -= optind;
87 argv += optind;
88
89 if (argc < 2)
90 usage(prog);
91
92 /* First check that the target tape image does not exist */
93 if (open(argv[0], O_RDONLY) != -1)
94 errx(1, "%s exists, won't overwrite.", argv[0]);
95
96 if (tape.open(argv[0], O_WRONLY|O_CREAT) != FakeTape::Response::OK) {
97 errx(1, "Cannot open tape (%d %s)", errno, strerror(errno));
98 }
99 argv++;
100 argc--;
101
102 while(argc--) {
103 /* Now open the dump image */
104 int dumpfd;
105 warnx("Starting %s", argv[0]);
106 dumpfd = open(argv[0], O_RDONLY);
107 if (dumpfd == -1) {
108 err(1, "%s does not exist.", argv[0]);
109 }
110
111 std::vector<uint8_t> buffer(1024);
112
113 /* Read the first block */
114 if(int rsize = read(dumpfd, (void*)buffer.data(), 1024); rsize != 1024)
115 err(1, "Failed to read first block from dump image (%d)", rsize);
116
117 uint32_t ntrec;
118 bool compressed;
119
120 // Manually copied from protocols/dumprestore.h
121 int32_t* c_magic = (int32_t*)(buffer.data()+24);
122 int32_t* c_flags = (int32_t*)(buffer.data()+888);
123 uint32_t* c_ntrec = (uint32_t*)(buffer.data()+896);
124 #define NFS_MAGIC (int)60012
125 #define DR_COMPRESSED 0x0080 /* dump tape is compressed */
126
127 // No bswap etc. Assume it's compatible
128 if (*c_magic == NFS_MAGIC) {
129 compressed = *c_flags & DR_COMPRESSED;
130 ntrec = *c_ntrec;
131 } else
132 errx(1, "This doesn't appear to be a dump tape, or was written on a system with byteswapped values");
133
134 if(block_size == 0) {
135 if (ntrec)
136 block_size = ntrec * (size_t)1024;
137 else
138 errx(1, "Old dump tapes do not have blocksize in header. You must specify -b <blocksize> on command line");
139 }
140
141 if(ntrec && block_size != (ssize_t)ntrec*(ssize_t)1024)
142 warnx("Tape appears to have been written with a blocksize of %zu but commandline specified %zu", ntrec*(size_t)1024, block_size);
143
144 buffer.resize(block_size + 1024);
145 /* Read in the rest of the first block */
146 if (int rsize = read(dumpfd, buffer.data() + 1024, block_size - 1024); rsize != block_size-1024)
147 err(1, "Failed to read the rest of the first block from dump image (%d)", rsize);
148
149 /* Write first block to tape */
150 if(tape.writeData(buffer.data(), block_size) != FakeTape::Response::OK)
151 err(1, "Write of first block failed");
152
153 while(true) {
154 int rsize;
155 if (!reblock && compressed) {
156 rsize = read(dumpfd, (void*)buffer.data(), sizeof(uint32_t));
157 if (rsize == 0)
158 break;
159 if (rsize != sizeof(uint32_t))
160 err(1, "Failed to read compress header from dump image (%d)", rsize);
161 int bsize = *(uint32_t*)buffer.data() >> 4;
162 rsize = read(dumpfd, (void*)(buffer.data()+sizeof(uint32_t)), bsize);
163 if (rsize != bsize)
164 err(1, "Failed to read compressed block from dump image (%d)", rsize);
165 rsize += 4;
166 } else {
167 rsize = read(dumpfd, (void*)buffer.data(), block_size);
168 if (rsize == 0)
169 break;
170 if (rsize == -1)
171 err(1, "Failed to read block from dump image");
172 if (rsize < block_size)
173 warnx("Short block read. This could be due to an incorrect -b setting");
174 }
175 if (rsize == block_size+4) {
176 warnx("Splitting oversized block");
177 tape.writeData(buffer.data(), rsize-4);
178 tape.writeData(buffer.data()+rsize-4, 4);
179 } else {
180 tape.writeData(buffer.data(), rsize);
181 }
182 }
183 close(dumpfd);
184 warnx("Dump %s read successfully", argv[0]);
185 argv++;
186 }
187 tape.close();
188 warnx("Finished");
189 }
190
191 /* vim: set sw=8 sts=8 ts=8 noexpandtab: */