(req *Request)
| 157 | } |
| 158 | |
| 159 | func (g *SmbClientGetter) smbclientGetFile(req *Request) error { |
| 160 | hostPath, filePath, err := g.findHostAndFilePath(req.u) |
| 161 | if err != nil { |
| 162 | return err |
| 163 | } |
| 164 | |
| 165 | // Get file and subdirectory name when existent |
| 166 | file := "" |
| 167 | if strings.Contains(filePath, "/") { |
| 168 | i := strings.LastIndex(filePath, "/") |
| 169 | file = filePath[i+1:] |
| 170 | filePath = filePath[:i] |
| 171 | } else { |
| 172 | file = filePath |
| 173 | filePath = "." |
| 174 | } |
| 175 | |
| 176 | cmdArgs := g.smbclientCmdArgs(req.u.User, hostPath, filePath) |
| 177 | // check file exists in the smb shared folder and is not a directory |
| 178 | isDir, err := g.isDirectory(cmdArgs, file) |
| 179 | if err != nil { |
| 180 | return err |
| 181 | } |
| 182 | if isDir { |
| 183 | return fmt.Errorf("%s source path must be a file", file) |
| 184 | } |
| 185 | |
| 186 | // download file |
| 187 | cmdArgs = append(cmdArgs, "-c") |
| 188 | if req.Dst != "" { |
| 189 | _, err := os.Lstat(req.Dst) |
| 190 | if err != nil { |
| 191 | if os.IsNotExist(err) { |
| 192 | // Create destination folder if it doesn't exist |
| 193 | if err := os.MkdirAll(filepath.Dir(req.Dst), 0755); err != nil { |
| 194 | return fmt.Errorf("failed to creat destination path: %s", err.Error()) |
| 195 | } |
| 196 | } else { |
| 197 | return err |
| 198 | } |
| 199 | } |
| 200 | cmdArgs = append(cmdArgs, fmt.Sprintf("get %s %s", file, req.Dst)) |
| 201 | } else { |
| 202 | cmdArgs = append(cmdArgs, fmt.Sprintf("get %s", file)) |
| 203 | } |
| 204 | |
| 205 | _, err = g.runSmbClientCommand("", cmdArgs) |
| 206 | return err |
| 207 | } |
| 208 | |
| 209 | func (g *SmbClientGetter) smbclientCmdArgs(used *url.Userinfo, hostPath string, fileDir string) (baseCmd []string) { |
| 210 | baseCmd = append(baseCmd, "-N") |
no test coverage detected