Skip to content

Commit 2441edf

Browse files
author
shin-
committed
Merged upstream changes in fs branch
2 parents 97a8209 + dd0227b commit 2441edf

12 files changed

Lines changed: 138 additions & 502 deletions

File tree

container.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package docker
22

33
import (
44
"./fs"
5+
"bytes"
56
"encoding/json"
67
"errors"
78
"github.com/kr/pty"
@@ -118,17 +119,25 @@ func createContainer(id string, root string, command string, args []string, imag
118119
return container, nil
119120
}
120121

121-
func loadContainer(containerPath string, netManager *NetworkManager) (*Container, error) {
122+
func loadContainer(store *fs.Store, containerPath string, netManager *NetworkManager) (*Container, error) {
122123
data, err := ioutil.ReadFile(path.Join(containerPath, "config.json"))
123124
if err != nil {
124125
return nil, err
125126
}
127+
mountpoint, err := store.FetchMountpoint(
128+
path.Join(containerPath, "rootfs"),
129+
path.Join(containerPath, "rw"),
130+
)
131+
if err != nil {
132+
return nil, err
133+
}
126134
container := &Container{
127135
stdout: newWriteBroadcaster(),
128136
stderr: newWriteBroadcaster(),
129137
lxcConfigPath: path.Join(containerPath, "config.lxc"),
130138
networkManager: netManager,
131139
NetworkSettings: &NetworkSettings{},
140+
Mountpoint: mountpoint,
132141
}
133142
// Load container settings
134143
if err := json.Unmarshal(data, container); err != nil {

docker.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,11 @@ func (docker *Docker) restore() error {
9494
return err
9595
}
9696
for _, v := range dir {
97-
container, err := loadContainer(path.Join(docker.repository, v.Name()), docker.networkManager)
97+
container, err := loadContainer(docker.Store, path.Join(docker.repository, v.Name()), docker.networkManager)
9898
if err != nil {
9999
log.Printf("Failed to load container %v: %v", v.Name(), err)
100100
continue
101101
}
102-
container.Mountpoint.Store = docker.Store
103102
docker.containers.PushBack(container)
104103
}
105104
return nil

dockerd/dockerd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package main
22

33
import (
4-
"flag"
54
".."
65
"../server"
6+
"flag"
77
"log"
88
)
99

fake/fake.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func FakeTar() (io.Reader, error) {
1313
content := []byte("Hello world!\n")
1414
buf := new(bytes.Buffer)
1515
tw := tar.NewWriter(buf)
16-
for _, name := range []string{"hello", "etc/postgres/postgres.conf", "etc/passwd", "var/log/postgres/postgres.conf"} {
16+
for _, name := range []string{"/etc/postgres/postgres.conf", "/etc/passwd", "/var/log/postgres/postgres.conf"} {
1717
hdr := new(tar.Header)
1818
hdr.Size = int64(len(content))
1919
hdr.Name = name

fs/changes.go

Lines changed: 106 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,129 @@
11
package fs
22

33
import (
4-
"fmt"
5-
"path/filepath"
6-
"os"
7-
"strings"
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"strings"
88
)
99

1010
type ChangeType int
1111

1212
const (
13-
ChangeModify = iota
14-
ChangeAdd
15-
ChangeDelete
13+
ChangeModify = iota
14+
ChangeAdd
15+
ChangeDelete
1616
)
1717

1818
type Change struct {
19-
Path string
20-
Kind ChangeType
19+
Path string
20+
Kind ChangeType
2121
}
2222

2323
func (change *Change) String() string {
24-
var kind string
25-
switch change.Kind {
26-
case ChangeModify:
27-
kind = "C"
28-
case ChangeAdd:
29-
kind = "A"
30-
case ChangeDelete:
31-
kind = "D"
32-
}
33-
return fmt.Sprintf("%s %s", kind, change.Path)
24+
var kind string
25+
switch change.Kind {
26+
case ChangeModify:
27+
kind = "C"
28+
case ChangeAdd:
29+
kind = "A"
30+
case ChangeDelete:
31+
kind = "D"
32+
}
33+
return fmt.Sprintf("%s %s", kind, change.Path)
3434
}
3535

3636
func (store *Store) Changes(mp *Mountpoint) ([]Change, error) {
37-
var changes []Change
38-
image, err := store.Get(mp.Image)
39-
if err != nil {
40-
return nil, err
41-
}
42-
layers, err := image.layers()
43-
if err != nil {
44-
return nil, err
45-
}
46-
47-
err = filepath.Walk(mp.Rw, func(path string, f os.FileInfo, err error) error {
48-
if err != nil {
49-
return err
50-
}
51-
52-
// Rebase path
53-
path, err = filepath.Rel(mp.Rw, path)
54-
if err != nil {
55-
return err
56-
}
57-
path = filepath.Join("/", path)
58-
59-
// Skip root
60-
if path == "/" {
61-
return nil
62-
}
63-
64-
// Skip AUFS metadata
65-
if matched, err := filepath.Match("/.wh..wh.*", path); err != nil || matched {
66-
return err
67-
}
68-
69-
change := Change{
70-
Path: path,
71-
}
72-
73-
// Find out what kind of modification happened
74-
file := filepath.Base(path)
75-
// If there is a whiteout, then the file was removed
76-
if strings.HasPrefix(file, ".wh.") {
77-
originalFile := strings.TrimLeft(file, ".wh.")
78-
change.Path = filepath.Join(filepath.Dir(path), originalFile)
79-
change.Kind = ChangeDelete
80-
} else {
81-
// Otherwise, the file was added
82-
change.Kind = ChangeAdd
83-
84-
// ...Unless it already existed in a top layer, in which case, it's a modification
85-
for _, layer := range layers {
86-
stat, err := os.Stat(filepath.Join(layer, path))
87-
if err != nil && !os.IsNotExist(err) {
88-
return err
89-
}
90-
if err == nil {
91-
// The file existed in the top layer, so that's a modification
92-
93-
// However, if it's a directory, maybe it wasn't actually modified.
94-
// If you modify /foo/bar/baz, then /foo will be part of the changed files only because it's the parent of bar
95-
if stat.IsDir() && f.IsDir() {
96-
if f.Size() == stat.Size() && f.Mode() == stat.Mode() && f.ModTime() == stat.ModTime() {
97-
// Both directories are the same, don't record the change
98-
return nil
99-
}
100-
}
101-
change.Kind = ChangeModify
102-
break
103-
}
104-
}
105-
}
106-
107-
// Record change
108-
changes = append(changes, change)
109-
return nil
110-
})
111-
if err != nil {
112-
return nil, err
113-
}
114-
return changes, nil
37+
var changes []Change
38+
image, err := store.Get(mp.Image)
39+
if err != nil {
40+
return nil, err
41+
}
42+
layers, err := image.layers()
43+
if err != nil {
44+
return nil, err
45+
}
46+
47+
err = filepath.Walk(mp.Rw, func(path string, f os.FileInfo, err error) error {
48+
if err != nil {
49+
return err
50+
}
51+
52+
// Rebase path
53+
path, err = filepath.Rel(mp.Rw, path)
54+
if err != nil {
55+
return err
56+
}
57+
path = filepath.Join("/", path)
58+
59+
// Skip root
60+
if path == "/" {
61+
return nil
62+
}
63+
64+
// Skip AUFS metadata
65+
if matched, err := filepath.Match("/.wh..wh.*", path); err != nil || matched {
66+
return err
67+
}
68+
69+
change := Change{
70+
Path: path,
71+
}
72+
73+
// Find out what kind of modification happened
74+
file := filepath.Base(path)
75+
// If there is a whiteout, then the file was removed
76+
if strings.HasPrefix(file, ".wh.") {
77+
originalFile := strings.TrimLeft(file, ".wh.")
78+
change.Path = filepath.Join(filepath.Dir(path), originalFile)
79+
change.Kind = ChangeDelete
80+
} else {
81+
// Otherwise, the file was added
82+
change.Kind = ChangeAdd
83+
84+
// ...Unless it already existed in a top layer, in which case, it's a modification
85+
for _, layer := range layers {
86+
stat, err := os.Stat(filepath.Join(layer, path))
87+
if err != nil && !os.IsNotExist(err) {
88+
return err
89+
}
90+
if err == nil {
91+
// The file existed in the top layer, so that's a modification
92+
93+
// However, if it's a directory, maybe it wasn't actually modified.
94+
// If you modify /foo/bar/baz, then /foo will be part of the changed files only because it's the parent of bar
95+
if stat.IsDir() && f.IsDir() {
96+
if f.Size() == stat.Size() && f.Mode() == stat.Mode() && f.ModTime() == stat.ModTime() {
97+
// Both directories are the same, don't record the change
98+
return nil
99+
}
100+
}
101+
change.Kind = ChangeModify
102+
break
103+
}
104+
}
105+
}
106+
107+
// Record change
108+
changes = append(changes, change)
109+
return nil
110+
})
111+
if err != nil {
112+
return nil, err
113+
}
114+
return changes, nil
115115
}
116116

117117
// Reset removes all changes to the filesystem, reverting it to its initial state.
118118
func (mp *Mountpoint) Reset() error {
119-
if err := os.RemoveAll(mp.Rw); err != nil {
120-
return err
121-
}
122-
// We removed the RW directory itself along with its content: let's re-create an empty one.
123-
if err := mp.createFolders(); err != nil {
124-
return err
125-
}
126-
return nil
119+
if err := os.RemoveAll(mp.Rw); err != nil {
120+
return err
121+
}
122+
// We removed the RW directory itself along with its content: let's re-create an empty one.
123+
if err := mp.createFolders(); err != nil {
124+
return err
125+
}
126+
return nil
127127
}
128128

129129
// Open opens the named file for reading.
@@ -141,4 +141,4 @@ func (mp *Mountpoint) Reset() error {
141141
// return nil, err
142142
// }
143143
// return ioutil.ReadDir(filepath.Join(fs.RootFS, dirname))
144-
// }
144+
// }

fs/mount_linux.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package fs
22

33
import "syscall"
44

5-
65
func mount(source string, target string, fstype string, flags uintptr, data string) (err error) {
76
return syscall.Mount(source, target, fstype, flags, data)
87
}

fs/store.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"database/sql"
55
"errors"
66
"fmt"
7-
"github.com/coopernurse/gorp"
87
"github.com/dotcloud/docker/future"
98
_ "github.com/mattn/go-sqlite3"
9+
"github.com/shykes/gorp" //Forked to implement CreateTablesOpts
1010
"io"
1111
"os"
1212
"path"
@@ -29,8 +29,6 @@ func New(root string) (*Store, error) {
2929

3030
if err := os.Mkdir(root, 0700); err != nil && !os.IsExist(err) {
3131
return nil, err
32-
} else if os.IsExist(err) {
33-
isNewStore = false
3432
}
3533
db, err := sql.Open("sqlite3", path.Join(root, "db"))
3634
if err != nil {
@@ -42,7 +40,7 @@ func New(root string) (*Store, error) {
4240
orm.AddTableWithName(Mountpoint{}, "mountpoints").SetKeys(false, "Root")
4341
orm.AddTableWithName(Tag{}, "tags").SetKeys(false, "TagName")
4442
if isNewStore {
45-
if err := orm.CreateTables(); err != nil {
43+
if err := orm.CreateTablesOpts(true); err != nil {
4644
return nil, err
4745
}
4846
}
@@ -227,7 +225,7 @@ func (image *Image) Mountpoints() ([]*Mountpoint, error) {
227225

228226
func (image *Image) Mount(root, rw string) (*Mountpoint, error) {
229227
var mountpoint *Mountpoint
230-
if mp, err := image.fetchMountpoint(root, rw); err != nil {
228+
if mp, err := image.store.FetchMountpoint(root, rw); err != nil {
231229
return nil, err
232230
} else if mp == nil {
233231
mountpoint, err = image.Mountpoint(root, rw)
@@ -345,16 +343,16 @@ func (mp *Mountpoint) Deregister() error {
345343
return err
346344
}
347345

348-
func (image *Image) fetchMountpoint(root, rw string) (*Mountpoint, error) {
349-
res, err := image.store.orm.Select(Mountpoint{}, "select * from mountpoints where Image=? and Root=? and Rw=?", image.Id, root, rw)
346+
func (store *Store) FetchMountpoint(root, rw string) (*Mountpoint, error) {
347+
res, err := store.orm.Select(Mountpoint{}, "select * from mountpoints where Root=? and Rw=?", root, rw)
350348
if err != nil {
351349
return nil, err
352350
} else if len(res) < 1 || res[0] == nil {
353351
return nil, nil
354352
}
355353

356354
mp := res[0].(*Mountpoint)
357-
mp.Store = image.store
355+
mp.Store = store
358356
return mp, nil
359357
}
360358

fs/store_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package fs
22

33
import (
4+
"../fake"
45
"errors"
56
"fmt"
6-
"../fake"
77
"io/ioutil"
88
"os"
99
"testing"

0 commit comments

Comments
 (0)