forked from opensciencegrid/stashcp
-
Notifications
You must be signed in to change notification settings - Fork 6
/
handle_cvmfs.go
68 lines (55 loc) · 1.5 KB
/
handle_cvmfs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package stashcp
import (
"errors"
"io"
"os"
"path"
log "github.com/sirupsen/logrus"
)
func download_cvmfs(sourceFile string, destination string, payload *payloadStruct) (int64, error) {
//Check if file is available in cvfms
var cvmfs_file string = path.Join("/cvmfs/stash.osgstorage.org", sourceFile)
// Log
log.Debugf("Checking if the CVMFS file exists: %s", cvmfs_file)
if _, err := os.Stat(cvmfs_file); !os.IsNotExist(err) {
// If path exists
in, err := os.Open(cvmfs_file)
if err != nil {
log.Debugln("Failed to open the source file:", err)
return 0, err
}
defer in.Close()
out, err := os.Create(destination)
if err != nil {
log.Debugln("Failed to create destination file:", err)
return 0, err
}
defer out.Close()
_, err = io.Copy(out, in)
if err != nil {
log.Debugln("Copy of file failed:", err)
return 0, err
}
err = out.Close()
if err != nil {
log.Debugln("Error while closing output file:", err)
return 0, err
}
log.Debug("Succesfully copied file from CVMFS!")
// var end1 int32 = int32(time.Now().Unix())
// payload := payloadStruct{tries: 1, cache: "CVMFS", host: "CVMFS"}
if err != nil {
log.Warnf("Unable to copy with CVMFS, even though file exists: %s", err)
return 0, err
}
} else {
log.Debugf("CVMFS File does not exist")
return 0, errors.New("CVMFS File does not exist")
}
// Get the size of the destination
dest_stat, err := os.Stat(destination)
if err != nil {
return 0, err
}
return dest_stat.Size(), nil
}