You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

322 lines
8.7 KiB

12 years ago
12 years ago
12 years ago
12 years ago
9 years ago
10 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
9 years ago
9 years ago
  1. package storage
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "os"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. )
  10. const (
  11. FlagGzip = 0x01
  12. FlagHasName = 0x02
  13. FlagHasMime = 0x04
  14. FlagHasLastModifiedDate = 0x08
  15. FlagHasTtl = 0x10
  16. FlagHasPairs = 0x20
  17. FlagIsChunkManifest = 0x80
  18. LastModifiedBytesLength = 5
  19. TtlBytesLength = 2
  20. )
  21. func (n *Needle) DiskSize() int64 {
  22. return getActualSize(n.Size)
  23. }
  24. func (n *Needle) Append(w io.Writer, version Version) (size uint32, actualSize int64, err error) {
  25. if s, ok := w.(io.Seeker); ok {
  26. if end, e := s.Seek(0, 1); e == nil {
  27. defer func(s io.Seeker, off int64) {
  28. if err != nil {
  29. if _, e = s.Seek(off, 0); e != nil {
  30. glog.V(0).Infof("Failed to seek %s back to %d with error: %v", w, off, e)
  31. }
  32. }
  33. }(s, end)
  34. } else {
  35. err = fmt.Errorf("Cannot Read Current Volume Position: %v", e)
  36. return
  37. }
  38. }
  39. switch version {
  40. case Version1:
  41. header := make([]byte, NeedleHeaderSize)
  42. util.Uint32toBytes(header[0:4], n.Cookie)
  43. util.Uint64toBytes(header[4:12], n.Id)
  44. n.Size = uint32(len(n.Data))
  45. size = n.Size
  46. util.Uint32toBytes(header[12:16], n.Size)
  47. if _, err = w.Write(header); err != nil {
  48. return
  49. }
  50. if _, err = w.Write(n.Data); err != nil {
  51. return
  52. }
  53. actualSize = NeedleHeaderSize + int64(n.Size)
  54. padding := NeedlePaddingSize - ((NeedleHeaderSize + n.Size + NeedleChecksumSize) % NeedlePaddingSize)
  55. util.Uint32toBytes(header[0:NeedleChecksumSize], n.Checksum.Value())
  56. _, err = w.Write(header[0 : NeedleChecksumSize+padding])
  57. return
  58. case Version2:
  59. header := make([]byte, NeedleHeaderSize)
  60. util.Uint32toBytes(header[0:4], n.Cookie)
  61. util.Uint64toBytes(header[4:12], n.Id)
  62. n.DataSize, n.NameSize, n.MimeSize = uint32(len(n.Data)), uint8(len(n.Name)), uint8(len(n.Mime))
  63. if n.DataSize > 0 {
  64. n.Size = 4 + n.DataSize + 1
  65. if n.HasName() {
  66. n.Size = n.Size + 1 + uint32(n.NameSize)
  67. }
  68. if n.HasMime() {
  69. n.Size = n.Size + 1 + uint32(n.MimeSize)
  70. }
  71. if n.HasLastModifiedDate() {
  72. n.Size = n.Size + LastModifiedBytesLength
  73. }
  74. if n.HasTtl() {
  75. n.Size = n.Size + TtlBytesLength
  76. }
  77. if n.HasPairs() {
  78. n.Size += 2 + uint32(n.PairsSize)
  79. }
  80. } else {
  81. n.Size = 0
  82. }
  83. size = n.DataSize
  84. util.Uint32toBytes(header[12:16], n.Size)
  85. if _, err = w.Write(header); err != nil {
  86. return
  87. }
  88. if n.DataSize > 0 {
  89. util.Uint32toBytes(header[0:4], n.DataSize)
  90. if _, err = w.Write(header[0:4]); err != nil {
  91. return
  92. }
  93. if _, err = w.Write(n.Data); err != nil {
  94. return
  95. }
  96. util.Uint8toBytes(header[0:1], n.Flags)
  97. if _, err = w.Write(header[0:1]); err != nil {
  98. return
  99. }
  100. if n.HasName() {
  101. util.Uint8toBytes(header[0:1], n.NameSize)
  102. if _, err = w.Write(header[0:1]); err != nil {
  103. return
  104. }
  105. if _, err = w.Write(n.Name); err != nil {
  106. return
  107. }
  108. }
  109. if n.HasMime() {
  110. util.Uint8toBytes(header[0:1], n.MimeSize)
  111. if _, err = w.Write(header[0:1]); err != nil {
  112. return
  113. }
  114. if _, err = w.Write(n.Mime); err != nil {
  115. return
  116. }
  117. }
  118. if n.HasLastModifiedDate() {
  119. util.Uint64toBytes(header[0:8], n.LastModified)
  120. if _, err = w.Write(header[8-LastModifiedBytesLength : 8]); err != nil {
  121. return
  122. }
  123. }
  124. if n.HasTtl() && n.Ttl != nil {
  125. n.Ttl.ToBytes(header[0:TtlBytesLength])
  126. if _, err = w.Write(header[0:TtlBytesLength]); err != nil {
  127. return
  128. }
  129. }
  130. if n.HasPairs() {
  131. util.Uint16toBytes(header[0:2], n.PairsSize)
  132. if _, err = w.Write(header[0:2]); err != nil {
  133. return
  134. }
  135. if _, err = w.Write(n.Pairs); err != nil {
  136. return
  137. }
  138. }
  139. }
  140. padding := NeedlePaddingSize - ((NeedleHeaderSize + n.Size + NeedleChecksumSize) % NeedlePaddingSize)
  141. util.Uint32toBytes(header[0:NeedleChecksumSize], n.Checksum.Value())
  142. _, err = w.Write(header[0 : NeedleChecksumSize+padding])
  143. return n.DataSize, getActualSize(n.Size), err
  144. }
  145. return 0, 0, fmt.Errorf("Unsupported Version! (%d)", version)
  146. }
  147. func ReadNeedleBlob(r *os.File, offset int64, size uint32) (dataSlice []byte, block *Block, err error) {
  148. return getBytesForFileBlock(r, offset, int(getActualSize(size)))
  149. }
  150. func (n *Needle) ReadData(r *os.File, offset int64, size uint32, version Version) (err error) {
  151. bytes, block, err := ReadNeedleBlob(r, offset, size)
  152. if err != nil {
  153. return err
  154. }
  155. n.rawBlock = block
  156. n.ParseNeedleHeader(bytes)
  157. if n.Size != size {
  158. return fmt.Errorf("File Entry Not Found. Needle %d Memory %d", n.Size, size)
  159. }
  160. switch version {
  161. case Version1:
  162. n.Data = bytes[NeedleHeaderSize : NeedleHeaderSize+size]
  163. case Version2:
  164. n.readNeedleDataVersion2(bytes[NeedleHeaderSize : NeedleHeaderSize+int(n.Size)])
  165. }
  166. if size == 0 {
  167. return nil
  168. }
  169. checksum := util.BytesToUint32(bytes[NeedleHeaderSize+size : NeedleHeaderSize+size+NeedleChecksumSize])
  170. newChecksum := NewCRC(n.Data)
  171. if checksum != newChecksum.Value() {
  172. return errors.New("CRC error! Data On Disk Corrupted")
  173. }
  174. n.Checksum = newChecksum
  175. return nil
  176. }
  177. func (n *Needle) ParseNeedleHeader(bytes []byte) {
  178. n.Cookie = util.BytesToUint32(bytes[0:4])
  179. n.Id = util.BytesToUint64(bytes[4:12])
  180. n.Size = util.BytesToUint32(bytes[12:NeedleHeaderSize])
  181. }
  182. func (n *Needle) readNeedleDataVersion2(bytes []byte) {
  183. index, lenBytes := 0, len(bytes)
  184. if index < lenBytes {
  185. n.DataSize = util.BytesToUint32(bytes[index : index+4])
  186. index = index + 4
  187. if int(n.DataSize)+index > lenBytes {
  188. // this if clause is due to bug #87 and #93, fixed in v0.69
  189. // remove this clause later
  190. return
  191. }
  192. n.Data = bytes[index : index+int(n.DataSize)]
  193. index = index + int(n.DataSize)
  194. n.Flags = bytes[index]
  195. index = index + 1
  196. }
  197. if index < lenBytes && n.HasName() {
  198. n.NameSize = uint8(bytes[index])
  199. index = index + 1
  200. n.Name = bytes[index : index+int(n.NameSize)]
  201. index = index + int(n.NameSize)
  202. }
  203. if index < lenBytes && n.HasMime() {
  204. n.MimeSize = uint8(bytes[index])
  205. index = index + 1
  206. n.Mime = bytes[index : index+int(n.MimeSize)]
  207. index = index + int(n.MimeSize)
  208. }
  209. if index < lenBytes && n.HasLastModifiedDate() {
  210. n.LastModified = util.BytesToUint64(bytes[index : index+LastModifiedBytesLength])
  211. index = index + LastModifiedBytesLength
  212. }
  213. if index < lenBytes && n.HasTtl() {
  214. n.Ttl = LoadTTLFromBytes(bytes[index : index+TtlBytesLength])
  215. index = index + TtlBytesLength
  216. }
  217. if index < lenBytes && n.HasPairs() {
  218. n.PairsSize = util.BytesToUint16(bytes[index : index+2])
  219. index += 2
  220. end := index + int(n.PairsSize)
  221. n.Pairs = bytes[index:end]
  222. index = end
  223. }
  224. }
  225. func ReadNeedleHeader(r *os.File, version Version, offset int64) (n *Needle, bodyLength uint32, err error) {
  226. n = new(Needle)
  227. if version == Version1 || version == Version2 {
  228. bytes := make([]byte, NeedleHeaderSize)
  229. var count int
  230. count, err = r.ReadAt(bytes, offset)
  231. if count <= 0 || err != nil {
  232. return nil, 0, err
  233. }
  234. n.ParseNeedleHeader(bytes)
  235. padding := NeedlePaddingSize - ((n.Size + NeedleHeaderSize + NeedleChecksumSize) % NeedlePaddingSize)
  236. bodyLength = n.Size + NeedleChecksumSize + padding
  237. }
  238. return
  239. }
  240. //n should be a needle already read the header
  241. //the input stream will read until next file entry
  242. func (n *Needle) ReadNeedleBody(r *os.File, version Version, offset int64, bodyLength uint32) (err error) {
  243. if bodyLength <= 0 {
  244. return nil
  245. }
  246. switch version {
  247. case Version1:
  248. bytes := make([]byte, bodyLength)
  249. if _, err = r.ReadAt(bytes, offset); err != nil {
  250. return
  251. }
  252. n.Data = bytes[:n.Size]
  253. n.Checksum = NewCRC(n.Data)
  254. case Version2:
  255. bytes := make([]byte, bodyLength)
  256. if _, err = r.ReadAt(bytes, offset); err != nil {
  257. return
  258. }
  259. n.readNeedleDataVersion2(bytes[0:n.Size])
  260. n.Checksum = NewCRC(n.Data)
  261. default:
  262. err = fmt.Errorf("Unsupported Version! (%d)", version)
  263. }
  264. return
  265. }
  266. func (n *Needle) IsGzipped() bool {
  267. return n.Flags&FlagGzip > 0
  268. }
  269. func (n *Needle) SetGzipped() {
  270. n.Flags = n.Flags | FlagGzip
  271. }
  272. func (n *Needle) HasName() bool {
  273. return n.Flags&FlagHasName > 0
  274. }
  275. func (n *Needle) SetHasName() {
  276. n.Flags = n.Flags | FlagHasName
  277. }
  278. func (n *Needle) HasMime() bool {
  279. return n.Flags&FlagHasMime > 0
  280. }
  281. func (n *Needle) SetHasMime() {
  282. n.Flags = n.Flags | FlagHasMime
  283. }
  284. func (n *Needle) HasLastModifiedDate() bool {
  285. return n.Flags&FlagHasLastModifiedDate > 0
  286. }
  287. func (n *Needle) SetHasLastModifiedDate() {
  288. n.Flags = n.Flags | FlagHasLastModifiedDate
  289. }
  290. func (n *Needle) HasTtl() bool {
  291. return n.Flags&FlagHasTtl > 0
  292. }
  293. func (n *Needle) SetHasTtl() {
  294. n.Flags = n.Flags | FlagHasTtl
  295. }
  296. func (n *Needle) IsChunkedManifest() bool {
  297. return n.Flags&FlagIsChunkManifest > 0
  298. }
  299. func (n *Needle) SetIsChunkManifest() {
  300. n.Flags = n.Flags | FlagIsChunkManifest
  301. }
  302. func (n *Needle) HasPairs() bool {
  303. return n.Flags&FlagHasPairs != 0
  304. }
  305. func (n *Needle) SetHasPairs() {
  306. n.Flags = n.Flags | FlagHasPairs
  307. }