25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

63 satır
1.7 KiB

  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file or at
  5. // https://developers.google.com/open-source/licenses/bsd.
  6. package gosrc
  7. import (
  8. "bytes"
  9. "go/build"
  10. "io"
  11. "io/ioutil"
  12. "os"
  13. "path"
  14. "strings"
  15. "time"
  16. )
  17. // Import returns details about the package in the directory.
  18. func (dir *Directory) Import(ctx *build.Context, mode build.ImportMode) (*build.Package, error) {
  19. safeCopy := *ctx
  20. ctx = &safeCopy
  21. ctx.JoinPath = path.Join
  22. ctx.IsAbsPath = path.IsAbs
  23. ctx.SplitPathList = func(list string) []string { return strings.Split(list, ":") }
  24. ctx.IsDir = func(path string) bool { return false }
  25. ctx.HasSubdir = func(root, dir string) (rel string, ok bool) { return "", false }
  26. ctx.ReadDir = dir.readDir
  27. ctx.OpenFile = dir.openFile
  28. return ctx.ImportDir(".", mode)
  29. }
  30. type fileInfo struct{ f *File }
  31. func (fi fileInfo) Name() string { return fi.f.Name }
  32. func (fi fileInfo) Size() int64 { return int64(len(fi.f.Data)) }
  33. func (fi fileInfo) Mode() os.FileMode { return 0 }
  34. func (fi fileInfo) ModTime() time.Time { return time.Time{} }
  35. func (fi fileInfo) IsDir() bool { return false }
  36. func (fi fileInfo) Sys() interface{} { return nil }
  37. func (dir *Directory) readDir(name string) ([]os.FileInfo, error) {
  38. if name != "." {
  39. return nil, os.ErrNotExist
  40. }
  41. fis := make([]os.FileInfo, len(dir.Files))
  42. for i, f := range dir.Files {
  43. fis[i] = fileInfo{f}
  44. }
  45. return fis, nil
  46. }
  47. func (dir *Directory) openFile(path string) (io.ReadCloser, error) {
  48. name := strings.TrimPrefix(path, "./")
  49. for _, f := range dir.Files {
  50. if f.Name == name {
  51. return ioutil.NopCloser(bytes.NewReader(f.Data)), nil
  52. }
  53. }
  54. return nil, os.ErrNotExist
  55. }