MCPcopy Create free account
hub / github.com/cli/cli / OpenFile

Function OpenFile

internal/safepaths/root.go:74–96  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

72// parent. Creation is always exclusive, including when replace is false.
73// filePerm and dirPerm specify creation modes before applying the umask.
74func 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].

Callers 6

CopyMethod · 0.92
TestFileCreationModesFunction · 0.92
WriteFileFunction · 0.85

Calls 7

hasTrailingPathSeparatorFunction · 0.85
OpenRootFunction · 0.85
MkdirAllMethod · 0.80
BaseMethod · 0.80
CreateMethod · 0.65
CloseMethod · 0.65