From 30f4c392a4236fd84958293eadc00ca1272eb23f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Wed, 12 Aug 2009 14:39:37 +0000 Subject: [PATCH] * Cleanup. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@32281 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- .../boot/loader/file_systems/tarfs/tarfs.cpp | 293 ++++++++++-------- 1 file changed, 159 insertions(+), 134 deletions(-) diff --git a/src/system/boot/loader/file_systems/tarfs/tarfs.cpp b/src/system/boot/loader/file_systems/tarfs/tarfs.cpp index e80ec5c24b..c85cb896a3 100644 --- a/src/system/boot/loader/file_systems/tarfs/tarfs.cpp +++ b/src/system/boot/loader/file_systems/tarfs/tarfs.cpp @@ -1,6 +1,6 @@ /* * Copyright 2005-2007, Ingo Weinhold, bonefish@cs.tu-berlin.de. - * Copyright 2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved. + * Copyright 2005-2009, Axel Dörfler, axeld@pinc-software.de. * * Distributed under the terms of the MIT License. */ @@ -33,13 +33,14 @@ #endif -static const uint32 kFloppyArchiveOffset = BOOT_ARCHIVE_IMAGE_OFFSET * 1024; // initially at 192 kB, now defined at build time +static const uint32 kFloppyArchiveOffset = BOOT_ARCHIVE_IMAGE_OFFSET * 1024; + // defined at build time, see build/jam/BuildSetup static const size_t kTarRegionSize = 16 * 1024 * 1024; // 16 MB namespace TarFS { struct RegionDelete { - inline void operator()(void *memory) + inline void operator()(void* memory) { if (memory != NULL) platform_free_region(memory, kTarRegionSize); @@ -47,24 +48,33 @@ struct RegionDelete { }; struct RegionDeleter : BPrivate::AutoDeleter { - RegionDeleter() : BPrivate::AutoDeleter() {} - RegionDeleter(void *memory) : BPrivate::AutoDeleter(memory) {} + RegionDeleter() + : + BPrivate::AutoDeleter() + { + } + + RegionDeleter(void* memory) + : + BPrivate::AutoDeleter(memory) + { + } }; class Directory; class Entry : public DoublyLinkedListLinkImpl { - public: - Entry(const char *name); - virtual ~Entry() {} +public: + Entry(const char* name); + virtual ~Entry() {} - const char *Name() const { return fName; } - virtual ::Node *ToNode() = 0; - virtual TarFS::Directory *ToTarDirectory() { return NULL; } + const char* Name() const { return fName; } + virtual ::Node* ToNode() = 0; + virtual TarFS::Directory* ToTarDirectory() { return NULL; } - protected: - const char *fName; - int32 fID; +protected: + const char* fName; + int32 fID; }; @@ -73,52 +83,58 @@ typedef EntryList::Iterator EntryIterator; class File : public ::Node, public Entry { - public: - File(tar_header *header, const char *name); - virtual ~File(); +public: + File(tar_header* header, const char* name); + virtual ~File(); - virtual ssize_t ReadAt(void *cookie, off_t pos, void *buffer, size_t bufferSize); - virtual ssize_t WriteAt(void *cookie, off_t pos, const void *buffer, size_t bufferSize); + virtual ssize_t ReadAt(void* cookie, off_t pos, void* buffer, + size_t bufferSize); + virtual ssize_t WriteAt(void* cookie, off_t pos, + const void* buffer, size_t bufferSize); - virtual status_t GetName(char *nameBuffer, size_t bufferSize) const; + virtual status_t GetName(char* nameBuffer, + size_t bufferSize) const; - virtual int32 Type() const; - virtual off_t Size() const; - virtual ino_t Inode() const; + virtual int32 Type() const; + virtual off_t Size() const; + virtual ino_t Inode() const; - virtual ::Node *ToNode() { return this; } + virtual ::Node* ToNode() { return this; } - private: - tar_header *fHeader; - off_t fSize; +private: + tar_header* fHeader; + off_t fSize; }; class Directory : public ::Directory, public Entry { - public: - Directory(const char *name); - virtual ~Directory(); +public: + Directory(const char* name); + virtual ~Directory(); - virtual status_t Open(void **_cookie, int mode); - virtual status_t Close(void *cookie); + virtual status_t Open(void** _cookie, int mode); + virtual status_t Close(void* cookie); - virtual status_t GetName(char *nameBuffer, size_t bufferSize) const; + virtual status_t GetName(char* nameBuffer, + size_t bufferSize) const; - virtual TarFS::Entry *LookupEntry(const char *name); - virtual ::Node *Lookup(const char *name, bool traverseLinks); + virtual TarFS::Entry* LookupEntry(const char* name); + virtual ::Node* Lookup(const char* name, bool traverseLinks); - virtual status_t GetNextEntry(void *cookie, char *nameBuffer, size_t bufferSize); - virtual status_t GetNextNode(void *cookie, Node **_node); - virtual status_t Rewind(void *cookie); - virtual bool IsEmpty(); + virtual status_t GetNextEntry(void* cookie, char* nameBuffer, + size_t bufferSize); + virtual status_t GetNextNode(void* cookie, Node** _node); + virtual status_t Rewind(void* cookie); + virtual bool IsEmpty(); - virtual ino_t Inode() const; + virtual ino_t Inode() const; - virtual ::Node *ToNode() { return this; }; - virtual TarFS::Directory *ToTarDirectory() { return this; } + virtual ::Node* ToNode() { return this; }; + virtual TarFS::Directory* ToTarDirectory() { return this; } - status_t AddDirectory(char *dirName, TarFS::Directory **_dir = NULL); - status_t AddFile(tar_header *header); + status_t AddDirectory(char* dirName, + TarFS::Directory** _dir = NULL); + status_t AddFile(tar_header* header); private: typedef ::Directory _inherited; @@ -128,44 +144,46 @@ class Directory : public ::Directory, public Entry { class Symlink : public ::Node, public Entry { - public: - Symlink(tar_header *header, const char *name); - virtual ~Symlink(); +public: + Symlink(tar_header* header, const char* name); + virtual ~Symlink(); - virtual ssize_t ReadAt(void *cookie, off_t pos, void *buffer, - size_t bufferSize); - virtual ssize_t WriteAt(void *cookie, off_t pos, const void *buffer, - size_t bufferSize); + virtual ssize_t ReadAt(void* cookie, off_t pos, void* buffer, + size_t bufferSize); + virtual ssize_t WriteAt(void* cookie, off_t pos, + const void* buffer, size_t bufferSize); - virtual status_t GetName(char *nameBuffer, size_t bufferSize) const; + virtual status_t GetName(char* nameBuffer, + size_t bufferSize) const; - virtual int32 Type() const; - virtual off_t Size() const; - virtual ino_t Inode() const; + virtual int32 Type() const; + virtual off_t Size() const; + virtual ino_t Inode() const; - const char* LinkPath() const { return fHeader->linkname; } + const char* LinkPath() const { return fHeader->linkname; } - virtual ::Node *ToNode() { return this; } + virtual ::Node* ToNode() { return this; } - private: - tar_header *fHeader; - size_t fSize; +private: + tar_header* fHeader; + size_t fSize; }; class Volume : public TarFS::Directory { - public: - Volume(); - ~Volume(); +public: + Volume(); + ~Volume(); - status_t Init(boot::Partition *partition); + status_t Init(boot::Partition* partition); - TarFS::Directory *Root() { return this; } + TarFS::Directory* Root() { return this; } - private: - status_t _Inflate(boot::Partition *partition, void* cookie, - off_t offset, RegionDeleter& regionDeleter, - size_t* inflatedBytes); +private: + status_t _Inflate(boot::Partition* partition, + void* cookie, off_t offset, + RegionDeleter& regionDeleter, + size_t* inflatedBytes); }; } // namespace TarFS @@ -178,10 +196,10 @@ static int32 sNextID = 1; bool -skip_gzip_header(z_stream *stream) +skip_gzip_header(z_stream* stream) { - uint8 *buffer = (uint8 *)stream->next_in; - + uint8* buffer = (uint8*)stream->next_in; + // check magic and skip method if (buffer[0] != 0x1f || buffer[1] != 0x8b) return false; @@ -224,7 +242,7 @@ skip_gzip_header(z_stream *stream) // #pragma mark - -TarFS::Entry::Entry(const char *name) +TarFS::Entry::Entry(const char* name) : fName(name), fID(sNextID++) @@ -235,7 +253,7 @@ TarFS::Entry::Entry(const char *name) // #pragma mark - -TarFS::File::File(tar_header *header, const char *name) +TarFS::File::File(tar_header* header, const char* name) : TarFS::Entry(name), fHeader(header) { @@ -249,9 +267,10 @@ TarFS::File::~File() ssize_t -TarFS::File::ReadAt(void *cookie, off_t pos, void *buffer, size_t bufferSize) +TarFS::File::ReadAt(void* cookie, off_t pos, void* buffer, size_t bufferSize) { - TRACE(("tarfs: read at %Ld, %lu bytes, fSize = %Ld\n", pos, bufferSize, fSize)); + TRACE(("tarfs: read at %Ld, %lu bytes, fSize = %Ld\n", pos, bufferSize, + fSize)); if (pos < 0 || !buffer) return B_BAD_VALUE; @@ -270,16 +289,18 @@ TarFS::File::ReadAt(void *cookie, off_t pos, void *buffer, size_t bufferSize) ssize_t -TarFS::File::WriteAt(void *cookie, off_t pos, const void *buffer, size_t bufferSize) +TarFS::File::WriteAt(void* cookie, off_t pos, const void* buffer, + size_t bufferSize) { return B_NOT_ALLOWED; } status_t -TarFS::File::GetName(char *nameBuffer, size_t bufferSize) const +TarFS::File::GetName(char* nameBuffer, size_t bufferSize) const { - return strlcpy(nameBuffer, Name(), bufferSize) >= bufferSize ? B_BUFFER_OVERFLOW : B_OK; + return strlcpy(nameBuffer, Name(), bufferSize) >= bufferSize + ? B_BUFFER_OVERFLOW : B_OK; } @@ -306,7 +327,7 @@ TarFS::File::Inode() const // #pragma mark - -TarFS::Directory::Directory(const char *name) +TarFS::Directory::Directory(const char* name) : TarFS::Entry(name) { } @@ -314,19 +335,20 @@ TarFS::Directory::Directory(const char *name) TarFS::Directory::~Directory() { - while (TarFS::Entry *entry = fEntries.Head()) { + while (TarFS::Entry* entry = fEntries.Head()) { fEntries.Remove(entry); delete entry; } } -status_t -TarFS::Directory::Open(void **_cookie, int mode) +status_t +TarFS::Directory::Open(void** _cookie, int mode) { _inherited::Open(_cookie, mode); - EntryIterator *iterator = new(nothrow) EntryIterator(fEntries.GetIterator()); + EntryIterator* iterator + = new(std::nothrow) EntryIterator(fEntries.GetIterator()); if (iterator == NULL) return B_NO_MEMORY; @@ -336,30 +358,31 @@ TarFS::Directory::Open(void **_cookie, int mode) } -status_t -TarFS::Directory::Close(void *cookie) +status_t +TarFS::Directory::Close(void* cookie) { _inherited::Close(cookie); - delete (EntryIterator *)cookie; + delete (EntryIterator*)cookie; return B_OK; } status_t -TarFS::Directory::GetName(char *nameBuffer, size_t bufferSize) const +TarFS::Directory::GetName(char* nameBuffer, size_t bufferSize) const { - return strlcpy(nameBuffer, Name(), bufferSize) >= bufferSize ? B_BUFFER_OVERFLOW : B_OK; + return strlcpy(nameBuffer, Name(), bufferSize) >= bufferSize + ? B_BUFFER_OVERFLOW : B_OK; } -TarFS::Entry * -TarFS::Directory::LookupEntry(const char *name) +TarFS::Entry* +TarFS::Directory::LookupEntry(const char* name) { EntryIterator iterator(fEntries.GetIterator()); while (iterator.HasNext()) { - TarFS::Entry *entry = iterator.Next(); + TarFS::Entry* entry = iterator.Next(); if (strcmp(name, entry->Name()) == 0) return entry; } @@ -368,10 +391,10 @@ TarFS::Directory::LookupEntry(const char *name) } -::Node * -TarFS::Directory::Lookup(const char *name, bool traverseLinks) +::Node* +TarFS::Directory::Lookup(const char* name, bool traverseLinks) { - TarFS::Entry *entry = LookupEntry(name); + TarFS::Entry* entry = LookupEntry(name); if (!entry) return NULL; @@ -395,11 +418,11 @@ TarFS::Directory::Lookup(const char *name, bool traverseLinks) } -status_t -TarFS::Directory::GetNextEntry(void *_cookie, char *name, size_t size) +status_t +TarFS::Directory::GetNextEntry(void* _cookie, char* name, size_t size) { - EntryIterator *iterator = (EntryIterator *)_cookie; - TarFS::Entry *entry = iterator->Next(); + EntryIterator* iterator = (EntryIterator*)_cookie; + TarFS::Entry* entry = iterator->Next(); if (entry != NULL) { strlcpy(name, entry->Name(), size); @@ -410,11 +433,11 @@ TarFS::Directory::GetNextEntry(void *_cookie, char *name, size_t size) } -status_t -TarFS::Directory::GetNextNode(void *_cookie, Node **_node) +status_t +TarFS::Directory::GetNextNode(void* _cookie, Node** _node) { - EntryIterator *iterator = (EntryIterator *)_cookie; - TarFS::Entry *entry = iterator->Next(); + EntryIterator* iterator = (EntryIterator*)_cookie; + TarFS::Entry* entry = iterator->Next(); if (entry != NULL) { *_node = entry->ToNode(); @@ -424,19 +447,19 @@ TarFS::Directory::GetNextNode(void *_cookie, Node **_node) } -status_t -TarFS::Directory::Rewind(void *_cookie) +status_t +TarFS::Directory::Rewind(void* _cookie) { - EntryIterator *iterator = (EntryIterator *)_cookie; + EntryIterator* iterator = (EntryIterator*)_cookie; *iterator = fEntries.GetIterator(); - return B_OK; + return B_OK; } status_t -TarFS::Directory::AddDirectory(char *dirName, TarFS::Directory **_dir) +TarFS::Directory::AddDirectory(char* dirName, TarFS::Directory** _dir) { - char *subDir = strchr(dirName, '/'); + char* subDir = strchr(dirName, '/'); if (subDir) { // skip slashes while (*subDir == '/') { @@ -451,8 +474,8 @@ TarFS::Directory::AddDirectory(char *dirName, TarFS::Directory **_dir) } // check, whether the directory does already exist - Entry *entry = LookupEntry(dirName); - TarFS::Directory *dir = (entry ? entry->ToTarDirectory() : NULL); + Entry* entry = LookupEntry(dirName); + TarFS::Directory* dir = (entry ? entry->ToTarDirectory() : NULL); if (entry) { if (!dir) return B_ERROR; @@ -480,10 +503,10 @@ TarFS::Directory::AddDirectory(char *dirName, TarFS::Directory **_dir) status_t -TarFS::Directory::AddFile(tar_header *header) +TarFS::Directory::AddFile(tar_header* header) { - char *leaf = strrchr(header->name, '/'); - char *dirName = NULL; + char* leaf = strrchr(header->name, '/'); + char* dirName = NULL; if (leaf) { dirName = header->name; *leaf = '\0'; @@ -492,7 +515,7 @@ TarFS::Directory::AddFile(tar_header *header) leaf = header->name; // create the parent directory - TarFS::Directory *dir = this; + TarFS::Directory* dir = this; if (dirName) { status_t error = AddDirectory(dirName, &dir); if (error != B_OK) @@ -500,7 +523,7 @@ TarFS::Directory::AddFile(tar_header *header) } // create the entry - TarFS::Entry *entry; + TarFS::Entry* entry; if (header->type == TAR_FILE || header->type == TAR_FILE2) entry = new(nothrow) TarFS::File(header, leaf); else if (header->type == TAR_SYMLINK) @@ -517,7 +540,7 @@ TarFS::Directory::AddFile(tar_header *header) } -bool +bool TarFS::Directory::IsEmpty() { return fEntries.IsEmpty(); @@ -534,7 +557,7 @@ TarFS::Directory::Inode() const // #pragma mark - -TarFS::Symlink::Symlink(tar_header *header, const char *name) +TarFS::Symlink::Symlink(tar_header* header, const char* name) : TarFS::Entry(name), fHeader(header) { @@ -550,14 +573,14 @@ TarFS::Symlink::~Symlink() ssize_t -TarFS::Symlink::ReadAt(void *cookie, off_t pos, void *buffer, size_t bufferSize) +TarFS::Symlink::ReadAt(void* cookie, off_t pos, void* buffer, size_t bufferSize) { return B_NOT_ALLOWED; } ssize_t -TarFS::Symlink::WriteAt(void *cookie, off_t pos, const void *buffer, +TarFS::Symlink::WriteAt(void* cookie, off_t pos, const void* buffer, size_t bufferSize) { return B_NOT_ALLOWED; @@ -565,7 +588,7 @@ TarFS::Symlink::WriteAt(void *cookie, off_t pos, const void *buffer, status_t -TarFS::Symlink::GetName(char *nameBuffer, size_t bufferSize) const +TarFS::Symlink::GetName(char* nameBuffer, size_t bufferSize) const { return strlcpy(nameBuffer, Name(), bufferSize) >= bufferSize ? B_BUFFER_OVERFLOW : B_OK; @@ -608,9 +631,9 @@ TarFS::Volume::~Volume() status_t -TarFS::Volume::Init(boot::Partition *partition) +TarFS::Volume::Init(boot::Partition* partition) { - void *cookie; + void* cookie; status_t error = partition->Open(&cookie, O_RDONLY); if (error != B_OK) return error; @@ -619,7 +642,7 @@ TarFS::Volume::Init(boot::Partition *partition) boot::Partition *partition; void *cookie; - PartitionCloser(boot::Partition *partition, void *cookie) + PartitionCloser(boot::Partition* partition, void* cookie) : partition(partition), cookie(cookie) { @@ -645,13 +668,13 @@ TarFS::Volume::Init(boot::Partition *partition) return status; // parse the tar file - char *block = (char*)regionDeleter.Get(); + char* block = (char*)regionDeleter.Get(); int blockCount = inflatedBytes / BLOCK_SIZE; int blockIndex = 0; while (blockIndex < blockCount) { // check header - tar_header *header = (tar_header*)(block + blockIndex * BLOCK_SIZE); + tar_header* header = (tar_header*)(block + blockIndex * BLOCK_SIZE); //dump_header(*header); if (header->magic[0] == '\0') @@ -669,7 +692,8 @@ TarFS::Volume::Init(boot::Partition *partition) TRACE(("tarfs: \"%s\", %Ld bytes\n", header->name, size)); - // TODO: this is old-style GNU tar which probably won't work with newer ones... + // TODO: this is old-style GNU tar which probably won't work with newer + // ones... switch (header->type) { case TAR_FILE: case TAR_FILE2: @@ -685,7 +709,8 @@ TarFS::Volume::Init(boot::Partition *partition) // this is a long file name // TODO: read long name default: - dprintf("tarfs: unsupported file type: %d ('%c')\n", header->type, header->type); + dprintf("tarfs: unsupported file type: %d ('%c')\n", + header->type, header->type); // unsupported type status = B_ERROR; break; @@ -707,7 +732,7 @@ TarFS::Volume::Init(boot::Partition *partition) status_t -TarFS::Volume::_Inflate(boot::Partition *partition, void* cookie, off_t offset, +TarFS::Volume::_Inflate(boot::Partition* partition, void* cookie, off_t offset, RegionDeleter& regionDeleter, size_t* inflatedBytes) { char in[2048]; @@ -742,7 +767,7 @@ TarFS::Volume::_Inflate(boot::Partition *partition, void* cookie, off_t offset, } zStream.avail_in = bytesRead; - zStream.next_in = (Bytef *)in; + zStream.next_in = (Bytef*)in; if (!headerRead) { // check and skip gzip header @@ -752,7 +777,7 @@ TarFS::Volume::_Inflate(boot::Partition *partition, void* cookie, off_t offset, if (!out) { // allocate memory for the uncompressed data - if (platform_allocate_region((void **)&out, kTarRegionSize, + if (platform_allocate_region((void**)&out, kTarRegionSize, B_READ_AREA | B_WRITE_AREA, false) != B_OK) { TRACE(("tarfs: allocating region failed!\n")); return B_NO_MEMORY; @@ -761,7 +786,7 @@ TarFS::Volume::_Inflate(boot::Partition *partition, void* cookie, off_t offset, } zStream.avail_out = kTarRegionSize; - zStream.next_out = (Bytef *)out; + zStream.next_out = (Bytef*)out; status = inflateInit2(&zStream, -15); if (status != Z_OK) @@ -793,9 +818,9 @@ TarFS::Volume::_Inflate(boot::Partition *partition, void* cookie, off_t offset, static status_t -tarfs_get_file_system(boot::Partition *partition, ::Directory **_root) +tarfs_get_file_system(boot::Partition* partition, ::Directory** _root) { - TarFS::Volume *volume = new(nothrow) TarFS::Volume; + TarFS::Volume* volume = new(nothrow) TarFS::Volume; if (volume == NULL) return B_NO_MEMORY;