2018-08-27 23:44:22 +00:00
|
|
|
package wclayer
|
2018-02-01 09:00:09 +00:00
|
|
|
|
|
|
|
import (
|
2020-05-11 18:32:09 +00:00
|
|
|
"context"
|
2018-02-01 09:00:09 +00:00
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/Microsoft/go-winio"
|
2018-08-27 23:44:22 +00:00
|
|
|
"github.com/Microsoft/hcsshim/internal/hcserror"
|
2020-05-11 18:32:09 +00:00
|
|
|
"github.com/Microsoft/hcsshim/internal/oc"
|
2018-08-27 23:44:22 +00:00
|
|
|
"github.com/Microsoft/hcsshim/internal/safefile"
|
2020-11-17 16:32:28 +00:00
|
|
|
"github.com/Microsoft/hcsshim/internal/winapi"
|
2020-05-11 18:32:09 +00:00
|
|
|
"go.opencensus.io/trace"
|
2018-02-01 09:00:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type baseLayerWriter struct {
|
2020-05-11 18:32:09 +00:00
|
|
|
ctx context.Context
|
|
|
|
s *trace.Span
|
|
|
|
|
2018-05-11 04:44:43 +00:00
|
|
|
root *os.File
|
2018-02-01 09:00:09 +00:00
|
|
|
f *os.File
|
|
|
|
bw *winio.BackupFileWriter
|
|
|
|
err error
|
|
|
|
hasUtilityVM bool
|
|
|
|
dirInfo []dirInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
type dirInfo struct {
|
|
|
|
path string
|
|
|
|
fileInfo winio.FileBasicInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// reapplyDirectoryTimes reapplies directory modification, creation, etc. times
|
|
|
|
// after processing of the directory tree has completed. The times are expected
|
|
|
|
// to be ordered such that parent directories come before child directories.
|
2018-05-11 04:44:43 +00:00
|
|
|
func reapplyDirectoryTimes(root *os.File, dis []dirInfo) error {
|
2018-02-01 09:00:09 +00:00
|
|
|
for i := range dis {
|
|
|
|
di := &dis[len(dis)-i-1] // reverse order: process child directories first
|
2020-11-17 16:32:28 +00:00
|
|
|
f, err := safefile.OpenRelative(di.path, root, syscall.GENERIC_READ|syscall.GENERIC_WRITE, syscall.FILE_SHARE_READ, winapi.FILE_OPEN, winapi.FILE_DIRECTORY_FILE|syscall.FILE_FLAG_OPEN_REPARSE_POINT)
|
2018-02-01 09:00:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = winio.SetFileBasicInfo(f, &di.fileInfo)
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-17 16:32:28 +00:00
|
|
|
|
2018-02-01 09:00:09 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *baseLayerWriter) closeCurrentFile() error {
|
|
|
|
if w.f != nil {
|
|
|
|
err := w.bw.Close()
|
|
|
|
err2 := w.f.Close()
|
|
|
|
w.f = nil
|
|
|
|
w.bw = nil
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *baseLayerWriter) Add(name string, fileInfo *winio.FileBasicInfo) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
w.err = err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = w.closeCurrentFile()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if filepath.ToSlash(name) == `UtilityVM/Files` {
|
|
|
|
w.hasUtilityVM = true
|
|
|
|
}
|
|
|
|
|
|
|
|
var f *os.File
|
|
|
|
defer func() {
|
|
|
|
if f != nil {
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-05-11 04:44:43 +00:00
|
|
|
extraFlags := uint32(0)
|
2018-02-01 09:00:09 +00:00
|
|
|
if fileInfo.FileAttributes&syscall.FILE_ATTRIBUTE_DIRECTORY != 0 {
|
2020-11-17 16:32:28 +00:00
|
|
|
extraFlags |= winapi.FILE_DIRECTORY_FILE
|
|
|
|
w.dirInfo = append(w.dirInfo, dirInfo{name, *fileInfo})
|
2018-02-01 09:00:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mode := uint32(syscall.GENERIC_READ | syscall.GENERIC_WRITE | winio.WRITE_DAC | winio.WRITE_OWNER | winio.ACCESS_SYSTEM_SECURITY)
|
2020-11-17 16:32:28 +00:00
|
|
|
f, err = safefile.OpenRelative(name, w.root, mode, syscall.FILE_SHARE_READ, winapi.FILE_CREATE, extraFlags)
|
2018-02-01 09:00:09 +00:00
|
|
|
if err != nil {
|
2018-08-27 23:44:22 +00:00
|
|
|
return hcserror.New(err, "Failed to safefile.OpenRelative", name)
|
2018-02-01 09:00:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = winio.SetFileBasicInfo(f, fileInfo)
|
|
|
|
if err != nil {
|
2018-08-27 23:44:22 +00:00
|
|
|
return hcserror.New(err, "Failed to SetFileBasicInfo", name)
|
2018-02-01 09:00:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w.f = f
|
|
|
|
w.bw = winio.NewBackupFileWriter(f, true)
|
|
|
|
f = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *baseLayerWriter) AddLink(name string, target string) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
w.err = err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = w.closeCurrentFile()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-27 23:44:22 +00:00
|
|
|
return safefile.LinkRelative(target, w.root, name, w.root)
|
2018-02-01 09:00:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *baseLayerWriter) Remove(name string) error {
|
|
|
|
return errors.New("base layer cannot have tombstones")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *baseLayerWriter) Write(b []byte) (int, error) {
|
|
|
|
n, err := w.bw.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
w.err = err
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2020-05-11 18:32:09 +00:00
|
|
|
func (w *baseLayerWriter) Close() (err error) {
|
|
|
|
defer w.s.End()
|
|
|
|
defer func() { oc.SetSpanStatus(w.s, err) }()
|
2018-05-11 04:44:43 +00:00
|
|
|
defer func() {
|
|
|
|
w.root.Close()
|
|
|
|
w.root = nil
|
|
|
|
}()
|
2020-05-11 18:32:09 +00:00
|
|
|
|
|
|
|
err = w.closeCurrentFile()
|
2018-02-01 09:00:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if w.err == nil {
|
|
|
|
// Restore the file times of all the directories, since they may have
|
|
|
|
// been modified by creating child directories.
|
2018-05-11 04:44:43 +00:00
|
|
|
err = reapplyDirectoryTimes(w.root, w.dirInfo)
|
2018-02-01 09:00:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-11 18:32:09 +00:00
|
|
|
err = ProcessBaseLayer(w.ctx, w.root.Name())
|
2018-02-01 09:00:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.hasUtilityVM {
|
2018-08-27 23:44:22 +00:00
|
|
|
err := safefile.EnsureNotReparsePointRelative("UtilityVM", w.root)
|
2018-05-11 04:44:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-11 18:32:09 +00:00
|
|
|
err = ProcessUtilityVMImage(w.ctx, filepath.Join(w.root.Name(), "UtilityVM"))
|
2018-02-01 09:00:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return w.err
|
|
|
|
}
|