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