Exemplo n.º 1
0
        public void CopyFromVM(string remoteFilePathGlob, string localFilePath, bool recursive, bool force)
        {
            CheckProfileCanResolveSlave();

            Log(string.Format("Copying from remote path '{0}' to local path '{1}'{2}{3}.", localFilePath, remoteFilePathGlob,
                              recursive ? " recursively" : "",
                              force ? " force overwrite" : ""));

            var client = GetSlaveClient();

            EnumerateRequest enumerateRequest = new EnumerateRequest()
            {
                PathGlob  = remoteFilePathGlob,
                Recursive = recursive
            };
            EnumerateResponse enumerateResponse = client.Enumerate(enumerateRequest);

            if (enumerateResponse.Items.Count == 0 && !FileUtil.HasWildcard(remoteFilePathGlob))
            {
                throw new OperationFailedException()
                      {
                          Why = string.Format("Remote file{0} not found '{1}'.", recursive ? " or directory" : "", remoteFilePathGlob)
                      }
            }
            ;

            foreach (EnumerateItem item in enumerateResponse.Items)
            {
                string path = Path.Combine(localFilePath, item.RelativePath);

                if (item.Kind == EnumerateItemKind.DIRECTORY)
                {
                    if (!Directory.Exists(path))
                    {
                        Log(string.Format("Creating directory '{0}'.", item.RelativePath));
                        Directory.CreateDirectory(path);
                    }
                }
                else
                {
                    Log(string.Format("Copying '{0}'.", item.RelativePath));

                    ReadFileRequest request = new ReadFileRequest()
                    {
                        Path = item.FullPath
                    };
                    ReadFileResponse response = client.ReadFile(request);
                    if (!force && File.Exists(path))
                    {
                        throw new OperationFailedException()
                              {
                                  Why = string.Format("Local file already exists '{0}'.", path)
                              }
                    }
                    ;
                    File.WriteAllBytes(path, response.Contents);
                }
            }
        }
        public ReadFileResponse ReadFile(ReadFileRequest request)
        {
            if (request.NameFile == null)
            {
                return(null);
            }
            ReadFileResponse dataTable = new ReadFileResponse();

            string path = DirectoryPath + request.NameFile;

            try
            {
                dataTable.Result = Helpers.LoadData(path);
                return(dataTable);
            }
            catch (Exception)
            {
                Helpers.ReturnRequest();
                return(null);
            }
        }