فهرست منبع

Convenience method Session.RemoveFile

Source commit: c7e3a0866633669f2e08fe6516042f59707843ad
Martin Prikryl 5 سال پیش
والد
کامیت
9d02c4f0ce
3فایلهای تغییر یافته به همراه53 افزوده شده و 18 حذف شده
  1. 32 1
      dotnet/Session.cs
  2. 19 16
      source/core/Script.cpp
  3. 2 1
      source/core/Script.h

+ 32 - 1
dotnet/Session.cs

@@ -954,10 +954,18 @@ namespace WinSCP
         public RemovalOperationResult RemoveFiles(string path)
         {
             using (Logger.CreateCallstackAndLock())
+            {
+                return DoRemoveFiles(path, string.Empty);
+            }
+        }
+
+        private RemovalOperationResult DoRemoveFiles(string path, string additionalParams)
+        {
+            using (Logger.CreateCallstack())
             {
                 CheckOpened();
 
-                WriteCommand(string.Format(CultureInfo.InvariantCulture, "rm -- \"{0}\"", Tools.ArgumentEscape(path)));
+                WriteCommand(string.Format(CultureInfo.InvariantCulture, "rm {0} -- \"{1}\"", additionalParams, Tools.ArgumentEscape(path)));
 
                 RemovalOperationResult result = new RemovalOperationResult();
 
@@ -977,6 +985,29 @@ namespace WinSCP
             }
         }
 
+        public RemovalEventArgs RemoveFile(string path)
+        {
+            using (Logger.CreateCallstackAndLock())
+            {
+                if (string.IsNullOrEmpty(path))
+                {
+                    throw Logger.WriteException(new ArgumentException("File to path cannot be empty", nameof(path)));
+                }
+
+                string remoteDirectory = RemotePath.GetDirectoryName(path);
+                string filemask = RemotePath.EscapeFileMask(RemotePath.GetFileName(path));
+                if (string.IsNullOrEmpty(filemask))
+                {
+                    throw Logger.WriteException(new ArgumentException("File name cannot be empty", nameof(path)));
+                }
+                path = RemotePath.Combine(remoteDirectory, filemask);
+
+                RemovalOperationResult operationResult = DoRemoveFiles(path, "-onlyfile");
+                operationResult.Check();
+                return GetOnlyFileOperation(operationResult.Removals);
+            }
+        }
+
         public SynchronizationResult SynchronizeDirectories(
             SynchronizationMode mode, string localPath, string remotePath,
             bool removeFiles, bool mirror = false, SynchronizationCriteria criteria = SynchronizationCriteria.Time,

+ 19 - 16
source/core/Script.cpp

@@ -352,7 +352,7 @@ void __fastcall TScript::Init()
   FCommands->Register(L"cd", SCRIPT_CD_DESC, SCRIPT_CD_HELP, &CdProc, 0, 1, false);
   FCommands->Register(L"ls", SCRIPT_LS_DESC, SCRIPT_LS_HELP2, &LsProc, 0, 1, false);
   FCommands->Register(L"dir", 0, SCRIPT_LS_HELP2, &LsProc, 0, 1, false);
-  FCommands->Register(L"rm", SCRIPT_RM_DESC, SCRIPT_RM_HELP2, &RmProc, 1, -1, false);
+  FCommands->Register(L"rm", SCRIPT_RM_DESC, SCRIPT_RM_HELP2, &RmProc, 1, -1, true);
   FCommands->Register(L"rmdir", SCRIPT_RMDIR_DESC, SCRIPT_RMDIR_HELP, &RmDirProc, 1, -1, false);
   FCommands->Register(L"mv", SCRIPT_MV_DESC, SCRIPT_MV_HELP2, &MvProc, 2, -1, false);
   FCommands->Register(L"rename", 0, SCRIPT_MV_HELP2, &MvProc, 2, -1, false);
@@ -709,6 +709,19 @@ TStrings * __fastcall TScript::CreateFileList(TScriptProcParams * Parameters, in
     Result->AddObject(Path, File);
   }
 
+  if (FLAGSET(ListType, fltOnlyFile))
+  {
+    // For internal use by .NET assembly
+    for (int Index = 0; Index < Result->Count; Index++)
+    {
+      TRemoteFile * File = dynamic_cast<TRemoteFile *>(Result->Objects[Index]);
+      if (File->IsDirectory)
+      {
+        throw Exception(FMTLOAD(NOT_FILE_ERROR, (File->FileName)));
+      }
+    }
+  }
+
   return Result;
 }
 //---------------------------------------------------------------------------
@@ -1311,11 +1324,13 @@ void __fastcall TScript::RmProc(TScriptProcParams * Parameters)
 {
   CheckSession();
 
+  bool OnlyFile = Parameters->FindSwitch(L"onlyfile");
   TStrings * FileList = CreateFileList(
     Parameters, 1, Parameters->ParamCount,
-    (TFileListType)(fltQueryServer | fltMask));
+    (TFileListType)(fltQueryServer | fltMask| FLAGMASK(OnlyFile, fltOnlyFile)));
   try
   {
+    CheckParams(Parameters);
     FTerminal->DeleteFiles(FileList);
   }
   __finally
@@ -1437,6 +1452,7 @@ void __fastcall TScript::GetProc(TScriptProcParams * Parameters)
   ResetTransfer();
 
   bool Latest = Parameters->FindSwitch(L"latest");
+  bool OnlyFile = Parameters->FindSwitch(L"onlyfile");
   CheckDefaultCopyParam();
   TCopyParamType CopyParam = FCopyParam;
   CopyParamParams(CopyParam, Parameters);
@@ -1446,22 +1462,9 @@ void __fastcall TScript::GetProc(TScriptProcParams * Parameters)
   RequireParams(Parameters, 1);
   int LastFileParam = (Parameters->ParamCount == 1 ? 1 : Parameters->ParamCount - 1);
   TStrings * FileList = CreateFileList(Parameters, 1, LastFileParam,
-    (TFileListType)(fltQueryServer | fltMask | FLAGMASK(Latest, fltLatest)));
+    (TFileListType)(fltQueryServer | fltMask | FLAGMASK(Latest, fltLatest) | FLAGMASK(OnlyFile, fltOnlyFile)));
   try
   {
-    // For internal use by .NET Session.GetFileToDirectory
-    if (Parameters->FindSwitch(L"onlyfile"))
-    {
-      for (int Index = 0; Index < FileList->Count; Index++)
-      {
-        TRemoteFile * File = dynamic_cast<TRemoteFile *>(FileList->Objects[Index]);
-        if (File->IsDirectory)
-        {
-          throw Exception(FMTLOAD(NOT_FILE_ERROR, (File->FileName)));
-        }
-      }
-    }
-
     UnicodeString TargetDirectory;
     if (Parameters->ParamCount == 1)
     {

+ 2 - 1
source/core/Script.h

@@ -122,7 +122,8 @@ protected:
     fltDirectories = 0x01,
     fltQueryServer = 0x02,
     fltMask =        0x04,
-    fltLatest =      0x08
+    fltLatest =      0x08,
+    fltOnlyFile =    0x10,
   };
   TStrings * __fastcall CreateFileList(TScriptProcParams * Parameters, int Start,
     int End, TFileListType ListType = fltDefault);