Debian Codegraph @

lib/comdoc/msat.go

raw symbols SCIP
1 //
2 // Copyright (c) SAS Institute Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 package comdoc
18
19 import (
20 "bytes"
21 "encoding/binary"
22 )
23
24 // Read the master/meta sector allocation table. It is an array of all the
25 // sectors holding pieces of the sector allocation table (SAT). The main file
26 // header holds 109 MSAT entries and MSATNextSector may point to a sector
27 // holding yet more. The last entry in each additional sector points to another
28 // sector holding more MSAT entries, or -2 if none.
29 func (r *ComDoc) readMSAT() error {
30 r.MSAT = r.Header.MSAT[:]
31 r.msatList = nil
32 nextSector := r.Header.MSATNextSector
33 count := r.SectorSize / 4
34 values := make([]SecID, count)
35 for nextSector >= 0 {
36 if err := r.readSectorStruct(nextSector, values); err != nil {
37 return err
38 }
39 r.MSAT = append(r.MSAT, values[:count-1]...)
40 r.msatList = append(r.msatList, nextSector)
41 nextSector = values[count-1]
42 }
43 // trim free slots
44 for i := len(r.MSAT) - 1; i >= 0; i-- {
45 if r.MSAT[i] >= 0 {
46 r.MSAT = r.MSAT[:i+1]
47 break
48 }
49 }
50 return nil
51 }
52
53 // Allocate sectors for the SAT and MSAT
54 func (r *ComDoc) allocSectorTables() {
55 // work out how many sectors are needed for both
56 satPerSector := r.SectorSize / 4
57 msatPerSector := satPerSector - 1
58 for {
59 if len(r.SAT)%satPerSector != 0 {
60 panic("irregularly sized sector table")
61 }
62 satSectors := len(r.SAT) / satPerSector
63 if satSectors > len(r.MSAT) {
64 // allocate a new SAT sector
65 sector := r.makeFreeSectors(1, false)[0]
66 r.MSAT = append(r.MSAT, sector)
67 r.SAT[sector] = SecIDSAT
68 // a new SAT might be needed so check again
69 continue
70 }
71 // 109 MSAT entries fit into the file header, the rest need more sectors
72 msatSectors := (len(r.MSAT) - msatInHeader + msatPerSector - 1) / msatPerSector
73 if msatSectors > len(r.msatList) {
74 // allocate a new MSAT sector
75 sector := r.makeFreeSectors(1, false)[0]
76 r.msatList = append(r.msatList, sector)
77 r.SAT[sector] = SecIDMSAT
78 // a new SAT might be needed so check again
79 continue
80 }
81 break
82 }
83 }
84
85 // Write an updated MSAT out to the header and msatSectors, with satSectors
86 // being the contents of the MSAT
87 func (r *ComDoc) writeMSAT() error {
88 satPerSector := r.SectorSize / 4
89 msatPerSector := satPerSector - 1
90 // round MSAT up to the next full sector and mark all unused spaces as free
91 msatCount := msatInHeader + len(r.msatList)*msatPerSector
92 msat := make([]SecID, msatCount)
93 copy(msat, r.MSAT)
94 for i := len(r.MSAT); i < msatCount; i++ {
95 msat[i] = SecIDFree
96 }
97 // copy the first 109 into the file header
98 copy(r.Header.MSAT[:], msat)
99 msat = msat[msatInHeader:]
100 // write remaining sectors
101 buf := bytes.NewBuffer(r.sectorBuf)
102 chunk := make([]SecID, r.SectorSize/4)
103 for i, sector := range r.msatList {
104 j := i * msatPerSector
105 copy(chunk, msat[j:j+msatPerSector])
106 // set pointer to next MSAT sector
107 if i < len(r.msatList)-1 {
108 chunk[msatPerSector] = r.msatList[i+1]
109 } else {
110 chunk[msatPerSector] = SecIDEndOfChain
111 }
112 // write
113 buf.Reset()
114 _ = binary.Write(buf, binary.LittleEndian, chunk)
115 if err := r.writeSector(sector, buf.Bytes()); err != nil {
116 return err
117 }
118 }
119 if len(r.msatList) > 0 {
120 r.Header.MSATNextSector = r.msatList[0]
121 } else {
122 r.Header.MSATNextSector = SecIDEndOfChain
123 }
124 return nil
125 }