github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/internal/lsp/fake/workdir.go (about)

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package fake
     6  
     7  import (
     8  	"bytes"
     9  	"context"
    10  	"crypto/sha256"
    11  	"fmt"
    12  	"io/ioutil"
    13  	"os"
    14  	"path/filepath"
    15  	"runtime"
    16  	"strings"
    17  	"sync"
    18  	"time"
    19  
    20  	"github.com/powerman/golang-tools/internal/lsp/protocol"
    21  	"github.com/powerman/golang-tools/internal/span"
    22  	errors "golang.org/x/xerrors"
    23  )
    24  
    25  // FileEvent wraps the protocol.FileEvent so that it can be associated with a
    26  // workdir-relative path.
    27  type FileEvent struct {
    28  	Path, Content string
    29  	ProtocolEvent protocol.FileEvent
    30  }
    31  
    32  // RelativeTo is a helper for operations relative to a given directory.
    33  type RelativeTo string
    34  
    35  // AbsPath returns an absolute filesystem path for the workdir-relative path.
    36  func (r RelativeTo) AbsPath(path string) string {
    37  	fp := filepath.FromSlash(path)
    38  	if filepath.IsAbs(fp) {
    39  		return fp
    40  	}
    41  	return filepath.Join(string(r), filepath.FromSlash(path))
    42  }
    43  
    44  // RelPath returns a '/'-encoded path relative to the working directory (or an
    45  // absolute path if the file is outside of workdir)
    46  func (r RelativeTo) RelPath(fp string) string {
    47  	root := string(r)
    48  	if rel, err := filepath.Rel(root, fp); err == nil && !strings.HasPrefix(rel, "..") {
    49  		return filepath.ToSlash(rel)
    50  	}
    51  	return filepath.ToSlash(fp)
    52  }
    53  
    54  // WriteFileData writes content to the relative path, replacing the special
    55  // token $SANDBOX_WORKDIR with the relative root given by rel.
    56  func WriteFileData(path string, content []byte, rel RelativeTo) error {
    57  	content = bytes.ReplaceAll(content, []byte("$SANDBOX_WORKDIR"), []byte(rel))
    58  	fp := rel.AbsPath(path)
    59  	if err := os.MkdirAll(filepath.Dir(fp), 0755); err != nil {
    60  		return errors.Errorf("creating nested directory: %w", err)
    61  	}
    62  	backoff := 1 * time.Millisecond
    63  	for {
    64  		err := ioutil.WriteFile(fp, []byte(content), 0644)
    65  		if err != nil {
    66  			if isWindowsErrLockViolation(err) {
    67  				time.Sleep(backoff)
    68  				backoff *= 2
    69  				continue
    70  			}
    71  			return errors.Errorf("writing %q: %w", path, err)
    72  		}
    73  		return nil
    74  	}
    75  }
    76  
    77  // isWindowsErrLockViolation reports whether err is ERROR_LOCK_VIOLATION
    78  // on Windows.
    79  var isWindowsErrLockViolation = func(err error) bool { return false }
    80  
    81  // Workdir is a temporary working directory for tests. It exposes file
    82  // operations in terms of relative paths, and fakes file watching by triggering
    83  // events on file operations.
    84  type Workdir struct {
    85  	RelativeTo
    86  
    87  	watcherMu sync.Mutex
    88  	watchers  []func(context.Context, []FileEvent)
    89  
    90  	fileMu sync.Mutex
    91  	files  map[string]string
    92  }
    93  
    94  // NewWorkdir writes the txtar-encoded file data in txt to dir, and returns a
    95  // Workir for operating on these files using
    96  func NewWorkdir(dir string) *Workdir {
    97  	return &Workdir{RelativeTo: RelativeTo(dir)}
    98  }
    99  
   100  func hashFile(data []byte) string {
   101  	return fmt.Sprintf("%x", sha256.Sum256(data))
   102  }
   103  
   104  func (w *Workdir) writeInitialFiles(files map[string][]byte) error {
   105  	w.files = map[string]string{}
   106  	for name, data := range files {
   107  		w.files[name] = hashFile(data)
   108  		if err := WriteFileData(name, data, w.RelativeTo); err != nil {
   109  			return errors.Errorf("writing to workdir: %w", err)
   110  		}
   111  	}
   112  	return nil
   113  }
   114  
   115  // RootURI returns the root URI for this working directory of this scratch
   116  // environment.
   117  func (w *Workdir) RootURI() protocol.DocumentURI {
   118  	return toURI(string(w.RelativeTo))
   119  }
   120  
   121  // AddWatcher registers the given func to be called on any file change.
   122  func (w *Workdir) AddWatcher(watcher func(context.Context, []FileEvent)) {
   123  	w.watcherMu.Lock()
   124  	w.watchers = append(w.watchers, watcher)
   125  	w.watcherMu.Unlock()
   126  }
   127  
   128  // URI returns the URI to a the workdir-relative path.
   129  func (w *Workdir) URI(path string) protocol.DocumentURI {
   130  	return toURI(w.AbsPath(path))
   131  }
   132  
   133  // URIToPath converts a uri to a workdir-relative path (or an absolute path,
   134  // if the uri is outside of the workdir).
   135  func (w *Workdir) URIToPath(uri protocol.DocumentURI) string {
   136  	fp := uri.SpanURI().Filename()
   137  	return w.RelPath(fp)
   138  }
   139  
   140  func toURI(fp string) protocol.DocumentURI {
   141  	return protocol.DocumentURI(span.URIFromPath(fp))
   142  }
   143  
   144  // ReadFile reads a text file specified by a workdir-relative path.
   145  func (w *Workdir) ReadFile(path string) (string, error) {
   146  	backoff := 1 * time.Millisecond
   147  	for {
   148  		b, err := ioutil.ReadFile(w.AbsPath(path))
   149  		if err != nil {
   150  			if runtime.GOOS == "plan9" && strings.HasSuffix(err.Error(), " exclusive use file already open") {
   151  				// Plan 9 enforces exclusive access to locked files.
   152  				// Give the owner time to unlock it and retry.
   153  				time.Sleep(backoff)
   154  				backoff *= 2
   155  				continue
   156  			}
   157  			return "", err
   158  		}
   159  		return string(b), nil
   160  	}
   161  }
   162  
   163  func (w *Workdir) RegexpRange(path, re string) (Pos, Pos, error) {
   164  	content, err := w.ReadFile(path)
   165  	if err != nil {
   166  		return Pos{}, Pos{}, err
   167  	}
   168  	return regexpRange(content, re)
   169  }
   170  
   171  // RegexpSearch searches the file corresponding to path for the first position
   172  // matching re.
   173  func (w *Workdir) RegexpSearch(path string, re string) (Pos, error) {
   174  	content, err := w.ReadFile(path)
   175  	if err != nil {
   176  		return Pos{}, err
   177  	}
   178  	start, _, err := regexpRange(content, re)
   179  	return start, err
   180  }
   181  
   182  // ChangeFilesOnDisk executes the given on-disk file changes in a batch,
   183  // simulating the action of changing branches outside of an editor.
   184  func (w *Workdir) ChangeFilesOnDisk(ctx context.Context, events []FileEvent) error {
   185  	for _, e := range events {
   186  		switch e.ProtocolEvent.Type {
   187  		case protocol.Deleted:
   188  			fp := w.AbsPath(e.Path)
   189  			if err := os.Remove(fp); err != nil {
   190  				return errors.Errorf("removing %q: %w", e.Path, err)
   191  			}
   192  		case protocol.Changed, protocol.Created:
   193  			if _, err := w.writeFile(ctx, e.Path, e.Content); err != nil {
   194  				return err
   195  			}
   196  		}
   197  	}
   198  	w.sendEvents(ctx, events)
   199  	return nil
   200  }
   201  
   202  // RemoveFile removes a workdir-relative file path.
   203  func (w *Workdir) RemoveFile(ctx context.Context, path string) error {
   204  	fp := w.AbsPath(path)
   205  	if err := os.RemoveAll(fp); err != nil {
   206  		return errors.Errorf("removing %q: %w", path, err)
   207  	}
   208  	w.fileMu.Lock()
   209  	defer w.fileMu.Unlock()
   210  
   211  	evts := []FileEvent{{
   212  		Path: path,
   213  		ProtocolEvent: protocol.FileEvent{
   214  			URI:  w.URI(path),
   215  			Type: protocol.Deleted,
   216  		},
   217  	}}
   218  	w.sendEvents(ctx, evts)
   219  	delete(w.files, path)
   220  	return nil
   221  }
   222  
   223  func (w *Workdir) sendEvents(ctx context.Context, evts []FileEvent) {
   224  	if len(evts) == 0 {
   225  		return
   226  	}
   227  	w.watcherMu.Lock()
   228  	watchers := make([]func(context.Context, []FileEvent), len(w.watchers))
   229  	copy(watchers, w.watchers)
   230  	w.watcherMu.Unlock()
   231  	for _, w := range watchers {
   232  		w(ctx, evts)
   233  	}
   234  }
   235  
   236  // WriteFiles writes the text file content to workdir-relative paths.
   237  // It batches notifications rather than sending them consecutively.
   238  func (w *Workdir) WriteFiles(ctx context.Context, files map[string]string) error {
   239  	var evts []FileEvent
   240  	for filename, content := range files {
   241  		evt, err := w.writeFile(ctx, filename, content)
   242  		if err != nil {
   243  			return err
   244  		}
   245  		evts = append(evts, evt)
   246  	}
   247  	w.sendEvents(ctx, evts)
   248  	return nil
   249  }
   250  
   251  // WriteFile writes text file content to a workdir-relative path.
   252  func (w *Workdir) WriteFile(ctx context.Context, path, content string) error {
   253  	evt, err := w.writeFile(ctx, path, content)
   254  	if err != nil {
   255  		return err
   256  	}
   257  	w.sendEvents(ctx, []FileEvent{evt})
   258  	return nil
   259  }
   260  
   261  func (w *Workdir) writeFile(ctx context.Context, path, content string) (FileEvent, error) {
   262  	fp := w.AbsPath(path)
   263  	_, err := os.Stat(fp)
   264  	if err != nil && !os.IsNotExist(err) {
   265  		return FileEvent{}, errors.Errorf("checking if %q exists: %w", path, err)
   266  	}
   267  	var changeType protocol.FileChangeType
   268  	if os.IsNotExist(err) {
   269  		changeType = protocol.Created
   270  	} else {
   271  		changeType = protocol.Changed
   272  	}
   273  	if err := WriteFileData(path, []byte(content), w.RelativeTo); err != nil {
   274  		return FileEvent{}, err
   275  	}
   276  	return FileEvent{
   277  		Path: path,
   278  		ProtocolEvent: protocol.FileEvent{
   279  			URI:  w.URI(path),
   280  			Type: changeType,
   281  		},
   282  	}, nil
   283  }
   284  
   285  // listFiles lists files in the given directory, returning a map of relative
   286  // path to modification time.
   287  func (w *Workdir) listFiles(dir string) (map[string]string, error) {
   288  	files := make(map[string]string)
   289  	absDir := w.AbsPath(dir)
   290  	if err := filepath.Walk(absDir, func(fp string, info os.FileInfo, err error) error {
   291  		if err != nil {
   292  			return err
   293  		}
   294  		if info.IsDir() {
   295  			return nil
   296  		}
   297  		path := w.RelPath(fp)
   298  		data, err := ioutil.ReadFile(fp)
   299  		if err != nil {
   300  			return err
   301  		}
   302  		files[path] = hashFile(data)
   303  		return nil
   304  	}); err != nil {
   305  		return nil, err
   306  	}
   307  	return files, nil
   308  }
   309  
   310  // CheckForFileChanges walks the working directory and checks for any files
   311  // that have changed since the last poll.
   312  func (w *Workdir) CheckForFileChanges(ctx context.Context) error {
   313  	evts, err := w.pollFiles()
   314  	if err != nil {
   315  		return err
   316  	}
   317  	w.sendEvents(ctx, evts)
   318  	return nil
   319  }
   320  
   321  // pollFiles updates w.files and calculates FileEvents corresponding to file
   322  // state changes since the last poll. It does not call sendEvents.
   323  func (w *Workdir) pollFiles() ([]FileEvent, error) {
   324  	w.fileMu.Lock()
   325  	defer w.fileMu.Unlock()
   326  
   327  	files, err := w.listFiles(".")
   328  	if err != nil {
   329  		return nil, err
   330  	}
   331  	var evts []FileEvent
   332  	// Check which files have been added or modified.
   333  	for path, hash := range files {
   334  		oldhash, ok := w.files[path]
   335  		delete(w.files, path)
   336  		var typ protocol.FileChangeType
   337  		switch {
   338  		case !ok:
   339  			typ = protocol.Created
   340  		case oldhash != hash:
   341  			typ = protocol.Changed
   342  		default:
   343  			continue
   344  		}
   345  		evts = append(evts, FileEvent{
   346  			Path: path,
   347  			ProtocolEvent: protocol.FileEvent{
   348  				URI:  w.URI(path),
   349  				Type: typ,
   350  			},
   351  		})
   352  	}
   353  	// Any remaining files must have been deleted.
   354  	for path := range w.files {
   355  		evts = append(evts, FileEvent{
   356  			Path: path,
   357  			ProtocolEvent: protocol.FileEvent{
   358  				URI:  w.URI(path),
   359  				Type: protocol.Deleted,
   360  			},
   361  		})
   362  	}
   363  	w.files = files
   364  	return evts, nil
   365  }