file.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package gannex
  2. import (
  3. "os"
  4. "path/filepath"
  5. "strings"
  6. "io"
  7. "fmt"
  8. "log"
  9. )
  10. type AFile struct {
  11. Filepath string
  12. OFilename string
  13. Info os.FileInfo
  14. }
  15. type AnnexFileNotFound struct {
  16. error
  17. }
  18. func NewAFile(annexpath, repopath, Ofilename string, APFileC []byte) (*AFile, error) {
  19. nAF := &AFile{OFilename: Ofilename}
  20. pathParts := strings.SplitAfter(string(APFileC), string(os.PathSeparator))
  21. filename := strings.TrimSpace(pathParts[len(pathParts)-1])
  22. // lets find the annex file
  23. filepath.Walk(filepath.Join(annexpath, repopath), func(path string, info os.FileInfo, err error) error {
  24. if err != nil {
  25. log.Printf("%v", err)
  26. return filepath.SkipDir
  27. }
  28. if info.IsDir() {
  29. return nil
  30. } else if info.Name() == filename {
  31. nAF.Filepath = path
  32. nAF.Info = info
  33. return io.EOF
  34. }
  35. return nil
  36. })
  37. if nAF.Filepath != "" {
  38. return nAF, nil
  39. } else {
  40. return nil, AnnexFileNotFound{error: fmt.Errorf("Could not find File: %s anywhere below: %s", filename,
  41. filepath.Join(annexpath, repopath))}
  42. }
  43. }
  44. func (af *AFile) Open() (*os.File, error) {
  45. fp, err := os.Open(af.Filepath)
  46. if err != nil {
  47. return nil, err
  48. }
  49. return fp, nil
  50. }