Skip to content

Commit 040dbdb

Browse files
committed
v1.2.3810.0
1 parent 55423be commit 040dbdb

24 files changed

+864
-233
lines changed

README.md

Lines changed: 33 additions & 31 deletions
Large diffs are not rendered by default.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Text;
5+
6+
namespace VirtualFileSystem
7+
{
8+
/// <summary>
9+
/// Thrown when a file can not be locked. For example when a lock-token file is blocked
10+
/// from another thread, during update, lock and unlock operations.
11+
/// </summary>
12+
public class ClientLockFailedException : IOException
13+
{
14+
/// <summary>
15+
/// Creates instance of this class.
16+
/// </summary>
17+
/// <param name="message">The error message that explains the reason for the exception.</param>
18+
public ClientLockFailedException(string message) : base(message)
19+
{
20+
}
21+
22+
/// <summary>
23+
/// Creates instance of this class with a specified
24+
/// error message and a reference to the inner exception that is the cause of this
25+
/// exception.
26+
/// </summary>
27+
/// <param name="message">The error message that explains the reason for the exception.</param>
28+
/// <param name="innerException">
29+
/// The exception that is the cause of the current exception. If the innerException
30+
/// parameter is not null, the current exception is raised in a catch block that
31+
/// handles the inner exception.
32+
/// </param>
33+
public ClientLockFailedException(string message, Exception innerException) : base(message, innerException)
34+
{
35+
}
36+
37+
/// <summary>
38+
/// Creates instance of this class with a message and HRESULT code.
39+
/// </summary>
40+
/// <param name="message">The error message that explains the reason for the exception.</param>
41+
/// <param name="hresult">An integer identifying the error that has occurred.</param>
42+
public ClientLockFailedException(string message, int hresult) : base(message, hresult)
43+
{
44+
}
45+
}
46+
}

VirtualFileSystem/Framework/CustomData.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ public static string GetOriginalPath(this PlaceholderItem placeholder)
9595
}
9696

9797
/// <summary>
98-
/// Returns true if the file was moved in the user file system and changes not yet synched to the remote storage.
98+
/// Returns true if the file was moved in the user file system and
99+
/// changes not yet synched to the remote storage.
99100
/// </summary>
100101
public static bool IsMoved(this PlaceholderItem placeholder)
101102
{
@@ -110,13 +111,20 @@ public static bool IsMoved(this PlaceholderItem placeholder)
110111
return !originalPath.Equals(placeholder.Path, StringComparison.InvariantCultureIgnoreCase);
111112
}
112113

114+
/// <summary>
115+
/// Returns true if the item was created and must be synched to remote storage.
116+
/// </summary>
117+
/// <returns>
118+
/// True if the item was created in the user file system and does not exists
119+
/// in the remote storage. False otherwise.
120+
/// </returns>
113121
public static bool IsNew(this PlaceholderItem placeholder)
114122
{
115-
// ETag presence signals if the item is new.
123+
// ETag absence signals that the item is new.
116124
// However, ETag file may not exists during move operation,
117125
// additionally checking OriginalPath presence.
118126
// Can not rely on OriginalPath only,
119-
// because MS Office transactional save deletes and recreates the file.
127+
// because MS Office files are being deleted and re-created during transactional save.
120128

121129
string originalPath = placeholder.GetOriginalPath();
122130

VirtualFileSystem/Framework/ETag.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ public static async Task<string> GetETagAsync(string userFileSystemPath)
4242
/// <summary>
4343
/// Deletes ETag associated with a file.
4444
/// </summary>
45-
/// <param name="userFileSystemSourcePath">Path in the user file system.</param>
46-
public static void DeleteETag(string userFileSystemSourcePath)
45+
/// <param name="userFileSystemPath">Path in the user file system.</param>
46+
public static void DeleteETag(string userFileSystemPath)
4747
{
48-
File.Delete(GetETagFilePath(userFileSystemSourcePath));
48+
File.Delete(GetETagFilePath(userFileSystemPath));
4949
}
5050

5151
/// <summary>

VirtualFileSystem/Framework/FsPath.cs

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,23 @@ public static string GetAttString(string path)
114114
}
115115

116116
/// <summary>
117-
/// Returns true if the file hase a format ~XXXXX.tmp, false - otherwise.
117+
/// Returns true if the file is in ~XXXXX.tmp, pptXXXX.tmp format, false - otherwise.
118118
/// </summary>
119-
/// <param name="path">Path to a file or folder.</param>
119+
/// <param name="path">Path to a file.</param>
120120
private static bool IsMsOfficeTemp(string path)
121121
{
122-
return (Path.GetFileName(path).StartsWith('~') && Path.GetExtension(path).Equals(".tmp", StringComparison.InvariantCultureIgnoreCase)) // Word temp files
123-
|| (Path.GetFileName(path).StartsWith("ppt") && Path.GetExtension(path).Equals(".tmp", StringComparison.InvariantCultureIgnoreCase)) // PowerPoint temp files
124-
|| (string.IsNullOrEmpty(Path.GetExtension(path)) && (Path.GetFileName(path).Length == 8)); // Excel temp files
122+
return (Path.GetFileName(path).StartsWith('~') && Path.GetExtension(path).Equals(".tmp", StringComparison.InvariantCultureIgnoreCase)) // Word temp files
123+
|| (Path.GetFileName(path).StartsWith("ppt") && Path.GetExtension(path).Equals(".tmp", StringComparison.InvariantCultureIgnoreCase)) // PowerPoint temp files
124+
|| (string.IsNullOrEmpty(Path.GetExtension(path)) && (Path.GetFileName(path).Length == 8)) // Excel temp files
125+
|| ((Path.GetFileNameWithoutExtension(path).Length == 8) && Path.GetExtension(path).Equals(".tmp", StringComparison.InvariantCultureIgnoreCase)); // Excel temp files
125126
}
126127

127128
/// <summary>
128129
/// Returns true if file system contains MS Office lock file (~$file.ext) in file
129130
/// system that corresponds to the provided path to MS Office file.
130131
/// </summary>
131132
/// <param name="path">Path to MS Office file.</param>
132-
private static bool IsMsOfficeLocked(string path)
133+
internal static bool IsMsOfficeLocked(string path)
133134
{
134135
string lockPath = GetLockPathFromMsOfficePath(path);
135136
return lockPath != null;
@@ -140,18 +141,18 @@ private static bool IsMsOfficeLocked(string path)
140141
/// Returns true if the provided path points to MS Office lock file (~$file.ext).
141142
/// </summary>
142143
/// <param name="path">Path to lock file.</param>
143-
private static bool IsMsOfficeLockFile(string path)
144+
internal static bool IsMsOfficeLockFile(string path)
144145
{
145146
return Path.GetFileName(path).StartsWith("~$");
146147
}
147148

148-
/*
149-
public static string GetMsOfficePathFromLock(string msOfficeLockFilePath)
150-
{
151-
int separatorIndex = msOfficeLockFilePath.LastIndexOf(Path.DirectorySeparatorChar);
152-
return msOfficeLockFilePath.Remove(separatorIndex + 1, "~$".Length);
153-
}
154-
*/
149+
150+
//public static string GetMsOfficePathFromLock(string msOfficeLockFilePath)
151+
//{
152+
// int separatorIndex = msOfficeLockFilePath.LastIndexOf(Path.DirectorySeparatorChar);
153+
// return msOfficeLockFilePath.Remove(separatorIndex + 1, "~$".Length);
154+
//}
155+
155156

156157
/// <summary>
157158
/// Returns MS Office lock file path if such file exists.
@@ -222,6 +223,15 @@ public static bool AvoidSync(string path)
222223
return IsMsOfficeLockFile(path) || IsMsOfficeLocked(path) || IsMsOfficeTemp(path) || IsHiddenOrTemp(path);
223224
}
224225

226+
/// <summary>
227+
/// Returns true if the file or folder should not be automatically locked. False - otherwise.
228+
/// </summary>
229+
/// <param name="path">Path to a file or folder.</param>
230+
public static bool AvoidAutoLock(string path)
231+
{
232+
return IsMsOfficeLockFile(path) || IsMsOfficeTemp(path) || IsHiddenOrTemp(path);
233+
}
234+
225235
/// <summary>
226236
/// Gets formatted file size or null for folders or if the file is not found.
227237
/// </summary>
@@ -253,5 +263,27 @@ public static string Size(string path)
253263
double num = Math.Round(bytes / Math.Pow(1024, place), 1);
254264
return (Math.Sign(length) * num).ToString() + suf[place];
255265
}
266+
267+
/// <summary>
268+
/// Returns true if the file is locked for writing, false otherwise.
269+
/// </summary>
270+
/// <param name="path">Path ot a file.</param>
271+
/// <returns>True if the file is locked for writing. False otherwise.</returns>
272+
public static bool IsWriteLocked(string path)
273+
{
274+
try
275+
{
276+
using (FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete))
277+
{
278+
stream.Close();
279+
}
280+
}
281+
catch (IOException)
282+
{
283+
return true;
284+
}
285+
286+
return false;
287+
}
256288
}
257289
}

0 commit comments

Comments
 (0)