Skip to content

Commit d620240

Browse files
committed
Merge pull request #14 from asimiqbal11/master
Fixed progress file issue in concurrent requests by adding a "Resume" switch parameter in Copy-SFItem command
2 parents 5b54ea7 + 89486c2 commit d620240

9 files changed

Lines changed: 71 additions & 39 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ packages/*
3434
Build/runbuild.txt
3535
docs/*
3636

37+
*/bin
38+
*/obj
Binary file not shown.
Binary file not shown.

ShareFileSnapIn/CopySfItem.cs

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -72,41 +72,57 @@ public string[] Path
7272
[Parameter(Mandatory = false)]
7373
public bool Force { get; set; }
7474

75+
/// <summary>
76+
/// Resume flag, if 'true' then it will keep track of command status
77+
/// if command is unsuccessful due to disconnection then it will prompt user next time to complete it first
78+
/// </summary>
79+
[Parameter()]
80+
[Alias("ResumeFlag")]
81+
public SwitchParameter Resume { get; set; }
82+
7583
protected override void ProcessRecord()
7684
{
7785
ResumeSupport = new Resume.ResumeSupport();
7886
FileSupport = new FileSupport(MarkFileStatus);
79-
80-
if (ResumeSupport.IsPending)
81-
{
82-
Logger.Instance.Info("Last command wasn't successful due to discconnection.");
8387

84-
Console.Write("Last command wasn't successful due to discconnection, enter 'y' to complete or any other key to ignore: ");
85-
Collection<PSObject> result = InvokeCommand.InvokeScript("Read-Host");
86-
string userOption = result != null && result.Count > 0 ? result[0].ToString() : string.Empty;
87-
88-
if (userOption.ToLower().Equals("y"))
88+
if (Resume)
89+
{
90+
if (ResumeSupport.IsPending)
8991
{
90-
Logger.Instance.Info("Copying those files which were missed due to discconnection with following parameters. Source Path:{0} Destination:{1} Forced:{2} Details:{3}",
91-
String.Join(",", ResumeSupport.GetPath), ResumeSupport.GetDestination, ResumeSupport.GetForce, ResumeSupport.GetDetails);
92+
Logger.Instance.Info("Last command wasn't successful due to discconnection.");
9293

93-
StartCopying(ResumeSupport.GetPath, ResumeSupport.GetDestination, ResumeSupport.GetForce, ResumeSupport.GetDetails);
94+
WriteObject("Last command wasn't successful due to discconnection, enter 'y' to complete or any other key to ignore: ");
95+
Collection<PSObject> result = InvokeCommand.InvokeScript("Read-Host");
96+
string userOption = result != null && result.Count > 0 ? result[0].ToString() : string.Empty;
9497

95-
Logger.Instance.Info("Copying operation completed for missing files");
96-
ResumeSupport.End();
97-
WriteObject("Last command completed, starting current operation");
98+
if (userOption.ToLower().Equals("y"))
99+
{
100+
Logger.Instance.Info("Copying those files which were missed due to discconnection with following parameters. Source Path:{0} Destination:{1} Forced:{2} Details:{3}",
101+
String.Join(",", ResumeSupport.GetPath), ResumeSupport.GetDestination, ResumeSupport.GetForce, ResumeSupport.GetDetails);
102+
103+
StartCopying(ResumeSupport.GetPath, ResumeSupport.GetDestination, ResumeSupport.GetForce, ResumeSupport.GetDetails);
104+
105+
Logger.Instance.Info("Copying operation completed for missing files");
106+
ResumeSupport.End();
107+
WriteObject("Last command completed, starting current operation");
108+
}
98109
}
99110
}
111+
else
112+
{
113+
ResumeSupport.UnmarkResumeFlag();
114+
}
100115

101-
Logger.Instance.Info(String.Format("Starting Copy Operation with following parameters. Source Path:{0} Destination:{1} Forced:{2} Details:{3}",
116+
Logger.Instance.Info(String.Format("Starting Copy Operation with following parameters. Source Path:{0} Destination:{1} Forced:{2} Details:{3}",
102117
String.Join(",", Path), Destination, Force, Details));
103-
ResumeSupport.Start(Path, Destination, Force, Details);
104118

119+
if (Resume) ResumeSupport.Start(Path, Destination, Force, Details);
120+
105121
StartCopying(Path, Destination.Trim(), Force, Details);
106122
Thread.Sleep(100);
107123

108-
ResumeSupport.End();
109-
Logger.Instance.Info("Command executed successfully");
124+
if (Resume) ResumeSupport.End();
125+
110126
WriteObject("Files copied successfully");
111127
}
112128

@@ -233,7 +249,7 @@ private void RecursiveUpload(ShareFileClient client, int uploadId, FileSystemInf
233249
{
234250
var newFolder = new Models.Folder() { Name = source.Name };
235251
newFolder = client.Items.CreateFolder(target.url, newFolder, Force || ResumeSupport.IsPending, false).Execute();
236-
252+
237253
ActionManager actionManager = new ActionManager(this, source.Name);
238254
ActionType actionType = Force ? ActionType.Force : ActionType.None;
239255

@@ -252,7 +268,7 @@ private void RecursiveUpload(ShareFileClient client, int uploadId, FileSystemInf
252268
}
253269
}
254270
}
255-
271+
256272
actionManager.Execute();
257273
}
258274
else if (source is FileInfo)
@@ -316,7 +332,10 @@ private void RecursiveDownload(ShareFileClient client, int downloadId, Models.It
316332

317333
private void MarkFileStatus(String fileName)
318334
{
319-
ResumeSupport.MarkFileStatus(fileName);
335+
if (Resume)
336+
{
337+
ResumeSupport.MarkFileStatus(fileName);
338+
}
320339
}
321340

322341
#region Previous Implementations (commented) > Upload/Download
@@ -410,7 +429,7 @@ private void MarkFileStatus(String fileName)
410429
// }
411430
// }
412431
//}
413-
432+
414433
#endregion
415434

416435
protected void RecursiveCopy(FileSystemInfo source, DirectoryInfo target)

ShareFileSnapIn/Parallel/DownloadAction.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class DownloadAction : IAction
2121
private int downloadId;
2222
private FileSystemInfo target;
2323
private ActionType actionType;
24-
private FileSupport fileSupport;
24+
private FileSupport fileSupportDelegate;
2525

2626
public DownloadAction(FileSupport fileSupport, Client.ShareFileClient client, int downloadId, Models.File child, FileSystemInfo target, ActionType type)
2727
{
@@ -30,7 +30,7 @@ public DownloadAction(FileSupport fileSupport, Client.ShareFileClient client, in
3030
this.downloadId = downloadId;
3131
this.target = target;
3232
this.actionType = type;
33-
this.fileSupport = fileSupport;
33+
this.fileSupportDelegate = fileSupport;
3434
}
3535

3636
void IAction.CopyFileItem(ProgressInfo progressInfo)
@@ -63,6 +63,7 @@ void IAction.CopyFileItem(ProgressInfo progressInfo)
6363
downloader.DownloadToAsync(fileStream).Wait();
6464

6565
fileStream.Close();
66+
fileSupportDelegate(fileName);
6667
}
6768
}
6869
}

ShareFileSnapIn/Parallel/UploadAction.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ class UploadAction : IAction
2424
private Client.ShareFileClient client;
2525
private Models.Item uploadTarget;
2626
private String details;
27-
private FileSupport copySfItem;
27+
private FileSupport fileSupportDelegate;
2828
private ActionType actionType;
2929

30-
public UploadAction(FileSupport cmdLet, Client.ShareFileClient client, FileSystemInfo source, Models.Item target, String details, ActionType type)
30+
public UploadAction(FileSupport fileSupport, Client.ShareFileClient client, FileSystemInfo source, Models.Item target, String details, ActionType type)
3131
{
3232
this.client = client;
3333
this.child = source;
3434
this.uploadTarget = target;
3535
this.details = details;
36-
this.copySfItem = cmdLet;
36+
this.fileSupportDelegate = fileSupport;
3737
actionType = type;
3838
}
3939

@@ -88,6 +88,7 @@ void IAction.CopyFileItem(ProgressInfo progressInfo)
8888
};
8989

9090
Task.Run(() => uploader.UploadAsync()).Wait();
91+
fileSupportDelegate(fileInfo.Name);
9192
}
9293
}
9394

ShareFileSnapIn/Resume/ResumeSupport.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class ResumeSupport
1818
{
1919
private String XML_FILE_NAME = @".progressfile";
2020
private ProgressFile progressObject;
21+
private bool flagResume;
2122

2223
/// <summary>
2324
/// Initialize/Load the progressfile and de-serialize the object
@@ -27,7 +28,7 @@ public ResumeSupport()
2728

2829
string directory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
2930
XML_FILE_NAME = Path.Combine(directory, Resources.AppName, XML_FILE_NAME);
30-
31+
flagResume = true;
3132
progressObject = LoadFile();
3233
}
3334

@@ -40,7 +41,7 @@ public ResumeSupport()
4041
/// <param name="details">Upload Specification Request Details</param>
4142
public void Start(String[] source, String target, bool force, String details)
4243
{
43-
progressObject = new ProgressFile();
44+
progressObject = new ProgressFile();
4445

4546
progressObject.ArgumentSource = source;
4647
progressObject.ArgumentTarget = target;
@@ -91,7 +92,7 @@ public bool IsPending
9192
{
9293
get
9394
{
94-
return progressObject.IsExist && progressObject.IsPending;
95+
return flagResume && progressObject.IsExist && progressObject.IsPending;
9596
}
9697
}
9798

@@ -139,6 +140,11 @@ public String GetDetails
139140
}
140141
}
141142

143+
public void UnmarkResumeFlag()
144+
{
145+
this.flagResume = false;
146+
}
147+
142148
/// <summary>
143149
/// Save/serialize progress to ".progressfile" file
144150
/// It will be called after each operation i.e. file completion, command completion & new command

ShareFileSnapIn/Resume/SupportHandler.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,20 @@ class SupportHandler<T> where T : class
2020
/// <returns>File Object</returns>
2121
public static T Load(string path)
2222
{
23-
T serializableObject = null;
24-
25-
using (Stream textReader = CreateTextReader(path))
23+
lock (syncFileLock)
2624
{
27-
XmlSerializer xmlSerializer = CreateXmlSerializer();
28-
serializableObject = xmlSerializer.Deserialize(textReader) as T;
25+
T serializableObject = null;
2926

30-
textReader.Close();
31-
}
27+
using (Stream textReader = CreateTextReader(path))
28+
{
29+
XmlSerializer xmlSerializer = CreateXmlSerializer();
30+
serializableObject = xmlSerializer.Deserialize(textReader) as T;
31+
32+
textReader.Close();
33+
}
3234

33-
return serializableObject;
35+
return serializableObject;
36+
}
3437
}
3538

3639
/// <summary>
512 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)