From 76e7f5688dcd7dd36cad23f75701a8b2299580b3 Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Mon, 13 Jul 2015 18:09:18 +0200 Subject: [PATCH] bfs_tools: add some extra checks to avoid crashes. I'm not sure I'm fixing the root cause of problems here, but this avoids some crashes and I could recover my files this way. --- src/bin/bfs_tools/lib/Inode.cpp | 14 +++++++++----- src/bin/bfs_tools/lib/Inode.h | 10 +++++----- src/bin/bfs_tools/recover.cpp | 10 +++++++++- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/bin/bfs_tools/lib/Inode.cpp b/src/bin/bfs_tools/lib/Inode.cpp index 9f4634fdee..eaaf6919f4 100644 --- a/src/bin/bfs_tools/lib/Inode.cpp +++ b/src/bin/bfs_tools/lib/Inode.cpp @@ -108,7 +108,7 @@ Inode::SetTo(bfs_inode *inode) status_t -Inode::InitCheck() +Inode::InitCheck() const { if (!fInode) return B_ERROR; @@ -304,6 +304,10 @@ Inode::SetName(const char *name) const char * Inode::Name() const { + if (InitCheck() != B_OK) { + puts("Not getting name because node is invalid"); + return NULL; + } small_data *data = fInode->small_data_start; while (!data->IsLast(fInode)) { if (data->type == FILE_NAME_TYPE @@ -929,7 +933,7 @@ File::~File() status_t -File::InitCheck() +File::InitCheck() const { status_t status = DataStream::InitCheck(); if (status == B_OK) @@ -1006,7 +1010,7 @@ Attribute::~Attribute() status_t -Attribute::InitCheck() +Attribute::InitCheck() const { status_t status = DataStream::InitCheck(); if (status == B_OK) @@ -1053,7 +1057,7 @@ Directory::~Directory() status_t -Directory::InitCheck() +Directory::InitCheck() const { status_t status = DataStream::InitCheck(); if (status == B_OK) @@ -1322,7 +1326,7 @@ Symlink::~Symlink() status_t -Symlink::InitCheck() +Symlink::InitCheck() const { status_t status = Inode::InitCheck(); if (status == B_OK) diff --git a/src/bin/bfs_tools/lib/Inode.h b/src/bin/bfs_tools/lib/Inode.h index f06e758585..67ff22faa4 100644 --- a/src/bin/bfs_tools/lib/Inode.h +++ b/src/bin/bfs_tools/lib/Inode.h @@ -20,7 +20,7 @@ class Inode { virtual ~Inode(); status_t SetTo(bfs_inode *inode); - virtual status_t InitCheck(); + virtual status_t InitCheck() const; bool IsFile() const { return S_ISREG(fInode->mode); } bool IsDirectory() const { return S_ISDIR(fInode->mode); } @@ -131,7 +131,7 @@ class File : public DataStream { File(const Inode &inode); ~File(); - virtual status_t InitCheck(); + virtual status_t InitCheck() const; virtual status_t CopyTo(const char *path, bool fullPath = true, Inode::Source *source = NULL); }; @@ -143,7 +143,7 @@ class Attribute : public File { Attribute(const Inode &inode); ~Attribute(); - virtual status_t InitCheck(); + virtual status_t InitCheck() const; virtual status_t CopyTo(const char *path, bool fullPath = true, Inode::Source *source = NULL); @@ -157,7 +157,7 @@ class Directory : public DataStream { Directory(const Inode &inode); ~Directory(); - virtual status_t InitCheck(); + virtual status_t InitCheck() const; virtual status_t CopyTo(const char *path, bool fullPath = true, Inode::Source *source = NULL); @@ -186,7 +186,7 @@ class Symlink : public Inode { Symlink(const Inode &inode); ~Symlink(); - virtual status_t InitCheck(); + virtual status_t InitCheck() const; virtual status_t CopyTo(const char *path, bool fullPath = true, Inode::Source *source = NULL); diff --git a/src/bin/bfs_tools/recover.cpp b/src/bin/bfs_tools/recover.cpp index 50ed65033f..1988a3d14c 100644 --- a/src/bin/bfs_tools/recover.cpp +++ b/src/bin/bfs_tools/recover.cpp @@ -440,7 +440,15 @@ checkStructure(Disk &disk) if (node->IsDirectory() && !node->IsIndex()) { // check if all entries are in the hashtable - checkDirectoryContents(disk, (Directory*)node); + Directory* directory = dynamic_cast(node); + if (directory != NULL) + checkDirectoryContents(disk, directory); + else { + printf("Node \"%s\" at %" B_PRId32 + ",%d looks like a directory, but isn't.\n", + node->Name(), node->BlockRun().allocation_group, + node->BlockRun().start); + } } // check for the parent directory