// OpenFile opens a file handle with a specified flag (os.O_RDONLY etc.) and perm (e.g. 0666).
// If success the returned File can be used for I/O. Otherwise an error is returned, which
// is a *os.PathError and can be extracted for further information.
183017
func (fs *MemFS) OpenFile(name string, flag int, perm os.FileMode) (vfs.File, error) {
	fs.lock.Lock()
	defer fs.lock.Unlock()

	name = filepath.Clean(name)
	base := filepath.Base(name)
	fiParent, fiNode, err := fs.fileInfo(name)
	if err != nil {
		return nil, &os.PathError{"open", name, err}
	}

	if fiNode == nil {
		if !hasFlag(os.O_CREATE, flag) {
			return nil, &os.PathError{"open", name, os.ErrNotExist}
		}
		fiNode = &fileInfo{
			name:    base,
			dir:     false,
			mode:    perm,
			parent:  fiParent,
			modTime: time.Now(),
			fs:      fs,
		}
		fiParent.childs[base] = fiNode
	} else { // file exists
		if hasFlag(os.O_CREATE|os.O_EXCL, flag) {
			return nil, &os.PathError{"open", name, os.ErrExist}
		}
		if fiNode.dir {
			return nil, &os.PathError{"open", name, ErrIsDirectory}
		}
	}

	if !hasFlag(os.O_RDONLY, flag) {
		fiNode.modTime = time.Now()
	}
	return fiNode.file(flag)
}
1158445
func OpenFile(path string) (*os.File, error) {
	newPath, err := NormalizePath(path)
	if err != nil {
		return nil, trace.Wrap(err)
	}
	fi, err := os.Stat(newPath)
	if err != nil {
		return nil, trace.ConvertSystemError(err)
	}
	if fi.IsDir() {
		return nil, trace.BadParameter("%v is not a file", path)
	}
	f, err := os.Open(newPath)
	if err != nil {
		return nil, trace.ConvertSystemError(err)
	}
	return f, nil
}
775443
func OpenFile(fileName string) (file *File, err error) {
	return OpenFileWithRowLimit(fileName, NoRowLimit)
}
852547
func OpenLogFile(logFile string) *os.File {
	f, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		fmt.Printf("error opening file: %v\n", err)
	}
	// at callee: defer f.Close()
	return f
}
1120046
func OpenFile(filename string) (file *File, err error) {
	var f *zip.ReadCloser
	f, err = zip.OpenReader(filename)
	if err != nil {
		return nil, err
	}
	file, err = ReadZip(f)
	return
}
344798
func (fsh *Handler) WriteFile(file string, data []byte) error {
	fileObj, err := os.OpenFile(file, os.O_TRUNC|os.O_WRONLY, os.ModePerm)
	if err != nil {
		return fmt.Errorf("failed to open status file %s for writing: %v", file, err)
	}
	defer fileObj.Close()
	if _, err := fileObj.Write(data); err != nil {
		return fmt.Errorf("failed to write status file %s for writing: %v", file, err)
	}
	return nil
}
861443
func openFile(name string) (*tar.Reader, *os.File, error) {
	f, err := os.Open(name)
	if err != nil {
		return nil, nil, err
	}

	gr, err := gzip.NewReader(f)
	if err != nil {
		f.Close()
		return nil, nil, err
	}

	return tar.NewReader(gr), f, nil
}
712588
func Open(path string, flag int, perm os.FileMode) (*os.File, error) {
	if path == "" {
		return nil, syscall.ERROR_FILE_NOT_FOUND
	}

	pathp, err := syscall.UTF16PtrFromString(fixLongPath(path))
	if err != nil {
		return nil, err
	}

	var access uint32
	switch flag {
	case syscall.O_RDONLY:
		access = syscall.GENERIC_READ
	case syscall.O_WRONLY:
		access = syscall.GENERIC_WRITE
	case syscall.O_RDWR:
		fallthrough
	case syscall.O_RDWR | syscall.O_CREAT:
		fallthrough
	case syscall.O_WRONLY | syscall.O_CREAT:
		access = syscall.GENERIC_READ | syscall.GENERIC_WRITE
	case syscall.O_WRONLY | syscall.O_CREAT | syscall.O_APPEND:
		access = syscall.FILE_APPEND_DATA
	default:
		return nil, fmt.Errorf("Unsupported flag (%d)", flag)
	}

	var createflag uint32
	switch {
	case flag&syscall.O_CREAT == syscall.O_CREAT:
		createflag = syscall.OPEN_ALWAYS
	default:
		createflag = syscall.OPEN_EXISTING
	}

	shareflag := uint32(syscall.FILE_SHARE_READ | syscall.FILE_SHARE_WRITE | syscall.FILE_SHARE_DELETE)
	accessAttr := uint32(syscall.FILE_ATTRIBUTE_NORMAL | 0x80000000)

	fd, err := syscall.CreateFile(pathp, access, shareflag, nil, createflag, accessAttr, 0)
	if err != nil {
		return nil, err
	}

	return os.NewFile(uintptr(fd), path), nil
}
862696
func (f *StandaloneFile) WriteFile(data []byte, perm os.FileMode) error {
	return f.filespace.WriteFile(f.path, data, perm)
}
702505
func (fs OsFS) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
	return os.OpenFile(name, flag, perm)
}
280322
func (p *Partition) openIndexFile(path string) (*IndexFile, error) {
	f := NewIndexFile(p.sfile)
	f.SetPath(path)
	if err := f.Open(); err != nil {
		return nil, err
	}
	return f, nil
}
1248946
func OpenFile(path string) error {
	logFilePath = path

	var err error
	logFile, err = os.OpenFile(logFilePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		return err
	}

	SetOutput(logFile)
	return nil
}
79308
func OpenHttpFile(url string, headers map[string]string) (*HttpFile, error) {
	f := HttpFile{Url: url, Headers: headers, client: &http.Client{}, pos: 0, len: -1}

	resp, err := f.do("HEAD", nil)
	defer CloseResponse(resp)

	if err != nil {
		return nil, &HttpFileError{Err: err}
	}
	if resp.StatusCode == http.StatusNotFound {
		return nil, os.ErrNotExist
	}
	if resp.StatusCode != http.StatusOK {
		return nil, &HttpFileError{Err: fmt.Errorf("Unexpected Status %s", resp.Status)}
	}

	f.len = resp.ContentLength
	return &f, nil
}
877307
func OpenFile(path string, readOnly bool) (Storage, error) {
	if fi, err := os.Stat(path); err == nil {
		if !fi.IsDir() {
			return nil, fmt.Errorf("leveldb/storage: open %s: not a directory", path)
		}
	} else if os.IsNotExist(err) && !readOnly {
		if err := os.MkdirAll(path, 0755); err != nil {
			return nil, err
		}
	} else {
		return nil, err
	}

	flock, err := newFileLock(filepath.Join(path, "LOCK"), readOnly)
	if err != nil {
		return nil, err
	}

	defer func() {
		if err != nil {
			flock.release()
		}
	}()

	var (
		logw    *os.File
		logSize int64
	)
	if !readOnly {
		logw, err = os.OpenFile(filepath.Join(path, "LOG"), os.O_WRONLY|os.O_CREATE, 0644)
		if err != nil {
			return nil, err
		}
		logSize, err = logw.Seek(0, os.SEEK_END)
		if err != nil {
			logw.Close()
			return nil, err
		}
	}

	fs := &fileStorage{
		path:     path,
		readOnly: readOnly,
		flock:    flock,
		logw:     logw,
		logSize:  logSize,
	}
	runtime.SetFinalizer(fs, (*fileStorage).Close)
	return fs, nil
}
1287720
func (l *localFileSystem) OpenFile(filePath string) (io.ReadCloser, error) {
	f, err := os.Open(filePath)
	if err != nil {
		return nil, trace.Wrap(err)
	}

	return f, nil
}
554196
func Open(path string) (*os.File, error) {
	return os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0660)
}
423631
func openFile(url string) (io.ReadCloser, error) {
	usedUrl := url
	if strings.HasPrefix(url, "assets/") {
		usedUrl = usedUrl[7:]
	}

	return asset.Open(usedUrl)
}