2015-05-15 04:31:57 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Alex Yatskov <alex@foosoft.net>
|
|
|
|
* Author: Alex Yatskov <alex@foosoft.net>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
|
|
* the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-05-16 07:07:45 +00:00
|
|
|
"strings"
|
2015-05-15 04:31:57 +00:00
|
|
|
)
|
|
|
|
|
2015-05-28 09:20:38 +00:00
|
|
|
//
|
2015-06-19 08:51:10 +00:00
|
|
|
// verMeta
|
2015-05-28 09:20:38 +00:00
|
|
|
//
|
|
|
|
|
2015-06-19 08:51:10 +00:00
|
|
|
type verMeta struct {
|
2015-05-15 04:31:57 +00:00
|
|
|
Deleted []string `json:"deleted"`
|
|
|
|
path string
|
2015-06-17 07:50:07 +00:00
|
|
|
dirty bool
|
2015-05-15 04:31:57 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 08:51:10 +00:00
|
|
|
func newVerMeta(path string) (*verMeta, error) {
|
|
|
|
meta := &verMeta{path: path}
|
2015-05-15 04:31:57 +00:00
|
|
|
if err := meta.load(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return meta, nil
|
|
|
|
}
|
|
|
|
|
2015-06-19 08:51:10 +00:00
|
|
|
func (m *verMeta) filter(nodes verNodeMap) {
|
2015-06-17 11:18:55 +00:00
|
|
|
for _, delPath := range m.Deleted {
|
2015-05-16 07:07:45 +00:00
|
|
|
for name, node := range nodes {
|
|
|
|
if strings.HasPrefix(node.path, delPath) {
|
|
|
|
delete(nodes, name)
|
|
|
|
}
|
2015-05-15 04:31:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-19 08:51:10 +00:00
|
|
|
func (m *verMeta) removeNode(path string) {
|
2015-06-17 11:18:55 +00:00
|
|
|
m.Deleted = append(m.Deleted, path)
|
|
|
|
m.dirty = true
|
2015-06-17 07:50:07 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 08:51:10 +00:00
|
|
|
func (m *verMeta) createNode(path string) {
|
2015-06-17 11:18:55 +00:00
|
|
|
m.dirty = true
|
2015-06-17 11:11:16 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 08:51:10 +00:00
|
|
|
func (m *verMeta) modifyNode(path string) {
|
2015-06-17 11:18:55 +00:00
|
|
|
m.dirty = true
|
2015-06-17 07:50:07 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 08:51:10 +00:00
|
|
|
func (m *verMeta) load() error {
|
2015-06-17 11:18:55 +00:00
|
|
|
m.dirty = false
|
2015-06-17 07:50:07 +00:00
|
|
|
|
2015-06-17 11:18:55 +00:00
|
|
|
if _, err := os.Stat(m.path); os.IsNotExist(err) {
|
2015-05-15 04:31:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-17 11:18:55 +00:00
|
|
|
bytes, err := ioutil.ReadFile(m.path)
|
2015-05-15 04:31:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-17 11:18:55 +00:00
|
|
|
if err := json.Unmarshal(bytes, &m); err != nil {
|
2015-05-15 04:31:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-19 08:51:10 +00:00
|
|
|
func (m *verMeta) save() error {
|
2015-06-17 11:18:55 +00:00
|
|
|
if !m.dirty {
|
2015-06-17 07:50:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-17 11:18:55 +00:00
|
|
|
js, err := json.Marshal(m)
|
2015-05-15 04:31:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-17 11:18:55 +00:00
|
|
|
return ioutil.WriteFile(m.path, js, 0644)
|
2015-05-15 04:31:57 +00:00
|
|
|
}
|