Commit Graph

22 Commits

Author SHA1 Message Date
Kir Kolyshkin 73b194b8ed Use for range over integers
This form is available since Go 1.22 (see
https://tip.golang.org/ref/spec#For_range) and will probably be seen
more and more in the new code.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-03-31 15:39:12 -07:00
Miloslav Trmač f4597cfee5 Use range iterations
... for the trivial cases where the loop body does not reference
the iteration variable at all.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2024-09-05 18:56:35 +02:00
Giuseppe Scrivano c06cd1c168
lockfile: add functions for non blocking lock
extend the public API to allow a non blocking usage.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2024-05-10 15:42:21 +02:00
Mike Norgate 1ad1a4ecb4
Implement windows lockfile
Signed-off-by: Mike Norgate <mike.norgate@mythical.games>
2023-07-12 09:24:17 +01:00
Miloslav Trmač d34bcddaa3 Add new LockFile state tracking API
lockfile.LastWrite allows callers to explicitly
track state across *LockFile.{GetLastWrite,ModifiedSince,
RecordWrite}.

*LockFile.{Modified,Touch} are now implemented
in terms of these APIs.

NOTE: This changes behavior, Touch() now updates
the built-in state only on success.  Either way there
can be suprious modification reports.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2022-11-29 18:12:43 +01:00
Miloslav Trmač 9bcb737123 Modify pkg/lockfile to return a struct instead of an interface
Add lockfile.GetLockFile and lockfile.GetROLockFile (with upper-case
F, as opposed to existing methods with lower-case f) which return a
*LockFile struct, instead of the Locker interface.

This follows the general Go design heuristic: "return structs, accept
interfaces"; more importantly, it allows us to add more methods to the lock
object without breaking the Go API by extending a pre-existing interface.

Should not change behavior.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2022-11-29 18:12:43 +01:00
Miloslav Trmač 669c13c7b0 Add missing error checks in tests
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2022-10-25 20:58:55 +02:00
Miloslav Trmač a5be9f31ac Avoid use of Locker.Locked() in tests
.Locked() will be going away.  These are just smoke-tests,
so use the .Assert* methods (which are not usable with other
concurrent users).

The AssertLockedForWriting() tests probably don't work on
Windows, but that was the case previously as well.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2022-10-21 21:10:33 +02:00
Nalin Dahyabhai a14e7d309e pkg/lockfile.TestLockfileMultiprocessModified(): check for wait errors
Check for errors from command Wait() methods.  Not sure how this
inconsistency even landed in the first version of this test.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2022-10-19 08:14:26 -04:00
Daniel J Walsh c68c699333
Update pkg/lockfile/lockfile_test.go
Co-authored-by: Miloslav Trmač <mitr@redhat.com>
2022-10-19 06:42:03 -04:00
Nalin Dahyabhai 4293c488da Add TestLockfileMultiprocessModified()
Test changes introduced in #1396.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2022-10-18 15:39:37 -04:00
Miloslav Trmač eeadee46d6 Fix races in lockfile_test.go
go { cmd.Run() } means the cmd will be destructed,
and stdin/stdout/stderr closed, concurrently with
other goroutines trying to access stdin/stdout/stderr.

Instead, do it the more traditional way and let the callers
who create those subprocesses explicitly manage their lifetime.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2022-10-14 17:17:54 +02:00
Miloslav Trmač 2d90000b09 Add missing error checks in tests
... and remove one WriteFile that was always failing.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2022-10-14 17:17:54 +02:00
Miloslav Trmač 082a8161ed Remove lockfile.Locker.RecursiveLock
It is not valid as currently implemented, and there is no known user.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2022-10-07 19:45:14 +02:00
Miloslav Trmač f42467020f Misc. warning cleanups
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2022-10-01 02:53:06 +02:00
Miloslav Trmač a1ccc9d862 Use os.WriteFile instead of ioutil.WriteFile
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2022-09-12 16:31:34 +02:00
Miloslav Trmač cfbc77122a Use os.CreateTemp instead of ioutil.TempFile
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2022-09-12 16:30:48 +02:00
Daniel J Walsh 534b0b3281
Standardize on capatalized logrus messages, and remove stutters
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2021-09-23 14:43:35 -04:00
Nalin Dahyabhai 38df75ce9b TestLockfileWriteConcurrent: stay below 8192 goroutines
The Go race detector will kill the test if it tries to have more than
8192 goroutines active at once, so start 8,000 instead of 100,000.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-03-22 14:39:40 -04:00
Nalin Dahyabhai 58be038c06 pkg/lockfile: fix a race and an incorrect unit test
Fix a race where the state lock in a Unixy lockfile structure wasn't
being used to protect access to all of the structure's state fields.

Fix usage of sync/atomic in the corresponding unit tests to not mix
atomic adds and reads with non-atomic reads, something which the race
detector complained about.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-02-22 18:24:04 -05:00
Sascha Grunert fd72b45a3f
Enable golint linter and fix lints
Signed-off-by: Sascha Grunert <sgrunert@suse.com>
2020-01-28 15:59:15 +01:00
Nalin Dahyabhai 2dfeb3edf7 Move lockfiles to their own package
Move the lockfile implementation into a subpackage so that we can use it
in the devicemapper driver.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2019-07-02 11:14:04 -04:00