github.com/pachyderm/pachyderm@v1.13.4/src/server/pfs/pfs.go (about)

     1  package pfs
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  
     7  	"github.com/pachyderm/pachyderm/src/client/pfs"
     8  	"github.com/pachyderm/pachyderm/src/client/pkg/grpcutil"
     9  )
    10  
    11  // ErrFileNotFound represents a file-not-found error.
    12  type ErrFileNotFound struct {
    13  	File *pfs.File
    14  }
    15  
    16  // ErrRepoNotFound represents a repo-not-found error.
    17  type ErrRepoNotFound struct {
    18  	Repo *pfs.Repo
    19  }
    20  
    21  // ErrRepoExists represents a repo-exists error.
    22  type ErrRepoExists struct {
    23  	Repo *pfs.Repo
    24  }
    25  
    26  // ErrRepoDeleted represents a repo-deleted error.
    27  type ErrRepoDeleted struct {
    28  	Repo *pfs.Repo
    29  }
    30  
    31  // ErrCommitNotFound represents a commit-not-found error.
    32  type ErrCommitNotFound struct {
    33  	Commit *pfs.Commit
    34  }
    35  
    36  // ErrNoHead represents an error encountered because a branch has no head (e.g.
    37  // inspectCommit(master) when 'master' has no commits)
    38  type ErrNoHead struct {
    39  	Branch *pfs.Branch
    40  }
    41  
    42  // ErrCommitExists represents an error where the commit already exists.
    43  type ErrCommitExists struct {
    44  	Commit *pfs.Commit
    45  }
    46  
    47  // ErrCommitFinished represents an error where the commit has been finished
    48  // (e.g from PutFile or DeleteFile)
    49  type ErrCommitFinished struct {
    50  	Commit *pfs.Commit
    51  }
    52  
    53  // ErrCommitDeleted represents an error where the commit has been deleted (e.g.
    54  // from InspectCommit)
    55  type ErrCommitDeleted struct {
    56  	Commit *pfs.Commit
    57  }
    58  
    59  // ErrParentCommitNotFound represents a parent-commit-not-found error.
    60  type ErrParentCommitNotFound struct {
    61  	Commit *pfs.Commit
    62  }
    63  
    64  // ErrOutputCommitNotFinished represents an error where the commit has not
    65  // been finished
    66  type ErrOutputCommitNotFinished struct {
    67  	Commit *pfs.Commit
    68  }
    69  
    70  // ErrCommitNotFinished represents an error where the commit has not been finished.
    71  type ErrCommitNotFinished struct {
    72  	Commit *pfs.Commit
    73  }
    74  
    75  func (e ErrFileNotFound) Error() string {
    76  	return fmt.Sprintf("file %v not found in repo %v at commit %v", e.File.Path, e.File.Commit.Repo.Name, e.File.Commit.ID)
    77  }
    78  
    79  func (e ErrRepoNotFound) Error() string {
    80  	return fmt.Sprintf("repo %v not found", e.Repo.Name)
    81  }
    82  
    83  func (e ErrRepoExists) Error() string {
    84  	return fmt.Sprintf("repo %v already exists", e.Repo.Name)
    85  }
    86  
    87  func (e ErrRepoDeleted) Error() string {
    88  	return fmt.Sprintf("repo %v was deleted", e.Repo.Name)
    89  }
    90  
    91  func (e ErrCommitNotFound) Error() string {
    92  	return fmt.Sprintf("commit %v not found in repo %v", e.Commit.ID, e.Commit.Repo.Name)
    93  }
    94  
    95  func (e ErrNoHead) Error() string {
    96  	// the dashboard is matching on this message in stats. Please open an issue on the dash before changing this
    97  	return fmt.Sprintf("the branch \"%s\" has no head (create one with 'start commit')", e.Branch.Name)
    98  }
    99  
   100  func (e ErrCommitExists) Error() string {
   101  	return fmt.Sprintf("commit %v already exists in repo %v", e.Commit.ID, e.Commit.Repo.Name)
   102  }
   103  
   104  func (e ErrCommitFinished) Error() string {
   105  	return fmt.Sprintf("commit %v in repo %v has already finished", e.Commit.ID, e.Commit.Repo.Name)
   106  }
   107  
   108  func (e ErrCommitDeleted) Error() string {
   109  	return fmt.Sprintf("commit %v/%v was deleted", e.Commit.Repo.Name, e.Commit.ID)
   110  }
   111  
   112  func (e ErrParentCommitNotFound) Error() string {
   113  	return fmt.Sprintf("parent commit %v not found in repo %v", e.Commit.ID, e.Commit.Repo.Name)
   114  }
   115  
   116  func (e ErrOutputCommitNotFinished) Error() string {
   117  	return fmt.Sprintf("output commit %v not finished", e.Commit.ID)
   118  }
   119  
   120  func (e ErrCommitNotFinished) Error() string {
   121  	return fmt.Sprintf("commit %v not finished", e.Commit.ID)
   122  }
   123  
   124  // ByteRangeSize returns byteRange.Upper - byteRange.Lower.
   125  func ByteRangeSize(byteRange *pfs.ByteRange) uint64 {
   126  	return byteRange.Upper - byteRange.Lower
   127  }
   128  
   129  var (
   130  	commitNotFoundRe          = regexp.MustCompile("commit [^ ]+ not found in repo [^ ]+")
   131  	commitDeletedRe           = regexp.MustCompile("commit [^ ]+/[^ ]+ was deleted")
   132  	commitFinishedRe          = regexp.MustCompile("commit [^ ]+ in repo [^ ]+ has already finished")
   133  	repoNotFoundRe            = regexp.MustCompile(`repos/ ?[a-zA-Z0-9.\-_]{1,255} not found`)
   134  	repoExistsRe              = regexp.MustCompile(`repo ?[a-zA-Z0-9.\-_]{1,255} already exists`)
   135  	branchNotFoundRe          = regexp.MustCompile(`branches/[a-zA-Z0-9.\-_]{1,255}/ [^ ]+ not found`)
   136  	fileNotFoundRe            = regexp.MustCompile(`file .+ not found`)
   137  	hasNoHeadRe               = regexp.MustCompile(`the branch .+ has no head \(create one with 'start commit'\)`)
   138  	outputCommitNotFinishedRe = regexp.MustCompile("output commit .+ not finished")
   139  	commitNotFinishedRe       = regexp.MustCompile("commit .+ not finished")
   140  )
   141  
   142  // IsCommitNotFoundErr returns true if 'err' has an error message that matches
   143  // ErrCommitNotFound
   144  func IsCommitNotFoundErr(err error) bool {
   145  	if err == nil {
   146  		return false
   147  	}
   148  	return commitNotFoundRe.MatchString(grpcutil.ScrubGRPC(err).Error())
   149  }
   150  
   151  // IsCommitDeletedErr returns true if 'err' has an error message that matches
   152  // ErrCommitDeleted
   153  func IsCommitDeletedErr(err error) bool {
   154  	if err == nil {
   155  		return false
   156  	}
   157  	return commitDeletedRe.MatchString(grpcutil.ScrubGRPC(err).Error())
   158  }
   159  
   160  // IsCommitFinishedErr returns true of 'err' has an error message that matches
   161  // ErrCommitFinished
   162  func IsCommitFinishedErr(err error) bool {
   163  	if err == nil {
   164  		return false
   165  	}
   166  	return commitFinishedRe.MatchString(grpcutil.ScrubGRPC(err).Error())
   167  }
   168  
   169  // IsRepoNotFoundErr returns true if 'err' is an error message about a repo
   170  // not being found
   171  func IsRepoNotFoundErr(err error) bool {
   172  	if err == nil {
   173  		return false
   174  	}
   175  	return repoNotFoundRe.MatchString(err.Error())
   176  }
   177  
   178  // IsRepoExistsErr returns true if 'err' is an error message about a repo
   179  // existing
   180  func IsRepoExistsErr(err error) bool {
   181  	if err == nil {
   182  		return false
   183  	}
   184  	return repoExistsRe.MatchString(err.Error())
   185  }
   186  
   187  // IsBranchNotFoundErr returns true if 'err' is an error message about a
   188  // branch not being found
   189  func IsBranchNotFoundErr(err error) bool {
   190  	if err == nil {
   191  		return false
   192  	}
   193  	return branchNotFoundRe.MatchString(err.Error())
   194  }
   195  
   196  // IsFileNotFoundErr returns true if 'err' is an error message about a PFS
   197  // file not being found
   198  func IsFileNotFoundErr(err error) bool {
   199  	if err == nil {
   200  		return false
   201  	}
   202  	return fileNotFoundRe.MatchString(err.Error())
   203  }
   204  
   205  // IsNoHeadErr returns true if the err is due to an operation that cannot be
   206  // performed on a headless branch
   207  func IsNoHeadErr(err error) bool {
   208  	if err == nil {
   209  		return false
   210  	}
   211  	return hasNoHeadRe.MatchString(err.Error())
   212  }
   213  
   214  // IsOutputCommitNotFinishedErr returns true if the err is due to an operation
   215  // that cannot be performed on an unfinished output commit
   216  func IsOutputCommitNotFinishedErr(err error) bool {
   217  	if err == nil {
   218  		return false
   219  	}
   220  	return outputCommitNotFinishedRe.MatchString(err.Error())
   221  }
   222  
   223  // IsCommitNotFinishedErr returns true if the err is due to an attempt at performing
   224  // an operation that only applies to finished commits on an unfinished commit.
   225  func IsCommitNotFinishedErr(err error) bool {
   226  	if err == nil {
   227  		return false
   228  	}
   229  	return commitNotFinishedRe.MatchString(err.Error())
   230  }