simple.go 2.56 KB
Newer Older
Jakob Borg's avatar
Jakob Borg committed
1 2 3
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
// All rights reserved. Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
Jakob Borg's avatar
Jakob Borg committed
4

5 6 7 8 9 10 11 12
package versioner

import (
	"os"
	"path/filepath"
	"sort"
	"strconv"

13
	"source.ind.ie/project/pulse/osutil"
14 15 16 17 18 19 20 21 22
)

func init() {
	// Register the constructor for this type of versioner with the name "simple"
	Factories["simple"] = NewSimple
}

// The type holds our configuration
type Simple struct {
23 24
	keep     int
	repoPath string
25 26 27
}

// The constructor function takes a map of parameters and creates the type.
28
func NewSimple(repoID, repoPath string, params map[string]string) Versioner {
29 30 31 32 33 34
	keep, err := strconv.Atoi(params["keep"])
	if err != nil {
		keep = 5 // A reasonable default
	}

	s := Simple{
35 36
		keep:     keep,
		repoPath: repoPath,
37 38 39 40 41 42 43 44 45 46
	}

	if debug {
		l.Debugf("instantiated %#v", s)
	}
	return s
}

// Move away the named file to a version archive. If this function returns
// nil, the named file does not exist any more (has been archived).
47
func (v Simple) Archive(filePath string) error {
48
	fileInfo, err := os.Stat(filePath)
Alexander Graf's avatar
Alexander Graf committed
49 50 51 52 53 54 55 56
	if err != nil {
		if os.IsNotExist(err) {
			if debug {
				l.Debugln("not archiving nonexistent file", filePath)
			}
			return nil
		} else {
			return err
57
		}
58 59
	}

60
	versionsDir := filepath.Join(v.repoPath, ".stversions")
61 62 63 64 65 66 67 68 69 70 71 72 73
	_, err = os.Stat(versionsDir)
	if err != nil {
		if os.IsNotExist(err) {
			if debug {
				l.Debugln("creating versions dir", versionsDir)
			}
			os.MkdirAll(versionsDir, 0755)
			osutil.HideFile(versionsDir)
		} else {
			return err
		}
	}

74
	if debug {
75
		l.Debugln("archiving", filePath)
76 77
	}

78
	file := filepath.Base(filePath)
79
	inRepoPath, err := filepath.Rel(v.repoPath, filepath.Dir(filePath))
80 81 82 83 84
	if err != nil {
		return err
	}

	dir := filepath.Join(versionsDir, inRepoPath)
85 86 87 88 89
	err = os.MkdirAll(dir, 0755)
	if err != nil && !os.IsExist(err) {
		return err
	}

90
	ver := file + "~" + fileInfo.ModTime().Format("20060102-150405")
91 92 93 94 95
	dst := filepath.Join(dir, ver)
	if debug {
		l.Debugln("moving to", dst)
	}
	err = osutil.Rename(filePath, dst)
96 97 98 99
	if err != nil {
		return err
	}

100
	versions, err := filepath.Glob(filepath.Join(dir, file+"~[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9]"))
101
	if err != nil {
102
		l.Warnln("globbing:", err)
103 104 105 106 107 108
		return nil
	}

	if len(versions) > v.keep {
		sort.Strings(versions)
		for _, toRemove := range versions[:len(versions)-v.keep] {
109 110 111
			if debug {
				l.Debugln("cleaning out", toRemove)
			}
112 113
			err = os.Remove(toRemove)
			if err != nil {
114
				l.Warnln("removing old version:", err)
115 116 117 118 119 120
			}
		}
	}

	return nil
}