vfs/util.go

86 lines
2.2 KiB
Go
Raw Normal View History

2015-05-28 09:20:38 +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
2015-06-17 11:18:55 +00:00
import (
2015-06-19 08:51:10 +00:00
"errors"
"fmt"
2015-06-17 11:18:55 +00:00
"io"
"os"
2015-06-19 08:51:10 +00:00
"regexp"
"strconv"
2015-06-17 11:18:55 +00:00
"sync/atomic"
2015-06-19 08:51:10 +00:00
"time"
2015-06-21 02:19:37 +00:00
"bazil.org/fuse"
2015-06-17 11:18:55 +00:00
)
2015-05-28 09:20:38 +00:00
var inodeCnt, handleCnt uint64
2015-05-28 09:20:38 +00:00
func allocInode() uint64 {
return atomic.AddUint64(&inodeCnt, 1)
}
2015-06-17 11:18:55 +00:00
2015-06-21 02:19:37 +00:00
func allocHandleId() fuse.HandleID {
return fuse.HandleID(atomic.AddUint64(&handleCnt, 1))
}
2015-06-19 08:51:10 +00:00
func copyFile(src, dst string) (int64, error) {
2015-06-17 11:18:55 +00:00
srcFile, err := os.Open(src)
if err != nil {
return 0, err
}
defer srcFile.Close()
dstFile, err := os.Create(dst)
if err != nil {
return 0, err
}
defer dstFile.Close()
return io.Copy(srcFile, dstFile)
}
2015-06-19 08:51:10 +00:00
func buildVerName(timestamp time.Time) string {
return fmt.Sprintf("ver_%.16x", timestamp.Unix())
}
func parseVerName(name string) (time.Time, error) {
re, err := regexp.Compile(`ver_([0-9a-f]+)$`)
if err != nil {
return time.Unix(0, 0), err
}
matches := re.FindStringSubmatch(name)
if len(matches) < 2 {
return time.Unix(0, 0), errors.New("invalid version identifier")
}
timestamp, err := strconv.ParseInt(matches[1], 16, 64)
if err != nil {
return time.Unix(0, 0), err
}
return time.Unix(timestamp, 0), nil
}