| 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 | "errors" |
| 23 | "strings" |
| 24 | ) |
| 25 | |
| 26 | // Add or replace a named stream with the given contents. Only streams within |
| 27 | // the root storage are currently supported. |
| 28 | func (r *ComDoc) AddFile(name string, contents []byte) error { |
| 29 | if r.writer == nil { |
| 30 | return errors.New("file is not open for writing") |
| 31 | } |
| 32 | if err := r.DeleteFile(name); err != nil { |
| 33 | return err |
| 34 | } |
| 35 | // store contents |
| 36 | isShort := len(contents) < int(r.Header.MinStdStreamSize) |
| 37 | nextSector, err := r.addStream(contents, isShort) |
| 38 | if err != nil { |
| 39 | return err |
| 40 | } |
| 41 | // create new dirent |
| 42 | dirent, err := r.newDirEnt(name, uint32(len(contents)), nextSector) |
| 43 | if err != nil { |
| 44 | return err |
| 45 | } |
| 46 | r.rootFiles = append(r.rootFiles, dirent.Index) |
| 47 | r.changed = true |
| 48 | return nil |
| 49 | } |
| 50 | |
| 51 | // Delete a file from the root storage if it exists |
| 52 | func (r *ComDoc) DeleteFile(name string) error { |
| 53 | keepFiles := make([]int, 0, len(r.rootFiles)) |
| 54 | for _, index := range r.rootFiles { |
| 55 | item := &r.Files[index] |
| 56 | if !strings.EqualFold(item.name, name) { |
| 57 | keepFiles = append(keepFiles, index) |
| 58 | continue |
| 59 | } |
| 60 | if item.Type != DirStream { |
| 61 | return errors.New("can't delete or replace storages") |
| 62 | } |
| 63 | // free storage |
| 64 | if item.StreamSize < r.Header.MinStdStreamSize { |
| 65 | freeSectors(r.SSAT, item.NextSector) |
| 66 | } else { |
| 67 | freeSectors(r.SAT, item.NextSector) |
| 68 | } |
| 69 | // blank out the dirent |
| 70 | *item = DirEnt{} |
| 71 | r.changed = true |
| 72 | } |
| 73 | r.rootFiles = keepFiles |
| 74 | return nil |
| 75 | } |
| 76 | |
| 77 | // Close the CDF and, if open for writing, commit the remainder of structures |
| 78 | // to disk. |
| 79 | func (r *ComDoc) Close() error { |
| 80 | if !r.changed { |
| 81 | if r.closer != nil { |
| 82 | r.closer.Close() |
| 83 | r.closer = nil |
| 84 | } |
| 85 | return nil |
| 86 | } |
| 87 | if err := r.writeShortSAT(); err != nil { |
| 88 | return err |
| 89 | } |
| 90 | if err := r.writeDirStream(); err != nil { |
| 91 | return err |
| 92 | } |
| 93 | // Write MSAT and SAT |
| 94 | r.allocSectorTables() |
| 95 | if err := r.writeSAT(); err != nil { |
| 96 | return err |
| 97 | } |
| 98 | if err := r.writeMSAT(); err != nil { |
| 99 | return err |
| 100 | } |
| 101 | // Write file header |
| 102 | copy(r.Header.Magic[:], fileMagic) |
| 103 | r.Header.ByteOrder = byteOrderMarker |
| 104 | r.Header.SATSectors = uint32(len(r.MSAT)) |
| 105 | r.Header.MSATSectorCount = uint32(len(r.msatList)) |
| 106 | buf := bytes.NewBuffer(make([]byte, 0, 512)) |
| 107 | _ = binary.Write(buf, binary.LittleEndian, r.Header) |
| 108 | if _, err := r.writer.WriteAt(buf.Bytes(), 0); err != nil { |
| 109 | return err |
| 110 | } |
| 111 | // Truncate past last sector. Trailing garbage will cause validation to fail. |
| 112 | for i := len(r.SAT) - 1; i >= 0; i-- { |
| 113 | if r.SAT[i] != SecIDFree { |
| 114 | if err := r.writer.Truncate(r.sectorToOffset(SecID(i + 1))); err != nil { |
| 115 | return err |
| 116 | } |
| 117 | break |
| 118 | } |
| 119 | } |
| 120 | if r.closer != nil { |
| 121 | r.closer.Close() |
| 122 | r.closer = nil |
| 123 | } |
| 124 | return nil |
| 125 | } |