OpenFile creates a file without traversing symbolic links below its rooted parent. Creation is always exclusive, including when replace is false. filePerm and dirPerm specify creation modes before applying the umask.
(path string, filePerm, dirPerm os.FileMode, replace bool)
| 72 | // parent. Creation is always exclusive, including when replace is false. |
| 73 | // filePerm and dirPerm specify creation modes before applying the umask. |
| 74 | func OpenFile(path string, filePerm, dirPerm os.FileMode, replace bool) (*os.File, error) { |
| 75 | if hasTrailingPathSeparator(path) { |
| 76 | return nil, trailingPathSeparatorError(path) |
| 77 | } |
| 78 | parent := filepath.Dir(path) |
| 79 | if err := os.MkdirAll(parent, dirPerm); err != nil { |
| 80 | return nil, err |
| 81 | } |
| 82 | root, err := OpenRoot(parent) |
| 83 | if err != nil { |
| 84 | return nil, err |
| 85 | } |
| 86 | file, createErr := root.Create(filepath.Base(path), filePerm, dirPerm, replace) |
| 87 | closeErr := root.Close() |
| 88 | if createErr != nil { |
| 89 | return nil, createErr |
| 90 | } |
| 91 | if closeErr != nil { |
| 92 | _ = file.Close() |
| 93 | return nil, closeErr |
| 94 | } |
| 95 | return file, nil |
| 96 | } |
| 97 | |
| 98 | // WriteFile writes a complete file without traversing symbolic links. |
| 99 | // Creation permissions follow [OpenFile]. |