This repository has been archived by the owner on Aug 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathVolumeDirectory.cpp
179 lines (124 loc) · 3.83 KB
/
VolumeDirectory.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
#include <cstring>
#include <memory>
#include "Bitmap.h"
#include "BlockDevice.h"
#include "Buffer.h"
#include "Endian.h"
#include "Entry.h"
#include "Exception.h"
using namespace ProFUSE;
using namespace LittleEndian;
#pragma mark VolumeDirectory
VolumeDirectory *VolumeDirectory::Create(const char *name, BlockDevice *device)
{
return new VolumeDirectory(name, device);
}
VolumeDirectory *VolumeDirectory::Create(BlockDevice *device)
{
uint8_t block[512];
// always block 2.
device->read(2, block);
return new VolumeDirectory(device, block);
}
VolumeDirectory::VolumeDirectory(const char *name, BlockDevice *device) :
Directory(VolumeHeader, name)
{
_totalBlocks = device->blocks();
_bitmapPointer = 6;
_entryBlocks.push_back(2);
_entryBlocks.push_back(3);
_entryBlocks.push_back(4);
_entryBlocks.push_back(5);
std::auto_ptr<Bitmap> bitmap(new Bitmap(_totalBlocks));
Buffer buffer(512);
// 2 bootcode blocks
for (unsigned i = 0; i < 2; ++i)
{
bitmap->allocBlock(i);
device->zeroBlock(i);
}
//4 volume header blocks.
for (unsigned i = 2; i < 6; ++i)
{
bitmap->allocBlock(i);
buffer.clear();
// prev block, next block
buffer.push16le(i == 2 ? 0 : i - 1);
buffer.push16le(i == 5 ? 0 : i + 1);
if (i == 2)
{
// create the volume header.
// all ivars must be set.
write(&buffer);
}
buffer.resize(512);
device->write(i, buffer.buffer());
}
// allocate blocks for the bitmap itself
unsigned bb = bitmap->bitmapBlocks();
for (unsigned i = 0; i < bb; ++i)
bitmap->allocBlock(_bitmapPointer + i);
// now write the bitmap...
const uint8_t *bm = (const uint8_t *)bitmap->bitmap();
for (unsigned i = 0; i < bb; ++i)
{
device->write(_bitmapPointer + i, 512 * i + bm);
}
_device = device;
_bitmap = bitmap.release();
}
VolumeDirectory::VolumeDirectory(BlockDevice *device, const void *bp) :
Directory(bp),
_modification(0,0)
{
#undef __METHOD__
#define __METHOD__ "VolumeDirectory::VolumeDirectory"
// + 4 to skip over the block poitners.
std::auto_ptr<Bitmap> bitmap;
const void *vp = 4 + (const uint8_t *)bp;
if (storageType() != VolumeHeader)
throw ProDOSException(__METHOD__ ": Invalid storage type.", 0x4b);
_modification = DateTime(Read16(vp, 0x12), Read16(vp, 0x14));
_bitmapPointer = Read16(vp, 0x23);
_totalBlocks = Read16(vp, 0x25);
// verify totalBlocks <= device->blocks() ?
if (_bitmapPointer >= _totalBlocks)
throw ProDOSException(__METHOD__ ": Invalid bitmap pointer.", 0x5a);
// bitmap pointer...
bitmap.reset(new Bitmap(device, _bitmapPointer, _totalBlocks));
// parse the directory header....
_bitmap = bitmap.release();
}
VolumeDirectory::~VolumeDirectory()
{
if (_device)
{
_device->sync();
delete _device;
}
delete _bitmap;
}
void VolumeDirectory::write(Buffer *out)
{
out->push8((VolumeHeader << 4 ) | nameLength());
out->pushBytes(namei(), 15);
// reserved. SOS uses 0x75 for the first byte [?]
out->push8(0);
out->push8(0);
// last mod
out->push16le(_modification.date());
out->push16le(_modification.time());
// filename case bits
out->push16le(caseFlag());
// creation
out->push16le(creation().date());
out->push16le(creation().time());
out->push8(version());
out->push8(minVersion());
out->push8(access());
out->push8(entryLength());
out->push8(entriesPerBlock());
out->push16le(fileCount());
out->push16le(_bitmapPointer);
out->push16le(_totalBlocks);
}