1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#![deny(
missing_debug_implementations,
missing_copy_implementations,
trivial_casts,
trivial_numeric_casts,
unused_import_braces,
unused_qualifications,
nonstandard_style,
rust_2018_idioms,
unused,
warnings
)]
#![deny(clippy::pedantic)]
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;
use std::str::FromStr;
use cpclib_common::clap::{Arg, ArgGroup, Command};
use cpclib_disc::amsdos::*;
use cpclib_disc::edsk::ExtendedDsk;
use custom_error::custom_error;
custom_error! {pub DskManagerError
IOError{source: std::io::Error} = "IO error: {source}.",
DiscConfigError{source: cpclib_disc::cfg::DiscConfigError} = "Disc configuration: {source}",
}
// Still everything to do
#[allow(clippy::too_many_lines)]
fn main() -> Result<(), DskManagerError> {
let app = Command::new("dsk_manager")
.about("Manipulate DSK files")
.author("Krusty/Benediction")
.after_help("Pale buggy copy of an old Ramlaid's tool")
.arg(
Arg::new("DSK_FILE")
.help("DSK file to manipulate")
.required(true)
.index(1)
)
.subcommand(
Command::new("format")
.about("Format a dsk")
.arg(
Arg::new("FORMAT_FILE")
.help("Provide a file that describes the format of the disc")
.long("description")
.short('d')
.takes_value(true)
)
.arg(
Arg::new("FORMAT_NAME")
.help("Provide the name of a format that can be used")
.short('f')
.long("format")
.takes_value(true)
.possible_values(&["data", "data42"])
)
.group(
ArgGroup::new("command")
.arg("FORMAT_FILE")
.arg("FORMAT_NAME")
.required(true)
)
)
.subcommand(
Command::new("catalog")
.about("Manipulate the catalog. Can only works for DSK having a Track 0 compatible with Amsdos")
.arg(
Arg::new("IMPORT")
.help("Import an existing catalog in the dsk. All entries are thus erased")
.long("import")
.short('i')
.takes_value(true)
)
.arg(
Arg::new("EXPORT")
.help("Export the catalog in a specific file")
.long("export")
.short('e')
.takes_value(true)
)
.arg(
Arg::new("LIST")
.help("Display the catalog on screen")
.long("list")
.short('l')
)
.arg(
Arg::new("CATART")
.help("[unimplemented] Display the catart version")
.long("--catart")
)
.group(
ArgGroup::new("command")
.arg("IMPORT")
.arg("EXPORT")
.arg("LIST")
.arg("CATART")
.required(true)
)
)
.subcommand(
Command::new("add")
.about("Add files in the disc in an Amsdos way")
.arg(
Arg::new("INPUT_FILES")
.help("The files to add. They MUST have a header")
.takes_value(true)
.multiple_occurrences(true)
.required(true)
)
.arg(
Arg::new("SYSTEM")
.help("Indicates if the files are system files")
.long("system")
.short('s')
)
.arg(
Arg::new("READ_ONLY")
.help("Indicates if the files are read only")
.long("read_only")
.short('r')
)
.arg(
Arg::new("AS_AMSDOS")
.help("[unimplemented] Uses the same strategy as amsdos when adding a file: add .???, delete .BAK, rename other as .BAK, rename .??? with real extension")
.long("secure")
)
)
.subcommand(
Command::new("put")
.about("Add files in the disc in a sectorial way")
.arg(
Arg::new("TRACK")
.help("The track of interest")
.short('a')
.takes_value(true)
.required(true)
)
.arg(
Arg::new("SECTOR")
.help("The sector of interest")
.short('o')
.takes_value(true)
.required(true)
)
.arg(
Arg::new("SIDE")
.help("The head of interest")
.short('p')
.takes_value(true)
.required(true)
)
.arg(
Arg::new("Z80_EXPORT")
.help("The path to the z80 files that will contains all the import information")
.short('z')
.takes_value(true)
.required(false)
)
.arg(
Arg::new("FILES")
.help("The ordered list of files to import in the dsk")
.takes_value(true)
.multiple_occurrences(true)
.required(true)
.last(true)
)
);
let matches = app.get_matches();
let dsk_fname = matches.value_of("DSK_FILE").unwrap();
// Manipulate the catalog of a disc
if let Some(sub) = matches.subcommand_matches("catalog") {
let mut dsk = ExtendedDsk::open(dsk_fname)
.unwrap_or_else(|_| panic!("Unable to open the file {}", dsk_fname));
eprintln!("WIP - We assume head 0 is chosen");
// Import the catalog from one file in one existing disc
if let Some(fname) = sub.value_of("IMPORT") {
let mut f = File::open(fname)?;
let mut bytes = Vec::new();
let size = f.read_to_end(&mut bytes)?;
if size != 64 * 32 {
eprintln!(
"Catalog size uses {} bytes whereas it should be {}",
size,
64 * 32
);
}
for idx in 0..4 {
let sector = dsk.sector_mut(0, 0, idx + 0xc1).expect("Wrong format");
let idx = idx as usize;
sector
.set_values(&bytes[idx * 512..(idx + 1) * 512])
.unwrap();
}
dsk.save(dsk_fname)?;
/*
// TODO find why this method DOES NOT WORK
// Generate the entry for this catart
let entries = AmsdosEntries::from_slice(&bytes);
// And inject it in the disc
let mut manager = AmsdosManager::new_from_disc(dsk, 0);
manager.set_catalog(&entries);
let copy = manager.catalog();
assert_eq!(
copy,
entries
);
manager.dsk_mut().save(dsk_fname)?;
*/
// override the disc
}
// Export the catalog of an existing disc in a file
else if let Some(fname) = sub.value_of("EXPORT") {
eprintln!("WIP - We assume the format of the Track 0 is similar to Amsdos one");
let manager = AmsdosManager::new_from_disc(dsk, 0);
let bytes = manager.catalog().as_bytes();
let mut f = File::create(fname)?;
f.write_all(&bytes)?;
} else if sub.is_present("LIST") {
let manager = AmsdosManager::new_from_disc(dsk, 0);
let catalog = manager.catalog();
let entries = catalog.visible_entries().collect::<Vec<_>>();
// TODO manage files instead of entries
println!("Dsk {} -- {} files", dsk_fname, entries.len());
for entry in &entries {
println!("{}", entry.to_string());
}
} else {
panic!("Error - missing argument");
}
}
else if let Some(sub) = matches.subcommand_matches("put") {
use cpclib_tokens::*;
// Add files in a sectorial way
let mut track = u8::from_str(sub.value_of("TRACK").unwrap()).expect("Wrong track format");
let mut sector = u8::from_str(sub.value_of("SECTOR").unwrap()).expect("Wrong track format");
let mut head = u8::from_str(sub.value_of("SIDE").unwrap()).expect("Wrong track format");
let _export = sub.value_of("Z80_EXPORT").unwrap();
let mut dsk = ExtendedDsk::open(dsk_fname)
.unwrap_or_else(|_| panic!("Unable to open the file {}", dsk_fname));
let mut listing = Listing::new();
for file in sub.values_of("FILES").unwrap() {
// get the file
let mut f = File::open(file)?;
let mut content = Vec::new();
f.read_to_end(&mut content)?;
let next_position = dsk
.add_file_sequentially(head, track, sector, &content)
.unwrap_or_else(|_| panic!("Unable to add {}", file));
let base_label = Path::new(file)
.file_name()
.unwrap()
.to_str()
.unwrap()
.replace(".", "_");
listing.add(builder::equ(format!("{}_head", &base_label), head));
listing.add(builder::equ(format!("{}_track", &base_label), track));
listing.add(builder::equ(format!("{}_sector", &base_label), sector));
head = next_position.0;
track = next_position.1;
sector = next_position.2;
}
}
else if let Some(sub) = matches.subcommand_matches("add") {
// Add files in an Amsdos compatible disc
// Get the input dsk
let dsk = ExtendedDsk::open(dsk_fname)
.unwrap_or_else(|_| panic!("Unable to open the file {}", dsk_fname));
let mut manager = AmsdosManager::new_from_disc(dsk, 0);
// Get the common parameters
let is_system = sub.is_present("SYSTEM");
let is_read_only = sub.is_present("READ_ONLY");
// loop over all the files to add them
for fname in sub.values_of("INPUT_FILES").unwrap() {
let ams_file = match AmsdosFile::open_valid(fname) {
Ok(ams_file) => {
if !ams_file.amsdos_filename().unwrap().is_valid() {
panic!("Invalid amsdos filename ! {:?}", ams_file.amsdos_filename());
}
println!("{:?} added", ams_file.amsdos_filename());
ams_file
}
Err(e) => {
panic!("Unable to load {}: {:?}", fname, e);
}
};
manager
.add_file(&ams_file, is_system, is_read_only)
.unwrap();
}
// Save the dsk on disc
manager.dsk().save(dsk_fname)?;
}
else if let Some(sub) = matches.subcommand_matches("format") {
// Manage the formating of a disc
use cpclib_disc::cfg::DiscConfig;
// Retrieve the format description
let cfg = if let Some(desc_fname) = sub.value_of("FORMAT_FILE") {
cpclib_disc::cfg::DiscConfig::new(desc_fname)?
}
else if let Some(desc) = sub.value_of("FORMAT_NAME") {
match desc {
"data42" => DiscConfig::single_head_data42_format(),
"data" => DiscConfig::single_head_data_format(),
_ => unreachable!()
}
}
else {
unreachable!();
};
// Make the dsk based on the format
let dsk = cpclib_disc::builder::build_disc_from_cfg(&cfg);
dsk.save(dsk_fname)?;
}
else {
eprintln!("Missing command\n");
}
Ok(())
}