aboutsummaryrefslogtreecommitdiff
path: root/backup/restore.go
blob: 3a384a9dfd57ddcef06b4dff4b0ea21fc509b9a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package backup

import (
	"code.laria.me/petrific/acl"
	"code.laria.me/petrific/fs"
	"code.laria.me/petrific/objects"
	"code.laria.me/petrific/storage"
	"fmt"
	"io"
	"math/rand"
	"os"
)

func RestoreFile(s storage.Storage, id objects.ObjectId, w io.Writer) error {
	file, err := storage.GetObjectOfType(s, id, objects.OTFile)
	if err != nil {
		return err
	}

	for i, fragment := range *file.(*objects.File) {
		blob_obj, err := storage.GetObjectOfType(s, fragment.Blob, objects.OTBlob)
		if err != nil {
			return err
		}
		blob := *blob_obj.(*objects.Blob)

		if uint64(len(blob)) != fragment.Size {
			return fmt.Errorf("RestoreFile: blob size of %s doesn't match size in fragment %d of file %s", fragment.Blob, i, id)
		}

		if _, err := w.Write(blob); err != nil {
			return err
		}
	}

	return nil
}

func execBitFromACL(a acl.ACL) bool {
	return a.ToUnixPerms()&0100 != 0
}

func RestoreDir(s storage.Storage, id objects.ObjectId, root fs.Dir) error {
	tree_obj, err := storage.GetObjectOfType(s, id, objects.OTTree)
	tree := tree_obj.(objects.Tree)

	seen := make(map[string]struct{})

	for name, file_info := range tree {
		switch file_info.Type() {
		case objects.TETFile:
			tmpname := fmt.Sprintf(".petrific-%d-%08x%08x%08x%08x", os.Getpid(), rand.Uint32(), rand.Uint32(), rand.Uint32(), rand.Uint32())
			new_file, err := root.CreateChildFile(tmpname, execBitFromACL(file_info.ACL()))
			if err != nil {
				return err
			}

			wc, err := new_file.OpenWritable()
			if err != nil {
				return err
			}

			if err := RestoreFile(s, file_info.(objects.TreeEntryFile).Ref, wc); err != nil {
				wc.Close()
				return err
			}
			wc.Close()

			if err := root.RenameChild(tmpname, name); err != nil {
				return err
			}
		case objects.TETDir:
			var subdir fs.Dir

			// Try to use existing directory
			child, err := root.GetChild(name)
			if err == nil {
				if child.Type() == fs.FDir {
					subdir = child.(fs.Dir)
				} else {
					if err := child.Delete(); err != nil {
						return err
					}
				}
			} else if err != os.ErrNotExist {
				return err
			}

			// Create directory, if it doesn't exist
			if subdir == nil {
				subdir, err = root.CreateChildDir(name)
				if err != nil {
					return err
				}
			}

			if err := RestoreDir(s, file_info.(objects.TreeEntryDir).Ref, subdir); err != nil {
				return err
			}
		case objects.TETSymlink:
			// Is there already a child of that name? If yes, delete it
			child, err := root.GetChild(name)
			if err == nil {
				if err := child.Delete(); err != nil {
					return err
				}
			} else if err != os.ErrNotExist {
				return err
			}

			if _, err := root.CreateChildSymlink(name, file_info.(objects.TreeEntrySymlink).Target); err != nil {
				return err
			}
		default:
			return fmt.Errorf("child '%s' of %s has unknown tree entry type %s", name, id, file_info.Type())
		}

		seen[name] = struct{}{}
	}

	// We now restored all children, we now need to remove the children of root, that shouldn't be there accoring to the backup
	children, err := root.Readdir()
	if err != nil {
		return err
	}
	for _, c := range children {
		_, ok := seen[c.Name()]
		if !ok {
			if err := c.Delete(); err != nil {
				return err
			}
		}
	}

	return nil
}