func isFileWriteable(p gopath.GoPath) bool { if p.IsExists() { var closer, err = os.OpenFile(p.Path(), os.O_WRONLY, 0) if closer != nil { closer.Close() } return err == nil } return isDirWriteable(p.Dir()) }
// getRelevantFileModeForPath returns the FileMode for the given file or, when // the file does not exist, its containing directory. func (a *accessControl) relevantFileModeForPath(p gopath.GoPath) os.FileMode { if a.hasErr() || p.HasErr() { return 0 } var pStat = p.Stat() if !pStat.IsExists() { // HINT: Inspect permissions of containing directory pStat = p.Dir().Stat() } a.setErr(pStat.Err()) return pStat.FileMode() }
// assertPathValidForWriteAccess sets the error flag when the path may not be // opened for writing by this process. func (i *pathIO) assertPathValidForWriteAccess(p gopath.GoPath) { if i.hasErr() { return } if p.HasErr() { i.setErr(p.Err()) return } if p.IsExists() { if !p.IsRegular() || !isPathWriteable(p) { i.setErr(store.NewAccessDeniedError(fmt.Sprintf( "path '%s' with mode %s denotes no regular file or no writeable directory", p.Path(), p.FileMode()))) } } else { var d = p.Dir() if !isPathWriteable(d) { i.setErr(store.NewAccessDeniedError( "parent directory of '" + p.Path() + "' is not writeable")) } } }