package fsutil

import (
	
	
	
	
	
	
)

// ErrClaimFileBadPattern is thrown when the pattern argument passed to
// ClaimFile does not contain exactly one asterisk.
var ErrClaimFileBadPattern = errors.New("ClaimFile: pattern must contain exactly one asterisk")

// ClaimFile takes a directory and a pattern string containing exactly one
// asterisk (e.g. "a*.log"). It opens a file in that directory, with a filename
// matching the template, with "*" replaced by a number. That number is one plus
// the largest of all existing files matching the template. If no such file
// exists, "*" is replaced by 1. The file is opened for read and write, with
// permission 0666 (before umask).
//
// For example, if the directory /tmp/elvish contains a1.log, a2.log and a9.log,
// calling ClaimFile("/tmp/elvish", "a*.log") will open a10.log. If the
// directory has no files matching the pattern, this same call will open a1.log.
//
// This function is useful for automatically determining unique names for log
// files. Unique filenames can also be derived by embedding the PID, but using
// this function preserves the chronical order of the files.
//
// This function is concurrency-safe: it always opens a new, unclaimed file and
// is not subject to race condition.
func (,  string) (*os.File, error) {
	if strings.Count(, "*") != 1 {
		return nil, ErrClaimFileBadPattern
	}
	 := strings.IndexByte(, '*')
	,  := [:], [+1:]
	,  := ioutil.ReadDir()
	if  != nil {
		return nil, 
	}
	 := 0
	for ,  := range  {
		 := .Name()
		if len() > len()+len() && strings.HasPrefix(, ) && strings.HasSuffix(, ) {
			 := [len() : len()-len()]
			if ,  := strconv.Atoi();  == nil {
				if  <  {
					 = 
				}
			}
		}
	}

	for  :=  + 1; ; ++ {
		 := filepath.Join(, +strconv.Itoa()+)
		,  := os.OpenFile(, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
		if  == nil {
			return , nil
		}
		if !os.IsExist() {
			return nil, 
		}
	}
}