-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiskutils.cpp
More file actions
788 lines (708 loc) · 26.5 KB
/
Copy pathdiskutils.cpp
File metadata and controls
788 lines (708 loc) · 26.5 KB
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
#include "diskutils.hpp"
#include <cstdio>
#include <cerrno>
// Global variable definitions
unsigned char* D = nullptr;
unsigned int NBLOCKS = 0;
unsigned int NFREEBLOCKS = 0;
unsigned int RBN = 0;
// Core disk management
void joindisk() {
key_t key = ftok(".", 'D');
if (key == -1) {
perror("Error: ftok failed in joindisk");
exit(1);
}
int shmid = shmget(key, DISK_SIZE, 0666);
if (shmid < 0) {
perror("Error: VD not found. Did you run './diskmanager -create'?");
exit(1);
}
D = (unsigned char*)shmat(shmid, NULL, 0);
if (D == (unsigned char*)-1) {
perror("Error: shmat failed in joindisk");
exit(1);
}
memcpy(&NBLOCKS, D + (SUPERBLOCK * BLOCK_SIZE), 4);
memcpy(&NFREEBLOCKS, D + (SUPERBLOCK * BLOCK_SIZE) + 4, 4);
memcpy(&RBN, D + (SUPERBLOCK * BLOCK_SIZE) + 8, 4);
}
void leavedisk() {
if (D != nullptr) {
// Refresh and print free block count before leaving
memcpy(&NFREEBLOCKS, D + (SUPERBLOCK * BLOCK_SIZE) + 4, 4);
std::cout << "+++ Number of free blocks = " << NFREEBLOCKS << "\n";
if (shmdt(D) == -1) perror("Error: shmdt failed in leavedisk");
D = nullptr;
}
}
unsigned int getfreeblock() {
memcpy(&NFREEBLOCKS, D + (SUPERBLOCK * BLOCK_SIZE) + 4, 4);
if (NFREEBLOCKS == 0) {
std::cerr << "***Error: Disk is full.\n";
return 0;
}
unsigned int bitmap_offset = BITMAP_START * BLOCK_SIZE;
unsigned int block_num;
do {
block_num = rand() % NBLOCKS;
int byte_idx = block_num / 8;
int bit_idx = block_num % 8;
if ((D[bitmap_offset + byte_idx] & (1 << bit_idx)) == 0) {
D[bitmap_offset + byte_idx] |= (1 << bit_idx);
break;
}
} while (true);
NFREEBLOCKS--;
memcpy(D + (SUPERBLOCK * BLOCK_SIZE) + 4, &NFREEBLOCKS, 4);
memset(D + (block_num * BLOCK_SIZE), 0, BLOCK_SIZE);
return block_num;
}
void freeblock(unsigned int block_num) {
if (block_num == 0 || block_num >= NBLOCKS)
return;
unsigned int bitmap_offset = BITMAP_START * BLOCK_SIZE;
int byte_idx = block_num / 8;
int bit_idx = block_num % 8;
D[bitmap_offset + byte_idx] &= ~(1 << bit_idx);
memcpy(&NFREEBLOCKS, D + (SUPERBLOCK * BLOCK_SIZE) + 4, 4);
NFREEBLOCKS++;
memcpy(D + (SUPERBLOCK * BLOCK_SIZE) + 4, &NFREEBLOCKS, 4);
}
// FAT helpers
unsigned int get_next_fat_block(unsigned int current_block) {
unsigned int fat_offset = FAT_START * BLOCK_SIZE;
unsigned int next_block = 0;
memcpy(&next_block, D + fat_offset + (current_block * 4), 4);
return next_block;
}
void set_fat(unsigned int block_num, unsigned int next_block) {
unsigned int fat_offset = FAT_START * BLOCK_SIZE;
memcpy(D + fat_offset + (block_num * 4), &next_block, 4);
}
// Path helpers
std::vector<std::string> split_path(const std::string& path) {
std::vector<std::string> tokens;
std::stringstream ss(path);
std::string item;
while (std::getline(ss, item, '/'))
if (!item.empty())
tokens.push_back(item);
return tokens;
}
// Search a single directory chain for target_name. Returns true on success.
bool find_dir_entry(unsigned int dir_first_block, const std::string& target_name, metadata& out_meta)
{
unsigned int current_block = dir_first_block;
while (current_block != 0) {
unsigned int block_offset = current_block * BLOCK_SIZE;
for (int i = 0; i < 32; i++) {
metadata entry;
memcpy(&entry, D + block_offset + (i * sizeof(metadata)), sizeof(metadata));
if (entry.name[0] == '\0')
continue;
if (std::string(entry.name) == target_name) {
out_meta = entry;
return true;
}
}
current_block = get_next_fat_block(current_block);
}
return false;
}
// Resolve path → (out_block = first block of result, out_meta = its metadata).
// Returns false and prints an error if resolution fails.
bool resolve_path(const std::string& path, unsigned int cbn, const std::string& /*cwd*/, unsigned int& out_block, metadata& out_meta, bool silent)
{
// Strip trailing slash
std::string p = path;
if (p.size() > 1 && p.back() == '/')
p.pop_back();
unsigned int search_block = (p[0] == '/') ? RBN : cbn;
if (p == "/" || p.empty()) {
// Root itself
out_block = RBN;
metadata root;
memcpy(&root, D + RBN * BLOCK_SIZE, sizeof(metadata));
out_meta = root;
return true;
}
auto parts = split_path(p);
for (size_t i = 0; i < parts.size(); i++) {
metadata entry;
if (!find_dir_entry(search_block, parts[i], entry)) {
if (!silent) std::cout << "***Error: " << path << ": no such file or directory\n"; // (!silent)
return false;
}
if (i + 1 < parts.size() && entry.type != 'd') {
if (!silent) std::cout << "***Error: " << parts[i] << " is not a directory\n"; // (!silent)
return false;
}
out_meta = entry;
out_block = entry.firstblock;
search_block = entry.firstblock;
}
return true;
}
// Directory helpers
// Count entries already stored in a directory chain
static unsigned int count_dir_entries(unsigned int dir_first_block) {
metadata dot;
memcpy(&dot, D + dir_first_block * BLOCK_SIZE, sizeof(metadata));
return dot.size;
}
// Update the size field of the '.' entry in a directory
static void update_dir_size(unsigned int dir_first_block, unsigned int new_size) {
metadata dot;
memcpy(&dot, D + dir_first_block * BLOCK_SIZE, sizeof(metadata));
dot.size = new_size;
memcpy(D + dir_first_block * BLOCK_SIZE, &dot, sizeof(metadata));
}
// Append a metadata entry to a directory, allocating a new block if needed.
// Also increments the '.' size counter.
static bool append_dir_entry(unsigned int dir_first_block, const metadata& entry) {
unsigned int cur = dir_first_block;
unsigned int slot = 0; // absolute slot index across all chained blocks
while (true) {
unsigned int block_offset = cur * BLOCK_SIZE;
for (int i = 0; i < 32; i++) {
metadata e;
memcpy(&e, D + block_offset + (i * sizeof(metadata)), sizeof(metadata));
if (e.name[0] == '\0') {
// Empty slot – write here
memcpy(D + block_offset + (i * sizeof(metadata)), &entry, sizeof(metadata));
// Increment '.' size
unsigned int sz = count_dir_entries(dir_first_block);
update_dir_size(dir_first_block, sz + 1);
return true;
}
slot++;
}
unsigned int next = get_next_fat_block(cur);
if (next == 0) {
// Need a new block
unsigned int nb = getfreeblock();
if (nb == 0) return false;
set_fat(cur, nb);
// Write entry to slot 0 of new block
memcpy(D + nb * BLOCK_SIZE, &entry, sizeof(metadata));
unsigned int sz = count_dir_entries(dir_first_block);
update_dir_size(dir_first_block, sz + 1);
return true;
}
cur = next;
}
}
// Update an existing metadata entry (identified by name) in a directory chain.
static bool update_dir_entry(unsigned int dir_first_block, const std::string& target_name, const metadata& new_entry)
{
unsigned int cur = dir_first_block;
while (cur != 0) {
unsigned int block_offset = cur * BLOCK_SIZE;
for (int i = 0; i < 32; i++) {
metadata e;
memcpy(&e, D + block_offset + (i * sizeof(metadata)), sizeof(metadata));
if (std::string(e.name) == target_name) {
memcpy(D + block_offset + (i * sizeof(metadata)), &new_entry, sizeof(metadata));
return true;
}
}
cur = get_next_fat_block(cur);
}
return false;
}
// fs_mkdir
void fs_mkdir(const std::string& path, unsigned int cbn, const std::string& cwd) {
// Strip trailing slash
std::string p = path;
if (p.size() > 1 && p.back() == '/')
p.pop_back();
// Split into parent path and new dir name
std::string parent_path, new_name;
size_t last_slash = p.find_last_of('/');
if (last_slash == std::string::npos) {
parent_path = "";
new_name = p;
} else {
parent_path = p.substr(0, last_slash);
new_name = p.substr(last_slash + 1);
}
if (new_name.empty() || new_name == "." || new_name == "..") {
std::cout << "***Error: Invalid directory name '" << new_name << "'\n";
return;
}
if (new_name.size() > 22) {
std::cout << "***Error: Name too long (max 22 chars)\n";
return;
}
// Resolve parent directory
unsigned int parent_block;
metadata parent_meta;
if (parent_path.empty()) {
parent_block = cbn;
memcpy(&parent_meta, D + cbn * BLOCK_SIZE, sizeof(metadata));
} else {
if (!resolve_path(parent_path, cbn, cwd, parent_block, parent_meta))
return;
if (parent_meta.type != 'd') {
std::cout << "***Error: " << parent_path << " is not a directory\n";
return;
}
}
// Check that new_name doesn't already exist
metadata dummy;
if (find_dir_entry(parent_block, new_name, dummy)) {
std::cout << "***Error: '" << new_name << "' already exists\n";
return;
}
// Allocate a block for the new directory
unsigned int nb = getfreeblock();
if (nb == 0)
return;
// Initialise new directory with '.' and '..'
// '.' size starts at 2 (for . and ..)
metadata dot, dotdot;
dot.type = 'd';
memset(dot.name, 0, 23); strncpy(dot.name, ".", 22);
dot.size = 2;
dot.firstblock = nb;
dotdot.type = 'd';
memset(dotdot.name, 0, 23);
strncpy(dotdot.name, "..", 22);
dotdot.size = 0; // actual size lives in parent's '.' entry
dotdot.firstblock = parent_block;
memcpy(D + nb * BLOCK_SIZE, &dot, sizeof(metadata));
memcpy(D + nb * BLOCK_SIZE + sizeof(metadata), &dotdot, sizeof(metadata));
// Add entry to parent directory
metadata entry;
entry.type = 'd';
memset(entry.name, 0, 23); strncpy(entry.name, new_name.c_str(), 22);
entry.size = 2;
entry.firstblock = nb;
if (!append_dir_entry(parent_block, entry)) {
std::cout << "***Error: Could not add entry to parent directory\n";
freeblock(nb);
return;
}
// Update parent's '.' size so ls shows correct count
// (append_dir_entry already did this – just confirm)
}
// fs_ls (detailed listing)
void fs_ls(const std::string& path, unsigned int cbn, const std::string& cwd) {
unsigned int target_block;
metadata target_meta;
if (path.empty() || path == ".") {
target_block = cbn;
memcpy(&target_meta, D + cbn * BLOCK_SIZE, sizeof(metadata));
} else {
if (!resolve_path(path, cbn, cwd, target_block, target_meta))
return;
}
if (target_meta.type == 'f') {
std::cout << target_meta.firstblock << " "
<< target_meta.type << " "
<< target_meta.size << " "
<< path << "\n";
return;
}
std::vector<metadata> entries;
unsigned int cur = target_block;
while (cur != 0) {
unsigned int block_offset = cur * BLOCK_SIZE;
for (int i = 0; i < 32; i++) {
metadata e;
memcpy(&e, D + block_offset + (i * sizeof(metadata)), sizeof(metadata));
if (e.name[0] == '\0') continue;
entries.push_back(e);
}
cur = get_next_fat_block(cur);
}
unsigned int total = 0;
for (auto& e : entries){
if (std::string(e.name) == ".") {
total = e.size;
break;
}
}
std::cout << "Total " << total << " entries\n";
std::cout << "-----------------------------------------------------------------------\n";
std::cout << "TYPE NAME SIZE FIRST BLOCK\n";
std::cout << "-----------------------------------------------------------------------\n";
for (auto& e : entries) {
std::string display_name = std::string(e.name);
unsigned int display_size = e.size;
// If it's a directory, dynamically fetch its TRUE size from its own '.' entry
if (e.type == 'd') {
display_name += "/";
metadata true_dot;
memcpy(&true_dot, D + e.firstblock * BLOCK_SIZE, sizeof(metadata));
display_size = true_dot.size;
}
printf(" %c %-26s %-10u %u\n",
e.type,
display_name.c_str(),
display_size,
e.firstblock);
}
std::cout << "-----------------------------------------------------------------------\n";
}
// fs_dir (names-only listing of current directory)
void fs_dir(unsigned int cbn, const std::string& /*cwd*/) {
std::vector<std::string> names;
unsigned int cur = cbn;
while (cur != 0) {
unsigned int block_offset = cur * BLOCK_SIZE;
for (int i = 0; i < 32; i++) {
metadata e;
memcpy(&e, D + block_offset + (i * sizeof(metadata)), sizeof(metadata));
if (e.name[0] == '\0')
continue;
std::string n = std::string(e.name);
if (e.type == 'd')
n += "/";
names.push_back(n);
}
cur = get_next_fat_block(cur);
}
// Print 4 per row (matching sample output)
for (size_t i = 0; i < names.size(); i++) {
printf(" %-15s", names[i].c_str());
if ((i + 1) % 4 == 0)
std::cout << "\n";
}
if (names.size() % 4 != 0)
std::cout << "\n";
}
// File write helpers
// Write 'data' of 'data_len' bytes into VD starting from 'first_block'.
// Allocates additional blocks via FAT as needed.
// Returns false on disk-full error.
static bool write_file_data(unsigned int first_block, const unsigned char* data, unsigned int data_len)
{
unsigned int cur = first_block;
unsigned int written = 0;
while (written < data_len) {
unsigned int to_write = data_len - written;
if (to_write > BLOCK_SIZE)
to_write = BLOCK_SIZE;
memcpy(D + cur * BLOCK_SIZE, data + written, to_write);
written += to_write;
if (written < data_len) {
unsigned int nb = getfreeblock();
if (nb == 0) return false;
set_fat(cur, nb);
cur = nb;
}
}
return true;
}
// Free all blocks in a FAT chain (but NOT the first block itself – caller frees that if needed)
static void free_fat_chain(unsigned int first_block) {
unsigned int cur = first_block;
while (cur != 0) {
unsigned int next = get_next_fat_block(cur);
set_fat(cur, 0);
freeblock(cur);
cur = next;
}
}
// fs_cp
void fs_cp(const std::string& src, const std::string& dst, unsigned int cbn, const std::string& cwd)
{
bool src_is_hd = (!src.empty() && src[0] == '`');
bool dst_is_hd = (!dst.empty() && dst[0] == '`');
std::string src_name = src_is_hd ? src.substr(1) : src;
std::string dst_name = dst_is_hd ? dst.substr(1) : dst;
// HD → VD
if (src_is_hd && !dst_is_hd) {
FILE* f = fopen(src_name.c_str(), "rb");
if (!f) {
std::cout << "***Error: Unable to read input file " << src_name << "\n";
return;
}
fseek(f, 0, SEEK_END);
long file_size = ftell(f);
rewind(f);
unsigned char* buf = new unsigned char[file_size];
size_t rd = fread(buf, 1, file_size, f); (void)rd;
fclose(f);
// Determine the base filename to use on VD
std::string base_name = src_name;
size_t sl = src_name.find_last_of('/');
if (sl != std::string::npos) base_name = src_name.substr(sl + 1);
// Resolve destination in VD
unsigned int dst_block;
metadata dst_meta;
bool dst_exists = resolve_path(dst_name, cbn, cwd, dst_block, dst_meta, true);
unsigned int dst_dir_block;
std::string final_name;
if (dst_exists && dst_meta.type == 'd') {
// Destination is a directory – keep source base name
dst_dir_block = dst_block;
final_name = base_name;
// Strip trailing slash from path for ls output
std::string canon_dst = dst_name;
if (!canon_dst.empty() && canon_dst.back() == '/')
canon_dst.pop_back();
} else if (!dst_exists) {
// Could be a new file name;
// resolve its parent directory
std::string parent_path, file_part;
size_t last_sl = dst_name.find_last_of('/');
if (last_sl == std::string::npos) {
parent_path = "";
file_part = dst_name;
} else {
parent_path = dst_name.substr(0, last_sl);
file_part = dst_name.substr(last_sl + 1);
}
if (parent_path.empty()) {
dst_dir_block = cbn;
} else {
unsigned int pb; metadata pm;
if (!resolve_path(parent_path, cbn, cwd, pb, pm)) {
delete[] buf;
return;
}
if (pm.type != 'd') {
std::cout << "***Error: " << parent_path << " is not a directory\n";
delete[] buf;
return;
}
dst_dir_block = pb;
}
final_name = file_part;
} else {
// dst_exists and is a file – overwrite
dst_dir_block = 0; // will find via parent
final_name = "";
// Overwrite path: free old chain, update metadata
free_fat_chain(dst_meta.firstblock);
unsigned int nb = getfreeblock();
if (nb == 0) {
delete[] buf;
return;
}
if (!write_file_data(nb, buf, (unsigned int)file_size)) {
delete[] buf;
return;
}
dst_meta.size = (unsigned int)file_size;
dst_meta.firstblock = nb;
// Find parent directory to update metadata
std::string p2 = dst_name;
if (p2.back() == '/')
p2.pop_back();
size_t ls2 = p2.find_last_of('/');
std::string ppath = (ls2 == std::string::npos) ? "" : p2.substr(0, ls2);
std::string fname = (ls2 == std::string::npos) ? p2 : p2.substr(ls2 + 1);
unsigned int par_block = cbn;
if (!ppath.empty()) {
unsigned int pb;
metadata pm;
if (resolve_path(ppath, cbn, cwd, pb, pm))
par_block = pb;
}
update_dir_entry(par_block, fname, dst_meta);
delete[] buf;
return;
}
if (final_name.size() > 22) {
std::cout << "***Error: Name too long (max 22 chars)\n";
delete[] buf; return;
}
// Check if file already exists in dst_dir_block (overwrite)
metadata existing;
if (find_dir_entry(dst_dir_block, final_name, existing) && existing.type == 'f') {
free_fat_chain(existing.firstblock);
unsigned int nb = getfreeblock();
if (nb == 0) {
delete[] buf;
return;
}
if (!write_file_data(nb, buf, (unsigned int)file_size)) {
delete[] buf;
return;
}
existing.size = (unsigned int)file_size;
existing.firstblock = nb;
update_dir_entry(dst_dir_block, final_name, existing);
} else {
unsigned int nb = getfreeblock();
if (nb == 0) {
delete[] buf;
return;
}
if (!write_file_data(nb, buf, (unsigned int)file_size)) {
delete[] buf;
return;
}
metadata new_entry;
new_entry.type = 'f';
memset(new_entry.name, 0, 23);
strncpy(new_entry.name, final_name.c_str(), 22);
new_entry.size = (unsigned int)file_size;
new_entry.firstblock = nb;
append_dir_entry(dst_dir_block, new_entry);
}
delete[] buf;
return;
}
// VD → HD
if (!src_is_hd && dst_is_hd) {
unsigned int src_block;
metadata src_meta;
if (!resolve_path(src_name, cbn, cwd, src_block, src_meta))
return;
if (src_meta.type != 'f') {
std::cout << "***Error: " << src_name << " is not a file\n";
return;
}
FILE* f = fopen(dst_name.c_str(), "wb");
if (!f) {
std::cout << "***Error: Unable to create output file " << dst_name << "\n";
return;
}
unsigned int bytes_left = src_meta.size;
unsigned int cur = src_meta.firstblock;
while (cur != 0 && bytes_left > 0) {
unsigned int to_write = (bytes_left < BLOCK_SIZE) ? bytes_left : BLOCK_SIZE;
fwrite(D + cur * BLOCK_SIZE, 1, to_write, f);
bytes_left -= to_write;
cur = get_next_fat_block(cur);
}
fclose(f);
return;
}
// VD → VD
if (!src_is_hd && !dst_is_hd) {
unsigned int src_block; metadata src_meta;
if (!resolve_path(src_name, cbn, cwd, src_block, src_meta))
return;
if (src_meta.type != 'f') {
std::cout << "***Error: " << src_name << " is not a file (VD→VD cp requires file source)\n";
return;
}
// Read source file into buffer
unsigned int file_size = src_meta.size;
unsigned char* buf = new unsigned char[file_size + 1];
unsigned int bytes_left = file_size;
unsigned int cur = src_meta.firstblock;
unsigned int offset = 0;
while (cur != 0 && bytes_left > 0) {
unsigned int to_read = (bytes_left < BLOCK_SIZE) ? bytes_left : BLOCK_SIZE;
memcpy(buf + offset, D + cur * BLOCK_SIZE, to_read);
offset += to_read;
bytes_left -= to_read;
cur = get_next_fat_block(cur);
}
// Determine source base name
std::string base_name = src_name;
size_t sl = src_name.find_last_of('/');
if (sl != std::string::npos) base_name = src_name.substr(sl + 1);
// Resolve destination
unsigned int dst_block; metadata dst_meta;
bool dst_exists = resolve_path(dst_name, cbn, cwd, dst_block, dst_meta, true);
unsigned int dst_dir_block;
std::string final_name;
if (dst_exists && dst_meta.type == 'd') {
dst_dir_block = dst_block;
final_name = base_name;
} else if (!dst_exists) {
std::string parent_path, file_part;
size_t ls2 = dst_name.find_last_of('/');
if (ls2 == std::string::npos) {
parent_path = "";
file_part = dst_name;
} else {
parent_path = dst_name.substr(0, ls2);
file_part = dst_name.substr(ls2 + 1);
}
if (parent_path.empty()) {
dst_dir_block = cbn;
} else {
unsigned int pb; metadata pm;
if (!resolve_path(parent_path, cbn, cwd, pb, pm)) {
delete[] buf;
return;
}
dst_dir_block = pb;
}
final_name = file_part;
} else {
// Overwrite existing file
free_fat_chain(dst_meta.firstblock);
unsigned int nb = getfreeblock();
if (nb == 0) { delete[] buf; return; }
write_file_data(nb, buf, file_size);
dst_meta.size = file_size;
dst_meta.firstblock = nb;
std::string p2 = dst_name;
size_t ls2 = p2.find_last_of('/');
std::string ppath = (ls2 == std::string::npos) ? "" : p2.substr(0, ls2);
std::string fname = (ls2 == std::string::npos) ? p2 : p2.substr(ls2 + 1);
unsigned int par_block = cbn;
if (!ppath.empty()) {
unsigned int pb; metadata pm;
if (resolve_path(ppath, cbn, cwd, pb, pm))
par_block = pb;
}
update_dir_entry(par_block, fname, dst_meta);
delete[] buf;
return;
}
if (final_name.size() > 22) {
std::cout << "***Error: Name too long (max 22 chars)\n";
delete[] buf; return;
}
metadata existing;
if (find_dir_entry(dst_dir_block, final_name, existing) && existing.type == 'f') {
free_fat_chain(existing.firstblock);
unsigned int nb = getfreeblock();
if (nb == 0) {
delete[] buf;
return;
}
write_file_data(nb, buf, file_size);
existing.size = file_size;
existing.firstblock = nb;
update_dir_entry(dst_dir_block, final_name, existing);
} else {
unsigned int nb = getfreeblock();
if (nb == 0) {
delete[] buf;
return;
}
write_file_data(nb, buf, file_size);
metadata new_entry;
new_entry.type = 'f';
memset(new_entry.name, 0, 23);
strncpy(new_entry.name, final_name.c_str(), 22);
new_entry.size = file_size;
new_entry.firstblock = nb;
append_dir_entry(dst_dir_block, new_entry);
}
delete[] buf;
return;
}
std::cout << "***Error: HD→HD copy is not supported\n";
}
// fs_prn
void fs_prn(const std::string& path, unsigned int cbn, const std::string& cwd) {
unsigned int target_block; metadata target_meta;
if (!resolve_path(path, cbn, cwd, target_block, target_meta))
return;
if (target_meta.type != 'f') {
std::cout << "***Error: " << path << " is a directory, not a file\n";
return;
}
unsigned int bytes_left = target_meta.size;
unsigned int cur = target_meta.firstblock;
while (cur != 0 && bytes_left > 0) {
unsigned int to_read = (bytes_left < BLOCK_SIZE) ? bytes_left : BLOCK_SIZE;
fwrite(D + cur * BLOCK_SIZE, 1, to_read, stdout);
bytes_left -= to_read;
cur = get_next_fat_block(cur);
}
std::cout << "\n";
}