@@ -42,27 +42,23 @@ type Entry struct {
4242//
4343// Create can return errors from the os package.
4444func Create (options * CreateOptions ) (* Entry , error ) {
45+ o , err := getValidOptions (options )
46+ if err != nil {
47+ return nil , err
48+ }
49+
4550 rp := & readerProxy {inner : options .Data , h : sha256 .New ()}
4651 // Use the proxy instead of the raw Reader.
47- optsCopy := * options
48- optsCopy .Data = rp
49- o := & optsCopy
52+ o .Data = rp
5053
51- if o .Root == "" {
52- return nil , fmt .Errorf ("internal error: CreateOptions.Root is unset" )
53- }
54- if o .Root != "/" {
55- o .Root = filepath .Clean (o .Root ) + "/"
56- }
5754 path , err := absPath (o .Root , o .Path )
5855 if err != nil {
5956 return nil , err
6057 }
61- o .Path = path
6258
6359 var hash string
6460 if o .MakeParents {
65- if err := os .MkdirAll (filepath .Dir (o . Path ), 0755 ); err != nil {
61+ if err := os .MkdirAll (filepath .Dir (path ), 0755 ); err != nil {
6662 return nil , err
6763 }
6864 }
@@ -73,7 +69,7 @@ func Create(options *CreateOptions) (*Entry, error) {
7369 o .Link = filepath .Clean (o .Link )
7470 if filepath .IsAbs (o .Link ) {
7571 if ! strings .HasPrefix (filepath .Clean (o .Link ), o .Root ) {
76- return nil , fmt .Errorf ("invalid hardlink %s target: %s is outside of root %s" , o . Path , o .Link , o .Root )
72+ return nil , fmt .Errorf ("invalid hardlink %s target: %s is outside of root %s" , path , o .Link , o .Root )
7773 }
7874 }
7975 err = createHardLink (o )
@@ -86,14 +82,14 @@ func Create(options *CreateOptions) (*Entry, error) {
8682 case fs .ModeSymlink :
8783 err = createSymlink (o )
8884 default :
89- err = fmt .Errorf ("unsupported file type: %s" , o . Path )
85+ err = fmt .Errorf ("unsupported file type: %s" , path )
9086 }
9187 if err != nil {
9288 return nil , err
9389 }
9490
9591 // Entry should describe the created file, not the target the link points to.
96- s , err := os .Lstat (o . Path )
92+ s , err := os .Lstat (path )
9793 if err != nil {
9894 return nil , err
9995 }
@@ -107,15 +103,15 @@ func Create(options *CreateOptions) (*Entry, error) {
107103 mode = mode &^ fs .ModeSymlink
108104 }
109105 } else if o .OverrideMode && mode != o .Mode {
110- err := os .Chmod (o . Path , o .Mode )
106+ err := os .Chmod (path , o .Mode )
111107 if err != nil {
112108 return nil , err
113109 }
114110 mode = o .Mode
115111 }
116112
117113 entry := & Entry {
118- Path : o . Path ,
114+ Path : path ,
119115 Mode : mode ,
120116 SHA256 : hash ,
121117 Size : rp .size ,
@@ -128,36 +124,31 @@ func Create(options *CreateOptions) (*Entry, error) {
128124// information recorded in Entry. The Hash and Size attributes are set on
129125// calling Close() on the Writer.
130126func CreateWriter (options * CreateOptions ) (io.WriteCloser , * Entry , error ) {
131- optsCopy := * options
132- o := & optsCopy
133-
134- if o .Root == "" {
135- return nil , nil , fmt .Errorf ("internal error: CreateOptions.Root is unset" )
136- }
137- if o .Root != "/" {
138- o .Root = filepath .Clean (o .Root ) + "/"
127+ o , err := getValidOptions (options )
128+ if err != nil {
129+ return nil , nil , err
139130 }
131+
140132 path , err := absPath (o .Root , o .Path )
141133 if err != nil {
142134 return nil , nil , err
143135 }
144- o .Path = path
145136
146137 if ! o .Mode .IsRegular () {
147- return nil , nil , fmt .Errorf ("unsupported file type: %s" , o . Path )
138+ return nil , nil , fmt .Errorf ("unsupported file type: %s" , path )
148139 }
149140 if o .MakeParents {
150141 if err := os .MkdirAll (filepath .Dir (path ), 0755 ); err != nil {
151142 return nil , nil , err
152143 }
153144 }
154145
155- file , err := os .OpenFile (o . Path , os .O_CREATE | os .O_TRUNC | os .O_WRONLY , o .Mode )
146+ file , err := os .OpenFile (path , os .O_CREATE | os .O_TRUNC | os .O_WRONLY , o .Mode )
156147 if err != nil {
157148 return nil , nil , err
158149 }
159150 entry := & Entry {
160- Path : o . Path ,
151+ Path : path ,
161152 Mode : o .Mode ,
162153 }
163154 wp := & writerProxy {
@@ -171,7 +162,11 @@ func CreateWriter(options *CreateOptions) (io.WriteCloser, *Entry, error) {
171162
172163func createDir (o * CreateOptions ) error {
173164 debugf ("Creating directory: %s (mode %#o)" , o .Path , o .Mode )
174- err := os .Mkdir (o .Path , o .Mode )
165+ path , err := absPath (o .Root , o .Path )
166+ if err != nil {
167+ return err
168+ }
169+ err = os .Mkdir (path , o .Mode )
175170 if os .IsExist (err ) {
176171 return nil
177172 }
@@ -180,7 +175,11 @@ func createDir(o *CreateOptions) error {
180175
181176func createFile (o * CreateOptions ) error {
182177 debugf ("Writing file: %s (mode %#o)" , o .Path , o .Mode )
183- file , err := os .OpenFile (o .Path , os .O_CREATE | os .O_TRUNC | os .O_WRONLY , o .Mode )
178+ path , err := absPath (o .Root , o .Path )
179+ if err != nil {
180+ return err
181+ }
182+ file , err := os .OpenFile (path , os .O_CREATE | os .O_TRUNC | os .O_WRONLY , o .Mode )
184183 if err != nil {
185184 return err
186185 }
@@ -194,36 +193,44 @@ func createFile(o *CreateOptions) error {
194193
195194func createSymlink (o * CreateOptions ) error {
196195 debugf ("Creating symlink: %s => %s" , o .Path , o .Link )
197- fileinfo , err := os .Lstat (o .Path )
196+ path , err := absPath (o .Root , o .Path )
197+ if err != nil {
198+ return err
199+ }
200+ fileinfo , err := os .Lstat (path )
198201 if err == nil {
199202 if (fileinfo .Mode () & os .ModeSymlink ) != 0 {
200- link , err := os .Readlink (o . Path )
203+ link , err := os .Readlink (path )
201204 if err != nil {
202205 return err
203206 }
204207 if link == o .Link {
205208 return nil
206209 }
207210 }
208- err = os .Remove (o . Path )
211+ err = os .Remove (path )
209212 if err != nil {
210213 return err
211214 }
212215 } else if ! os .IsNotExist (err ) {
213216 return err
214217 }
215- return os .Symlink (o .Link , o . Path )
218+ return os .Symlink (o .Link , path )
216219}
217220
218221func createHardLink (o * CreateOptions ) error {
219222 debugf ("Creating hard link: %s => %s" , o .Path , o .Link )
220- err := os .Link (o .Link , o .Path )
223+ path , err := absPath (o .Root , o .Path )
224+ if err != nil {
225+ return err
226+ }
227+ err = os .Link (o .Link , path )
221228 if err != nil && os .IsExist (err ) {
222229 linkInfo , serr := os .Lstat (o .Link )
223230 if serr != nil {
224231 return serr
225232 }
226- pathInfo , serr := os .Lstat (o . Path )
233+ pathInfo , serr := os .Lstat (path )
227234 if serr != nil {
228235 return serr
229236 }
@@ -234,6 +241,18 @@ func createHardLink(o *CreateOptions) error {
234241 return err
235242}
236243
244+ func getValidOptions (options * CreateOptions ) (* CreateOptions , error ) {
245+ optsCopy := * options
246+ o := & optsCopy
247+ if o .Root == "" {
248+ return nil , fmt .Errorf ("internal error: CreateOptions.Root is unset" )
249+ }
250+ if o .Root != "/" {
251+ o .Root = filepath .Clean (o .Root ) + "/"
252+ }
253+ return o , nil
254+ }
255+
237256// absPath requires root to be a clean path that ends in "/".
238257func absPath (root , relPath string ) (string , error ) {
239258 path := filepath .Clean (filepath .Join (root , relPath ))
0 commit comments