xref: /haiku/src/bin/bfs_tools/lib/Disk.h (revision 7457ccb4b2f4786525d3b7bda42598487d57ab7d)
1 #ifndef DISK_H
2 #define DISK_H
3 /* Disk - handles BFS superblock, disk access etc.
4 **
5 ** Copyright (c) 2001-2003 pinc Software. All Rights Reserved.
6 ** Released under the terms of the MIT license.
7 */
8 
9 
10 #include <BufferIO.h>
11 #include <File.h>
12 #include <Path.h>
13 
14 #include <string.h>
15 
16 #include "Bitmap.h"
17 #include "bfs.h"
18 #include "Cache.h"
19 
20 
21 class Disk;
22 
23 class BlockRunCache : public Cache<block_run>
24 {
25 	public:
26 		BlockRunCache(Disk *disk);
27 //		~BlockRunCache();
28 
29 		virtual Cacheable *NewCacheable(block_run run);
30 
31 	protected:
32 		Disk	*fDisk;
33 };
34 
35 
36 class Disk : public BPositionIO
37 {
38 	public:
39 		Disk(const char *device, bool rawMode = false, off_t start = 0, off_t stop = -1);
40 		virtual ~Disk();
41 
42 		status_t			InitCheck();
43 		const BPath			&Path() const { return fPath; }
44 
45 		off_t				Size() const { return fSize; }
46 		off_t				NumBlocks() const { return fSuperBlock.num_blocks; }
47 		uint32				BlockSize() const { return fSuperBlock.block_size; }
48 		uint32				BlockShift() const { return fSuperBlock.block_shift; }
49 		uint32				AllocationGroups() const { return fSuperBlock.num_ags; }
50 		uint32				AllocationGroupShift() const { return fSuperBlock.ag_shift; }
51 		uint32				BitmapSize() const { return fBitmap.Size(); }
52 		off_t				LogSize() const;
53 
54 		disk_super_block	*SuperBlock() { return &fSuperBlock; }
55 		block_run			Root() const { return fSuperBlock.root_dir; }
56 		block_run			Indices() const { return fSuperBlock.indices; }
57 		block_run			Log() const { return fSuperBlock.log_blocks; }
58 		Bitmap				*BlockBitmap() { return &fBitmap; }
59 
60 		const char			*Name() const { return fSuperBlock.name; }
61 		void				SetName(const char *name) { strcpy(fSuperBlock.name,name); }
62 
63 		off_t				ToOffset(block_run run) const { return ToBlock(run) << fSuperBlock.block_shift; }
64 		off_t				ToBlock(block_run run) const { return ((((off_t)run.allocation_group) << fSuperBlock.ag_shift) | (off_t)run.start); }
65 		block_run			ToBlockRun(off_t start,int16 length = 1) const;
66 
67 		uint8				*ReadBlockRun(block_run run);
68 
69 		status_t			ScanForSuperBlock(off_t start = 0,off_t stop = -1);
70 		status_t			ValidateSuperBlock();
71 		status_t			RecreateSuperBlock();
72 
73 		status_t			DumpBootBlockToFile();
74 
75 		// BPositionIO methods
76 		virtual ssize_t		Read(void *buffer, size_t size);
77 		virtual ssize_t		Write(const void *buffer, size_t size);
78 
79 		virtual ssize_t		ReadAt(off_t pos, void *buffer, size_t size);
80 		virtual ssize_t		WriteAt(off_t pos, const void *buffer, size_t size);
81 
82 		virtual off_t		Seek(off_t position, uint32 seek_mode);
83 		virtual off_t		Position() const;
84 
85 		virtual status_t	SetSize(off_t size);
86 
87 	protected:
88 		status_t			GetNextSpecialInode(char *,off_t *,off_t,bool);
89 		void				SaveInode(bfs_inode *,bool *,bfs_inode *,bool *,bfs_inode *);
90 		status_t			ScanForIndexAndRoot(bfs_inode *,bfs_inode *);
91 		status_t			DetermineBlockSize();
92 		status_t			ValidateSuperBlock(disk_super_block &superBlock);
93 
94 		status_t			LoadBootBlock();
95 
96 	protected:
97 		BFile				fFile;
98 		BBufferIO*			fBufferedFile;
99 		BPath				fPath;
100 		off_t				fRawDiskOffset;
101 		off_t				fSize;
102 		disk_super_block	fSuperBlock;
103 		Bitmap				fBitmap;
104 
105 		block_run			fValidBlockRun;
106 		off_t				fValidOffset;
107 		off_t				fLogStart;
108 
109 		BlockRunCache		fCache;
110 
111 		bool				fRawMode;
112 };
113 
114 #endif	/* DISK_H */
115