MCPcopy Create free account
hub / github.com/AdRoll/baker / Close

Method Close

pkg/splitwriter/split_writer.go:80–128  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

78}
79
80func (w *splitWriter) Close() error {
81 if err := w.f.Close(); err != nil {
82 return err
83 }
84
85 stat, err := os.Stat(w.fname)
86 if err != nil {
87 return err
88 }
89
90 cursize := stat.Size()
91 fname := w.fname
92
93 for {
94 if cursize <= w.maxsize {
95 // All good: splitting not needed
96 break
97 }
98
99 // Split needed, reopen the file and look for the split point.
100 f, err := os.OpenFile(fname, os.O_RDWR, 0)
101 if err != nil {
102 return err
103 }
104
105 off, err := w.findSplitPoint(f)
106 if err != nil {
107 return err
108 }
109 if off == 0 {
110 // Nothing to do since f can't be split.
111 f.Close()
112 break
113 }
114
115 f, err = doSplit(f, off)
116 if err != nil {
117 return fmt.Errorf("splitWriter: doSplit: %v", err)
118 }
119
120 // Update for the next iteration
121 fname = f.Name()
122 cursize -= off
123 if err := f.Close(); err != nil {
124 return err
125 }
126 }
127 return nil
128}
129
130// findSplitPoint searches for a suitable split point in f, which is the offset
131// of the last line feed which is prior to the split size.

Callers

nothing calls this directly

Calls 3

findSplitPointMethod · 0.95
doSplitFunction · 0.85
CloseMethod · 0.65

Tested by

no test coverage detected