Source File
fs_real.go
Belonging Package
github.com/evanw/esbuild/internal/fs
package fs
import (
)
fp goFilepath
}
type entriesOrErr struct {
entries DirEntries
err error
}
type watchState uint8
const (
stateNone watchState = iota
stateDirHasEntries // Compare "dirEntries"
stateDirMissing // Compare directory presence
stateFileHasModKey // Compare "modKey"
stateFileNeedModKey // Need to transition to "stateFileHasModKey" or "stateFileUnusableModKey" before "WatchData()" returns
stateFileMissing // Compare file presence
stateFileUnusableModKey // Compare "fileContents"
)
type privateWatchData struct {
dirEntries []string
fileContents string
modKey ModKey
state watchState
}
type RealFSOptions struct {
WantWatchData bool
AbsWorkingDir string
DoNotCache bool
}
func ( RealFSOptions) (FS, error) {
var goFilepath
if CheckIfWindows() {
.isWindows = true
.pathSeparator = '\\'
} else {
.isWindows = false
.pathSeparator = '/'
}
if , := .evalSymlinks(.cwd); == nil {
.cwd =
}
var map[string]privateWatchData
if .WantWatchData {
= make(map[string]privateWatchData)
}
return &realFS{
entries: make(map[string]entriesOrErr),
fp: ,
watchData: ,
doNotCacheEntries: .DoNotCache,
}, nil
}
func ( *realFS) ( string) (DirEntries, error) {
, := func() ( entriesOrErr, bool) {
.entriesMutex.Lock()
defer .entriesMutex.Unlock()
, = .entries[]
return
}()
, := readdir()
:= DirEntries{, make(map[string]*Entry)}
if .watchData != nil {
defer .watchMutex.Unlock()
.watchMutex.Lock()
:= stateDirHasEntries
if != nil {
= stateDirMissing
}
sort.Strings()
.watchData[] = privateWatchData{
dirEntries: ,
state: ,
}
}
if != nil {
.data = nil
}
if !.doNotCacheEntries {
.entriesMutex.Lock()
defer .entriesMutex.Unlock()
.entries[] = entriesOrErr{entries: , err: }
}
return ,
}
func ( *realFS) ( string) (string, error) {
BeforeFileOpen()
defer AfterFileClose()
, := ioutil.ReadFile()
:= string()
if .watchData != nil {
defer .watchMutex.Unlock()
.watchMutex.Lock()
, := .watchData[]
if != nil {
.state = stateFileMissing
} else if ! {
.state = stateFileNeedModKey
}
.fileContents =
.watchData[] =
}
return ,
}
func ( *realFS) ( string) (ModKey, error) {
BeforeFileOpen()
defer AfterFileClose()
, := modKey()
if .watchData != nil {
defer .watchMutex.Unlock()
.watchMutex.Lock()
, := .watchData[]
if ! {
if == modKeyUnusable {
.state = stateFileUnusableModKey
} else if != nil {
.state = stateFileMissing
} else {
.state = stateFileHasModKey
}
} else if .state == stateFileNeedModKey {
.state = stateFileHasModKey
}
.modKey =
.watchData[] =
}
return ,
}
func ( *realFS) ( string) bool {
return .fp.isAbs()
}
func ( *realFS) ( string) (string, bool) {
, := .fp.abs()
return , == nil
}
func ( *realFS) ( string) string {
return .fp.dir()
}
func ( *realFS) ( string) string {
return .fp.base()
}
func ( *realFS) ( string) string {
return .fp.ext()
}
func ( *realFS) ( ...string) string {
return .fp.clean(.fp.join())
}
func ( *realFS) () string {
return .fp.cwd
}
func ( *realFS) ( string, string) (string, bool) {
if , := .fp.rel(, ); == nil {
return , true
}
return "", false
}
func ( string) ([]string, error) {
BeforeFileOpen()
defer AfterFileClose()
, := os.Open()
if != nil {
return nil,
}
defer .Close()
, := .Readdirnames(-1)
if , := .(*os.SyscallError); {
= .Unwrap()
}
BeforeFileOpen()
defer AfterFileClose()
, := os.Lstat()
if != nil {
return
}
:= .Mode()
:=
:=
if .state == stateFileNeedModKey {
, := modKey()
if == modKeyUnusable {
.state = stateFileUnusableModKey
} else if != nil {
.state = stateFileMissing
} else {
.state = stateFileHasModKey
.modKey =
}
}
switch .state {
case stateDirMissing:
[] = func() bool {
, := os.Stat()
return == nil && .IsDir()
}
case stateDirHasEntries:
[] = func() bool {
, := readdir()
if != nil || len() != len(.dirEntries) {
return true
}
sort.Strings()
for , := range {
if != .dirEntries[] {
return true
}
}
return false
}
case stateFileMissing:
[] = func() bool {
, := os.Stat()
return == nil && !.IsDir()
}
case stateFileHasModKey:
[] = func() bool {
, := modKey()
return != nil || != .modKey
}
case stateFileUnusableModKey:
[] = func() bool {
, := ioutil.ReadFile()
return != nil || string() != .fileContents
}
}
}
return WatchData{
Paths: ,
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |