forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs_unix.go
286 lines (249 loc) · 7.75 KB
/
fs_unix.go
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright 2013-2017 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !plan9
package cpio
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"syscall"
"time"
"github.com/u-root/u-root/pkg/ls"
"github.com/u-root/u-root/pkg/uio"
"golang.org/x/sys/unix"
)
var modeMap = map[uint64]os.FileMode{
modeSocket: os.ModeSocket,
modeSymlink: os.ModeSymlink,
modeFile: 0,
modeBlock: os.ModeDevice,
modeDir: os.ModeDir,
modeChar: os.ModeCharDevice,
modeFIFO: os.ModeNamedPipe,
}
// setModes sets the modes, changing the easy ones first and the harder ones last.
// In this way, we set as much as we can before bailing out.
// N.B.: if you set something with S_ISUID, then change the owner,
// the kernel (Linux, OSX, etc.) clears S_ISUID (a good idea). So, the simple thing:
// Do the chmod operations in order of difficulty, and give up as soon as we fail.
// Set the basic permissions -- not including SUID, GUID, etc.
// Set the times
// Set the owner
// Set ALL the mode bits, in case we need to do SUID, etc. If we could not
// set the owner, we won't even try this operation of course, so we won't
// have SUID incorrectly set for the wrong user.
func setModes(r Record) error {
if err := os.Chmod(r.Name, toFileMode(r)&os.ModePerm); err != nil {
return err
}
/*if err := os.Chtimes(r.Name, time.Time{}, time.Unix(int64(r.MTime), 0)); err != nil {
return err
}*/
if err := os.Chown(r.Name, int(r.UID), int(r.GID)); err != nil {
return err
}
if err := os.Chmod(r.Name, toFileMode(r)); err != nil {
return err
}
return nil
}
func toFileMode(r Record) os.FileMode {
m := os.FileMode(perm(r))
if r.Mode&unix.S_ISUID != 0 {
m |= os.ModeSetuid
}
if r.Mode&unix.S_ISGID != 0 {
m |= os.ModeSetgid
}
if r.Mode&unix.S_ISVTX != 0 {
m |= os.ModeSticky
}
return m
}
func perm(r Record) uint32 {
return uint32(r.Mode) & modePermissions
}
func dev(r Record) int {
return int(r.Rmajor<<8 | r.Rminor)
}
func linuxModeToFileType(m uint64) (os.FileMode, error) {
if t, ok := modeMap[m&modeTypeMask]; ok {
return t, nil
}
return 0, fmt.Errorf("invalid file type %#o", m&modeTypeMask)
}
// CreateFile creates a local file for f relative to the current working
// directory.
//
// CreateFile will attempt to set all metadata for the file, including
// ownership, times, and permissions.
func CreateFile(f Record) error {
return CreateFileInRoot(f, ".", true)
}
// CreateFileInRoot creates a local file for f relative to rootDir.
//
// It will attempt to set all metadata for the file, including ownership,
// times, and permissions. If these fail, it only returns an error if
// forcePriv is true.
//
// Block and char device creation will only return error if forcePriv is true.
func CreateFileInRoot(f Record, rootDir string, forcePriv bool) error {
m, err := linuxModeToFileType(f.Mode)
if err != nil {
return err
}
f.Name = filepath.Clean(filepath.Join(rootDir, f.Name))
dir := filepath.Dir(f.Name)
// The problem: many cpio archives do not specify the directories and
// hence the permissions. They just specify the whole path. In order
// to create files in these directories, we have to make them at least
// mode 755.
if _, err := os.Stat(dir); os.IsNotExist(err) && len(dir) > 0 {
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("CreateFileInRoot %q: %v", f.Name, err)
}
}
switch m {
case os.ModeSocket, os.ModeNamedPipe:
return fmt.Errorf("%q: type %v: cannot create IPC endpoints", f.Name, m)
case os.ModeSymlink:
content, err := ioutil.ReadAll(uio.Reader(f))
if err != nil {
return err
}
return os.Symlink(string(content), f.Name)
case os.FileMode(0):
nf, err := os.Create(f.Name)
if err != nil {
return err
}
defer nf.Close()
if _, err := io.Copy(nf, uio.Reader(f)); err != nil {
return err
}
case os.ModeDir:
if err := os.MkdirAll(f.Name, toFileMode(f)); err != nil {
return err
}
case os.ModeDevice:
if err := mknod(f.Name, perm(f)|syscall.S_IFBLK, dev(f)); err != nil && forcePriv {
return err
}
case os.ModeCharDevice:
if err := mknod(f.Name, perm(f)|syscall.S_IFCHR, dev(f)); err != nil && forcePriv {
return err
}
default:
return fmt.Errorf("%v: Unknown type %#o", f.Name, m)
}
if err := setModes(f); err != nil && forcePriv {
return err
}
return nil
}
// Inumber and devnumbers are unique to Unix-like
// operating systems. You can not uniquely disambiguate a file in a
// Unix system with just an inumber, you need a device number too.
// To handle hard links (unique to Unix) we need to figure out if a
// given file has been seen before. To do this we see if a file has the
// same [dev,ino] tuple as one we have seen. If so, we won't bother
// reading it in.
type devInode struct {
dev uint64
ino uint64
}
// A Recorder is a structure that contains variables used to calculate
// file parameters such as inode numbers for a CPIO file. The life-time
// of a Record structure is meant to be the same as the construction of a
// single CPIO archive. Do not reuse between CPIOs if you don't know what
// you're doing.
type Recorder struct {
inodeMap map[devInode]Info
inumber uint64
}
// Certain elements of the file can not be set by cpio:
// the Inode #
// the Dev
// maintaining these elements leaves us with a non-reproducible
// output stream. In this function, we figure out what inumber
// we need to use, and clear out anything we can.
// We always zero the Dev.
// We try to find the matching inode. If found, we use its inumber.
// If not, we get a new inumber for it and save the inode away.
// This eliminates two of the messier parts of creating reproducible
// output streams.
func (r *Recorder) inode(i Info) (Info, bool) {
d := devInode{dev: i.Dev, ino: i.Ino}
i.Dev = 0
if d, ok := r.inodeMap[d]; ok {
i.Ino = d.Ino
return i, true
}
i.Ino = r.inumber
r.inumber++
r.inodeMap[d] = i
return i, false
}
// GetRecord returns a cpio Record for the given path on the local file system.
//
// GetRecord does not follow symlinks. If path is a symlink, the record
// returned will reflect that symlink.
func (r *Recorder) GetRecord(path string) (Record, error) {
fi, err := os.Lstat(path)
if err != nil {
return Record{}, err
}
sys := fi.Sys().(*syscall.Stat_t)
info, done := r.inode(sysInfo(path, sys))
switch fi.Mode() & os.ModeType {
case 0: // Regular file.
if done {
return Record{Info: info}, nil
}
return Record{Info: info, ReaderAt: uio.NewLazyFile(path)}, nil
case os.ModeSymlink:
linkname, err := os.Readlink(path)
if err != nil {
return Record{}, err
}
return StaticRecord([]byte(linkname), info), nil
default:
return StaticRecord(nil, info), nil
}
}
// NewRecorder creates a new Recorder.
//
// A recorder is a structure that contains variables used to calculate
// file parameters such as inode numbers for a CPIO file. The life-time
// of a Record structure is meant to be the same as the construction of a
// single CPIO archive. Do not reuse between CPIOs if you don't know what
// you're doing.
func NewRecorder() *Recorder {
return &Recorder{make(map[devInode]Info), 0}
}
// LSInfoFromRecord converts a Record to be usable with the ls package for
// listing files.
func LSInfoFromRecord(rec Record) ls.FileInfo {
var target string
mode := modeFromLinux(rec.Mode)
if mode&os.ModeType == os.ModeSymlink {
if l, err := uio.ReadAll(rec); err != nil {
target = err.Error()
} else {
target = string(l)
}
}
return ls.FileInfo{
Name: rec.Name,
Mode: mode,
Rdev: unix.Mkdev(uint32(rec.Rmajor), uint32(rec.Rminor)),
UID: uint32(rec.UID),
GID: uint32(rec.GID),
Size: int64(rec.FileSize),
MTime: time.Unix(int64(rec.MTime), 0).UTC(),
SymlinkTarget: target,
}
}