Serialization support for cow images
dev/disk_image.cc: dev/disk_image.hh: Serialization of cow images --HG-- extra : convert_revision : 144179b8cad00b32df8c448a8556a465e24737aa
This commit is contained in:
parent
fd232ecb6a
commit
1044b29a1d
2 changed files with 34 additions and 9 deletions
|
@ -190,7 +190,7 @@ CowDiskImage::CowDiskImage(const string &name, DiskImage *kid, int hash_size,
|
||||||
const string &file, bool read_only)
|
const string &file, bool read_only)
|
||||||
: DiskImage(name), filename(file), child(kid), table(NULL)
|
: DiskImage(name), filename(file), child(kid), table(NULL)
|
||||||
{
|
{
|
||||||
if (!open()) {
|
if (!open(filename)) {
|
||||||
assert(!read_only && "why have a non-existent read only file?");
|
assert(!read_only && "why have a non-existent read only file?");
|
||||||
init(hash_size);
|
init(hash_size);
|
||||||
}
|
}
|
||||||
|
@ -230,20 +230,20 @@ SafeRead(ifstream &stream, T &data)
|
||||||
{ SafeRead(stream, &data, sizeof(data)); }
|
{ SafeRead(stream, &data, sizeof(data)); }
|
||||||
|
|
||||||
bool
|
bool
|
||||||
CowDiskImage::open()
|
CowDiskImage::open(const string &file)
|
||||||
{
|
{
|
||||||
ifstream stream(filename.c_str());
|
ifstream stream(file.c_str());
|
||||||
if (!stream.is_open())
|
if (!stream.is_open())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (stream.fail() || stream.bad())
|
if (stream.fail() || stream.bad())
|
||||||
panic("Error opening %s", filename);
|
panic("Error opening %s", file);
|
||||||
|
|
||||||
uint64_t magic;
|
uint64_t magic;
|
||||||
SafeRead(stream, magic);
|
SafeRead(stream, magic);
|
||||||
|
|
||||||
if (memcmp(&magic, "COWDISK!", sizeof(magic)) != 0)
|
if (memcmp(&magic, "COWDISK!", sizeof(magic)) != 0)
|
||||||
panic("Could not open %s: Invalid magic", filename);
|
panic("Could not open %s: Invalid magic", file);
|
||||||
|
|
||||||
uint32_t major, minor;
|
uint32_t major, minor;
|
||||||
SafeRead(stream, major);
|
SafeRead(stream, major);
|
||||||
|
@ -251,7 +251,7 @@ CowDiskImage::open()
|
||||||
|
|
||||||
if (major != VersionMajor && minor != VersionMinor)
|
if (major != VersionMajor && minor != VersionMinor)
|
||||||
panic("Could not open %s: invalid version %d.%d != %d.%d",
|
panic("Could not open %s: invalid version %d.%d != %d.%d",
|
||||||
filename, major, minor, VersionMajor, VersionMinor);
|
file, major, minor, VersionMajor, VersionMinor);
|
||||||
|
|
||||||
uint64_t sector_count;
|
uint64_t sector_count;
|
||||||
SafeRead(stream, sector_count);
|
SafeRead(stream, sector_count);
|
||||||
|
@ -304,13 +304,19 @@ SafeWrite(ofstream &stream, const T &data)
|
||||||
|
|
||||||
void
|
void
|
||||||
CowDiskImage::save()
|
CowDiskImage::save()
|
||||||
|
{
|
||||||
|
save(filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
CowDiskImage::save(const string &file)
|
||||||
{
|
{
|
||||||
if (!initialized)
|
if (!initialized)
|
||||||
panic("RawDiskImage not initialized");
|
panic("RawDiskImage not initialized");
|
||||||
|
|
||||||
ofstream stream(filename.c_str());
|
ofstream stream(file.c_str());
|
||||||
if (!stream.is_open() || stream.fail() || stream.bad())
|
if (!stream.is_open() || stream.fail() || stream.bad())
|
||||||
panic("Error opening %s", filename);
|
panic("Error opening %s", file);
|
||||||
|
|
||||||
uint64_t magic;
|
uint64_t magic;
|
||||||
memcpy(&magic, "COWDISK!", sizeof(magic));
|
memcpy(&magic, "COWDISK!", sizeof(magic));
|
||||||
|
@ -396,6 +402,22 @@ CowDiskImage::write(const uint8_t *data, off_t offset)
|
||||||
return SectorSize;
|
return SectorSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
CowDiskImage::serialize(ostream &os)
|
||||||
|
{
|
||||||
|
string cowFilename = serializeFilename + ".cow";
|
||||||
|
SERIALIZE_SCALAR(cowFilename);
|
||||||
|
save(cowFilename);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
CowDiskImage::unserialize(Checkpoint *cp, const string §ion)
|
||||||
|
{
|
||||||
|
string cowFilename;
|
||||||
|
UNSERIALIZE_SCALAR(cowFilename);
|
||||||
|
open(cowFilename);
|
||||||
|
}
|
||||||
|
|
||||||
BEGIN_DECLARE_SIM_OBJECT_PARAMS(CowDiskImage)
|
BEGIN_DECLARE_SIM_OBJECT_PARAMS(CowDiskImage)
|
||||||
|
|
||||||
SimObjectParam<DiskImage *> child;
|
SimObjectParam<DiskImage *> child;
|
||||||
|
|
|
@ -117,9 +117,12 @@ class CowDiskImage : public DiskImage
|
||||||
~CowDiskImage();
|
~CowDiskImage();
|
||||||
|
|
||||||
void init(int hash_size);
|
void init(int hash_size);
|
||||||
bool open();
|
bool open(const std::string &file);
|
||||||
void save();
|
void save();
|
||||||
|
void save(const std::string &file);
|
||||||
void writeback();
|
void writeback();
|
||||||
|
void serialize(std::ostream &os);
|
||||||
|
void unserialize(Checkpoint *cp, const std::string §ion);
|
||||||
|
|
||||||
virtual off_t size() const;
|
virtual off_t size() const;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue