本文整理汇总了Golang中os.File.Chmod方法的典型用法代码示例。如果您正苦于以下问题:Golang File.Chmod方法的具体用法?Golang File.Chmod怎么用?Golang File.Chmod使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os.File
的用法示例。
在下文中一共展示了File.Chmod方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GetProperty
// returns string property prop
func (proc *SProcess) GetProperty(prop string) string {
var (
file *os.File
err error
)
// file exists
if proc.files[prop] != nil {
file = proc.files[prop]
file.Seek(0, 0)
// doesn't exist; create
} else {
file, err = os.Create("/system/process/" + strconv.Itoa(proc.pid) + "/" + prop)
file.Chmod(0755)
}
// read up to 1024 bytes
b := make([]byte, 1024)
_, err = file.Read(b)
// an error occured, and it was not an EOF
if err != nil && err != io.EOF {
return "(undefined)"
}
// file was more than 1M
if err != io.EOF {
return "(maxed out)"
}
return string(b)
}
示例2: Create
// try hard to create a file with MODE at PATH. creates any missing
// directories in PATH and will try to move un-writable files out of
// the way if necessary.
func Create(path string, mode os.FileMode) (*os.File, error) {
var err error
err = os.MkdirAll(filepath.Dir(path), 0744)
if err != nil {
return nil, err
}
var wr *os.File
for tries := 0; tries < 2; tries++ {
wr, err = os.OpenFile(path, os.O_CREATE|os.O_WRONLY, mode)
// sometimes, can't overwrite a file, but can move it out of the way
if err != nil {
trash := path + ".trash"
os.Remove(trash)
rename := os.Rename(path, trash)
if rename == nil {
continue
} else {
break
}
} else {
break
}
}
if err == nil && wr != nil {
wr.Chmod(mode)
}
return wr, err
}
示例3: copyPathToPath
func copyPathToPath(fromPath, toPath string) (err error) {
srcFileInfo, err := os.Stat(fromPath)
if err != nil {
return
}
if srcFileInfo.IsDir() {
err = os.MkdirAll(toPath, srcFileInfo.Mode())
if err != nil {
return
}
} else {
var dst *os.File
dst, err = fileutils.Create(toPath)
if err != nil {
return
}
defer dst.Close()
dst.Chmod(srcFileInfo.Mode())
err = fileutils.CopyPathToWriter(fromPath, dst)
}
return err
}
示例4: DestructiveSavePath
func (st *Store) DestructiveSavePath(path string) (hash string, err error) {
start := time.Now()
var f *os.File
f, err = os.Open(path)
if err != nil {
return "", err
}
before, _ := f.Stat()
defer f.Close()
h := st.Options.Hash.New()
var content []byte
var size int
if before.Size() < st.Options.MemMaxSize {
content, err = ioutil.ReadAll(f)
if err != nil {
return "", err
}
size, _ = h.Write(content)
} else {
sz, _ := io.Copy(h, f)
size = int(sz)
}
s := string(h.Sum(nil))
if st.HasHash(s) {
os.Remove(path)
return s, nil
}
st.mutex.Lock()
st.have[s] = true
if content != nil && st.inMemoryCache != nil {
st.inMemoryCache.Add(s, content)
}
st.mutex.Unlock()
p := st.Path(s)
err = os.Rename(path, p)
if err != nil {
log.Fatal("Rename failed", err)
}
f.Chmod(0444)
after, _ := f.Stat()
if !after.ModTime().Equal(before.ModTime()) || after.Size() != before.Size() {
log.Fatal("File changed during save", before, after)
}
dt := time.Now().Sub(start)
st.AddTiming("DestructiveSave", size, dt)
log.Printf("Saving %s as %x destructively", path, s)
return s, nil
}
示例5: TextEditor
func TextEditor(inPath string, inContent []byte) ([]byte, error) {
var f *os.File
var err error
var path string
// Detect the text editor to use
editor := os.Getenv("VISUAL")
if editor == "" {
editor = os.Getenv("EDITOR")
if editor == "" {
editor = "vi"
}
}
if inPath == "" {
// If provided input, create a new file
f, err = ioutil.TempFile("", "lxd_editor_")
if err != nil {
return []byte{}, err
}
if err = f.Chmod(0600); err != nil {
f.Close()
os.Remove(f.Name())
return []byte{}, err
}
f.Write(inContent)
f.Close()
path = f.Name()
defer os.Remove(path)
} else {
path = inPath
}
cmdParts := strings.Fields(editor)
cmd := exec.Command(cmdParts[0], append(cmdParts[1:], path)...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return []byte{}, err
}
content, err := ioutil.ReadFile(path)
if err != nil {
return []byte{}, err
}
return content, nil
}
示例6: CopyFiles
func (appfiles ApplicationFiles) CopyFiles(appFiles []models.AppFileFields, fromDir, toDir string) error {
for _, file := range appFiles {
err := func() error {
fromPath := filepath.Join(fromDir, file.Path)
srcFileInfo, err := os.Stat(fromPath)
if err != nil {
return err
}
toPath := filepath.Join(toDir, file.Path)
if srcFileInfo.IsDir() {
err = os.MkdirAll(toPath, srcFileInfo.Mode())
if err != nil {
return err
}
return nil
}
var dst *os.File
dst, err = fileutils.Create(toPath)
if err != nil {
return err
}
defer dst.Close()
dst.Chmod(srcFileInfo.Mode())
src, err := os.Open(fromPath)
if err != nil {
return err
}
defer src.Close()
_, err = io.Copy(dst, src)
if err != nil {
return err
}
return nil
}()
if err != nil {
return err
}
}
return nil
}
示例7: writeFile
func (u *Unarchiver) writeFile(blockSource chan block, workInProgress *sync.WaitGroup) {
var file *os.File = nil
var bufferedFile *bufio.Writer
for block := range blockSource {
if block.blockType == blockTypeStartOfFile {
u.Logger.Verbose(block.filePath)
if u.DryRun {
continue
}
tmp, err := os.Create(block.filePath)
if err != nil {
u.Logger.Warning("File create error:", err.Error())
file = nil
continue
}
file = tmp
bufferedFile = bufio.NewWriter(file)
if !u.IgnoreOwners {
err = file.Chown(block.uid, block.gid)
if err != nil {
u.Logger.Warning("Unable to chown file to", block.uid, "/", block.gid, ":", err.Error())
}
}
if !u.IgnorePerms {
err = file.Chmod(block.mode)
if err != nil {
u.Logger.Warning("Unable to chmod file to", block.mode, ":", err.Error())
}
}
} else if file == nil {
// do nothing; file couldn't be opened for write
} else if block.blockType == blockTypeEndOfFile {
bufferedFile.Flush()
file.Close()
file = nil
} else {
_, err := bufferedFile.Write(block.buffer[:block.numBytes])
if err != nil {
u.Logger.Warning("File write error:", err.Error())
}
}
}
workInProgress.Done()
}
示例8: CopyPathToPath
func CopyPathToPath(fromPath, toPath string) (err error) {
srcFileInfo, err := os.Stat(fromPath)
if err != nil {
return err
}
if srcFileInfo.IsDir() {
err = os.MkdirAll(toPath, srcFileInfo.Mode())
if err != nil {
return err
}
files, err := ioutil.ReadDir(fromPath)
if err != nil {
return err
}
for _, file := range files {
err = CopyPathToPath(path.Join(fromPath, file.Name()), path.Join(toPath, file.Name()))
if err != nil {
return err
}
}
} else {
var dst *os.File
dst, err = Create(toPath)
if err != nil {
return err
}
defer dst.Close()
dst.Chmod(srcFileInfo.Mode())
src, err := os.Open(fromPath)
if err != nil {
return err
}
defer src.Close()
_, err = io.Copy(dst, src)
if err != nil {
return err
}
}
return err
}
示例9: Expand
func (u *Unzip) Expand() error {
file_handler := func(meta *ZipMeta) error {
if meta.artifacts == nil {
meta.artifacts = list.New()
}
Zipfile := meta.current_file.file
meta.artifacts.PushBack(meta.current_file)
var (
err error
file *os.File
rc io.ReadCloser
)
if rc, err = Zipfile.Open(); err != nil {
return err
}
if file, err = os.Create(Zipfile.Name); err != nil {
return err
}
if _, err = io.Copy(file, rc); err != nil {
return err
}
if err = file.Chmod(Zipfile.Mode().Perm()); err != nil {
//log.Println(err) // Windows WTF?
}
file.Close()
rc.Close()
return nil
}
folder_handler := func(meta *ZipMeta) error {
return os.MkdirAll(meta.last_folder, 0755)
}
meta := ZipMeta{file_handler, folder_handler, make(map[string]bool), "", nil, nil}
if err := meta.walk(u.Zipfile); err != nil {
return err
} else {
u.Artifacts = meta.artifacts
}
return nil
}
示例10: main
func main() {
var err error
var path string
var mode *os.FileMode
if len(os.Args) > 1 {
path = os.Args[1]
} else {
fmt.Fprintf(os.Stderr, "usage: putfile path [mode]\n")
os.Exit(1)
}
if len(os.Args) > 2 {
var m uint64
m, err = strconv.ParseUint(os.Args[2], 8, 32)
if err != nil {
fmt.Fprintf(os.Stderr, "bad file mode=`%s`\n", os.Args[2])
os.Exit(1)
}
var m2 os.FileMode
m2 = os.FileMode(m)
mode = &m2
}
var f *os.File
f, err = os.Create(path)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer f.Close()
if mode != nil {
err = f.Chmod(*mode)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
_, err = io.Copy(f, os.Stdin)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
示例11: UnzipMem
func UnzipMem(arc []byte, size int64) error {
var (
err error
file *os.File
rc io.ReadCloser
r *zip.Reader
)
ra := bytes.NewReader(arc)
if r, err = zip.NewReader(ra, size); err != nil {
log.Fatal(err)
return err
}
for _, f := range r.File {
fmt.Println(f.Name)
if f.Mode().IsDir() { // if file is a directory
if err = os.MkdirAll(f.Name, f.Mode().Perm()); err != nil {
return err
}
continue
}
if rc, err = f.Open(); err != nil {
return err
}
if file, err = os.Create(f.Name); err != nil {
return err
}
if _, err = io.Copy(file, rc); err != nil {
return err
}
if err = file.Chmod(f.Mode().Perm()); err != nil {
return err
}
file.Close()
rc.Close()
}
return nil
}
示例12: UnzipFile
func UnzipFile(name string) error {
var (
err error
file *os.File
rc io.ReadCloser
r *zip.ReadCloser
)
if r, err = zip.OpenReader(name); err != nil {
log.Fatal(err)
return err
}
defer r.Close()
for _, f := range r.File {
fmt.Println(f.Name)
if f.Mode().IsDir() { // if file is a directory
if err = os.MkdirAll(f.Name, f.Mode().Perm()); err != nil {
return err
}
continue
}
if rc, err = f.Open(); err != nil {
return err
}
if file, err = os.Create(f.Name); err != nil {
return err
}
if _, err = io.Copy(file, rc); err != nil {
return err
}
if err = file.Chmod(f.Mode().Perm()); err != nil {
return err
}
file.Close()
rc.Close()
}
return nil
}
示例13: SetProperty
// assign a property
func (proc *SProcess) SetProperty(prop string, value string) {
var file *os.File
// file exists; empty
if proc.files[prop] != nil {
file = proc.files[prop]
file.Truncate(0)
// doesn't exist; create
} else {
file, _ = os.Create("/system/process/" + strconv.Itoa(proc.pid) + "/" + prop)
file.Chmod(0755)
}
proc.files[prop] = file
// write
file.Seek(0, 0)
file.WriteString(value)
}
示例14: getBenchClientPath
func getBenchClientPath() (path string, err error) {
benchClientOnce.Do(func() {
var tempFile *os.File
tempFile, err = ioutil.TempFile("", "benchclient")
if err != nil {
return
}
if err = tempFile.Chmod(0755); err != nil {
return
}
benchClientPath = tempFile.Name()
cmd := exec.Command("go", "build", "-o", tempFile.Name(), ".")
cmd.Dir = "./benchclient"
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
})
return benchClientPath, err
}
示例15: copyFile
/*
Brute copy from [source] to [target].
*/
func copyFile(source, target string) error {
var sourceFile, targetFile *os.File
var err error
sourceFile, err = os.Open(source)
if err != nil {
return err
}
defer sourceFile.Close()
targetFile, err = os.Create(target)
if err != nil {
return err
}
targetFile.Chmod(0755)
defer targetFile.Close()
_, err = io.Copy(targetFile, sourceFile)
return err
}