-
-
Notifications
You must be signed in to change notification settings - Fork 148
/
program_mount.cpp
438 lines (395 loc) · 14.5 KB
/
program_mount.cpp
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* Copyright (C) 2002-2021 The DOSBox Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "program_mount_common.h"
#include "program_mount.h"
#include "dosbox.h"
#include "std_filesystem.h"
#include <system_error>
#include "bios_disk.h"
#include "control.h"
#include "drives.h"
#include "fs_utils.h"
#include "shell.h"
#include "string_utils.h"
#include "support.h"
#include "../ints/int10.h"
void MOUNT::Move_Z(char new_z)
{
const char new_drive_z = toupper(new_z);
if (new_drive_z < 'A' || new_drive_z > 'Z') {
WriteOut(MSG_Get("PROGRAM_MOUNT_DRIVEID_ERROR"), new_drive_z);
return;
}
const uint8_t new_idx = drive_index(new_drive_z);
if (Drives[new_idx]) {
WriteOut(MSG_Get("PROGRAM_MOUNT_MOVE_Z_ERROR_1"), new_drive_z);
return;
}
if (new_idx < DOS_DRIVES - 1) {
ZDRIVE_NUM = new_idx;
/* remap drives */
Drives[new_idx] = Drives[25];
Drives[25] = 0;
if (!first_shell) return; //Should not be possible
/* Update environment */
std::string line = "";
std::string tempenv = {new_drive_z, ':', '\\'};
if (first_shell->GetEnvStr("PATH",line)) {
std::string::size_type idx = line.find('=');
std::string value = line.substr(idx +1 , std::string::npos);
while ( (idx = value.find("Z:\\")) != std::string::npos ||
(idx = value.find("z:\\")) != std::string::npos )
value.replace(idx,3,tempenv);
line = std::move(value);
}
if (!line.size()) line = tempenv;
first_shell->SetEnv("PATH",line.c_str());
tempenv += "COMMAND.COM";
first_shell->SetEnv("COMSPEC",tempenv.c_str());
/* Update batch file if running from Z: (very likely: autoexec) */
if (first_shell->bf) {
std::string &name = first_shell->bf->filename;
if (starts_with("Z:", name))
name[0] = new_drive_z;
}
/* Change the active drive */
if (DOS_GetDefaultDrive() == 25)
DOS_SetDrive(new_idx);
}
}
void MOUNT::ListMounts()
{
const std::string header_drive = MSG_Get("PROGRAM_MOUNT_STATUS_DRIVE");
const std::string header_type = MSG_Get("PROGRAM_MOUNT_STATUS_TYPE");
const std::string header_label = MSG_Get("PROGRAM_MOUNT_STATUS_LABEL");
const int term_width = real_readw(BIOSMEM_SEG, BIOSMEM_NB_COLS);
const auto width_1 = static_cast<int>(header_drive.size());
const auto width_3 = std::max(11, static_cast<int>(header_label.size()));
const auto width_2 = term_width - 3 - width_1 - width_3;
auto print_row = [&](const std::string &txt_1,
const std::string &txt_2,
const std::string &txt_3) {
WriteOut("%-*s %-*s %-*s\n",
width_1, txt_1.c_str(),
width_2, txt_2.c_str(),
width_3, txt_3.c_str());
};
WriteOut(MSG_Get("PROGRAM_MOUNT_STATUS_1"));
print_row(header_drive, header_type, header_label);
for (int i = 0; i < term_width; i++)
WriteOut_NoParsing("-");
for (uint8_t d = 0; d < DOS_DRIVES; d++) {
if (Drives[d]) {
print_row(std::string{drive_letter(d)},
Drives[d]->GetInfo(),
To_Label(Drives[d]->GetLabel()));
}
}
}
void MOUNT::Run(void) {
DOS_Drive * newdrive;char drive;
std::string label;
std::string umount;
std::string newz;
//Hack To allow long commandlines
ChangeToLongCmd();
/* Parse the command line */
/* if the command line is empty show current mounts */
if (!cmd->GetCount()) {
ListMounts();
return;
}
/* In secure mode don't allow people to change mount points.
* Neither mount nor unmount */
if (control->SecureMode()) {
WriteOut(MSG_Get("PROGRAM_CONFIG_SECURE_DISALLOW"));
return;
}
const bool path_relative_to_last_config = cmd->FindExist("-pr", true);
/* Check for unmounting */
if (cmd->FindString("-u",umount,false)) {
WriteOut(UnmountHelper(umount[0]), toupper(umount[0]));
return;
}
/* Check for moving Z: */
/* Only allowing moving it once. It is merely a convenience added for the wine team */
if (ZDRIVE_NUM == 25 && cmd->FindString("-z", newz,false)) {
Move_Z(newz[0]);
return;
}
if (cmd->FindExist("-cd", false)) {
WriteOut(MSG_Get("PROGRAM_MOUNT_NO_OPTION"), "-cd");
return;
}
std::string type="dir";
cmd->FindString("-t",type,true);
bool iscdrom = (type =="cdrom"); //Used for mscdex bug cdrom label name emulation
if (type=="floppy" || type=="dir" || type=="cdrom" || type =="overlay") {
Bit16u sizes[4] ={0};
Bit8u mediaid;
std::string str_size = "";
if (type=="floppy") {
str_size="512,1,2880,2880";/* All space free */
mediaid=0xF0; /* Floppy 1.44 media */
} else if (type=="dir" || type == "overlay") {
// 512*32*32765==~500MB total size
// 512*32*16000==~250MB total free size
str_size="512,32,32765,16000";
mediaid=0xF8; /* Hard Disk */
} else if (type=="cdrom") {
str_size="2048,1,65535,0";
mediaid=0xF8; /* Hard Disk */
} else {
WriteOut(MSG_Get("PROGAM_MOUNT_ILL_TYPE"),type.c_str());
return;
}
/* Parse the free space in mb's (kb's for floppies) */
std::string mb_size;
if (cmd->FindString("-freesize",mb_size,true)) {
char teststr[1024];
Bit16u freesize = static_cast<Bit16u>(atoi(mb_size.c_str()));
if (type=="floppy") {
// freesize in kb
sprintf(teststr,"512,1,2880,%d",freesize*1024/(512*1));
} else {
Bit32u total_size_cyl=32765;
Bit32u free_size_cyl=(Bit32u)freesize*1024*1024/(512*32);
if (free_size_cyl>65534) free_size_cyl=65534;
if (total_size_cyl<free_size_cyl) total_size_cyl=free_size_cyl+10;
if (total_size_cyl>65534) total_size_cyl=65534;
sprintf(teststr,"512,32,%d,%d",total_size_cyl,free_size_cyl);
}
str_size=teststr;
}
cmd->FindString("-size",str_size,true);
char number[21] = { 0 };const char * scan = str_size.c_str();
Bitu index = 0;Bitu count = 0;
/* Parse the str_size string */
while (*scan && index < 20 && count < 4) {
if (*scan==',') {
number[index] = 0;
sizes[count++] = atoi(number);
index = 0;
} else number[index++] = *scan;
scan++;
}
if (count < 4) {
number[index] = 0; //always goes correct as index is max 20 at this point.
sizes[count] = atoi(number);
}
// get the drive letter
cmd->FindCommand(1,temp_line);
if ((temp_line.size() > 2) || ((temp_line.size() > 1) && (temp_line[1]!=':'))) goto showusage;
const int i_drive = toupper(temp_line[0]);
if (i_drive < 'A' || i_drive > 'Z') {
goto showusage;
}
drive = int_to_char(i_drive);
if (type == "overlay") {
//Ensure that the base drive exists:
if (!Drives[drive_index(drive)]) {
WriteOut(MSG_Get("PROGRAM_MOUNT_OVERLAY_NO_BASE"));
return;
}
} else if (Drives[drive_index(drive)]) {
WriteOut(MSG_Get("PROGRAM_MOUNT_ALREADY_MOUNTED"),
drive,
Drives[drive_index(drive)]->GetInfo());
return;
}
if (!cmd->FindCommand(2, temp_line) || temp_line.empty()) {
goto showusage;
}
// Error-code to be used in throw-free std::filesystem calls
std::error_code ec;
// Extra handling for MinGW GCC build handles the following:
// - mount c ./ -> should behave like mount c .
// - mount c / -> should behave like mount c c:/
// - mount c c: -> should behave like mount c c:/
//
// Note: MSVC and Clang do not need this extra babysitting.
// Remove this block when MinGW's GCC std::filesystem is fixed.
//
#if defined(WIN32) && defined(__GNUC__) && !defined(__clang__)
const auto saved_line = temp_line;
// GCC on Windows has problems with traling slashes, so strip them
const bool has_drive_spec = (temp_line.size() > 1 && temp_line[1] == ':');
const size_t strip_slashes_up_to = has_drive_spec ? 3 : 1;
while (temp_line.size() > strip_slashes_up_to &&
(temp_line.back() == '/' || temp_line.back() == '\\'))
temp_line.pop_back();
// GCC on Windows doesn't upgrade drive-specs (d:) to absolute (d:\)
if (temp_line.size() == 2 && temp_line[1] == ':')
temp_line += '\\';
// GCC on Windows doesn't map absolute paths (/) to the current drive
const bool is_absolute = (temp_line.size() &&
(temp_line.front() == '/' ||
temp_line.front() == '\\'));
if (is_absolute) {
// Try to prepend the current-working drive
auto cp = std_fs::current_path(ec);
if (cp.has_root_name()) {
temp_line = cp.root_name().string() + temp_line;
} else {
WriteOut(MSG_Get("PROGRAM_MOUNT_NO_DRIVE_PREFIX"),
saved_line.c_str(), saved_line.c_str());
return;
}
}
// if we made it here, the path is relative or has a drive
// prefix, which is sufficient for GCC to handle properly
assert(temp_line.size());
#endif
// Construct a std::filesystem path from the string
std_fs::path host_path = temp_line;
// Check if the path is relative to the last config file
if (path_relative_to_last_config && control->configfiles.size()) {
std_fs::path config_path = control->configfiles.back();
config_path = config_path.parent_path();
host_path = std::string("./") + host_path.string(); // make relative
host_path = std_fs::canonical(config_path / host_path, ec);
LOG_INFO("MOUNT: Will try mounting '%s' relative to last config path '%s'",
host_path.string().c_str(), config_path.string().c_str());
}
// If the path doesn't exist, try it in native-form
if (!std_fs::is_directory(host_path, ec)) {
const std_fs::path native_path = to_native_path(host_path.string());
if (std_fs::is_directory(native_path, ec)) {
LOG_INFO("MOUNT: Will try mounting host path '%s' in native form '%s'",
host_path.string().c_str(), native_path.string().c_str());
host_path = native_path;
} else {
// If the native_path doesn't exist, try
// expanding the home directory
std::string home_resolve = temp_line;
Cross::ResolveHomedir(home_resolve);
std_fs::path home_path = home_resolve;
if (std_fs::is_directory(home_path, ec)) {
LOG_INFO("MOUNT: Will try mounting host path '%s' a home directory '%s'",
host_path.string().c_str(), home_path.string().c_str());
host_path = home_path;
}
}
}
if (!std_fs::exists(host_path, ec)) {
WriteOut(MSG_Get("PROGRAM_MOUNT_ERROR_1"),temp_line.c_str());
return;
}
if (!std_fs::is_directory(host_path, ec)) {
WriteOut(MSG_Get("PROGRAM_MOUNT_ERROR_2"), temp_line.c_str());
return;
}
Bit8u bit8size = (Bit8u)sizes[1];
if (type == "cdrom") {
// Following options were relevant only for physical CD-ROM support:
for (auto opt : {"-usecd", "-noioctl", "-ioctl", "-ioctl_dx", "-ioctl_mci", "-ioctl_dio"}) {
if (cmd->FindExist(opt, false))
WriteOut(MSG_Get("MSCDEX_WARNING_NO_OPTION"), opt);
}
int error = 0;
newdrive = new cdromDrive(drive, host_path.string().c_str(), sizes[0], bit8size,
sizes[2], 0, mediaid, error);
// Check Mscdex, if it worked out...
switch (error) {
case 0 : WriteOut(MSG_Get("MSCDEX_SUCCESS")); break;
case 1 : WriteOut(MSG_Get("MSCDEX_ERROR_MULTIPLE_CDROMS")); break;
case 2 : WriteOut(MSG_Get("MSCDEX_ERROR_NOT_SUPPORTED")); break;
case 3 : WriteOut(MSG_Get("MSCDEX_ERROR_PATH")); break;
case 4 : WriteOut(MSG_Get("MSCDEX_TOO_MANY_DRIVES")); break;
case 5 : WriteOut(MSG_Get("MSCDEX_LIMITED_SUPPORT")); break;
default : WriteOut(MSG_Get("MSCDEX_UNKNOWN_ERROR")); break;
};
if (error && error!=5) {
delete newdrive;
return;
}
} else {
std_fs::path root_path = {};
if (is_path_a_root_path(host_path, root_path))
WriteOut(MSG_Get("PROGRAM_MOUNT_ROOT_PATH_WARNING"), host_path.string().c_str(),
root_path.string().c_str());
if (type == "overlay") {
localDrive *ldp = dynamic_cast<localDrive *>(
Drives[drive_index(drive)]);
cdromDrive *cdp = dynamic_cast<cdromDrive *>(
Drives[drive_index(drive)]);
if (!ldp || cdp) {
WriteOut(MSG_Get("PROGRAM_MOUNT_OVERLAY_INCOMPAT_BASE"));
return;
}
std::string base = ldp->GetBasedir();
Bit8u o_error = 0;
newdrive = new Overlay_Drive(base.c_str(), host_path.string().c_str(), sizes[0],
bit8size, sizes[2], sizes[3], mediaid, o_error);
// Erase old drive on success
if (o_error) {
if (o_error == 1) WriteOut("No mixing of relative and absolute paths. Overlay failed.");
else if (o_error == 2) WriteOut("overlay directory can not be the same as underlying file system.");
else WriteOut("Something went wrong");
delete newdrive;
return;
}
//Copy current directory if not marked as deleted.
if (newdrive->TestDir(ldp->curdir)) {
safe_strcpy(newdrive->curdir,
ldp->curdir);
}
delete Drives[drive_index(drive)];
Drives[drive_index(drive)] = nullptr;
} else {
newdrive = new localDrive(host_path.string().c_str(), sizes[0], bit8size,
sizes[2], sizes[3], mediaid);
}
}
} else {
WriteOut(MSG_Get("PROGRAM_MOUNT_ILL_TYPE"),type.c_str());
return;
}
Drives[drive_index(drive)] = newdrive;
/* Set the correct media byte in the table */
mem_writeb(Real2Phys(dos.tables.mediaid) + (drive_index(drive)) * 9,
newdrive->GetMediaByte());
if (type != "overlay")
WriteOut(MSG_Get("PROGRAM_MOUNT_STATUS_2"), drive,
newdrive->GetInfo());
else
WriteOut(MSG_Get("PROGRAM_MOUNT_OVERLAY_STATUS"),
temp_line.c_str(), drive);
/* check if volume label is given and don't allow it to updated in the future */
if (cmd->FindString("-label",label,true)) newdrive->dirCache.SetLabel(label.c_str(),iscdrom,false);
/* For hard drives set the label to DRIVELETTER_Drive.
* For floppy drives set the label to DRIVELETTER_Floppy.
* This way every drive except cdroms should get a label.*/
else if (type == "dir" || type == "overlay") {
label = drive; label += "_DRIVE";
newdrive->dirCache.SetLabel(label.c_str(),iscdrom,false);
} else if (type == "floppy") {
label = drive; label += "_FLOPPY";
newdrive->dirCache.SetLabel(label.c_str(),iscdrom,true);
}
if (type == "floppy") incrementFDD();
return;
showusage:
WriteOut(MSG_Get("SHELL_CMD_MOUNT_HELP_LONG"));
return;
}
void MOUNT_ProgramStart(Program **make) {
*make=new MOUNT;
}