Browse code

Remove os from devmapper

Guillaume J. Charmes authored on 2013/11/21 06:05:17
Showing 6 changed files
... ...
@@ -6,7 +6,6 @@ import (
6 6
 	"github.com/dotcloud/docker/utils"
7 7
 	"io"
8 8
 	"io/ioutil"
9
-	"os"
10 9
 	"os/exec"
11 10
 	"path"
12 11
 	"path/filepath"
... ...
@@ -104,7 +103,7 @@ func (devices *DeviceSet) hasImage(name string) bool {
104 104
 	dirname := devices.loopbackDir()
105 105
 	filename := path.Join(dirname, name)
106 106
 
107
-	_, err := os.Stat(filename)
107
+	_, err := osStat(filename)
108 108
 	return err == nil
109 109
 }
110 110
 
... ...
@@ -116,16 +115,16 @@ func (devices *DeviceSet) ensureImage(name string, size int64) (string, error) {
116 116
 	dirname := devices.loopbackDir()
117 117
 	filename := path.Join(dirname, name)
118 118
 
119
-	if err := os.MkdirAll(dirname, 0700); err != nil && !os.IsExist(err) {
119
+	if err := osMkdirAll(dirname, 0700); err != nil && !osIsExist(err) {
120 120
 		return "", err
121 121
 	}
122 122
 
123
-	if _, err := os.Stat(filename); err != nil {
124
-		if !os.IsNotExist(err) {
123
+	if _, err := osStat(filename); err != nil {
124
+		if !osIsNotExist(err) {
125 125
 			return "", err
126 126
 		}
127 127
 		utils.Debugf("Creating loopback file %s for device-manage use", filename)
128
-		file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0600)
128
+		file, err := osOpenFile(filename, osORdWr|osOCreate, 0600)
129 129
 		if err != nil {
130 130
 			return "", err
131 131
 		}
... ...
@@ -173,7 +172,7 @@ func (devices *DeviceSet) saveMetadata() error {
173 173
 	if err := tmpFile.Close(); err != nil {
174 174
 		return fmt.Errorf("Error closing metadata file %s: %s", tmpFile.Name(), err)
175 175
 	}
176
-	if err := os.Rename(tmpFile.Name(), devices.jsonFile()); err != nil {
176
+	if err := osRename(tmpFile.Name(), devices.jsonFile()); err != nil {
177 177
 		return fmt.Errorf("Error committing metadata file", err)
178 178
 	}
179 179
 
... ...
@@ -251,7 +250,7 @@ func (devices *DeviceSet) loadMetaData() error {
251 251
 	devices.NewTransactionId = devices.TransactionId
252 252
 
253 253
 	jsonData, err := ioutil.ReadFile(devices.jsonFile())
254
-	if err != nil && !os.IsNotExist(err) {
254
+	if err != nil && !osIsNotExist(err) {
255 255
 		utils.Debugf("\n--->Err: %s\n", err)
256 256
 		return err
257 257
 	}
... ...
@@ -336,10 +335,9 @@ func (devices *DeviceSet) setupBaseImage() error {
336 336
 }
337 337
 
338 338
 func setCloseOnExec(name string) {
339
-	fileInfos, _ := ioutil.ReadDir("/proc/self/fd")
340
-	if fileInfos != nil {
339
+	if fileInfos, _ := ioutil.ReadDir("/proc/self/fd"); fileInfos != nil {
341 340
 		for _, i := range fileInfos {
342
-			link, _ := os.Readlink(filepath.Join("/proc/self/fd", i.Name()))
341
+			link, _ := osReadlink(filepath.Join("/proc/self/fd", i.Name()))
343 342
 			if link == name {
344 343
 				fd, err := strconv.Atoi(i.Name())
345 344
 				if err == nil {
... ...
@@ -371,7 +369,7 @@ func (devices *DeviceSet) ResizePool(size int64) error {
371 371
 	datafilename := path.Join(dirname, "data")
372 372
 	metadatafilename := path.Join(dirname, "metadata")
373 373
 
374
-	datafile, err := os.OpenFile(datafilename, os.O_RDWR, 0)
374
+	datafile, err := osOpenFile(datafilename, osORdWr, 0)
375 375
 	if datafile == nil {
376 376
 		return err
377 377
 	}
... ...
@@ -386,19 +384,19 @@ func (devices *DeviceSet) ResizePool(size int64) error {
386 386
 		return fmt.Errorf("Can't shrink file")
387 387
 	}
388 388
 
389
-	dataloopback := FindLoopDeviceFor(datafile)
389
+	dataloopback := FindLoopDeviceFor(&osFile{File: datafile})
390 390
 	if dataloopback == nil {
391 391
 		return fmt.Errorf("Unable to find loopback mount for: %s", datafilename)
392 392
 	}
393 393
 	defer dataloopback.Close()
394 394
 
395
-	metadatafile, err := os.OpenFile(metadatafilename, os.O_RDWR, 0)
395
+	metadatafile, err := osOpenFile(metadatafilename, osORdWr, 0)
396 396
 	if metadatafile == nil {
397 397
 		return err
398 398
 	}
399 399
 	defer metadatafile.Close()
400 400
 
401
-	metadataloopback := FindLoopDeviceFor(metadatafile)
401
+	metadataloopback := FindLoopDeviceFor(&osFile{File: metadatafile})
402 402
 	if metadataloopback == nil {
403 403
 		return fmt.Errorf("Unable to find loopback mount for: %s", metadatafilename)
404 404
 	}
... ...
@@ -463,7 +461,7 @@ func (devices *DeviceSet) initDevmapper(doInit bool) error {
463 463
 
464 464
 	// Set the device prefix from the device id and inode of the docker root dir
465 465
 
466
-	st, err := os.Stat(devices.root)
466
+	st, err := osStat(devices.root)
467 467
 	if err != nil {
468 468
 		return fmt.Errorf("Error looking up dir %s: %s", devices.root, err)
469 469
 	}
... ...
@@ -4,7 +4,6 @@ import (
4 4
 	"errors"
5 5
 	"fmt"
6 6
 	"github.com/dotcloud/docker/utils"
7
-	"os"
8 7
 	"runtime"
9 8
 )
10 9
 
... ...
@@ -179,16 +178,16 @@ func (t *Task) GetNextTarget(next uintptr) (nextPtr uintptr, start uint64,
179 179
 		start, length, targetType, params
180 180
 }
181 181
 
182
-func AttachLoopDevice(filename string) (*os.File, error) {
182
+func AttachLoopDevice(filename string) (*osFile, error) {
183 183
 	var fd int
184 184
 	res := DmAttachLoopDevice(filename, &fd)
185 185
 	if res == "" {
186 186
 		return nil, ErrAttachLoopbackDevice
187 187
 	}
188
-	return os.NewFile(uintptr(fd), res), nil
188
+	return &osFile{File: osNewFile(uintptr(fd), res)}, nil
189 189
 }
190 190
 
191
-func getLoopbackBackingFile(file *os.File) (uint64, uint64, error) {
191
+func getLoopbackBackingFile(file *osFile) (uint64, uint64, error) {
192 192
 	dev, inode, err := dmGetLoopbackBackingFile(file.Fd())
193 193
 	if err != 0 {
194 194
 		return 0, 0, ErrGetLoopbackBackingFile
... ...
@@ -196,7 +195,7 @@ func getLoopbackBackingFile(file *os.File) (uint64, uint64, error) {
196 196
 	return dev, inode, nil
197 197
 }
198 198
 
199
-func LoopbackSetCapacity(file *os.File) error {
199
+func LoopbackSetCapacity(file *osFile) error {
200 200
 	err := dmLoopbackSetCapacity(file.Fd())
201 201
 	if err != 0 {
202 202
 		return ErrLoopbackSetCapacity
... ...
@@ -204,7 +203,7 @@ func LoopbackSetCapacity(file *os.File) error {
204 204
 	return nil
205 205
 }
206 206
 
207
-func FindLoopDeviceFor(file *os.File) *os.File {
207
+func FindLoopDeviceFor(file *osFile) *osFile {
208 208
 	stat, err := file.Stat()
209 209
 	if err != nil {
210 210
 		return nil
... ...
@@ -215,9 +214,9 @@ func FindLoopDeviceFor(file *os.File) *os.File {
215 215
 	for i := 0; true; i++ {
216 216
 		path := fmt.Sprintf("/dev/loop%d", i)
217 217
 
218
-		file, err := osOpenFile(path, os.O_RDWR, 0)
218
+		file, err := osOpenFile(path, osORdWr, 0)
219 219
 		if err != nil {
220
-			if os.IsNotExist(err) {
220
+			if osIsNotExist(err) {
221 221
 				return nil
222 222
 			}
223 223
 
... ...
@@ -226,9 +225,9 @@ func FindLoopDeviceFor(file *os.File) *os.File {
226 226
 			continue
227 227
 		}
228 228
 
229
-		dev, inode, err := getLoopbackBackingFile(file)
229
+		dev, inode, err := getLoopbackBackingFile(&osFile{File: file})
230 230
 		if err == nil && dev == targetDevice && inode == targetInode {
231
-			return file
231
+			return &osFile{File: file}
232 232
 		}
233 233
 
234 234
 		file.Close()
... ...
@@ -288,7 +287,7 @@ func RemoveDevice(name string) error {
288 288
 	return nil
289 289
 }
290 290
 
291
-func GetBlockDeviceSize(file *os.File) (uint64, error) {
291
+func GetBlockDeviceSize(file *osFile) (uint64, error) {
292 292
 	size, errno := DmGetBlockSize(file.Fd())
293 293
 	if size == -1 || errno != 0 {
294 294
 		return 0, ErrGetBlockSize
... ...
@@ -297,7 +296,7 @@ func GetBlockDeviceSize(file *os.File) (uint64, error) {
297 297
 }
298 298
 
299 299
 // This is the programmatic example of "dmsetup create"
300
-func createPool(poolName string, dataFile *os.File, metadataFile *os.File) error {
300
+func createPool(poolName string, dataFile, metadataFile *osFile) error {
301 301
 	task, err := createTask(DeviceCreate, poolName)
302 302
 	if task == nil {
303 303
 		return err
... ...
@@ -327,7 +326,7 @@ func createPool(poolName string, dataFile *os.File, metadataFile *os.File) error
327 327
 	return nil
328 328
 }
329 329
 
330
-func reloadPool(poolName string, dataFile *os.File, metadataFile *os.File) error {
330
+func reloadPool(poolName string, dataFile, metadataFile *osFile) error {
331 331
 	task, err := createTask(DeviceReload, poolName)
332 332
 	if task == nil {
333 333
 		return err
... ...
@@ -4,7 +4,6 @@ import (
4 4
 	"fmt"
5 5
 	"github.com/dotcloud/docker/graphdriver"
6 6
 	"io/ioutil"
7
-	"os"
8 7
 	"path"
9 8
 )
10 9
 
... ...
@@ -67,7 +66,7 @@ func (d *Driver) Create(id string, parent string) error {
67 67
 		return err
68 68
 	}
69 69
 
70
-	if err := os.MkdirAll(path.Join(mp, "rootfs"), 0755); err != nil && !os.IsExist(err) {
70
+	if err := osMkdirAll(path.Join(mp, "rootfs"), 0755); err != nil && !osIsExist(err) {
71 71
 		return err
72 72
 	}
73 73
 
... ...
@@ -98,7 +97,7 @@ func (d *Driver) Get(id string) (string, error) {
98 98
 
99 99
 func (d *Driver) mount(id, mountPoint string) error {
100 100
 	// Create the target directories if they don't exist
101
-	if err := os.MkdirAll(mountPoint, 0755); err != nil && !os.IsExist(err) {
101
+	if err := osMkdirAll(mountPoint, 0755); err != nil && !osIsExist(err) {
102 102
 		return err
103 103
 	}
104 104
 	// If mountpoint is already mounted, do nothing
... ...
@@ -2,7 +2,6 @@ package devmapper
2 2
 
3 3
 import (
4 4
 	"io/ioutil"
5
-	"os"
6 5
 	"path"
7 6
 	"testing"
8 7
 )
... ...
@@ -34,12 +33,12 @@ func newDriver(t *testing.T) *Driver {
34 34
 
35 35
 func cleanup(d *Driver) {
36 36
 	d.Cleanup()
37
-	os.RemoveAll(d.home)
37
+	osRemoveAll(d.home)
38 38
 }
39 39
 
40 40
 func TestInit(t *testing.T) {
41 41
 	home := mkTestDirectory(t)
42
-	defer os.RemoveAll(home)
42
+	defer osRemoveAll(home)
43 43
 	driver, err := Init(home)
44 44
 	if err != nil {
45 45
 		t.Fatal(err)
... ...
@@ -58,7 +57,7 @@ func TestInit(t *testing.T) {
58 58
 	if err != nil {
59 59
 		t.Fatal(err)
60 60
 	}
61
-	if st, err := os.Stat(dir); err != nil {
61
+	if st, err := osStat(dir); err != nil {
62 62
 		t.Fatal(err)
63 63
 	} else if !st.IsDir() {
64 64
 		t.Fatalf("Get(%V) did not return a directory", id)
... ...
@@ -99,7 +98,7 @@ func TestDriverRemove(t *testing.T) {
99 99
 func TestCleanup(t *testing.T) {
100 100
 	t.Skip("Unimplemented")
101 101
 	d := newDriver(t)
102
-	defer os.RemoveAll(d.home)
102
+	defer osRemoveAll(d.home)
103 103
 
104 104
 	mountPoints := make([]string, 2)
105 105
 
... ...
@@ -284,7 +283,7 @@ func TestDriverGetSize(t *testing.T) {
284 284
 
285 285
 	size := int64(1024)
286 286
 
287
-	f, err := os.Create(path.Join(mountPoint, "test_file"))
287
+	f, err := osCreate(path.Join(mountPoint, "test_file"))
288 288
 	if err != nil {
289 289
 		t.Fatal(err)
290 290
 	}
... ...
@@ -1,7 +1,6 @@
1 1
 package devmapper
2 2
 
3 3
 import (
4
-	"os"
5 4
 	"path/filepath"
6 5
 )
7 6
 
... ...
@@ -9,14 +8,14 @@ import (
9 9
 // It should be moved into the core.
10 10
 
11 11
 func Mounted(mountpoint string) (bool, error) {
12
-	mntpoint, err := os.Stat(mountpoint)
12
+	mntpoint, err := osStat(mountpoint)
13 13
 	if err != nil {
14
-		if os.IsNotExist(err) {
14
+		if osIsNotExist(err) {
15 15
 			return false, nil
16 16
 		}
17 17
 		return false, err
18 18
 	}
19
-	parent, err := os.Stat(filepath.Join(mountpoint, ".."))
19
+	parent, err := osStat(filepath.Join(mountpoint, ".."))
20 20
 	if err != nil {
21 21
 		return false, err
22 22
 	}
... ...
@@ -8,15 +8,26 @@ import (
8 8
 type (
9 9
 	sysStatT syscall.Stat_t
10 10
 	sysErrno syscall.Errno
11
+
12
+	osFile struct{ *os.File }
11 13
 )
12 14
 
13 15
 var (
14
-	// functions
15 16
 	sysMount       = syscall.Mount
16 17
 	sysUnmount     = syscall.Unmount
17 18
 	sysCloseOnExec = syscall.CloseOnExec
18 19
 	sysSyscall     = syscall.Syscall
19
-	osOpenFile     = os.OpenFile
20
+
21
+	osOpenFile   = os.OpenFile
22
+	osNewFile    = os.NewFile
23
+	osCreate     = os.Create
24
+	osStat       = os.Stat
25
+	osIsNotExist = os.IsNotExist
26
+	osIsExist    = os.IsExist
27
+	osMkdirAll   = os.MkdirAll
28
+	osRemoveAll  = os.RemoveAll
29
+	osRename     = os.Rename
30
+	osReadlink   = os.Readlink
20 31
 )
21 32
 
22 33
 const (
... ...
@@ -24,6 +35,9 @@ const (
24 24
 	sysMsRdOnly = syscall.MS_RDONLY
25 25
 	sysEInval   = syscall.EINVAL
26 26
 	sysSysIoctl = syscall.SYS_IOCTL
27
+
28
+	osORdWr   = os.O_RDWR
29
+	osOCreate = os.O_CREATE
27 30
 )
28 31
 
29 32
 func toSysStatT(i interface{}) *sysStatT {