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-06-23 07:36:57 +00:00
|
|
|
"path"
|
2015-05-16 07:07:45 +00:00
|
|
|
"strings"
|
2015-06-23 07:36:57 +00:00
|
|
|
"sync"
|
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-23 07:36:57 +00:00
|
|
|
type verFmt struct {
|
2015-05-15 04:31:57 +00:00
|
|
|
Deleted []string `json:"deleted"`
|
2015-06-23 07:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type verMeta struct {
|
|
|
|
path string
|
|
|
|
deleted map[string]bool
|
|
|
|
modified bool
|
|
|
|
mutex sync.Mutex
|
2015-05-15 04:31:57 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 08:51:10 +00:00
|
|
|
func newVerMeta(path string) (*verMeta, error) {
|
2015-06-23 07:36:57 +00:00
|
|
|
meta := &verMeta{path, make(map[string]bool), false, sync.Mutex{}}
|
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-23 07:36:57 +00:00
|
|
|
for path, deleted := range m.deleted {
|
|
|
|
if !deleted {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-05-16 07:07:45 +00:00
|
|
|
for name, node := range nodes {
|
2015-06-23 07:36:57 +00:00
|
|
|
if strings.HasPrefix(node.path, path) {
|
2015-05-16 07:07:45 +00:00
|
|
|
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-23 07:36:57 +00:00
|
|
|
m.mutex.Lock()
|
|
|
|
m.deleted[path] = true
|
|
|
|
m.mutex.Unlock()
|
|
|
|
|
|
|
|
m.modified = 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-23 07:36:57 +00:00
|
|
|
m.mutex.Lock()
|
|
|
|
m.deleted[path] = false
|
|
|
|
m.mutex.Unlock()
|
|
|
|
|
|
|
|
m.modified = 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-23 07:36:57 +00:00
|
|
|
m.modified = 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
|
|
|
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-23 07:36:57 +00:00
|
|
|
var vd verFmt
|
|
|
|
if err := json.Unmarshal(bytes, &vd); err != nil {
|
2015-05-15 04:31:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-23 07:36:57 +00:00
|
|
|
m.deleted = make(map[string]bool)
|
|
|
|
for _, path := range vd.Deleted {
|
|
|
|
m.deleted[path] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
m.modified = false
|
2015-05-15 04:31:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-23 07:36:57 +00:00
|
|
|
func (m *verMeta) checkRedundantDel(child string) bool {
|
|
|
|
parent := path.Dir(child)
|
|
|
|
if parent == "/" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if deleted, _ := m.deleted[parent]; deleted {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.checkRedundantDel(parent)
|
|
|
|
}
|
|
|
|
|
2015-06-19 08:51:10 +00:00
|
|
|
func (m *verMeta) save() error {
|
2015-06-23 07:36:57 +00:00
|
|
|
if !m.modified {
|
2015-06-17 07:50:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-23 07:36:57 +00:00
|
|
|
var vd verFmt
|
|
|
|
for path, deleted := range m.deleted {
|
|
|
|
if !deleted {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.checkRedundantDel(path) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
vd.Deleted = append(vd.Deleted, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
js, err := json.Marshal(vd)
|
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
|
|
|
}
|