| 1 |
// Copyright The Notary Project Authors. |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package file |
| 15 |
|
| 16 |
import ( |
| 17 |
"errors" |
| 18 |
"fmt" |
| 19 |
"io" |
| 20 |
"io/fs" |
| 21 |
"os" |
| 22 |
"path/filepath" |
| 23 |
"regexp" |
| 24 |
"strings" |
| 25 |
) |
| 26 |
|
| 27 |
const ( |
| 28 |
// tempFileNamePrefix is the prefix of the temporary file |
| 29 |
tempFileNamePrefix = "notation-*" |
| 30 |
) |
| 31 |
|
| 32 |
// ErrNotRegularFile is returned when the file is not an regular file. |
| 33 |
var ErrNotRegularFile = errors.New("not regular file") |
| 34 |
|
| 35 |
// ErrNotDirectory is returned when the path is not a directory. |
| 36 |
var ErrNotDirectory = errors.New("not directory") |
| 37 |
|
| 38 |
// IsValidFileName checks if a file name is cross-platform compatible |
| 39 |
func IsValidFileName(fileName string) bool { |
| 40 |
return regexp.MustCompile(`^[a-zA-Z0-9_.-]+$`).MatchString(fileName) |
| 41 |
} |
| 42 |
|
| 43 |
// CopyToDir copies the src file to dst dir. All parent directories are created |
| 44 |
// with permissions 0755. |
| 45 |
// |
| 46 |
// Source file's read and execute permissions are preserved for everyone. |
| 47 |
// Write permission is preserved for owner. Group and others cannot write. |
| 48 |
// Existing file will be overwritten. |
| 49 |
func CopyToDir(src, dst string) error { |
| 50 |
sourceFileInfo, err := os.Stat(src) |
| 51 |
if err != nil { |
| 52 |
return err |
| 53 |
} |
| 54 |
if !sourceFileInfo.Mode().IsRegular() { |
| 55 |
return ErrNotRegularFile |
| 56 |
} |
| 57 |
source, err := os.Open(src) |
| 58 |
if err != nil { |
| 59 |
return err |
| 60 |
} |
| 61 |
defer source.Close() |
| 62 |
if err := os.MkdirAll(dst, 0755); err != nil { |
| 63 |
return err |
| 64 |
} |
| 65 |
dstFile := filepath.Join(dst, filepath.Base(src)) |
| 66 |
destination, err := os.Create(dstFile) |
| 67 |
if err != nil { |
| 68 |
return err |
| 69 |
} |
| 70 |
defer destination.Close() |
| 71 |
err = destination.Chmod(sourceFileInfo.Mode() & os.FileMode(0755)) |
| 72 |
if err != nil { |
| 73 |
return err |
| 74 |
} |
| 75 |
_, err = io.Copy(destination, source) |
| 76 |
return err |
| 77 |
} |
| 78 |
|
| 79 |
// CopyDirToDir copies contents in src dir to dst dir. Only regular files are |
| 80 |
// copied. Existing files will be overwritten. |
| 81 |
func CopyDirToDir(src, dst string) error { |
| 82 |
fi, err := os.Stat(src) |
| 83 |
if err != nil { |
| 84 |
return err |
| 85 |
} |
| 86 |
if !fi.Mode().IsDir() { |
| 87 |
return ErrNotDirectory |
| 88 |
} |
| 89 |
return filepath.WalkDir(src, func(path string, d fs.DirEntry, err error) error { |
| 90 |
if err != nil { |
| 91 |
return err |
| 92 |
} |
| 93 |
// skip sub-directories |
| 94 |
if d.IsDir() && d.Name() != filepath.Base(path) { |
| 95 |
return fs.SkipDir |
| 96 |
} |
| 97 |
info, err := d.Info() |
| 98 |
if err != nil { |
| 99 |
return err |
| 100 |
} |
| 101 |
// only copy regular files |
| 102 |
if info.Mode().IsRegular() { |
| 103 |
return CopyToDir(path, dst) |
| 104 |
} |
| 105 |
return nil |
| 106 |
}) |
| 107 |
} |
| 108 |
|
| 109 |
// TrimFileExtension returns the file name without extension. |
| 110 |
// |
| 111 |
// For example, |
| 112 |
// |
| 113 |
// when input is xyz.exe, output is xyz |
| 114 |
// |
| 115 |
// when input is xyz.tar.gz, output is xyz.tar |
| 116 |
func TrimFileExtension(fileName string) string { |
| 117 |
return strings.TrimSuffix(fileName, filepath.Ext(fileName)) |
| 118 |
} |
| 119 |
|
| 120 |
// WriteFile writes content to a temporary file and moves it to path. |
| 121 |
// If path already exists and is a file, WriteFile overwrites it. |
| 122 |
// |
| 123 |
// Parameters: |
| 124 |
// - tempDir is the directory to create the temporary file. It should be |
| 125 |
// in the same mount point as path. If tempDir is empty, the default |
| 126 |
// directory for temporary files is used. |
| 127 |
// - path is the destination file path. |
| 128 |
// - content is the content to write. |
| 129 |
func WriteFile(tempDir, path string, content []byte) (writeErr error) { |
| 130 |
tempFile, err := os.CreateTemp(tempDir, tempFileNamePrefix) |
| 131 |
if err != nil { |
| 132 |
return fmt.Errorf("failed to create temp file: %w", err) |
| 133 |
} |
| 134 |
defer func() { |
| 135 |
// remove the temp file in case of error |
| 136 |
if writeErr != nil { |
| 137 |
tempFile.Close() |
| 138 |
os.Remove(tempFile.Name()) |
| 139 |
} |
| 140 |
}() |
| 141 |
|
| 142 |
if _, err := tempFile.Write(content); err != nil { |
| 143 |
return fmt.Errorf("failed to write content to temp file: %w", err) |
| 144 |
} |
| 145 |
|
| 146 |
// close before moving |
| 147 |
if err := tempFile.Close(); err != nil { |
| 148 |
return fmt.Errorf("failed to close temp file: %w", err) |
| 149 |
} |
| 150 |
|
| 151 |
// rename is atomic on UNIX-like platforms |
| 152 |
return os.Rename(tempFile.Name(), path) |
| 153 |
} |