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
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2015 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package osutil
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
)
// CopyFlag is used to tweak the behaviour of CopyFile
type CopyFlag uint8
const (
// CopyFlagDefault is the default behaviour
CopyFlagDefault CopyFlag = 0
// CopyFlagSync does a sync after copying the files
CopyFlagSync CopyFlag = 1 << iota
// CopyFlagOverwrite overwrites the target if it exists
CopyFlagOverwrite
// CopyFlagPreserveAll preserves mode,owner,time attributes
CopyFlagPreserveAll
)
var (
openfile = doOpenFile
copyfile = doCopyFile
)
type fileish interface {
Close() error
Sync() error
Fd() uintptr
Stat() (os.FileInfo, error)
Read([]byte) (int, error)
Write([]byte) (int, error)
}
func doOpenFile(name string, flag int, perm os.FileMode) (fileish, error) {
return os.OpenFile(name, flag, perm)
}
// CopyFile copies src to dst
func CopyFile(src, dst string, flags CopyFlag) (err error) {
if flags&CopyFlagPreserveAll != 0 {
// Our native copy code does not preserve all attributes
// (yet). If the user needs this functionality we just
// fallback to use the system's "cp" binary to do the copy.
if err := runCpPreserveAll(src, dst, "copy all"); err != nil {
return err
}
if flags&CopyFlagSync != 0 {
return runSync()
}
return nil
}
fin, err := openfile(src, os.O_RDONLY, 0)
if err != nil {
return fmt.Errorf("unable to open %s: %w", src, err)
}
defer func() {
if cerr := fin.Close(); cerr != nil && err == nil {
err = fmt.Errorf("when closing %s: %w", src, cerr)
}
}()
fi, err := fin.Stat()
if err != nil {
return fmt.Errorf("unable to stat %s: %w", src, err)
}
outflags := os.O_WRONLY | os.O_CREATE | os.O_TRUNC
if flags&CopyFlagOverwrite == 0 {
outflags |= os.O_EXCL
}
fout, err := openfile(dst, outflags, fi.Mode())
if err != nil {
return fmt.Errorf("unable to create %s: %w", dst, err)
}
defer func() {
if cerr := fout.Close(); cerr != nil && err == nil {
err = fmt.Errorf("when closing %s: %w", dst, cerr)
}
}()
if err := copyfile(fin, fout, fi); err != nil {
return fmt.Errorf("unable to copy %s to %s: %w", src, dst, err)
}
if flags&CopyFlagSync != 0 {
if err = fout.Sync(); err != nil {
return fmt.Errorf("unable to sync %s: %w", dst, err)
}
}
return nil
}
// AtomicWriteFileCopy writes to dst a copy of src using AtomicFile
// internally to create the destination.
// The destination path is always overwritten. The destination and
// the owning directory are synced after copy completes. Pass additional flags
// for AtomicFile wrapping the destination.
func AtomicWriteFileCopy(dst, src string, flags AtomicWriteFlags) (err error) {
fin, err := openfile(src, os.O_RDONLY, 0)
if err != nil {
return fmt.Errorf("unable to open source file %s: %v", src, err)
}
defer func() {
if cerr := fin.Close(); cerr != nil && err == nil {
err = fmt.Errorf("when closing %s: %v", src, cerr)
}
}()
fi, err := fin.Stat()
if err != nil {
return fmt.Errorf("unable to stat %s: %v", src, err)
}
fout, err := NewAtomicFile(dst, fi.Mode(), flags, NoChown, NoChown)
if err != nil {
return fmt.Errorf("cannot create atomic file: %v", err)
}
fout.SetModTime(fi.ModTime())
defer func() {
if cerr := fout.Cancel(); cerr != ErrCannotCancel && err == nil {
err = fmt.Errorf("cannot cancel temporary file copy %s: %v", fout.Name(), cerr)
}
}()
if err := copyfile(fin, fout, fi); err != nil {
return fmt.Errorf("unable to copy %s to %s: %v", src, fout.Name(), err)
}
if err := fout.Commit(); err != nil {
return fmt.Errorf("cannot commit atomic file copy: %v", err)
}
return nil
}
func runCmd(cmd *exec.Cmd, errdesc string) error {
if output, err := cmd.CombinedOutput(); err != nil {
output = bytes.TrimSpace(output)
if exitCode, err := ExitCode(err); err == nil {
return &CopySpecialFileError{
desc: errdesc,
exitCode: exitCode,
output: output,
}
}
return &CopySpecialFileError{
desc: errdesc,
err: err,
output: output,
}
}
return nil
}
func runSync(args ...string) error {
return runCmd(exec.Command("sync", args...), "sync")
}
func runCpPreserveAll(path, dest, errdesc string) error {
return runCmd(exec.Command("cp", "-av", path, dest), errdesc)
}
// CopySpecialFile is used to copy all the things that are not files
// (like device nodes, named pipes etc)
func CopySpecialFile(path, dest string) error {
if err := runCpPreserveAll(path, dest, "copy device node"); err != nil {
return err
}
return runSync(filepath.Dir(dest))
}
// CopySpecialFileError is returned if a special file copy fails
type CopySpecialFileError struct {
desc string
exitCode int
output []byte
err error
}
func (e CopySpecialFileError) Error() string {
if e.err == nil {
return fmt.Sprintf("failed to %s: %q (%v)", e.desc, e.output, e.exitCode)
}
return fmt.Sprintf("failed to %s: %q (%v)", e.desc, e.output, e.err)
}
|