2 * Copyright (C) 2017-2018 "IoT.bzh"
3 * Author Sebastien Douheret <sebastien@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
27 common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib"
28 "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1"
29 uuid "github.com/satori/go.uuid"
32 // IFOLDER interface implementation for native/path mapping folders
37 fConfig xsapiv1.FolderConfig
40 // NewFolderPathMap Create a new instance of PathMap
41 func NewFolderPathMap(ctx *Context) *PathMap {
44 fConfig: xsapiv1.FolderConfig{
45 Status: xsapiv1.StatusDisable,
52 func (f *PathMap) NewUID(suffix string) string {
53 uuid := uuid.NewV1().String()
61 func (f *PathMap) Add(cfg xsapiv1.FolderConfig) (*xsapiv1.FolderConfig, error) {
65 // Setup Setup local project config
66 func (f *PathMap) Setup(cfg xsapiv1.FolderConfig) (*xsapiv1.FolderConfig, error) {
68 if cfg.DataPathMap.ServerPath == "" {
69 return nil, fmt.Errorf("ServerPath must be set")
72 // Use shareRootDir if ServerPath is a relative path
73 dir := cfg.DataPathMap.ServerPath
74 if !filepath.IsAbs(dir) {
75 dir = filepath.Join(f.Config.FileConf.ShareRootDir, dir)
79 if !common.Exists(dir) {
80 // try to create if not existing
81 if err := os.MkdirAll(dir, 0755); err != nil {
82 return nil, fmt.Errorf("Cannot create ServerPath directory: %s", dir)
85 if !common.Exists(dir) {
86 return nil, fmt.Errorf("ServerPath directory is not accessible: %s", dir)
90 f.fConfig.RootPath = dir
91 f.fConfig.DataPathMap.ServerPath = dir
92 f.fConfig.IsInSync = true
94 // Verify file created by XDS agent when needed
95 if cfg.DataPathMap.CheckFile != "" {
96 errMsg := "ServerPath sanity check error (%d): %v"
97 ckFile := f.ConvPathCli2Svr(cfg.DataPathMap.CheckFile)
98 if !common.Exists(ckFile) {
99 return nil, fmt.Errorf(errMsg, 1, "file not present")
101 if cfg.DataPathMap.CheckContent != "" {
102 fd, err := os.OpenFile(ckFile, os.O_APPEND|os.O_RDWR, 0600)
104 return nil, fmt.Errorf(errMsg, 2, err)
108 // Check specific message written by agent
109 content, err := ioutil.ReadAll(fd)
111 return nil, fmt.Errorf(errMsg, 3, err)
113 if string(content) != cfg.DataPathMap.CheckContent {
114 return nil, fmt.Errorf(errMsg, 4, "file content differ")
117 // Write a specific message that will be check back on agent side
118 msg := "Pathmap checked message written by xds-server ID: " + f.Config.ServerUID + "\n"
119 if n, err := fd.WriteString(msg); n != len(msg) || err != nil {
120 return nil, fmt.Errorf(errMsg, 5, err)
125 f.fConfig.Status = xsapiv1.StatusEnable
127 return &f.fConfig, nil
130 // GetConfig Get public part of folder config
131 func (f *PathMap) GetConfig() xsapiv1.FolderConfig {
135 // GetFullPath returns the full path of a directory (from server POV)
136 func (f *PathMap) GetFullPath(dir string) string {
138 return f.fConfig.DataPathMap.ServerPath
140 return filepath.Join(f.fConfig.DataPathMap.ServerPath, dir)
143 // ConvPathCli2Svr Convert path from Client to Server
144 func (f *PathMap) ConvPathCli2Svr(s string) string {
145 if f.fConfig.ClientPath != "" && f.fConfig.DataPathMap.ServerPath != "" {
146 return strings.Replace(s,
147 f.fConfig.ClientPath,
148 f.fConfig.DataPathMap.ServerPath,
154 // ConvPathSvr2Cli Convert path from Server to Client
155 func (f *PathMap) ConvPathSvr2Cli(s string) string {
156 if f.fConfig.ClientPath != "" && f.fConfig.DataPathMap.ServerPath != "" {
157 return strings.Replace(s,
158 f.fConfig.DataPathMap.ServerPath,
159 f.fConfig.ClientPath,
166 func (f *PathMap) Remove() error {
171 // Update update some fields of a folder
172 func (f *PathMap) Update(cfg xsapiv1.FolderConfig) (*xsapiv1.FolderConfig, error) {
173 if f.fConfig.ID != cfg.ID {
174 return nil, fmt.Errorf("Invalid id")
177 return &f.fConfig, nil
180 // Sync Force folder files synchronization
181 func (f *PathMap) Sync() error {
185 // IsInSync Check if folder files are in-sync
186 func (f *PathMap) IsInSync() (bool, error) {