| 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 are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | * this list of conditions and the following disclaimer in the documentation |
| 13 | * and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 25 | * POSSIBILITY OF SUCH DAMAGE. |
| 26 | * |
| 27 | * SPDX short identifier: BSD-2-Clause |
| 28 | */ |
| 29 | |
| 30 | /* |
| 31 | * file-to-tape. This reads a file created by dump and writes it to a faketape |
| 32 | * recreating the blocking as if it had been written by dump |
| 33 | */ |
| 34 | |
| 35 | #include "config.h" // IWYU pragma: keep |
| 36 | #include "faketape-lib.h" |
| 37 | |
| 38 | #include <optional> |
| 39 | #include <algorithm> |
| 40 | #include <fcntl.h> |
| 41 | #include <getopt.h> |
| 42 | #include <stdint.h> |
| 43 | #include <stdlib.h> |
| 44 | #include <sys/types.h> |
| 45 | #include <unistd.h> |
| 46 | #include <iostream> |
| 47 | #include <iterator> |
| 48 | #include <limits> |
| 49 | #include <memory> |
| 50 | #include <string> |
| 51 | #include <vector> |
| 52 | |
| 53 | |
| 54 | using Response = FakeTape::Response; |
| 55 | |
| 56 | struct CMD { |
| 57 | std::string cmd; |
| 58 | Response (*callback)(FakeTape&, const std::string&, std::optional<off_t>); |
| 59 | }; |
| 60 | |
| 61 | static Response asf(FakeTape&, const std::string&, std::optional<off_t>); |
| 62 | static Response bsf(FakeTape&, const std::string&, std::optional<off_t>); |
| 63 | static Response bsfm(FakeTape&, const std::string&, std::optional<off_t>); |
| 64 | static Response bsr(FakeTape&, const std::string&, std::optional<off_t>); |
| 65 | static Response bss(FakeTape&, const std::string&, std::optional<off_t>); |
| 66 | static Response compression(FakeTape&, const std::string&, std::optional<off_t>); |
| 67 | static Response datasize(FakeTape&, const std::string&, std::optional<off_t>); |
| 68 | static Response defblksize(FakeTape&, const std::string&, std::optional<off_t>); |
| 69 | static Response defcompression(FakeTape&, const std::string&, std::optional<off_t>); |
| 70 | static Response defdensity(FakeTape&, const std::string&, std::optional<off_t>); |
| 71 | static Response defdrvbuffer(FakeTape&, const std::string&, std::optional<off_t>); |
| 72 | static Response densities(FakeTape&, const std::string&, std::optional<off_t>); |
| 73 | static Response drvbuffer(FakeTape&, const std::string&, std::optional<off_t>); |
| 74 | static Response eject(FakeTape&, const std::string&, std::optional<off_t>); |
| 75 | static Response eod(FakeTape&, const std::string&, std::optional<off_t>); |
| 76 | static Response eof(FakeTape&, const std::string&, std::optional<off_t>); |
| 77 | static Response erase(FakeTape&, const std::string&, std::optional<off_t>); |
| 78 | static Response fsf(FakeTape&, const std::string&, std::optional<off_t>); |
| 79 | static Response fsfm(FakeTape&, const std::string&, std::optional<off_t>); |
| 80 | static Response fsr(FakeTape&, const std::string&, std::optional<off_t>); |
| 81 | static Response fss(FakeTape&, const std::string&, std::optional<off_t>); |
| 82 | static Response load(FakeTape&, const std::string&, std::optional<off_t>); |
| 83 | static Response lock(FakeTape&, const std::string&, std::optional<off_t>); |
| 84 | static Response mkpartition(FakeTape&, const std::string&, std::optional<off_t>); |
| 85 | static Response offline(FakeTape&, const std::string&, std::optional<off_t>); |
| 86 | static Response partseek(FakeTape&, const std::string&, std::optional<off_t>); |
| 87 | static Response readdata(FakeTape&, const std::string&, std::optional<off_t>); |
| 88 | static Response retension(FakeTape&, const std::string&, std::optional<off_t>); |
| 89 | static Response rewind(FakeTape&, const std::string&, std::optional<off_t>); |
| 90 | static Response rewoffl(FakeTape&, const std::string&, std::optional<off_t>); |
| 91 | static Response seek(FakeTape&, const std::string&, std::optional<off_t>); |
| 92 | static Response seod(FakeTape&, const std::string&, std::optional<off_t>); |
| 93 | static Response setblk(FakeTape&, const std::string&, std::optional<off_t>); |
| 94 | static Response setdensity(FakeTape&, const std::string&, std::optional<off_t>); |
| 95 | static Response setpartition(FakeTape&, const std::string&, std::optional<off_t>); |
| 96 | static Response status(FakeTape&, const std::string&, std::optional<off_t>); |
| 97 | static Response stclearoptions(FakeTape&, const std::string&, std::optional<off_t>); |
| 98 | static Response stlongtimeout(FakeTape&, const std::string&, std::optional<off_t>); |
| 99 | static Response stoptions(FakeTape&, const std::string&, std::optional<off_t>); |
| 100 | static Response stsetcln(FakeTape&, const std::string&, std::optional<off_t>); |
| 101 | static Response stsetoptions(FakeTape&, const std::string&, std::optional<off_t>); |
| 102 | static Response stshowoptions(FakeTape&, const std::string&, std::optional<off_t>); |
| 103 | static Response sttimeout(FakeTape&, const std::string&, std::optional<off_t>); |
| 104 | static Response stwrthreshold(FakeTape&, const std::string&, std::optional<off_t>); |
| 105 | static Response tell(FakeTape&, const std::string&, std::optional<off_t>); |
| 106 | static Response unlock(FakeTape&, const std::string&, std::optional<off_t>); |
| 107 | static Response weof(FakeTape&, const std::string&, std::optional<off_t>); |
| 108 | static Response writedata(FakeTape&, const std::string&, std::optional<off_t>); |
| 109 | static Response wset(FakeTape&, const std::string&, std::optional<off_t>); |
| 110 | |
| 111 | const std::vector<CMD> cmds = { |
| 112 | {"asf", asf }, |
| 113 | {"bsf", bsf }, |
| 114 | {"bsfm", bsfm }, |
| 115 | {"bsr", bsr }, |
| 116 | {"bss", bss }, |
| 117 | {"compression", compression }, |
| 118 | {"datasize", datasize }, |
| 119 | {"defblksize", defblksize }, |
| 120 | {"defcompression", defcompression }, |
| 121 | {"defdensity", defdensity }, |
| 122 | {"defdrvbuffer", defdrvbuffer }, |
| 123 | {"densities", densities }, |
| 124 | {"drvbuffer", drvbuffer }, |
| 125 | {"eject", eject }, |
| 126 | {"eod", eod }, |
| 127 | {"eof", eof }, |
| 128 | {"erase", erase }, |
| 129 | {"fsf", fsf }, |
| 130 | {"fsfm", fsfm }, |
| 131 | {"fsr", fsr }, |
| 132 | {"fss", fss }, |
| 133 | {"load", load }, |
| 134 | {"lock", lock }, |
| 135 | {"mkpartition", mkpartition }, |
| 136 | {"offline", offline }, |
| 137 | {"partseek", partseek }, |
| 138 | {"readdata", readdata }, |
| 139 | {"retension", retension }, |
| 140 | {"rewind", rewind }, |
| 141 | {"rewoffl", rewoffl }, |
| 142 | {"seek", seek }, |
| 143 | {"seod", seod }, |
| 144 | {"setblk", setblk }, |
| 145 | {"setdensity", setdensity }, |
| 146 | {"setpartition", setpartition }, |
| 147 | {"status", status }, |
| 148 | {"stclearoptions", stclearoptions }, |
| 149 | {"stlongtimeout", stlongtimeout }, |
| 150 | {"stoptions", stoptions }, |
| 151 | {"stsetcln", stsetcln }, |
| 152 | {"stsetoptions", stsetoptions }, |
| 153 | {"stshowoptions", stshowoptions }, |
| 154 | {"sttimeout", sttimeout }, |
| 155 | {"stwrthreshold", stwrthreshold }, |
| 156 | {"tell", tell }, |
| 157 | {"unlock", unlock }, |
| 158 | {"weof", weof }, |
| 159 | {"writedata", writedata }, |
| 160 | {"wset", wset }, |
| 161 | }; |
| 162 | |
| 163 | void usage(const char* prog) { |
| 164 | std::cout << std::endl; |
| 165 | std::cout << "Usage: " << prog << "[--debug/-d] [--version/-v] [-h] -f <tape> command [ count ]" << std::endl; |
| 166 | std::cout << "commands: "; |
| 167 | size_t len = 0; |
| 168 | for(const auto& c : cmds) { |
| 169 | if (len == 0) { |
| 170 | std::cout << c.cmd; |
| 171 | len=10 + c.cmd.size(); |
| 172 | } else if (len + c.cmd.size() + 3 > 80) { |
| 173 | std::cout << std::endl << " " << c.cmd; |
| 174 | len=10 + c.cmd.size(); |
| 175 | } else { |
| 176 | std::cout << ", " << c.cmd; |
| 177 | len += 3 + c.cmd.size(); |
| 178 | } |
| 179 | } |
| 180 | std::cout << std::endl; |
| 181 | exit(1); |
| 182 | } |
| 183 | |
| 184 | int main(int argc, char* argv[]) { |
| 185 | |
| 186 | const char* prog = argv[0]; |
| 187 | |
| 188 | int ch; |
| 189 | |
| 190 | const char* opts = "dhf:v"; |
| 191 | struct option lopts[] = { |
| 192 | { .name = "version", .has_arg = 0, .flag = nullptr, .val = 'v' }, |
| 193 | { .name = "debug", .has_arg = 0, .flag = nullptr, .val = 'd' }, |
| 194 | {}, |
| 195 | }; |
| 196 | std::string tapedev; |
| 197 | FakeTape tape; |
| 198 | |
| 199 | while (( ch = getopt_long(argc, argv, opts, lopts, nullptr)) != -1) |
| 200 | switch(ch) { |
| 201 | case 'd': |
| 202 | tape.debug = std::make_unique<std::ostream>(std::cerr.rdbuf()); |
| 203 | tape.debug->copyfmt(std::cerr); |
| 204 | tape.debug->clear(std::cout.rdstate()); |
| 205 | break; |
| 206 | case 'v': |
| 207 | std::cout << prog << " version 1.0" << std::endl; |
| 208 | break; |
| 209 | case 'f': |
| 210 | tapedev = optarg; |
| 211 | break; |
| 212 | default: |
| 213 | usage(prog); |
| 214 | } |
| 215 | argc -= optind; |
| 216 | argv += optind; |
| 217 | |
| 218 | if (argc < 1) |
| 219 | usage(prog); |
| 220 | |
| 221 | std::optional<off_t> count = std::nullopt; |
| 222 | if (argc == 2) |
| 223 | count = atoll(argv[1]); |
| 224 | |
| 225 | auto it = std::find_if(std::begin(cmds), std::end(cmds), [argv](const CMD& s){ return std::string(s.cmd)==argv[0]; }); |
| 226 | if (it == std::end(cmds)) { |
| 227 | std::cerr << "unrecognised command: " << argv[0] << std::endl; |
| 228 | usage(prog); |
| 229 | } |
| 230 | if (it->callback(tape, tapedev, count) == Response::ERROR) |
| 231 | std::cerr << prog << " " << argv[0] << ":Error" << std::endl; |
| 232 | } |
| 233 | |
| 234 | static Response unimplemented(const std::string& cmd) { |
| 235 | std::cerr << "'" << cmd << "' unimplemented'." << std::endl; |
| 236 | return Response::ERROR; |
| 237 | } |
| 238 | |
| 239 | static Response extraarg(const std::string& cmd) { |
| 240 | std::cerr << "Too many arguments for the command '" << cmd << "'." << std::endl; |
| 241 | return Response::ERROR; |
| 242 | } |
| 243 | |
| 244 | #define TAPECMDNOFATAL(x) do { if(x == Response::FATAL) return Response::ERROR; } while(0) |
| 245 | #define TAPECMDNOERROR(x) do { if(x != Response::OK) return Response::ERROR; } while(0) |
| 246 | |
| 247 | static Response fsf_impl(FakeTape& tape, off_t count) { |
| 248 | while(count--) { |
| 249 | uint32_t fno = tape.getFileNo(); |
| 250 | do { |
| 251 | TAPECMDNOERROR(tape.fsr(1)); |
| 252 | } while(fno == tape.getFileNo()); |
| 253 | } |
| 254 | return Response::OK; |
| 255 | } |
| 256 | |
| 257 | static Response bsf_impl(FakeTape& tape, off_t count) { |
| 258 | while(count--) { |
| 259 | uint32_t fno = tape.getFileNo(); |
| 260 | do { |
| 261 | TAPECMDNOERROR(tape.bsr(1)); |
| 262 | } while(fno == tape.getFileNo()); |
| 263 | } |
| 264 | return Response::OK; |
| 265 | } |
| 266 | |
| 267 | static Response asf(FakeTape& tape, const std::string& tapedev, std::optional<off_t> count) { |
| 268 | TAPECMDNOERROR(tape.open(tapedev, O_RDONLY)); |
| 269 | TAPECMDNOERROR(tape.rewind()); |
| 270 | TAPECMDNOERROR(fsf_impl(tape, count.has_value()?*count:1)); |
| 271 | return Response::OK; |
| 272 | } |
| 273 | |
| 274 | static Response bsf(FakeTape& tape, const std::string& tapedev, std::optional<off_t> count) { |
| 275 | TAPECMDNOERROR(tape.open(tapedev, O_RDONLY)); |
| 276 | return bsf_impl(tape, count.has_value()?*count:1); |
| 277 | } |
| 278 | |
| 279 | static Response bsfm(FakeTape& tape, const std::string& tapedev, std::optional<off_t> count) { |
| 280 | TAPECMDNOERROR(tape.open(tapedev, O_RDONLY)); |
| 281 | TAPECMDNOERROR(bsf_impl(tape, count.has_value()?*count:1)); |
| 282 | TAPECMDNOERROR(tape.fsr(1)); |
| 283 | return Response::OK; |
| 284 | } |
| 285 | |
| 286 | static Response bsr(FakeTape& tape, const std::string& tapedev, std::optional<off_t> count) { |
| 287 | TAPECMDNOERROR(tape.open(tapedev, O_RDONLY)); |
| 288 | TAPECMDNOERROR(tape.bsr(count.has_value()?*count:1)); |
| 289 | return Response::OK; |
| 290 | } |
| 291 | |
| 292 | static Response bss(FakeTape& tape, const std::string& tapedev, std::optional<off_t> count) { |
| 293 | return bsr(tape, tapedev, count); |
| 294 | } |
| 295 | |
| 296 | static Response compression(FakeTape&, const std::string&, std::optional<off_t>) { |
| 297 | return unimplemented("compression"); |
| 298 | } |
| 299 | |
| 300 | static Response datasize(FakeTape& tape, const std::string& tapedev, std::optional<off_t> count) { |
| 301 | if(count.has_value()) return extraarg("datasize"); |
| 302 | TAPECMDNOERROR(tape.open(tapedev, O_RDONLY)); |
| 303 | std::vector<uint8_t> data(FakeTape::getMaxBlockSize()); |
| 304 | size_t rl; |
| 305 | TAPECMDNOERROR(tape.readData(data.data(), data.size(), &rl)); |
| 306 | std::cout << rl << std::endl; |
| 307 | TAPECMDNOERROR(tape.bsr(1)); |
| 308 | return Response::OK; |
| 309 | } |
| 310 | |
| 311 | static Response defblksize(FakeTape&, const std::string&, std::optional<off_t>) { |
| 312 | return unimplemented("defblksize"); |
| 313 | } |
| 314 | |
| 315 | static Response defcompression(FakeTape&, const std::string&, std::optional<off_t>) { |
| 316 | return unimplemented("defcompression"); |
| 317 | } |
| 318 | |
| 319 | static Response defdensity(FakeTape&, const std::string&, std::optional<off_t>) { |
| 320 | return unimplemented("defdensity"); |
| 321 | } |
| 322 | |
| 323 | static Response defdrvbuffer(FakeTape&, const std::string&, std::optional<off_t>) { |
| 324 | return unimplemented("defdrvbuffer"); |
| 325 | } |
| 326 | |
| 327 | static Response densities(FakeTape&, const std::string&, std::optional<off_t> count) { |
| 328 | if(count.has_value()) return extraarg("densities"); |
| 329 | |
| 330 | std::cout << "Some SCSI tape density codes:" << std::endl; |
| 331 | std::cout << "code explanation code explanation" << std::endl; |
| 332 | std::cout << "0x00 default 0x33 SLR6" << std::endl; |
| 333 | std::cout << "0x01 NRZI (800 bpi) 0x34 SLR100" << std::endl; |
| 334 | std::cout << "0x02 PE (1600 bpi) 0x40 DLT1 40 GB, or Ultrium" << std::endl; |
| 335 | std::cout << "0x03 GCR (6250 bpi) 0x41 DLT 40GB, or Ultrium2" << std::endl; |
| 336 | std::cout << "0x04 QIC-11 0x42 LTO-2" << std::endl; |
| 337 | std::cout << "0x05 QIC-45/60 (GCR, 8000 bpi) 0x44 LTO-3" << std::endl; |
| 338 | std::cout << "0x06 PE (3200 bpi) 0x45 QIC-3095-MC (TR-4)" << std::endl; |
| 339 | std::cout << "0x07 IMFM (6400 bpi) 0x46 LTO-4" << std::endl; |
| 340 | std::cout << "0x08 GCR (8000 bpi) 0x47 DDS-5 or TR-5" << std::endl; |
| 341 | std::cout << "0x09 GCR (37871 bpi) 0x48 SDLT220" << std::endl; |
| 342 | std::cout << "0x0a MFM (6667 bpi) 0x49 SDLT320" << std::endl; |
| 343 | std::cout << "0x0b PE (1600 bpi) 0x4a SDLT600, T10000A" << std::endl; |
| 344 | std::cout << "0x0c GCR (12960 bpi) 0x4b T10000B" << std::endl; |
| 345 | std::cout << "0x0d GCR (25380 bpi) 0x4c T10000C" << std::endl; |
| 346 | std::cout << "0x0f QIC-120 (GCR 10000 bpi) 0x4d T10000D" << std::endl; |
| 347 | std::cout << "0x10 QIC-150/250 (GCR 10000 bpi) 0x51 IBM 3592 J1A" << std::endl; |
| 348 | std::cout << "0x11 QIC-320/525 (GCR 16000 bpi) 0x52 IBM 3592 E05" << std::endl; |
| 349 | std::cout << "0x12 QIC-1350 (RLL 51667 bpi) 0x53 IBM 3592 E06" << std::endl; |
| 350 | std::cout << "0x13 DDS (61000 bpi) 0x54 IBM 3592 E07" << std::endl; |
| 351 | std::cout << "0x14 EXB-8200 (RLL 43245 bpi) 0x55 IBM 3592 E08" << std::endl; |
| 352 | std::cout << "0x15 EXB-8500 or QIC-1000 0x58 LTO-5" << std::endl; |
| 353 | std::cout << "0x16 MFM 10000 bpi 0x5a LTO-6" << std::endl; |
| 354 | std::cout << "0x17 MFM 42500 bpi 0x5c LTO-7" << std::endl; |
| 355 | std::cout << "0x18 TZ86 0x5d LTO-7-M8" << std::endl; |
| 356 | std::cout << "0x19 DLT 10GB 0x5e LTO-8" << std::endl; |
| 357 | std::cout << "0x1a DLT 20GB 0x71 IBM 3592 J1A, encrypted" << std::endl; |
| 358 | std::cout << "0x1b DLT 35GB 0x72 IBM 3592 E05, encrypted" << std::endl; |
| 359 | std::cout << "0x1c QIC-385M 0x73 IBM 3592 E06, encrypted" << std::endl; |
| 360 | std::cout << "0x1d QIC-410M 0x74 IBM 3592 E07, encrypted" << std::endl; |
| 361 | std::cout << "0x1e QIC-1000C 0x75 IBM 3592 E08, encrypted" << std::endl; |
| 362 | std::cout << "0x1f QIC-2100C 0x80 DLT 15GB uncomp. or Ecrix" << std::endl; |
| 363 | std::cout << "0x20 QIC-6GB 0x81 DLT 15GB compressed" << std::endl; |
| 364 | std::cout << "0x21 QIC-20GB 0x82 DLT 20GB uncompressed" << std::endl; |
| 365 | std::cout << "0x22 QIC-2GB 0x83 DLT 20GB compressed" << std::endl; |
| 366 | std::cout << "0x23 QIC-875 0x84 DLT 35GB uncompressed" << std::endl; |
| 367 | std::cout << "0x24 DDS-2 0x85 DLT 35GB compressed" << std::endl; |
| 368 | std::cout << "0x25 DDS-3 0x86 DLT1 40 GB uncompressed" << std::endl; |
| 369 | std::cout << "0x26 DDS-4 or QIC-4GB 0x87 DLT1 40 GB compressed" << std::endl; |
| 370 | std::cout << "0x27 Exabyte Mammoth 0x88 DLT 40GB uncompressed" << std::endl; |
| 371 | std::cout << "0x28 Exabyte Mammoth-2 0x89 DLT 40GB compressed" << std::endl; |
| 372 | std::cout << "0x29 QIC-3080MC, IBM 3590 B 0x8c EXB-8505 compressed" << std::endl; |
| 373 | std::cout << "0x2a IBM 3590 E 0x90 SDLT110 uncompr/EXB-8205 compr" << std::endl; |
| 374 | std::cout << "0x30 AIT-1 or MLR3 0x91 SDLT110 compressed" << std::endl; |
| 375 | std::cout << "0x31 AIT-2 0x92 SDLT160 uncompressed" << std::endl; |
| 376 | std::cout << "0x32 AIT-3 or SLR7 0x93 SDLT160 comprssed" << std::endl; |
| 377 | return Response::OK; |
| 378 | } |
| 379 | |
| 380 | static Response drvbuffer(FakeTape&, const std::string&, std::optional<off_t>) { |
| 381 | return unimplemented("drvbuffer"); |
| 382 | } |
| 383 | |
| 384 | static Response eject(FakeTape& tape, const std::string& tapedev, std::optional<off_t> count) { |
| 385 | if(count.has_value()) return extraarg("eject"); |
| 386 | return rewind(tape, tapedev, count); |
| 387 | } |
| 388 | |
| 389 | static Response eod(FakeTape& tape, const std::string& tapedev, std::optional<off_t> count) { |
| 390 | if(count.has_value()) return extraarg("eod"); |
| 391 | TAPECMDNOERROR(tape.open(tapedev, O_RDONLY)); |
| 392 | size_t seek = (std::numeric_limits<size_t>::max()>>1)+1; |
| 393 | while (seek) { |
| 394 | TAPECMDNOFATAL(tape.fsr(seek)); |
| 395 | seek >>= 1; |
| 396 | } |
| 397 | return Response::OK; |
| 398 | } |
| 399 | |
| 400 | static Response eof(FakeTape& tape, const std::string& tapedev, std::optional<off_t> count) { |
| 401 | int c = 1; |
| 402 | if(count.has_value()) |
| 403 | c = *count; |
| 404 | TAPECMDNOERROR(tape.open(tapedev, O_WRONLY)); |
| 405 | while(c--) |
| 406 | TAPECMDNOERROR(tape.writeData(nullptr, 0)); |
| 407 | return Response::OK; |
| 408 | } |
| 409 | |
| 410 | static Response erase(FakeTape& tape, const std::string& tapedev, std::optional<off_t>) { |
| 411 | TAPECMDNOERROR(tape.open(tapedev, O_WRONLY|O_CREAT)); |
| 412 | TAPECMDNOERROR(tape.erase()); |
| 413 | return Response::OK; |
| 414 | } |
| 415 | |
| 416 | static Response fsf(FakeTape& tape, const std::string& tapedev, std::optional<off_t> count) { |
| 417 | TAPECMDNOERROR(tape.open(tapedev, O_RDONLY)); |
| 418 | return fsf_impl(tape, count.has_value()?*count:1); |
| 419 | } |
| 420 | |
| 421 | static Response fsfm(FakeTape& tape, const std::string& tapedev, std::optional<off_t> count) { |
| 422 | TAPECMDNOERROR(tape.open(tapedev, O_RDONLY)); |
| 423 | TAPECMDNOERROR(fsf_impl(tape, count.has_value()?*count:1)); |
| 424 | TAPECMDNOERROR(tape.bsr(1)); |
| 425 | return Response::OK; |
| 426 | } |
| 427 | |
| 428 | static Response fsr(FakeTape& tape, const std::string& tapedev, std::optional<off_t> count) { |
| 429 | TAPECMDNOERROR(tape.open(tapedev, O_RDONLY)); |
| 430 | TAPECMDNOERROR(tape.fsr(count.has_value()?*count:1)); |
| 431 | return Response::OK; |
| 432 | } |
| 433 | |
| 434 | static Response fss(FakeTape& tape, const std::string& tapedev, std::optional<off_t> count) { |
| 435 | return fsr(tape, tapedev, count); |
| 436 | } |
| 437 | |
| 438 | static Response load(FakeTape& tape, const std::string& tapedev, std::optional<off_t>) { |
| 439 | TAPECMDNOERROR(tape.open(tapedev, O_RDONLY)); |
| 440 | return Response::OK; |
| 441 | } |
| 442 | |
| 443 | static Response lock(FakeTape&, const std::string&, std::optional<off_t> count) { |
| 444 | if(count.has_value()) return extraarg("lock"); |
| 445 | return unimplemented("lock"); |
| 446 | } |
| 447 | |
| 448 | static Response mkpartition(FakeTape&, const std::string&, std::optional<off_t>) { |
| 449 | return unimplemented("mkpartition"); |
| 450 | } |
| 451 | |
| 452 | static Response offline(FakeTape& tape, const std::string& tapedev, std::optional<off_t> count) { |
| 453 | if(count.has_value()) return extraarg("offline"); |
| 454 | return rewind(tape, tapedev, count); |
| 455 | } |
| 456 | |
| 457 | static Response readdata(FakeTape& tape, const std::string& tapedev, std::optional<off_t> count) { |
| 458 | if(count.has_value()) return extraarg("readdata"); |
| 459 | TAPECMDNOERROR(tape.open(tapedev, O_RDONLY)); |
| 460 | std::vector<uint8_t> data(FakeTape::getMaxBlockSize()); |
| 461 | size_t rl; |
| 462 | TAPECMDNOERROR(tape.readData(data.data(), data.size(), &rl)); |
| 463 | if (rl == 0) |
| 464 | return Response::OK; |
| 465 | if (write(1, data.data(), rl) != (ssize_t)rl) |
| 466 | return Response::ERROR; |
| 467 | return Response::OK; |
| 468 | } |
| 469 | |
| 470 | static Response partseek(FakeTape&, const std::string&, std::optional<off_t>) { |
| 471 | return unimplemented("partseek"); |
| 472 | } |
| 473 | |
| 474 | static Response retension(FakeTape&, const std::string&, std::optional<off_t> count) { |
| 475 | if(count.has_value()) return extraarg("retension"); |
| 476 | return unimplemented("retension"); |
| 477 | } |
| 478 | |
| 479 | static Response rewind(FakeTape& tape, const std::string& tapedev, std::optional<off_t> count) { |
| 480 | if(count.has_value()) return extraarg("rewind"); |
| 481 | TAPECMDNOERROR(tape.open(tapedev, O_RDONLY)); |
| 482 | TAPECMDNOERROR(tape.rewind()); |
| 483 | return Response::OK; |
| 484 | } |
| 485 | |
| 486 | static Response rewoffl(FakeTape& tape, const std::string& tapedev, std::optional<off_t> count) { |
| 487 | if(count.has_value()) return extraarg("rewoffl"); |
| 488 | return rewind(tape, tapedev, count); |
| 489 | } |
| 490 | |
| 491 | static Response seek(FakeTape&, const std::string&, std::optional<off_t>) { |
| 492 | return unimplemented("seek"); |
| 493 | } |
| 494 | |
| 495 | static Response seod(FakeTape& tape, const std::string& tapedev, std::optional<off_t> count) { |
| 496 | if(count.has_value()) return extraarg("seod"); |
| 497 | return eod(tape, tapedev, count); |
| 498 | } |
| 499 | |
| 500 | static Response setblk(FakeTape&, const std::string&, std::optional<off_t>) { |
| 501 | return unimplemented("setblk"); |
| 502 | } |
| 503 | |
| 504 | static Response setdensity(FakeTape&, const std::string&, std::optional<off_t>) { |
| 505 | return unimplemented("setdensity"); |
| 506 | } |
| 507 | |
| 508 | static Response setpartition(FakeTape&, const std::string&, std::optional<off_t>) { |
| 509 | return unimplemented("setpartition"); |
| 510 | } |
| 511 | |
| 512 | static Response status(FakeTape& tape, const std::string& tapedev, std::optional<off_t> count) { |
| 513 | if(count.has_value()) return extraarg("status"); |
| 514 | |
| 515 | TAPECMDNOERROR(tape.open(tapedev, O_RDONLY)); |
| 516 | std::cout << "SCSI 2 tape drive:" << std::endl; |
| 517 | std::cout << "File number=" << tape.getFileNo() << ", block number=" << tape.getFileBlock() << ", partition=0." << std::endl; |
| 518 | std::cout << "Tape block size 0 bytes. Density code 0x5e (LTO-8)." << std::endl; |
| 519 | std::cout << "Soft error count since last status=0" << std::endl; |
| 520 | std::cout << "General status bits on (81010000):" << std::endl; |
| 521 | std::cout << " EOF ONLINE IM_REP_EN" << std::endl; |
| 522 | return Response::OK; |
| 523 | } |
| 524 | |
| 525 | static Response stclearoptions(FakeTape&, const std::string&, std::optional<off_t>) { |
| 526 | return unimplemented("stclearoptions"); |
| 527 | } |
| 528 | |
| 529 | static Response stlongtimeout(FakeTape&, const std::string&, std::optional<off_t>) { |
| 530 | return unimplemented("stlongtimeout"); |
| 531 | } |
| 532 | |
| 533 | static Response stoptions(FakeTape&, const std::string&, std::optional<off_t>) { |
| 534 | return unimplemented("stoptions"); |
| 535 | } |
| 536 | |
| 537 | static Response stsetcln(FakeTape&, const std::string&, std::optional<off_t>) { |
| 538 | return unimplemented("stsetcln"); |
| 539 | } |
| 540 | |
| 541 | static Response stsetoptions(FakeTape&, const std::string&, std::optional<off_t>) { |
| 542 | return unimplemented("stsetoptions"); |
| 543 | } |
| 544 | |
| 545 | static Response stshowoptions(FakeTape&, const std::string&, std::optional<off_t>) { |
| 546 | return unimplemented("stshowoptions"); |
| 547 | } |
| 548 | |
| 549 | static Response sttimeout(FakeTape&, const std::string&, std::optional<off_t>) { |
| 550 | return unimplemented("sttimeout"); |
| 551 | } |
| 552 | |
| 553 | static Response stwrthreshold(FakeTape&, const std::string&, std::optional<off_t>) { |
| 554 | return unimplemented("stwrthreshold"); |
| 555 | } |
| 556 | |
| 557 | static Response tell(FakeTape&, const std::string&, std::optional<off_t> count) { |
| 558 | if(count.has_value()) return extraarg("tell"); |
| 559 | return unimplemented("tell"); |
| 560 | } |
| 561 | |
| 562 | static Response unlock(FakeTape&, const std::string&, std::optional<off_t> count) { |
| 563 | if(count.has_value()) return extraarg("unlock"); |
| 564 | return unimplemented("unlock"); |
| 565 | } |
| 566 | |
| 567 | static Response weof(FakeTape& tape, const std::string& tapedev, std::optional<off_t> count) { |
| 568 | return eof(tape, tapedev, count); |
| 569 | } |
| 570 | |
| 571 | static Response writedata(FakeTape& tape, const std::string& tapedev, std::optional<off_t> count) { |
| 572 | if(count.has_value()) return extraarg("writedata"); |
| 573 | TAPECMDNOERROR(tape.open(tapedev, O_WRONLY)); |
| 574 | std::vector<uint8_t> data(FakeTape::getMaxBlockSize()); |
| 575 | ssize_t rl; |
| 576 | rl = read(0, data.data(), data.size()); |
| 577 | if (rl <= 0) |
| 578 | return Response::ERROR; |
| 579 | TAPECMDNOERROR(tape.writeData(data.data(), rl)); |
| 580 | return Response::OK; |
| 581 | } |
| 582 | |
| 583 | static Response wset(FakeTape&, const std::string&, std::optional<off_t>) { |
| 584 | return unimplemented("wset"); |
| 585 | } |
| 586 | |
| 587 | /* vim: set sw=8 sts=8 ts=8 noexpandtab: */ |