aboutsummaryrefslogtreecommitdiff
path: root/src/unstable/myriadfs.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/unstable/myriadfs.h')
-rw-r--r--src/unstable/myriadfs.h203
1 files changed, 203 insertions, 0 deletions
diff --git a/src/unstable/myriadfs.h b/src/unstable/myriadfs.h
new file mode 100644
index 0000000..cc9961a
--- /dev/null
+++ b/src/unstable/myriadfs.h
@@ -0,0 +1,203 @@
1/*
2 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#ifndef MYRIAD_FS_H
9#define MYRIAD_FS_H
10
11#include <sys/types.h>
12
13#include "bu/myriad.h"
14
15namespace Bu
16{
17 class Stream;
18
19 subExceptionDecl( MyriadFsException );
20
21 /**
22 * A POSIX compliant, node based filesystem built on top of Myriad.
23 *
24 * A header is placed into stream 1.
25 * Header format:
26 * int32_t iMagicHeader (A7188B39)
27 * int8_t iVersion (1)
28 * int32_t iNumNodes
29 * NodeLookup[iNumNodes] nNode
30 *
31 * Node lookup:
32 * int32_t iInode
33 * int32_t iPosition
34 *
35 * The node headers or inode structures have a base size of 44 bytes.
36 * The name is stored in the directory format.
37 * Basic node header format:
38 * int32_t iNode
39 * int32_t iUser
40 * int32_t iGroup
41 * uint16_t uPerms
42 * int16_t iLinks
43 * uint32_t uStreamIndex
44 * int64_t iATime
45 * int64_t iMTime
46 * int64_t iCTime
47 *
48 * Some types get special formats for their assosiated data stream, or
49 * other special considerations, here's a list:
50 *
51 * - typeFifo: No stream, uStreamIndex unused (probably)
52 * - typeChrDev: No stream, uStreamIndex is device hi/lo
53 * - typeDir: The stream contains a directory contents listing, described
54 * below
55 * - typeBlkDev: No stream, uStreamIndex is device hi/lo
56 * - typeRegFile: The stream is the file data
57 * - typeSymLink: The stream is the destination of the symlink
58 * - typeSocket: No steram, uStreamIndex unused (probably)
59 *
60 * Directory streams have this simple listing format. They contain a list
61 * of all child elements, with no particular order at the moment. The . and
62 * .. entries are not listed, they are implicit:
63 * int32_t iNumNodes
64 * NodeTable[iNumNodes] nChildren
65 *
66 * NodeTable:
67 * int32_t iInode
68 * uint8_t uNameSize
69 * char[uNameSize] sName
70 */
71 class MyriadFs
72 {
73 public:
74 MyriadFs( Bu::Stream &rStore, int iBlockSize=512 );
75 virtual ~MyriadFs();
76
77 enum
78 {
79 permOthX = 0000001,
80 permOthW = 0000002,
81 permOthR = 0000004,
82 permGrpX = 0000010,
83 permGrpW = 0000020,
84 permGrpR = 0000040,
85 permUsrX = 0000100,
86 permUsrW = 0000200,
87 permUsrR = 0000400,
88 permSticky = 0001000,
89 permSetGid = 0002000,
90 permSetUid = 0004000,
91 permMask = 0007777,
92 typeFifo = 0010000,
93 typeChrDev = 0020000,
94 typeDir = 0040000,
95 typeBlkDev = 0060000,
96 typeRegFile = 0100000,
97 typeSymLink = 0120000,
98 typeSocket = 0140000,
99 typeMask = 0170000
100 };
101
102 enum
103 {
104 Read = 0x01, ///< Open file for reading
105 Write = 0x02, ///< Open file for writing
106 Create = 0x04, ///< Create file if it doesn't exist
107 Truncate = 0x08, ///< Truncate file if it does exist
108 Append = 0x10, ///< Always append on every write
109 NonBlock = 0x20, ///< Open file in non-blocking mode
110 Exclusive = 0x44, ///< Create file, if it exists then fail
111
112 // Helpful mixes
113 ReadWrite = 0x03, ///< Open for reading and writing
114 WriteNew = 0x0E ///< Create a file (or truncate) for writing.
115 /// Same as Write|Create|Truncate
116 };
117
118 class Stat
119 {
120 public:
121 int32_t iNode;
122 int32_t iUser;
123 int32_t iGroup;
124 uint16_t uPerms;
125 int16_t iLinks;
126 int64_t iATime;
127 int64_t iMTime;
128 int64_t iCTime;
129 int32_t iSize;
130 uint32_t uDev;
131 Bu::String sName;
132 };
133 typedef Bu::List<Stat> Dir;
134
135 void stat( const Bu::String &sPath, Stat &rBuf );
136 MyriadStream open( const Bu::String &sPath, int iMode,
137 uint16_t uPerms=0664 );
138 void create( const Bu::String &sPath, uint16_t iPerms );
139 void create( const Bu::String &sPath, uint16_t iPerms,
140 uint16_t iDevHi, uint16_t iDevLo );
141 void create( const Bu::String &sPath, uint16_t iPerms,
142 uint32_t uSpecial );
143 void mkDir( const Bu::String &sPath, uint16_t iPerms );
144 void mkSymLink( const Bu::String &sTarget, const Bu::String &sPath );
145 void mkHardLink( const Bu::String &sTarget, const Bu::String &sPath );
146 Bu::String readSymLink( const Bu::String &sPath );
147 Dir readDir( const Bu::String &sPath );
148 void setTimes( const Bu::String &sPath, int64_t iATime,
149 int64_t iMTime );
150 void unlink( const Bu::String &sPath );
151 void setFileSize( const Bu::String &sPath, int32_t iSize );
152 void rename( const Bu::String &sFrom, const Bu::String &sTo );
153
154 static dev_t devToSys( uint32_t uDev );
155 static uint32_t sysToDev( dev_t uDev );
156
157 private:
158 class RawStat
159 {
160 public:
161 int32_t iNode;
162 int32_t iUser;
163 int32_t iGroup;
164 uint16_t uPerms;
165 int16_t iLinks;
166 uint32_t uStreamIndex;
167 int64_t iATime;
168 int64_t iMTime;
169 int64_t iCTime;
170 };
171 typedef Bu::Hash<int32_t, int32_t> NodeIndex;
172
173 private:
174 int32_t lookupInode( const Bu::String &sPath, int32_t &iParent );
175 int32_t lookupInode( Bu::String::const_iterator iStart,
176 int32_t iNode, int32_t &iParent );
177 void readInode( int32_t iNode, RawStat &rs, MyriadStream &rIs );
178 void readInode( int32_t iNode, RawStat &rs );
179 void writeInode( const RawStat &rs );
180 void writeInode( const RawStat &rs, MyriadStream &rOs );
181 Dir readDir( int32_t iNode );
182 MyriadStream openByInode( int32_t iNode );
183 void addToDir( int32_t iDir, int32_t iNode, const Bu::String &sName );
184 int32_t create( int32_t iParent, const Bu::String &sName,
185 uint16_t uPerms, uint32_t uSpecial );
186 int32_t allocInode( uint16_t uPerms, uint32_t uSpecial );
187 void stat( int32_t iNode, Stat &rBuf, MyriadStream &rIs );
188 void writeHeader();
189 void setTimes( int32_t iNode, int64_t iATime, int64_t iMTime );
190 void destroyNode( int32_t iNode );
191
192 Bu::String filePart( const Bu::String &sPath );
193
194 private:
195 Bu::Stream &rStore;
196 Bu::Myriad mStore;
197 NodeIndex hNodeIndex;
198 int32_t iUser;
199 int32_t iGroup;
200 };
201};
202
203#endif