This repository was archived by the owner on Mar 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAcLocks.cs
More file actions
629 lines (573 loc) · 27.3 KB
/
AcLocks.cs
File metadata and controls
629 lines (573 loc) · 27.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*! \file
Copyright (C) 2016-2018 Verizon. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace AcUtils
{
#region enums
/*! \ingroup acenum */
///@{
/// <summary>
/// The kind of AccuRev \c lock.
/// </summary>
public enum LockKind {
/*! \var from
A \e from lock on the stream. */
from,
/*! \var to
A \e to lock on the stream. */
to,
/*! \var all
Both \e to and \e from lock on the stream. */
all
};
/*! \ingroup acenum */
/// <summary>
/// Whether the \c lock is for a user or a group.
/// </summary>
public enum PrinType
{
/*! \var group
Lock applies to a group. */
group,
/*! \var user
Lock applies to a user. */
user,
/*! \var none
Lock does not apply to a principal. */
none
};
///@}
/*! \ingroup acenum */
/// <summary>
/// Lock applies to the principal only or to all except the principal.
/// </summary>
public enum OnlyExcept {
/*! \var Only
Lock applies to the principal only. */
Only,
/*! \var Except
Lock applies to all except the principal. */
Except
};
///@}
#endregion
/// <summary>
/// A lock object that defines the attributes of an AccuRev lock:
/// The stream name the lock is on and manner ([from, to, all](@ref AcUtils#LockKind)),
/// the principal name and [type it applies to](@ref AcUtils#PrinType), and how
/// it's applied ([except-for, only-for](@ref AcUtils#OnlyExcept)).
/// </summary>
/// <remarks>AcLock objects are instantiated during AcLocks construction.</remarks>
[Serializable]
public sealed class AcLock : IFormattable, IEquatable<AcLock>, IComparable<AcLock>, IComparable
{
#region class variables
// Kind and Name are the only ones that always exist in the XML.
// Others exist only if there are values for them.
private LockKind _kind;
private string _name;
private PrinType _type;
private string _exceptFor;
private string _onlyFor;
private string _comment;
#endregion
/// <summary>
/// Constructor used during AcLocks list construction. It is called internally and not by user code.
/// </summary>
internal AcLock() {}
#region Equality comparison
/*! \name Equality comparison */
/**@{*/
/// <summary>
/// IEquatable implementation to determine the equality of instances of type AcLock.
/// Uses stream name the lock is on and manner ([from, to, all](@ref AcUtils#LockKind)) to compare instances.
/// </summary>
/// <param name="other">The AcLock object being compared to \e this instance.</param>
/// <returns>\e true if AcLock \e other is the same, \e false otherwise.</returns>
public bool Equals(AcLock other)
{
if (ReferenceEquals(other, null)) return false;
if (ReferenceEquals(this, other)) return true;
// [stream], [from|to|all]
var left = Tuple.Create(Name, Kind);
var right = Tuple.Create(other.Name, other.Kind);
return left.Equals(right);
}
/// <summary>
/// Overridden to determine equality.
/// </summary>
/// <returns>Return value of generic [Equals(AcLock)](@ref AcLock#Equals) version.</returns>
public override bool Equals(object other)
{
if (ReferenceEquals(other, null)) return false;
if (ReferenceEquals(this, other)) return true;
if (GetType() != other.GetType()) return false;
return Equals(other as AcLock);
}
/// <summary>
/// Override appropriate for type AcLock.
/// </summary>
/// <returns>Hash of stream name and [LockKind](@ref AcUtils#LockKind).</returns>
public override int GetHashCode()
{
var hash = Tuple.Create(Name, Kind);
return hash.GetHashCode();
}
/**@}*/
#endregion
#region Order comparison
/*! \name Order comparison */
/**@{*/
/// <summary>
/// Generic IComparable implementation (default) for comparing AcLock objects to sort
/// by [LockKind](@ref AcUtils#LockKind) (descending) then stream name.
/// </summary>
/// <param name="other">An AcLock object to compare with this instance.</param>
/// <returns>Value indicating the relative order of the AcLock objects being compared.</returns>
/*! \sa [OrderBy example](@ref AcUtils#AcLocks#AcLocks) */
public int CompareTo(AcLock other)
{
int result;
if (AcLock.ReferenceEquals(this, other))
result = 0;
else
{
result = (-1 * Kind.CompareTo(other.Kind)); // descending
if (result == 0)
result = String.Compare(Name, other.Name);
}
return result;
}
/// <summary>
/// Pre-generic interface implementation for code using reflection.
/// </summary>
/// <param name="other">An AcLock object to compare with this instance.</param>
/// <returns>Return value of generic [CompareTo(AcLock)](@ref AcLock#CompareTo) version.</returns>
/// <exception cref="ArgumentException">thrown if argument is not an AcLock object.</exception>
int IComparable.CompareTo(object other)
{
if (!(other is AcLock))
throw new ArgumentException("Argument is not an AcLock", "other");
AcLock o = (AcLock)other;
return this.CompareTo(o);
}
/**@}*/
#endregion
/// <summary>
/// Kind of AccuRev lock: \e from, \e to, or \e all.
/// </summary>
public LockKind Kind
{
get { return _kind; }
internal set { _kind = value; }
}
/// <summary>
/// Name of stream the lock applies to.
/// </summary>
public string Name
{
get { return _name ?? String.Empty; }
internal set { _name = value; }
}
/// <summary>
/// Whether the lock is for a \e user or \e group.
/// </summary>
public PrinType Type
{
get { return _type; }
internal set { _type = value; }
}
/// <summary>
/// Lock applies to all \e except this principal.
/// </summary>
public string ExceptFor
{
get { return _exceptFor ?? String.Empty; }
internal set { _exceptFor = value; }
}
/// <summary>
/// Lock applies \e only to this principal.
/// </summary>
public string OnlyFor
{
get { return _onlyFor ?? String.Empty; }
internal set { _onlyFor = value; }
}
/// <summary>
/// Comment given to the lock.
/// </summary>
public string Comment
{
get { return _comment ?? String.Empty; }
internal set { _comment = value; }
}
#region ToString
/// <summary>
/// The ToString implementation.
/// </summary>
/// <param name="format">The format specifier to use, e.g. <b>Console.WriteLine(rule.ToString("e"));</b></param>
/// <param name="provider">Allow clients to format output for their own types using [ICustomFormatter](https://msdn.microsoft.com/en-us/library/system.icustomformatter.aspx).</param>
/// <returns>The formatted string.</returns>
/// <exception cref="FormatException">thrown if an invalid format string is specified.</exception>
/// \par Format specifiers:
/// \arg \c G Default when not using a format specifier.
/// \arg \c K Kind - Kind of AccuRev lock: \e from, \e to, or \e all.
/// \arg \c N Name - Name of stream the lock applies to.
/// \arg \c T Type - Whether the lock is for a \e user or \e group.
/// \arg \c E ExceptFor - Lock applies to all \e except this principal.
/// \arg \c O OnlyFor - Lock applies \e only to this principal.
/// \arg \c C Comment - Comment given to the lock.
public string ToString(string format, IFormatProvider provider)
{
if (provider != null)
{
ICustomFormatter fmt = provider.GetFormat(this.GetType()) as ICustomFormatter;
if (fmt != null)
return fmt.Format(format, this, provider);
}
if (String.IsNullOrEmpty(format))
format = "G";
switch (format.ToUpperInvariant())
{
case "G": // long version and default when not using a format specifier
{
string kind = $"{((Kind == LockKind.all) ? "promotions to and from" : "promotions " + Kind)}";
string howfor = String.Format("{0}",
(Kind == LockKind.all) || (String.IsNullOrEmpty(ExceptFor) && String.IsNullOrEmpty(OnlyFor)) ?
"for all" : !String.IsNullOrEmpty(ExceptFor) ?
("except for " + Type + " " + ExceptFor) : ("for " + Type + " " + OnlyFor + " only"));
return $"Lock {kind} {Name} {howfor}. {Comment}";
}
case "K": // kind of AccuRev lock: from, to, or all
return Kind.ToString();
case "N": // name of stream the lock applies to
return Name;
case "T": // whether lock is for a user or group
return Type.ToString();
case "E": // lock applies to all except this principal
return ExceptFor;
case "O": // lock applies only to this principal
return OnlyFor;
case "C": // comment given to the lock
return Comment;
default:
throw new FormatException($"The {format} format string is not supported.");
}
}
// Calls ToString(string, IFormatProvider) version with a null IFormatProvider argument.
public string ToString(string format)
{
return ToString(format, null);
}
// Calls ToString(string, IFormatProvider) version with the general format and a null IFormatProvider argument.
public override string ToString()
{
return ToString("G", null);
}
#endregion ToString
}
/// <summary>
/// A container of AcLock objects that define the AccuRev locks that prevent certain users
/// from making changes to streams.
/// </summary>
[Serializable]
public sealed class AcLocks : List<AcLock>
{
#region class variables
[NonSerialized] private readonly object _locker = new object();
#endregion
#region object construction:
//! \name Two-part object construction:
//@{
/// <summary>
/// A container of AcLock objects that define the AccuRev locks that prevent certain users
/// from making changes to streams.
/// </summary>
/*! \code
// get locks on all streams in the repository
AcLocks locks = new AcLocks();
if (!(await locks.initAsync())) return false;
// show DEV locks in NEPTUNE depot and MAINT locks in JUPITER depot
var arr = new[] { "NEPTUNE_DEV", "JUPITER_MAINT" }; // elements are string subsets of our targets
IEnumerable<AcLock> query = locks.Where(n => arr.Any(s => n.Name.Contains(s)));
// list in stream name order with "to" lock followed by "from" lock
foreach (AcLock lk in query.OrderBy(n => n.Name).ThenByDescending(n => n.Kind))
Console.WriteLine(lk);
...
Lock promotions to JUPITER_MAINT except for group LEADS. Authorized users only.
Lock promotions from JUPITER_MAINT for all.
Lock promotions to JUPITER_MAINT1 except for group REPORTING. Work on defect 1405.
Lock promotions from JUPITER_MAINT1 except for group LEADS.
Lock promotions to JUPITER_MAINT2 for all. On hold until further notice.
Lock promotions from JUPITER_MAINT2 for group SYSTEM_TEST only. UAT in progress.
Lock promotions to JUPITER_MAINT3 except for user robert. GUI defect 1345.
Lock promotions from JUPITER_MAINT3 except for group ADMIN.
Lock promotions to NEPTUNE_DEV1 except for group DEV. October ER.
Lock promotions from NEPTUNE_DEV1 except for group ENV_SUPPORT.
Lock promotions to NEPTUNE_DEV2 except for user thomas.
Lock promotions from NEPTUNE_DEV2 except for group LEADS. Authorized users only.
Lock promotions to and from NEPTUNE_DEV3 for all.
\endcode */
/*! \sa initAsync(AcDepot), initAsync(DepotsCollection), initAsync(StreamsCollection),
[default comparer](@ref AcLock#CompareTo),
<a href="_lock_streams_8cs-example.html">LockStreams.cs</a>,
<a href="_locks_8cs-example.html">Locks.cs</a>,
<a href="_promo_rights_8cs-example.html">PromoRights.cs</a> */
public AcLocks() { }
/// <summary>
/// Populate this container with AcLock objects on streams in \e depot or all AcLock objects in the repository.
/// </summary>
/// <param name="depot">Limit the list of locks to those on \e depot only, otherwise
/// \e null for all locks in the repository.</param>
/// <returns>\e true if initialization succeeded, \e false otherwise.</returns>
/// <exception cref="AcUtilsException">caught and [logged](@ref AcUtils#AcDebug#initAcLogging)
/// in <tt>\%LOCALAPPDATA\%\\AcTools\\Logs\\<prog_name\>-YYYY-MM-DD.log</tt> on \c show command failure.</exception>
/// <exception cref="Exception">caught and logged in same on failure to handle a range of exceptions.</exception>
/*! \sa [AcLocks constructor](@ref AcUtils#AcLocks#AcLocks), initAsync(DepotsCollection), initAsync(StreamsCollection) */
/*! \show_ <tt>show -fx locks</tt> */
public async Task<bool> initAsync(AcDepot depot = null)
{
bool ret = false; // assume failure
try
{
AcResult r = await AcCommand.runAsync("show -fx locks").ConfigureAwait(false);
if (r != null && r.RetVal == 0)
{
XElement xml = XElement.Parse(r.CmdResult);
IEnumerable<XElement> query = null;
if (depot == null)
query = from e in xml.Elements("Element")
select e;
else
query = from e in xml.Elements("Element")
join AcStream s in depot.Streams on (string)e.Attribute("Name") equals s.Name
select e;
ret = initList(query);
}
}
catch (AcUtilsException ecx)
{
AcDebug.Log($"AcUtilsException caught and logged in AcLocks.initAsync(AcDepot){Environment.NewLine}{ecx.Message}");
}
catch (Exception ecx)
{
AcDebug.Log($"Exception caught and logged in AcLocks.initAsync(AcDepot){Environment.NewLine}{ecx.Message}");
}
return ret;
}
/// <summary>
/// Populate this container with AcLock objects on streams in \e depots.
/// </summary>
/// <param name="depots">Limit the list of locks to those on streams in \e depots only. Depot names in \e depots
/// must match their respective AccuRev depot name exactly.</param>
/// <returns>\e true if initialization succeeded, \e false otherwise.</returns>
/// <exception cref="AcUtilsException">caught and [logged](@ref AcUtils#AcDebug#initAcLogging)
/// in <tt>\%LOCALAPPDATA\%\\AcTools\\Logs\\<prog_name\>-YYYY-MM-DD.log</tt> on \c show command failure.</exception>
/// <exception cref="Exception">caught and logged in same on failure to handle a range of exceptions.</exception>
/*! \sa [AcLocks constructor](@ref AcUtils#AcLocks#AcLocks), initAsync(AcDepot), initAsync(StreamsCollection) */
/*! \show_ <tt>show -fx locks</tt> */
public async Task<bool> initAsync(DepotsCollection depots)
{
AcDepots dlist = new AcDepots();
if (!(await dlist.initAsync(depots).ConfigureAwait(false))) return false;
bool ret = false; // assume failure
try
{
AcResult r = await AcCommand.runAsync("show -fx locks").ConfigureAwait(false);
if (r != null && r.RetVal == 0)
{
bool result = true;
XElement xml = XElement.Parse(r.CmdResult);
for (int ii = 0; ii < dlist.Count && result; ii++)
{
AcDepot depot = dlist[ii];
IEnumerable<XElement> query = from e in xml.Elements("Element")
join AcStream s in depot.Streams on (string)e.Attribute("Name") equals s.Name
select e;
result = initList(query);
}
ret = result;
}
}
catch (AcUtilsException ecx)
{
AcDebug.Log($"AcUtilsException caught and logged in AcLocks.initAsync(DepotsCollection){Environment.NewLine}{ecx.Message}");
}
catch (Exception ecx)
{
AcDebug.Log($"Exception caught and logged in AcLocks.initAsync(DepotsCollection){Environment.NewLine}{ecx.Message}");
}
return ret;
}
/// <summary>
/// Populate this container with AcLock objects on \e streams.
/// </summary>
/// <param name="streams">Limit the list of locks to those on \e streams only. Stream names in \e streams
/// must match their respective AccuRev stream name exactly.</param>
/// <returns>\e true if initialization succeeded, \e false otherwise.</returns>
/// <exception cref="AcUtilsException">caught and [logged](@ref AcUtils#AcDebug#initAcLogging)
/// in <tt>\%LOCALAPPDATA\%\\AcTools\\Logs\\<prog_name\>-YYYY-MM-DD.log</tt> on \c show command failure.</exception>
/// <exception cref="Exception">caught and logged in same on failure to handle a range of exceptions.</exception>
/*! \sa [AcLocks constructor](@ref AcUtils#AcLocks#AcLocks), initAsync(AcDepot), initAsync(DepotsCollection) */
/*! \show_ <tt>show -fx locks</tt> */
public async Task<bool> initAsync(StreamsCollection streams)
{
bool ret = false; // assume failure
try
{
AcResult r = await AcCommand.runAsync("show -fx locks").ConfigureAwait(false);
if (r != null && r.RetVal == 0)
{
XElement xml = XElement.Parse(r.CmdResult);
IEnumerable<XElement> query = from e in xml.Elements("Element")
where streams.OfType<StreamElement>().Any(se => se.Stream == (string)e.Attribute("Name"))
select e;
ret = initList(query);
}
}
catch (AcUtilsException ecx)
{
AcDebug.Log($"AcUtilsException caught and logged in AcLocks.initAsync(StreamsCollection){Environment.NewLine}{ecx.Message}");
}
catch (Exception ecx)
{
AcDebug.Log($"Exception caught and logged in AcLocks.initAsync(StreamsCollection){Environment.NewLine}{ecx.Message}");
}
return ret;
}
//@}
#endregion
/// <summary>
/// Helper function that populates this container with AcLock objects as per \e query sent by an initAsync method.
/// </summary>
/// <param name="query">The query to iterate.</param>
/// <returns>\e true if no failure occurred and initialization was successful, \e false otherwise.</returns>
/// <exception cref="Exception">caught and [logged](@ref AcUtils#AcDebug#initAcLogging)
/// in <tt>\%LOCALAPPDATA\%\\AcTools\\Logs\\<prog_name\>-YYYY-MM-DD.log</tt> on failure to handle a range of exceptions.</exception>
/*! \note <tt>show -fx locks</tt> XML attributes for stream name and [LockKind](@ref AcUtils#LockKind)
always exist. Other attributes exist only if they have values. */
private bool initList(IEnumerable<XElement> query)
{
bool ret = false; // assume failure
try
{
foreach (XElement e in query)
{
AcLock lk = new AcLock();
// Kind and Name are the only ones that always exist in the XML
// others exist only if there are values for them
string kind = (string)e.Attribute("kind");
lk.Kind = (LockKind)Enum.Parse(typeof(LockKind), kind);
lk.Name = (string)e.Attribute("Name");
string type = (string)e.Attribute("userType") ?? String.Empty;
lk.Type = String.IsNullOrEmpty(type) ? PrinType.none :
(PrinType)Enum.Parse(typeof(PrinType), type);
lk.ExceptFor = (string)e.Attribute("exceptFor") ?? String.Empty;
lk.OnlyFor = (string)e.Attribute("onlyFor") ?? String.Empty;
lk.Comment = (string)e.Attribute("comment") ?? String.Empty;
lock (_locker) { Add(lk); }
}
ret = true; // operation succeeded
}
catch (Exception ecx)
{
AcDebug.Log($"Exception caught and logged in AcLocks.initList{Environment.NewLine}{ecx.Message}");
}
return ret;
}
/// <summary>
/// Determine if \e stream has a \c lock on it.
/// </summary>
/// <param name="stream">Name of stream to query.</param>
/// <returns>\e true if \e stream has a lock on it, \e false otherwise.</returns>
public bool hasLock(string stream)
{
AcLock lk = this.FirstOrDefault(n => n.Name == stream);
return (lk != null);
}
/// <summary>
/// Put a \c lock on \e stream as per lock \e kind.
/// </summary>
/// <param name="stream">Name of stream or workspace to lock.</param>
/// <param name="comment">Comment to be used for the lock.</param>
/// <param name="kind">Type of lock to apply: \e to, \e from, or \e all.</param>
/// <param name="prncpl">AccuRev principal name of user or group in the case of \e to or \e from lock.</param>
/// <param name="onlyexcept">Apply \c lock to \e prncpl only or to all except \e prncpl.</param>
/// <returns>\e true if operation succeeded, \e false otherwise.</returns>
/// <exception cref="AcUtilsException">caught and [logged](@ref AcUtils#AcDebug#initAcLogging)
/// in <tt>\%LOCALAPPDATA\%\\AcTools\\Logs\\<prog_name\>-YYYY-MM-DD.log</tt> on \c lock command failure.</exception>
/*! \lock_ <tt>lock -c \<comment\> [-kf [-e|-o prncpl] | -kt [-e|-o prncpl]] \<stream\></tt> */
/*! \accunote_ The CLI \c lock command can be used on a workspace stream but the same cannot be done using the AccuRev GUI client. AccuRev defect 23850. */
public async Task<bool> lockAsync(string stream, string comment, LockKind kind = LockKind.all, AcPrincipal prncpl = null, OnlyExcept onlyexcept = OnlyExcept.Except)
{
bool ret = false; // assume failure
try
{
string cmd = null;
if (kind == LockKind.from)
if (prncpl != null)
cmd = $@"lock -c ""{comment}"" -kf {((onlyexcept == OnlyExcept.Except) ? "-e" : "-o")} ""{prncpl}"" ""{stream}""";
else
cmd = $@"lock -c ""{comment}"" -kf ""{stream}"""; // lock 'from' for all
else if (kind == LockKind.to)
if (prncpl != null)
cmd = $@"lock -c ""{comment}"" -kt {((onlyexcept == OnlyExcept.Except) ? "-e" : "-o")} ""{prncpl}"" ""{stream}""";
else
cmd = $@"lock -c ""{comment}"" -kt ""{stream}"""; // lock 'to' for all
else if (kind == LockKind.all)
cmd = $@"lock -c ""{comment}"" ""{stream}"""; // lock 'to and from' for all
AcResult r = await AcCommand.runAsync(cmd).ConfigureAwait(false);
ret = (r != null && r.RetVal == 0);
}
catch (AcUtilsException ecx)
{
AcDebug.Log($"AcUtilsException in AcLocks.lockAsync caught and logged.{Environment.NewLine}{ecx.Message}");
}
return ret;
}
/// <summary>
/// Remove \c lock \e kind on \e stream.
/// </summary>
/// <param name="stream">Name of stream or workspace to unlock.</param>
/// <param name="kind">Type of lock to remove: \e to, \e from, or \e all.</param>
/// <returns>\e true if \e stream was unlocked successfully, \e false otherwise.</returns>
/// <exception cref="AcUtilsException">caught and [logged](@ref AcUtils#AcDebug#initAcLogging)
/// in <tt>\%LOCALAPPDATA\%\\AcTools\\Logs\\<prog_name\>-YYYY-MM-DD.log</tt> on \c unlock command failure.</exception>
/*! \unlock_ <tt>unlock [-kf | -kt] \<stream\></tt> */
public async Task<bool> unlockAsync(string stream, LockKind kind = LockKind.all)
{
bool ret = false; // assume failure
try
{
string cmd = null;
if (kind == LockKind.from)
cmd = $@"unlock -kf ""{stream}""";
else if (kind == LockKind.to)
cmd = $@"unlock -kt ""{stream}""";
else if (kind == LockKind.all)
cmd = $@"unlock ""{stream}""";
AcResult r = await AcCommand.runAsync(cmd).ConfigureAwait(false);
ret = (r != null && r.RetVal == 0);
}
catch (AcUtilsException ecx)
{
AcDebug.Log($"AcUtilsException in AcLocks.unlockAsync caught and logged.{Environment.NewLine}{ecx.Message}");
}
return ret;
}
}
}