abstraction-cas 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- abstraction_cas-0.1.0/LICENSE +202 -0
- abstraction_cas-0.1.0/PKG-INFO +259 -0
- abstraction_cas-0.1.0/README.md +248 -0
- abstraction_cas-0.1.0/abstraction_cas.egg-info/PKG-INFO +259 -0
- abstraction_cas-0.1.0/abstraction_cas.egg-info/SOURCES.txt +8 -0
- abstraction_cas-0.1.0/abstraction_cas.egg-info/dependency_links.txt +1 -0
- abstraction_cas-0.1.0/abstraction_cas.egg-info/top_level.txt +1 -0
- abstraction_cas-0.1.0/abstraction_cas.py +227 -0
- abstraction_cas-0.1.0/pyproject.toml +19 -0
- abstraction_cas-0.1.0/setup.cfg +4 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 the openabstractions contributors
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
202
|
+
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: abstraction-cas
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Compare-and-set over a file. The floor under every service that keeps state in a file.
|
|
5
|
+
License-Expression: Apache-2.0
|
|
6
|
+
Project-URL: Source, https://github.com/openabstractions/abstraction-cas
|
|
7
|
+
Requires-Python: >=3.9
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Dynamic: license-file
|
|
11
|
+
|
|
12
|
+
# abstraction-cas
|
|
13
|
+
|
|
14
|
+
**In development.** Tagged `go/v0.1.0`, but this page predates that tag: no
|
|
15
|
+
capability claim, no contract page, no conformance scenario of its own.
|
|
16
|
+
|
|
17
|
+
A file is replaced whole or not at all, and every change is applied to the value
|
|
18
|
+
the previous change left — across processes and across languages sharing one
|
|
19
|
+
kernel.
|
|
20
|
+
|
|
21
|
+
## The problem
|
|
22
|
+
|
|
23
|
+
Every service that keeps state in a file rewrites it, and two writers on one
|
|
24
|
+
file lose each other's updates unless they agree on a lock and a rename. Each
|
|
25
|
+
service of ours hand-rolled its own writer. This is the floor under all of them:
|
|
26
|
+
one lock, one rename, one implementation per language we ship. Not a
|
|
27
|
+
layer — no contract page, no capability claim. A stranger implementing `job`
|
|
28
|
+
needs its page, not this.
|
|
29
|
+
|
|
30
|
+
## Words
|
|
31
|
+
|
|
32
|
+
| word | meaning |
|
|
33
|
+
|---|---|
|
|
34
|
+
| **read** | the whole file, or nothing when there is none |
|
|
35
|
+
| **write** | replace the file only if it still holds `base`; else `Moved` |
|
|
36
|
+
| **change** | read, edit and write under the lock, so an edit always sees the truth and never needs a retry |
|
|
37
|
+
| **base** | the value a writer read; `nil` means *only if the file does not exist* |
|
|
38
|
+
| **lock** | byte 0 of `<path>.lock`, exclusive, blocking, released by the kernel when the holder dies |
|
|
39
|
+
|
|
40
|
+
No rule on this page carries a tag. The invariants below each carry a test, and
|
|
41
|
+
*What a fourth implementation must do* is cited by
|
|
42
|
+
[the job page](https://github.com/openabstractions/abstraction-job).
|
|
43
|
+
|
|
44
|
+
## Obtain
|
|
45
|
+
|
|
46
|
+
- **Go.** `go get github.com/openabstractions/abstraction-cas/go`. No tag yet;
|
|
47
|
+
`go get` resolves a pseudo-version of `main`.
|
|
48
|
+
- **Python.** Not on any index. `python/abstraction_cas.py` is one module with
|
|
49
|
+
no imports of ours; `python/pyproject.toml` builds a wheel
|
|
50
|
+
(`pip wheel python/`).
|
|
51
|
+
- **C++.** One header and one source, C++17, standard library only, and no build
|
|
52
|
+
file: name them. `g++ -std=c++17 -I cpp/include cpp/test/test_cas.cpp
|
|
53
|
+
cpp/src/cas.cpp -o test_cas` compiles and passes this layer's own test; MSVC
|
|
54
|
+
19.51 takes the same two files under `/std:c++17` and `/I`. Measured
|
|
55
|
+
2026-09-09. There
|
|
56
|
+
is no tagged release and no package.
|
|
57
|
+
|
|
58
|
+
## Example
|
|
59
|
+
|
|
60
|
+
```go
|
|
61
|
+
import cas "github.com/openabstractions/abstraction-cas/go"
|
|
62
|
+
|
|
63
|
+
err := cas.Change(path, func(cur []byte) ([]byte, error) {
|
|
64
|
+
var f file
|
|
65
|
+
if cur != nil {
|
|
66
|
+
json.Unmarshal(cur, &f)
|
|
67
|
+
}
|
|
68
|
+
f.Apps = append(f.Apps, app)
|
|
69
|
+
return json.Marshal(f)
|
|
70
|
+
})
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
`Read` returns the whole file, or `nil` when there is none. `Write(path, base,
|
|
74
|
+
data)` replaces the file only if it still holds `base` (`nil`: only if it does
|
|
75
|
+
not exist), else `ErrMoved`. `Change` reads, edits and writes under the lock, so
|
|
76
|
+
an edit always sees the truth and never needs a retry.
|
|
77
|
+
|
|
78
|
+
The same three calls in `python/abstraction_cas.py` (`read`, `write`, `change`,
|
|
79
|
+
`None` for missing, `Moved`) and `cpp/include/abstraction/cas.h` (`std::nullopt`
|
|
80
|
+
for missing, `Moved` thrown, an edit refuses by throwing). All three share one
|
|
81
|
+
lock and one rename, so writers in different languages on one file do not lose
|
|
82
|
+
each other's updates; `mixed.py` proves it and `scripts/check.sh` runs it.
|
|
83
|
+
|
|
84
|
+
## Invariants, each with a test in `go/cas_test.go`, `python/test_abstraction_cas.py`, `cpp/test/test_cas.cpp`
|
|
85
|
+
|
|
86
|
+
- **Whole or nothing.** A reader at any moment, from any process, sees a value
|
|
87
|
+
some writer wrote in full. Never a partial file, never a mixture.
|
|
88
|
+
- **No lost update.** Every successful `Change` was applied to the value left
|
|
89
|
+
by the previous successful one, across goroutines and across processes.
|
|
90
|
+
- **A stale base is refused** and leaves the file untouched.
|
|
91
|
+
- **An ended record stays ended.** An edit that refuses on what it reads is
|
|
92
|
+
refusing on the truth, so nothing can be walked backwards by a writer that
|
|
93
|
+
read earlier.
|
|
94
|
+
- **Nothing left behind.** Contention and refusal leave no temporary files.
|
|
95
|
+
|
|
96
|
+
## How
|
|
97
|
+
|
|
98
|
+
The data is staged in a temporary file beside the target, fsynced, and renamed
|
|
99
|
+
over it, under the lock the contract below names, which the kernel releases
|
|
100
|
+
when the holder dies — no lock is ever broken by a timeout. On Windows every
|
|
101
|
+
read opens with `FILE_SHARE_DELETE` and the rename is `FileRenameInfoEx` with
|
|
102
|
+
POSIX semantics, so a reader of ours never blocks a replace; Go gets both from
|
|
103
|
+
`os.Root`, Python and C++ call them. A Samba share answers `FileRenameInfoEx`
|
|
104
|
+
with `ERROR_INVALID_PARAMETER`, flag or no flag, so there the fallback runs on
|
|
105
|
+
every write — `MoveFileEx` in Python and C++, `FileRenameInformation` in Go —
|
|
106
|
+
and a reader does block a replace; see below.
|
|
107
|
+
|
|
108
|
+
**The lock is one machine's.** A byte-range lock taken over SMB and a `flock`
|
|
109
|
+
taken on the server's own volume never meet: writers on the PC and on the NAS
|
|
110
|
+
on one file lost or refused 147–149 of 2150 updates in three runs of three,
|
|
111
|
+
every PC writer dying within its first few changes
|
|
112
|
+
(`feedback/2026-09-06-shareloc.md`, harness `research/locks/twohost.py`). Six
|
|
113
|
+
writers in three languages on the PC alone, over the same share, lost nothing
|
|
114
|
+
in 1800. Two hosts on one record need a protocol this file does not have.
|
|
115
|
+
|
|
116
|
+
## What a fourth implementation must do
|
|
117
|
+
|
|
118
|
+
Two implementations on one file agree on the bytes they write and on the way
|
|
119
|
+
they keep each other out. Every single-language test proves the first only: a
|
|
120
|
+
writer with another lock passes all of them and loses updates against ours.
|
|
121
|
+
|
|
122
|
+
- **The lock is byte 0 of `<path>.lock`, exclusive, blocking, no timeout.**
|
|
123
|
+
The lock file sits beside the data file, is opened read-write and created
|
|
124
|
+
`0600` when missing, and is never written, truncated or deleted: a deleted
|
|
125
|
+
lock file is a new inode, and holders of two inodes exclude nobody. Windows
|
|
126
|
+
is `LockFileEx(LOCKFILE_EXCLUSIVE_LOCK)` at offset 0, length 1. POSIX is
|
|
127
|
+
`flock(LOCK_EX)`. Not `fcntl`, `lockf` or an OFD lock: on Linux those are a
|
|
128
|
+
separate lock table, invisible to `flock`. Not `msvcrt.locking`: it retries
|
|
129
|
+
once a second and fails after ten.
|
|
130
|
+
- **`Write` and `Change` hold it from before the read to after the rename.
|
|
131
|
+
`Read` never takes it.**
|
|
132
|
+
- **Missing and empty are different values**, and a `Change` between them
|
|
133
|
+
writes.
|
|
134
|
+
- **The replacement is a fresh temporary file in the same directory, fsynced,
|
|
135
|
+
renamed over `<path>`.** The name is unique to the writer — `CreateTemp`,
|
|
136
|
+
`mkstemp`, `CREATE_NEW` — so two languages staging at once never open each
|
|
137
|
+
other's file. Windows is `FileRenameInfoEx` with `REPLACE_IF_EXISTS |
|
|
138
|
+
POSIX_SEMANTICS`, falling back to `MoveFileEx(REPLACE_EXISTING)` where the
|
|
139
|
+
volume refuses the flags; `ERROR_SHARING_VIOLATION` and `ERROR_ACCESS_DENIED`
|
|
140
|
+
are retried a bounded number of times, then returned with the temporary
|
|
141
|
+
removed. POSIX is `rename(2)`.
|
|
142
|
+
- **`Read` opens with every share mode on Windows** and holds the handle for
|
|
143
|
+
the read only.
|
|
144
|
+
|
|
145
|
+
Prove it by being the seventh process in `mixed.py`: a program that, given
|
|
146
|
+
`CAS_PATH`, `CAS_ROLE` and `CAS_N`, applies `CAS_N` increments of the `"n n"`
|
|
147
|
+
counter as a `writer` or reads until it sees `CAS_N` as a `reader`, aborting on
|
|
148
|
+
a torn read. Add its command to `commands()`; the run counts its updates with
|
|
149
|
+
ours. A fourth implementation whose lock ours cannot see loses a third of its
|
|
150
|
+
updates on the first run — measured with a Python writer on byte 1 on Windows
|
|
151
|
+
and on `lockf` on Linux, in `feedback/2026-09-06-mixedgate.md`. `mixed.py`
|
|
152
|
+
takes a directory as its second argument; a UNC path runs the six on a share.
|
|
153
|
+
|
|
154
|
+
## Today
|
|
155
|
+
|
|
156
|
+
Go, Python and C++, each the three calls. Consumed by `job`'s file store,
|
|
157
|
+
`config`, `rights` and `asks`.
|
|
158
|
+
|
|
159
|
+
What may break:
|
|
160
|
+
|
|
161
|
+
- A handle on the data file without delete sharing — an editor, `type`,
|
|
162
|
+
Python's `open()` — makes a Windows replace fail with `ERROR_SHARING_VIOLATION`
|
|
163
|
+
for as long as it is held. The writer retries 1000 times — 160 to 470 ms on
|
|
164
|
+
local NTFS, process start included, the same in all three languages — then
|
|
165
|
+
returns the error with nothing left behind; the lock was held throughout, so
|
|
166
|
+
no other writer's update is lost, and the file still holds the previous
|
|
167
|
+
value. A handle opened with `FILE_SHARE_DELETE` keeps reading the bytes it
|
|
168
|
+
opened while the rename succeeds under it, 2 ms measured.
|
|
169
|
+
- Over SMB every open handle on the data file blocks the replace, ours
|
|
170
|
+
included, share-delete or not, from the same client or another process on
|
|
171
|
+
it: the fallback rename fails with `ERROR_ACCESS_DENIED`, 11 to 126 ms a
|
|
172
|
+
try, so the 1000 tries last 25 s in Python and 31 s in C++ under a held
|
|
173
|
+
handle, and Go's `os.Root` rename does not return at all — killed at 90 s,
|
|
174
|
+
its temporary left behind. A reader that polls keeps the tries failing for as
|
|
175
|
+
long as it polls: six writers and three readers of ours on one share stalled
|
|
176
|
+
at 85 and 50 of 600, C++ and Python returned the error, Go never returned. A
|
|
177
|
+
process on the server holding the file open blocks nothing,
|
|
178
|
+
and the server's own rename is never blocked. `Read` over SMB can itself
|
|
179
|
+
fail with `ERROR_ACCESS_DENIED` on open while the server renames underneath
|
|
180
|
+
it, rather than returning either value.
|
|
181
|
+
- Every write is an fsync: about 6 ms on this machine. Right for a policy file
|
|
182
|
+
written at human rate; wrong for a lease renewed every millisecond.
|
|
183
|
+
- **Python flushes the directory after the rename; Go and C++ do not yet.**
|
|
184
|
+
Without that flush a power cut can lose the rename: the previous value
|
|
185
|
+
survives, the acknowledged one may not. Python opens the directory and
|
|
186
|
+
flushes it — `fsync` on POSIX, `FlushFileBuffers` on a
|
|
187
|
+
`FILE_FLAG_BACKUP_SEMANTICS` handle on Windows — and raises anything it does
|
|
188
|
+
not recognise. Where the volume answers `ERROR_INVALID_FUNCTION`,
|
|
189
|
+
`ERROR_NOT_SUPPORTED` or `ERROR_ACCESS_DENIED` — Samba over SMB is the one we
|
|
190
|
+
have met — the write is acknowledged without that guarantee. The flush costs
|
|
191
|
+
0.1 ms on NTFS, 1.7 ms on ext4 and 33 ms on the NAS's btrfs. What it buys is
|
|
192
|
+
the rename reaching the disk, not that the file system would have lost it
|
|
193
|
+
without it: whether NTFS, ext4, btrfs or APFS keep the rename anyway is a
|
|
194
|
+
power-cut test nobody has run.
|
|
195
|
+
- A writer killed between staging and renaming — power cut, `TerminateProcess`,
|
|
196
|
+
`SIGKILL` — leaves one `<name>.<unique>.tmp` beside the file. The data file
|
|
197
|
+
is untouched and still holds its previous value, and nothing removes the
|
|
198
|
+
temporary on the write path: finding it means reading the directory, which
|
|
199
|
+
would cost every write the directory's size. The name is the sweeper's
|
|
200
|
+
handle — anything matching `<name>.*.tmp` beside the file is dead, because a
|
|
201
|
+
live writer stages under the lock. Python's `sweep(path)` takes that lock and
|
|
202
|
+
removes them, and returns how many; Go and C++ have no such call yet.
|
|
203
|
+
- A reader that polls is a tax on every writer of the file on Windows: 4.4 ms
|
|
204
|
+
per update alone, 6 to 16 ms with one reader spinning, from open-close
|
|
205
|
+
contention on the name and not from the rename retry.
|
|
206
|
+
- The names we derive are longer than the one you gave us — `<path>.lock` and
|
|
207
|
+
`<path>.XXXXXXXX.tmp` — so on Windows with `LongPathsEnabled = 0` a path a
|
|
208
|
+
bare `open()` accepts can be one `cas` cannot. **Python prefixes `\\?\` once
|
|
209
|
+
the staged name would pass `MAX_PATH`, and Go's own `os` package does the
|
|
210
|
+
same for it, so both take every path a bare `open()` takes and some it does
|
|
211
|
+
not; C++ refuses from 246 characters up, where `open()` still works.**
|
|
212
|
+
- An edit that calls `write`, `change` or `sweep` on the file it is editing
|
|
213
|
+
cannot be served: the lock is held and a second handle on it is another
|
|
214
|
+
holder, so the call would wait for itself. **Python raises `Reentrant`; Go
|
|
215
|
+
and C++ block forever with no message.** Nesting on a different file is fine
|
|
216
|
+
and is how a record and its index are written together.
|
|
217
|
+
- An edit that returns no value where the file holds one is refused —
|
|
218
|
+
`NoValue` in Python, `ErrNoValue` in Go — and the file is left as it was. It
|
|
219
|
+
is not a way to empty a file (write an empty value) or to delete one (there
|
|
220
|
+
is no delete). An edit that returns what it was given changes nothing, and on
|
|
221
|
+
a missing file that means the file stays missing. C++ cannot express it: its
|
|
222
|
+
`Edit` returns `std::string`.
|
|
223
|
+
- The lock file stays. Delete the data file and `<path>.lock` together.
|
|
224
|
+
|
|
225
|
+
## Conformance
|
|
226
|
+
|
|
227
|
+
[`mixed.py`](mixed.py): two Go, two C++ and two Python writers making 100
|
|
228
|
+
changes each to one file whose name is the last line of
|
|
229
|
+
[`names.list`](names.list) — an emoji, a CJK pair, a macron and a combining
|
|
230
|
+
acute, because until 2026-09-09 no test in any language used a file name longer
|
|
231
|
+
than one ASCII letter and the Windows rename in Python had been counting code
|
|
232
|
+
points where the kernel counts UTF-16 units — a reader per language spinning
|
|
233
|
+
through every rename —
|
|
234
|
+
[`CAS-MIXED1.txt`](https://github.com/openabstractions/abstractions/blob/main/docs/results/CAS-MIXED1.txt).
|
|
235
|
+
Windows; macOS `UNPROVEN`. Each language's own tests are named above.
|
|
236
|
+
|
|
237
|
+
## Where it sits
|
|
238
|
+
|
|
239
|
+
Below: nothing of ours. Above:
|
|
240
|
+
[abstraction-job](https://github.com/openabstractions/abstraction-job) writes
|
|
241
|
+
every record through it, and
|
|
242
|
+
[abstraction-config](https://github.com/openabstractions/abstraction-config),
|
|
243
|
+
[abstraction-asks](https://github.com/openabstractions/abstraction-asks) and
|
|
244
|
+
[abstraction-rights](https://github.com/openabstractions/abstraction-rights)
|
|
245
|
+
keep their files with it.
|
|
246
|
+
|
|
247
|
+
One layer of [openabstractions](https://github.com/openabstractions/abstractions).
|
|
248
|
+
Every layer names one thing local tools rebuild on their own; the name means the
|
|
249
|
+
same in each language that implements it, and the conformance scenarios are what
|
|
250
|
+
hold an implementation to it.
|
|
251
|
+
|
|
252
|
+
## Requirements
|
|
253
|
+
|
|
254
|
+
Go 1.25 or newer, standard library only. Python 3.9 or newer, standard library
|
|
255
|
+
only. C++17.
|
|
256
|
+
|
|
257
|
+
## Licence
|
|
258
|
+
|
|
259
|
+
Apache-2.0. See [LICENSE](https://github.com/openabstractions/abstraction-cas/blob/main/LICENSE).
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
# abstraction-cas
|
|
2
|
+
|
|
3
|
+
**In development.** Tagged `go/v0.1.0`, but this page predates that tag: no
|
|
4
|
+
capability claim, no contract page, no conformance scenario of its own.
|
|
5
|
+
|
|
6
|
+
A file is replaced whole or not at all, and every change is applied to the value
|
|
7
|
+
the previous change left — across processes and across languages sharing one
|
|
8
|
+
kernel.
|
|
9
|
+
|
|
10
|
+
## The problem
|
|
11
|
+
|
|
12
|
+
Every service that keeps state in a file rewrites it, and two writers on one
|
|
13
|
+
file lose each other's updates unless they agree on a lock and a rename. Each
|
|
14
|
+
service of ours hand-rolled its own writer. This is the floor under all of them:
|
|
15
|
+
one lock, one rename, one implementation per language we ship. Not a
|
|
16
|
+
layer — no contract page, no capability claim. A stranger implementing `job`
|
|
17
|
+
needs its page, not this.
|
|
18
|
+
|
|
19
|
+
## Words
|
|
20
|
+
|
|
21
|
+
| word | meaning |
|
|
22
|
+
|---|---|
|
|
23
|
+
| **read** | the whole file, or nothing when there is none |
|
|
24
|
+
| **write** | replace the file only if it still holds `base`; else `Moved` |
|
|
25
|
+
| **change** | read, edit and write under the lock, so an edit always sees the truth and never needs a retry |
|
|
26
|
+
| **base** | the value a writer read; `nil` means *only if the file does not exist* |
|
|
27
|
+
| **lock** | byte 0 of `<path>.lock`, exclusive, blocking, released by the kernel when the holder dies |
|
|
28
|
+
|
|
29
|
+
No rule on this page carries a tag. The invariants below each carry a test, and
|
|
30
|
+
*What a fourth implementation must do* is cited by
|
|
31
|
+
[the job page](https://github.com/openabstractions/abstraction-job).
|
|
32
|
+
|
|
33
|
+
## Obtain
|
|
34
|
+
|
|
35
|
+
- **Go.** `go get github.com/openabstractions/abstraction-cas/go`. No tag yet;
|
|
36
|
+
`go get` resolves a pseudo-version of `main`.
|
|
37
|
+
- **Python.** Not on any index. `python/abstraction_cas.py` is one module with
|
|
38
|
+
no imports of ours; `python/pyproject.toml` builds a wheel
|
|
39
|
+
(`pip wheel python/`).
|
|
40
|
+
- **C++.** One header and one source, C++17, standard library only, and no build
|
|
41
|
+
file: name them. `g++ -std=c++17 -I cpp/include cpp/test/test_cas.cpp
|
|
42
|
+
cpp/src/cas.cpp -o test_cas` compiles and passes this layer's own test; MSVC
|
|
43
|
+
19.51 takes the same two files under `/std:c++17` and `/I`. Measured
|
|
44
|
+
2026-09-09. There
|
|
45
|
+
is no tagged release and no package.
|
|
46
|
+
|
|
47
|
+
## Example
|
|
48
|
+
|
|
49
|
+
```go
|
|
50
|
+
import cas "github.com/openabstractions/abstraction-cas/go"
|
|
51
|
+
|
|
52
|
+
err := cas.Change(path, func(cur []byte) ([]byte, error) {
|
|
53
|
+
var f file
|
|
54
|
+
if cur != nil {
|
|
55
|
+
json.Unmarshal(cur, &f)
|
|
56
|
+
}
|
|
57
|
+
f.Apps = append(f.Apps, app)
|
|
58
|
+
return json.Marshal(f)
|
|
59
|
+
})
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
`Read` returns the whole file, or `nil` when there is none. `Write(path, base,
|
|
63
|
+
data)` replaces the file only if it still holds `base` (`nil`: only if it does
|
|
64
|
+
not exist), else `ErrMoved`. `Change` reads, edits and writes under the lock, so
|
|
65
|
+
an edit always sees the truth and never needs a retry.
|
|
66
|
+
|
|
67
|
+
The same three calls in `python/abstraction_cas.py` (`read`, `write`, `change`,
|
|
68
|
+
`None` for missing, `Moved`) and `cpp/include/abstraction/cas.h` (`std::nullopt`
|
|
69
|
+
for missing, `Moved` thrown, an edit refuses by throwing). All three share one
|
|
70
|
+
lock and one rename, so writers in different languages on one file do not lose
|
|
71
|
+
each other's updates; `mixed.py` proves it and `scripts/check.sh` runs it.
|
|
72
|
+
|
|
73
|
+
## Invariants, each with a test in `go/cas_test.go`, `python/test_abstraction_cas.py`, `cpp/test/test_cas.cpp`
|
|
74
|
+
|
|
75
|
+
- **Whole or nothing.** A reader at any moment, from any process, sees a value
|
|
76
|
+
some writer wrote in full. Never a partial file, never a mixture.
|
|
77
|
+
- **No lost update.** Every successful `Change` was applied to the value left
|
|
78
|
+
by the previous successful one, across goroutines and across processes.
|
|
79
|
+
- **A stale base is refused** and leaves the file untouched.
|
|
80
|
+
- **An ended record stays ended.** An edit that refuses on what it reads is
|
|
81
|
+
refusing on the truth, so nothing can be walked backwards by a writer that
|
|
82
|
+
read earlier.
|
|
83
|
+
- **Nothing left behind.** Contention and refusal leave no temporary files.
|
|
84
|
+
|
|
85
|
+
## How
|
|
86
|
+
|
|
87
|
+
The data is staged in a temporary file beside the target, fsynced, and renamed
|
|
88
|
+
over it, under the lock the contract below names, which the kernel releases
|
|
89
|
+
when the holder dies — no lock is ever broken by a timeout. On Windows every
|
|
90
|
+
read opens with `FILE_SHARE_DELETE` and the rename is `FileRenameInfoEx` with
|
|
91
|
+
POSIX semantics, so a reader of ours never blocks a replace; Go gets both from
|
|
92
|
+
`os.Root`, Python and C++ call them. A Samba share answers `FileRenameInfoEx`
|
|
93
|
+
with `ERROR_INVALID_PARAMETER`, flag or no flag, so there the fallback runs on
|
|
94
|
+
every write — `MoveFileEx` in Python and C++, `FileRenameInformation` in Go —
|
|
95
|
+
and a reader does block a replace; see below.
|
|
96
|
+
|
|
97
|
+
**The lock is one machine's.** A byte-range lock taken over SMB and a `flock`
|
|
98
|
+
taken on the server's own volume never meet: writers on the PC and on the NAS
|
|
99
|
+
on one file lost or refused 147–149 of 2150 updates in three runs of three,
|
|
100
|
+
every PC writer dying within its first few changes
|
|
101
|
+
(`feedback/2026-09-06-shareloc.md`, harness `research/locks/twohost.py`). Six
|
|
102
|
+
writers in three languages on the PC alone, over the same share, lost nothing
|
|
103
|
+
in 1800. Two hosts on one record need a protocol this file does not have.
|
|
104
|
+
|
|
105
|
+
## What a fourth implementation must do
|
|
106
|
+
|
|
107
|
+
Two implementations on one file agree on the bytes they write and on the way
|
|
108
|
+
they keep each other out. Every single-language test proves the first only: a
|
|
109
|
+
writer with another lock passes all of them and loses updates against ours.
|
|
110
|
+
|
|
111
|
+
- **The lock is byte 0 of `<path>.lock`, exclusive, blocking, no timeout.**
|
|
112
|
+
The lock file sits beside the data file, is opened read-write and created
|
|
113
|
+
`0600` when missing, and is never written, truncated or deleted: a deleted
|
|
114
|
+
lock file is a new inode, and holders of two inodes exclude nobody. Windows
|
|
115
|
+
is `LockFileEx(LOCKFILE_EXCLUSIVE_LOCK)` at offset 0, length 1. POSIX is
|
|
116
|
+
`flock(LOCK_EX)`. Not `fcntl`, `lockf` or an OFD lock: on Linux those are a
|
|
117
|
+
separate lock table, invisible to `flock`. Not `msvcrt.locking`: it retries
|
|
118
|
+
once a second and fails after ten.
|
|
119
|
+
- **`Write` and `Change` hold it from before the read to after the rename.
|
|
120
|
+
`Read` never takes it.**
|
|
121
|
+
- **Missing and empty are different values**, and a `Change` between them
|
|
122
|
+
writes.
|
|
123
|
+
- **The replacement is a fresh temporary file in the same directory, fsynced,
|
|
124
|
+
renamed over `<path>`.** The name is unique to the writer — `CreateTemp`,
|
|
125
|
+
`mkstemp`, `CREATE_NEW` — so two languages staging at once never open each
|
|
126
|
+
other's file. Windows is `FileRenameInfoEx` with `REPLACE_IF_EXISTS |
|
|
127
|
+
POSIX_SEMANTICS`, falling back to `MoveFileEx(REPLACE_EXISTING)` where the
|
|
128
|
+
volume refuses the flags; `ERROR_SHARING_VIOLATION` and `ERROR_ACCESS_DENIED`
|
|
129
|
+
are retried a bounded number of times, then returned with the temporary
|
|
130
|
+
removed. POSIX is `rename(2)`.
|
|
131
|
+
- **`Read` opens with every share mode on Windows** and holds the handle for
|
|
132
|
+
the read only.
|
|
133
|
+
|
|
134
|
+
Prove it by being the seventh process in `mixed.py`: a program that, given
|
|
135
|
+
`CAS_PATH`, `CAS_ROLE` and `CAS_N`, applies `CAS_N` increments of the `"n n"`
|
|
136
|
+
counter as a `writer` or reads until it sees `CAS_N` as a `reader`, aborting on
|
|
137
|
+
a torn read. Add its command to `commands()`; the run counts its updates with
|
|
138
|
+
ours. A fourth implementation whose lock ours cannot see loses a third of its
|
|
139
|
+
updates on the first run — measured with a Python writer on byte 1 on Windows
|
|
140
|
+
and on `lockf` on Linux, in `feedback/2026-09-06-mixedgate.md`. `mixed.py`
|
|
141
|
+
takes a directory as its second argument; a UNC path runs the six on a share.
|
|
142
|
+
|
|
143
|
+
## Today
|
|
144
|
+
|
|
145
|
+
Go, Python and C++, each the three calls. Consumed by `job`'s file store,
|
|
146
|
+
`config`, `rights` and `asks`.
|
|
147
|
+
|
|
148
|
+
What may break:
|
|
149
|
+
|
|
150
|
+
- A handle on the data file without delete sharing — an editor, `type`,
|
|
151
|
+
Python's `open()` — makes a Windows replace fail with `ERROR_SHARING_VIOLATION`
|
|
152
|
+
for as long as it is held. The writer retries 1000 times — 160 to 470 ms on
|
|
153
|
+
local NTFS, process start included, the same in all three languages — then
|
|
154
|
+
returns the error with nothing left behind; the lock was held throughout, so
|
|
155
|
+
no other writer's update is lost, and the file still holds the previous
|
|
156
|
+
value. A handle opened with `FILE_SHARE_DELETE` keeps reading the bytes it
|
|
157
|
+
opened while the rename succeeds under it, 2 ms measured.
|
|
158
|
+
- Over SMB every open handle on the data file blocks the replace, ours
|
|
159
|
+
included, share-delete or not, from the same client or another process on
|
|
160
|
+
it: the fallback rename fails with `ERROR_ACCESS_DENIED`, 11 to 126 ms a
|
|
161
|
+
try, so the 1000 tries last 25 s in Python and 31 s in C++ under a held
|
|
162
|
+
handle, and Go's `os.Root` rename does not return at all — killed at 90 s,
|
|
163
|
+
its temporary left behind. A reader that polls keeps the tries failing for as
|
|
164
|
+
long as it polls: six writers and three readers of ours on one share stalled
|
|
165
|
+
at 85 and 50 of 600, C++ and Python returned the error, Go never returned. A
|
|
166
|
+
process on the server holding the file open blocks nothing,
|
|
167
|
+
and the server's own rename is never blocked. `Read` over SMB can itself
|
|
168
|
+
fail with `ERROR_ACCESS_DENIED` on open while the server renames underneath
|
|
169
|
+
it, rather than returning either value.
|
|
170
|
+
- Every write is an fsync: about 6 ms on this machine. Right for a policy file
|
|
171
|
+
written at human rate; wrong for a lease renewed every millisecond.
|
|
172
|
+
- **Python flushes the directory after the rename; Go and C++ do not yet.**
|
|
173
|
+
Without that flush a power cut can lose the rename: the previous value
|
|
174
|
+
survives, the acknowledged one may not. Python opens the directory and
|
|
175
|
+
flushes it — `fsync` on POSIX, `FlushFileBuffers` on a
|
|
176
|
+
`FILE_FLAG_BACKUP_SEMANTICS` handle on Windows — and raises anything it does
|
|
177
|
+
not recognise. Where the volume answers `ERROR_INVALID_FUNCTION`,
|
|
178
|
+
`ERROR_NOT_SUPPORTED` or `ERROR_ACCESS_DENIED` — Samba over SMB is the one we
|
|
179
|
+
have met — the write is acknowledged without that guarantee. The flush costs
|
|
180
|
+
0.1 ms on NTFS, 1.7 ms on ext4 and 33 ms on the NAS's btrfs. What it buys is
|
|
181
|
+
the rename reaching the disk, not that the file system would have lost it
|
|
182
|
+
without it: whether NTFS, ext4, btrfs or APFS keep the rename anyway is a
|
|
183
|
+
power-cut test nobody has run.
|
|
184
|
+
- A writer killed between staging and renaming — power cut, `TerminateProcess`,
|
|
185
|
+
`SIGKILL` — leaves one `<name>.<unique>.tmp` beside the file. The data file
|
|
186
|
+
is untouched and still holds its previous value, and nothing removes the
|
|
187
|
+
temporary on the write path: finding it means reading the directory, which
|
|
188
|
+
would cost every write the directory's size. The name is the sweeper's
|
|
189
|
+
handle — anything matching `<name>.*.tmp` beside the file is dead, because a
|
|
190
|
+
live writer stages under the lock. Python's `sweep(path)` takes that lock and
|
|
191
|
+
removes them, and returns how many; Go and C++ have no such call yet.
|
|
192
|
+
- A reader that polls is a tax on every writer of the file on Windows: 4.4 ms
|
|
193
|
+
per update alone, 6 to 16 ms with one reader spinning, from open-close
|
|
194
|
+
contention on the name and not from the rename retry.
|
|
195
|
+
- The names we derive are longer than the one you gave us — `<path>.lock` and
|
|
196
|
+
`<path>.XXXXXXXX.tmp` — so on Windows with `LongPathsEnabled = 0` a path a
|
|
197
|
+
bare `open()` accepts can be one `cas` cannot. **Python prefixes `\\?\` once
|
|
198
|
+
the staged name would pass `MAX_PATH`, and Go's own `os` package does the
|
|
199
|
+
same for it, so both take every path a bare `open()` takes and some it does
|
|
200
|
+
not; C++ refuses from 246 characters up, where `open()` still works.**
|
|
201
|
+
- An edit that calls `write`, `change` or `sweep` on the file it is editing
|
|
202
|
+
cannot be served: the lock is held and a second handle on it is another
|
|
203
|
+
holder, so the call would wait for itself. **Python raises `Reentrant`; Go
|
|
204
|
+
and C++ block forever with no message.** Nesting on a different file is fine
|
|
205
|
+
and is how a record and its index are written together.
|
|
206
|
+
- An edit that returns no value where the file holds one is refused —
|
|
207
|
+
`NoValue` in Python, `ErrNoValue` in Go — and the file is left as it was. It
|
|
208
|
+
is not a way to empty a file (write an empty value) or to delete one (there
|
|
209
|
+
is no delete). An edit that returns what it was given changes nothing, and on
|
|
210
|
+
a missing file that means the file stays missing. C++ cannot express it: its
|
|
211
|
+
`Edit` returns `std::string`.
|
|
212
|
+
- The lock file stays. Delete the data file and `<path>.lock` together.
|
|
213
|
+
|
|
214
|
+
## Conformance
|
|
215
|
+
|
|
216
|
+
[`mixed.py`](mixed.py): two Go, two C++ and two Python writers making 100
|
|
217
|
+
changes each to one file whose name is the last line of
|
|
218
|
+
[`names.list`](names.list) — an emoji, a CJK pair, a macron and a combining
|
|
219
|
+
acute, because until 2026-09-09 no test in any language used a file name longer
|
|
220
|
+
than one ASCII letter and the Windows rename in Python had been counting code
|
|
221
|
+
points where the kernel counts UTF-16 units — a reader per language spinning
|
|
222
|
+
through every rename —
|
|
223
|
+
[`CAS-MIXED1.txt`](https://github.com/openabstractions/abstractions/blob/main/docs/results/CAS-MIXED1.txt).
|
|
224
|
+
Windows; macOS `UNPROVEN`. Each language's own tests are named above.
|
|
225
|
+
|
|
226
|
+
## Where it sits
|
|
227
|
+
|
|
228
|
+
Below: nothing of ours. Above:
|
|
229
|
+
[abstraction-job](https://github.com/openabstractions/abstraction-job) writes
|
|
230
|
+
every record through it, and
|
|
231
|
+
[abstraction-config](https://github.com/openabstractions/abstraction-config),
|
|
232
|
+
[abstraction-asks](https://github.com/openabstractions/abstraction-asks) and
|
|
233
|
+
[abstraction-rights](https://github.com/openabstractions/abstraction-rights)
|
|
234
|
+
keep their files with it.
|
|
235
|
+
|
|
236
|
+
One layer of [openabstractions](https://github.com/openabstractions/abstractions).
|
|
237
|
+
Every layer names one thing local tools rebuild on their own; the name means the
|
|
238
|
+
same in each language that implements it, and the conformance scenarios are what
|
|
239
|
+
hold an implementation to it.
|
|
240
|
+
|
|
241
|
+
## Requirements
|
|
242
|
+
|
|
243
|
+
Go 1.25 or newer, standard library only. Python 3.9 or newer, standard library
|
|
244
|
+
only. C++17.
|
|
245
|
+
|
|
246
|
+
## Licence
|
|
247
|
+
|
|
248
|
+
Apache-2.0. See [LICENSE](https://github.com/openabstractions/abstraction-cas/blob/main/LICENSE).
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: abstraction-cas
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Compare-and-set over a file. The floor under every service that keeps state in a file.
|
|
5
|
+
License-Expression: Apache-2.0
|
|
6
|
+
Project-URL: Source, https://github.com/openabstractions/abstraction-cas
|
|
7
|
+
Requires-Python: >=3.9
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Dynamic: license-file
|
|
11
|
+
|
|
12
|
+
# abstraction-cas
|
|
13
|
+
|
|
14
|
+
**In development.** Tagged `go/v0.1.0`, but this page predates that tag: no
|
|
15
|
+
capability claim, no contract page, no conformance scenario of its own.
|
|
16
|
+
|
|
17
|
+
A file is replaced whole or not at all, and every change is applied to the value
|
|
18
|
+
the previous change left — across processes and across languages sharing one
|
|
19
|
+
kernel.
|
|
20
|
+
|
|
21
|
+
## The problem
|
|
22
|
+
|
|
23
|
+
Every service that keeps state in a file rewrites it, and two writers on one
|
|
24
|
+
file lose each other's updates unless they agree on a lock and a rename. Each
|
|
25
|
+
service of ours hand-rolled its own writer. This is the floor under all of them:
|
|
26
|
+
one lock, one rename, one implementation per language we ship. Not a
|
|
27
|
+
layer — no contract page, no capability claim. A stranger implementing `job`
|
|
28
|
+
needs its page, not this.
|
|
29
|
+
|
|
30
|
+
## Words
|
|
31
|
+
|
|
32
|
+
| word | meaning |
|
|
33
|
+
|---|---|
|
|
34
|
+
| **read** | the whole file, or nothing when there is none |
|
|
35
|
+
| **write** | replace the file only if it still holds `base`; else `Moved` |
|
|
36
|
+
| **change** | read, edit and write under the lock, so an edit always sees the truth and never needs a retry |
|
|
37
|
+
| **base** | the value a writer read; `nil` means *only if the file does not exist* |
|
|
38
|
+
| **lock** | byte 0 of `<path>.lock`, exclusive, blocking, released by the kernel when the holder dies |
|
|
39
|
+
|
|
40
|
+
No rule on this page carries a tag. The invariants below each carry a test, and
|
|
41
|
+
*What a fourth implementation must do* is cited by
|
|
42
|
+
[the job page](https://github.com/openabstractions/abstraction-job).
|
|
43
|
+
|
|
44
|
+
## Obtain
|
|
45
|
+
|
|
46
|
+
- **Go.** `go get github.com/openabstractions/abstraction-cas/go`. No tag yet;
|
|
47
|
+
`go get` resolves a pseudo-version of `main`.
|
|
48
|
+
- **Python.** Not on any index. `python/abstraction_cas.py` is one module with
|
|
49
|
+
no imports of ours; `python/pyproject.toml` builds a wheel
|
|
50
|
+
(`pip wheel python/`).
|
|
51
|
+
- **C++.** One header and one source, C++17, standard library only, and no build
|
|
52
|
+
file: name them. `g++ -std=c++17 -I cpp/include cpp/test/test_cas.cpp
|
|
53
|
+
cpp/src/cas.cpp -o test_cas` compiles and passes this layer's own test; MSVC
|
|
54
|
+
19.51 takes the same two files under `/std:c++17` and `/I`. Measured
|
|
55
|
+
2026-09-09. There
|
|
56
|
+
is no tagged release and no package.
|
|
57
|
+
|
|
58
|
+
## Example
|
|
59
|
+
|
|
60
|
+
```go
|
|
61
|
+
import cas "github.com/openabstractions/abstraction-cas/go"
|
|
62
|
+
|
|
63
|
+
err := cas.Change(path, func(cur []byte) ([]byte, error) {
|
|
64
|
+
var f file
|
|
65
|
+
if cur != nil {
|
|
66
|
+
json.Unmarshal(cur, &f)
|
|
67
|
+
}
|
|
68
|
+
f.Apps = append(f.Apps, app)
|
|
69
|
+
return json.Marshal(f)
|
|
70
|
+
})
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
`Read` returns the whole file, or `nil` when there is none. `Write(path, base,
|
|
74
|
+
data)` replaces the file only if it still holds `base` (`nil`: only if it does
|
|
75
|
+
not exist), else `ErrMoved`. `Change` reads, edits and writes under the lock, so
|
|
76
|
+
an edit always sees the truth and never needs a retry.
|
|
77
|
+
|
|
78
|
+
The same three calls in `python/abstraction_cas.py` (`read`, `write`, `change`,
|
|
79
|
+
`None` for missing, `Moved`) and `cpp/include/abstraction/cas.h` (`std::nullopt`
|
|
80
|
+
for missing, `Moved` thrown, an edit refuses by throwing). All three share one
|
|
81
|
+
lock and one rename, so writers in different languages on one file do not lose
|
|
82
|
+
each other's updates; `mixed.py` proves it and `scripts/check.sh` runs it.
|
|
83
|
+
|
|
84
|
+
## Invariants, each with a test in `go/cas_test.go`, `python/test_abstraction_cas.py`, `cpp/test/test_cas.cpp`
|
|
85
|
+
|
|
86
|
+
- **Whole or nothing.** A reader at any moment, from any process, sees a value
|
|
87
|
+
some writer wrote in full. Never a partial file, never a mixture.
|
|
88
|
+
- **No lost update.** Every successful `Change` was applied to the value left
|
|
89
|
+
by the previous successful one, across goroutines and across processes.
|
|
90
|
+
- **A stale base is refused** and leaves the file untouched.
|
|
91
|
+
- **An ended record stays ended.** An edit that refuses on what it reads is
|
|
92
|
+
refusing on the truth, so nothing can be walked backwards by a writer that
|
|
93
|
+
read earlier.
|
|
94
|
+
- **Nothing left behind.** Contention and refusal leave no temporary files.
|
|
95
|
+
|
|
96
|
+
## How
|
|
97
|
+
|
|
98
|
+
The data is staged in a temporary file beside the target, fsynced, and renamed
|
|
99
|
+
over it, under the lock the contract below names, which the kernel releases
|
|
100
|
+
when the holder dies — no lock is ever broken by a timeout. On Windows every
|
|
101
|
+
read opens with `FILE_SHARE_DELETE` and the rename is `FileRenameInfoEx` with
|
|
102
|
+
POSIX semantics, so a reader of ours never blocks a replace; Go gets both from
|
|
103
|
+
`os.Root`, Python and C++ call them. A Samba share answers `FileRenameInfoEx`
|
|
104
|
+
with `ERROR_INVALID_PARAMETER`, flag or no flag, so there the fallback runs on
|
|
105
|
+
every write — `MoveFileEx` in Python and C++, `FileRenameInformation` in Go —
|
|
106
|
+
and a reader does block a replace; see below.
|
|
107
|
+
|
|
108
|
+
**The lock is one machine's.** A byte-range lock taken over SMB and a `flock`
|
|
109
|
+
taken on the server's own volume never meet: writers on the PC and on the NAS
|
|
110
|
+
on one file lost or refused 147–149 of 2150 updates in three runs of three,
|
|
111
|
+
every PC writer dying within its first few changes
|
|
112
|
+
(`feedback/2026-09-06-shareloc.md`, harness `research/locks/twohost.py`). Six
|
|
113
|
+
writers in three languages on the PC alone, over the same share, lost nothing
|
|
114
|
+
in 1800. Two hosts on one record need a protocol this file does not have.
|
|
115
|
+
|
|
116
|
+
## What a fourth implementation must do
|
|
117
|
+
|
|
118
|
+
Two implementations on one file agree on the bytes they write and on the way
|
|
119
|
+
they keep each other out. Every single-language test proves the first only: a
|
|
120
|
+
writer with another lock passes all of them and loses updates against ours.
|
|
121
|
+
|
|
122
|
+
- **The lock is byte 0 of `<path>.lock`, exclusive, blocking, no timeout.**
|
|
123
|
+
The lock file sits beside the data file, is opened read-write and created
|
|
124
|
+
`0600` when missing, and is never written, truncated or deleted: a deleted
|
|
125
|
+
lock file is a new inode, and holders of two inodes exclude nobody. Windows
|
|
126
|
+
is `LockFileEx(LOCKFILE_EXCLUSIVE_LOCK)` at offset 0, length 1. POSIX is
|
|
127
|
+
`flock(LOCK_EX)`. Not `fcntl`, `lockf` or an OFD lock: on Linux those are a
|
|
128
|
+
separate lock table, invisible to `flock`. Not `msvcrt.locking`: it retries
|
|
129
|
+
once a second and fails after ten.
|
|
130
|
+
- **`Write` and `Change` hold it from before the read to after the rename.
|
|
131
|
+
`Read` never takes it.**
|
|
132
|
+
- **Missing and empty are different values**, and a `Change` between them
|
|
133
|
+
writes.
|
|
134
|
+
- **The replacement is a fresh temporary file in the same directory, fsynced,
|
|
135
|
+
renamed over `<path>`.** The name is unique to the writer — `CreateTemp`,
|
|
136
|
+
`mkstemp`, `CREATE_NEW` — so two languages staging at once never open each
|
|
137
|
+
other's file. Windows is `FileRenameInfoEx` with `REPLACE_IF_EXISTS |
|
|
138
|
+
POSIX_SEMANTICS`, falling back to `MoveFileEx(REPLACE_EXISTING)` where the
|
|
139
|
+
volume refuses the flags; `ERROR_SHARING_VIOLATION` and `ERROR_ACCESS_DENIED`
|
|
140
|
+
are retried a bounded number of times, then returned with the temporary
|
|
141
|
+
removed. POSIX is `rename(2)`.
|
|
142
|
+
- **`Read` opens with every share mode on Windows** and holds the handle for
|
|
143
|
+
the read only.
|
|
144
|
+
|
|
145
|
+
Prove it by being the seventh process in `mixed.py`: a program that, given
|
|
146
|
+
`CAS_PATH`, `CAS_ROLE` and `CAS_N`, applies `CAS_N` increments of the `"n n"`
|
|
147
|
+
counter as a `writer` or reads until it sees `CAS_N` as a `reader`, aborting on
|
|
148
|
+
a torn read. Add its command to `commands()`; the run counts its updates with
|
|
149
|
+
ours. A fourth implementation whose lock ours cannot see loses a third of its
|
|
150
|
+
updates on the first run — measured with a Python writer on byte 1 on Windows
|
|
151
|
+
and on `lockf` on Linux, in `feedback/2026-09-06-mixedgate.md`. `mixed.py`
|
|
152
|
+
takes a directory as its second argument; a UNC path runs the six on a share.
|
|
153
|
+
|
|
154
|
+
## Today
|
|
155
|
+
|
|
156
|
+
Go, Python and C++, each the three calls. Consumed by `job`'s file store,
|
|
157
|
+
`config`, `rights` and `asks`.
|
|
158
|
+
|
|
159
|
+
What may break:
|
|
160
|
+
|
|
161
|
+
- A handle on the data file without delete sharing — an editor, `type`,
|
|
162
|
+
Python's `open()` — makes a Windows replace fail with `ERROR_SHARING_VIOLATION`
|
|
163
|
+
for as long as it is held. The writer retries 1000 times — 160 to 470 ms on
|
|
164
|
+
local NTFS, process start included, the same in all three languages — then
|
|
165
|
+
returns the error with nothing left behind; the lock was held throughout, so
|
|
166
|
+
no other writer's update is lost, and the file still holds the previous
|
|
167
|
+
value. A handle opened with `FILE_SHARE_DELETE` keeps reading the bytes it
|
|
168
|
+
opened while the rename succeeds under it, 2 ms measured.
|
|
169
|
+
- Over SMB every open handle on the data file blocks the replace, ours
|
|
170
|
+
included, share-delete or not, from the same client or another process on
|
|
171
|
+
it: the fallback rename fails with `ERROR_ACCESS_DENIED`, 11 to 126 ms a
|
|
172
|
+
try, so the 1000 tries last 25 s in Python and 31 s in C++ under a held
|
|
173
|
+
handle, and Go's `os.Root` rename does not return at all — killed at 90 s,
|
|
174
|
+
its temporary left behind. A reader that polls keeps the tries failing for as
|
|
175
|
+
long as it polls: six writers and three readers of ours on one share stalled
|
|
176
|
+
at 85 and 50 of 600, C++ and Python returned the error, Go never returned. A
|
|
177
|
+
process on the server holding the file open blocks nothing,
|
|
178
|
+
and the server's own rename is never blocked. `Read` over SMB can itself
|
|
179
|
+
fail with `ERROR_ACCESS_DENIED` on open while the server renames underneath
|
|
180
|
+
it, rather than returning either value.
|
|
181
|
+
- Every write is an fsync: about 6 ms on this machine. Right for a policy file
|
|
182
|
+
written at human rate; wrong for a lease renewed every millisecond.
|
|
183
|
+
- **Python flushes the directory after the rename; Go and C++ do not yet.**
|
|
184
|
+
Without that flush a power cut can lose the rename: the previous value
|
|
185
|
+
survives, the acknowledged one may not. Python opens the directory and
|
|
186
|
+
flushes it — `fsync` on POSIX, `FlushFileBuffers` on a
|
|
187
|
+
`FILE_FLAG_BACKUP_SEMANTICS` handle on Windows — and raises anything it does
|
|
188
|
+
not recognise. Where the volume answers `ERROR_INVALID_FUNCTION`,
|
|
189
|
+
`ERROR_NOT_SUPPORTED` or `ERROR_ACCESS_DENIED` — Samba over SMB is the one we
|
|
190
|
+
have met — the write is acknowledged without that guarantee. The flush costs
|
|
191
|
+
0.1 ms on NTFS, 1.7 ms on ext4 and 33 ms on the NAS's btrfs. What it buys is
|
|
192
|
+
the rename reaching the disk, not that the file system would have lost it
|
|
193
|
+
without it: whether NTFS, ext4, btrfs or APFS keep the rename anyway is a
|
|
194
|
+
power-cut test nobody has run.
|
|
195
|
+
- A writer killed between staging and renaming — power cut, `TerminateProcess`,
|
|
196
|
+
`SIGKILL` — leaves one `<name>.<unique>.tmp` beside the file. The data file
|
|
197
|
+
is untouched and still holds its previous value, and nothing removes the
|
|
198
|
+
temporary on the write path: finding it means reading the directory, which
|
|
199
|
+
would cost every write the directory's size. The name is the sweeper's
|
|
200
|
+
handle — anything matching `<name>.*.tmp` beside the file is dead, because a
|
|
201
|
+
live writer stages under the lock. Python's `sweep(path)` takes that lock and
|
|
202
|
+
removes them, and returns how many; Go and C++ have no such call yet.
|
|
203
|
+
- A reader that polls is a tax on every writer of the file on Windows: 4.4 ms
|
|
204
|
+
per update alone, 6 to 16 ms with one reader spinning, from open-close
|
|
205
|
+
contention on the name and not from the rename retry.
|
|
206
|
+
- The names we derive are longer than the one you gave us — `<path>.lock` and
|
|
207
|
+
`<path>.XXXXXXXX.tmp` — so on Windows with `LongPathsEnabled = 0` a path a
|
|
208
|
+
bare `open()` accepts can be one `cas` cannot. **Python prefixes `\\?\` once
|
|
209
|
+
the staged name would pass `MAX_PATH`, and Go's own `os` package does the
|
|
210
|
+
same for it, so both take every path a bare `open()` takes and some it does
|
|
211
|
+
not; C++ refuses from 246 characters up, where `open()` still works.**
|
|
212
|
+
- An edit that calls `write`, `change` or `sweep` on the file it is editing
|
|
213
|
+
cannot be served: the lock is held and a second handle on it is another
|
|
214
|
+
holder, so the call would wait for itself. **Python raises `Reentrant`; Go
|
|
215
|
+
and C++ block forever with no message.** Nesting on a different file is fine
|
|
216
|
+
and is how a record and its index are written together.
|
|
217
|
+
- An edit that returns no value where the file holds one is refused —
|
|
218
|
+
`NoValue` in Python, `ErrNoValue` in Go — and the file is left as it was. It
|
|
219
|
+
is not a way to empty a file (write an empty value) or to delete one (there
|
|
220
|
+
is no delete). An edit that returns what it was given changes nothing, and on
|
|
221
|
+
a missing file that means the file stays missing. C++ cannot express it: its
|
|
222
|
+
`Edit` returns `std::string`.
|
|
223
|
+
- The lock file stays. Delete the data file and `<path>.lock` together.
|
|
224
|
+
|
|
225
|
+
## Conformance
|
|
226
|
+
|
|
227
|
+
[`mixed.py`](mixed.py): two Go, two C++ and two Python writers making 100
|
|
228
|
+
changes each to one file whose name is the last line of
|
|
229
|
+
[`names.list`](names.list) — an emoji, a CJK pair, a macron and a combining
|
|
230
|
+
acute, because until 2026-09-09 no test in any language used a file name longer
|
|
231
|
+
than one ASCII letter and the Windows rename in Python had been counting code
|
|
232
|
+
points where the kernel counts UTF-16 units — a reader per language spinning
|
|
233
|
+
through every rename —
|
|
234
|
+
[`CAS-MIXED1.txt`](https://github.com/openabstractions/abstractions/blob/main/docs/results/CAS-MIXED1.txt).
|
|
235
|
+
Windows; macOS `UNPROVEN`. Each language's own tests are named above.
|
|
236
|
+
|
|
237
|
+
## Where it sits
|
|
238
|
+
|
|
239
|
+
Below: nothing of ours. Above:
|
|
240
|
+
[abstraction-job](https://github.com/openabstractions/abstraction-job) writes
|
|
241
|
+
every record through it, and
|
|
242
|
+
[abstraction-config](https://github.com/openabstractions/abstraction-config),
|
|
243
|
+
[abstraction-asks](https://github.com/openabstractions/abstraction-asks) and
|
|
244
|
+
[abstraction-rights](https://github.com/openabstractions/abstraction-rights)
|
|
245
|
+
keep their files with it.
|
|
246
|
+
|
|
247
|
+
One layer of [openabstractions](https://github.com/openabstractions/abstractions).
|
|
248
|
+
Every layer names one thing local tools rebuild on their own; the name means the
|
|
249
|
+
same in each language that implements it, and the conformance scenarios are what
|
|
250
|
+
hold an implementation to it.
|
|
251
|
+
|
|
252
|
+
## Requirements
|
|
253
|
+
|
|
254
|
+
Go 1.25 or newer, standard library only. Python 3.9 or newer, standard library
|
|
255
|
+
only. C++17.
|
|
256
|
+
|
|
257
|
+
## Licence
|
|
258
|
+
|
|
259
|
+
Apache-2.0. See [LICENSE](https://github.com/openabstractions/abstraction-cas/blob/main/LICENSE).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
abstraction_cas
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
"""Compare-and-set over a file. Semantics in cas/README.md."""
|
|
2
|
+
|
|
3
|
+
import contextlib
|
|
4
|
+
import glob
|
|
5
|
+
import os
|
|
6
|
+
import sys
|
|
7
|
+
import tempfile
|
|
8
|
+
import threading
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Moved(Exception):
|
|
12
|
+
"""The file changed since it was read."""
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class NoValue(Exception):
|
|
16
|
+
"""An edit returned no value where the file holds one."""
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class Reentrant(Exception):
|
|
20
|
+
"""A write or change ran inside an edit on the same file."""
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def read(path):
|
|
24
|
+
try:
|
|
25
|
+
return _read(_native(path))
|
|
26
|
+
except FileNotFoundError:
|
|
27
|
+
return None
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def write(path, base, data):
|
|
31
|
+
with _locked(path):
|
|
32
|
+
_replace(path, base, data)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def change(path, edit):
|
|
36
|
+
with _locked(path):
|
|
37
|
+
cur = read(path)
|
|
38
|
+
nxt = edit(cur)
|
|
39
|
+
if nxt != cur:
|
|
40
|
+
_replace(path, cur, nxt)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def sweep(path):
|
|
44
|
+
"""Remove the temporaries a killed writer left beside path; return how many.
|
|
45
|
+
|
|
46
|
+
Not on the write path, where it would cost every write the size of the
|
|
47
|
+
directory. Holding the lock is what makes it safe: a live writer stages
|
|
48
|
+
under the lock, so every temporary beside the file is a dead one.
|
|
49
|
+
"""
|
|
50
|
+
with _locked(path):
|
|
51
|
+
native = _native(path)
|
|
52
|
+
gone = 0
|
|
53
|
+
for orphan in glob.glob(glob.escape(native) + ".*.tmp"):
|
|
54
|
+
with contextlib.suppress(OSError):
|
|
55
|
+
os.unlink(orphan)
|
|
56
|
+
gone += 1
|
|
57
|
+
return gone
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
_held = threading.local()
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@contextlib.contextmanager
|
|
64
|
+
def _locked(path):
|
|
65
|
+
native = _native(path)
|
|
66
|
+
holding = getattr(_held, "paths", None)
|
|
67
|
+
if holding is None:
|
|
68
|
+
holding = _held.paths = set()
|
|
69
|
+
if native in holding:
|
|
70
|
+
raise Reentrant(os.fspath(path))
|
|
71
|
+
os.makedirs(os.path.dirname(native), exist_ok=True)
|
|
72
|
+
fd = os.open(native + ".lock", os.O_CREAT | os.O_RDWR, 0o600)
|
|
73
|
+
holding.add(native)
|
|
74
|
+
try:
|
|
75
|
+
_flock(fd)
|
|
76
|
+
yield
|
|
77
|
+
finally:
|
|
78
|
+
holding.discard(native)
|
|
79
|
+
os.close(fd)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _replace(path, base, data):
|
|
83
|
+
if data is None:
|
|
84
|
+
raise NoValue(os.fspath(path))
|
|
85
|
+
if read(path) != base:
|
|
86
|
+
raise Moved(os.fspath(path))
|
|
87
|
+
native = _native(path)
|
|
88
|
+
directory = os.path.dirname(native)
|
|
89
|
+
fd, tmp = tempfile.mkstemp(prefix=os.path.basename(native) + ".", suffix=".tmp", dir=directory)
|
|
90
|
+
try:
|
|
91
|
+
with os.fdopen(fd, "wb") as f:
|
|
92
|
+
f.write(data)
|
|
93
|
+
f.flush()
|
|
94
|
+
os.fsync(f.fileno())
|
|
95
|
+
_rename_over(tmp, native)
|
|
96
|
+
except BaseException:
|
|
97
|
+
with contextlib.suppress(OSError):
|
|
98
|
+
os.unlink(tmp)
|
|
99
|
+
raise
|
|
100
|
+
_fsync_dir(directory)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _rename_over(tmp, native):
|
|
104
|
+
for _ in range(999):
|
|
105
|
+
try:
|
|
106
|
+
return _rename(tmp, native)
|
|
107
|
+
except _TRANSIENT:
|
|
108
|
+
pass
|
|
109
|
+
_rename(tmp, native)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
_MAX_PATH = 260
|
|
113
|
+
_DERIVED = len(".XXXXXXXX.tmp")
|
|
114
|
+
|
|
115
|
+
if sys.platform == "win32":
|
|
116
|
+
import ctypes
|
|
117
|
+
import msvcrt
|
|
118
|
+
from ctypes import wintypes
|
|
119
|
+
|
|
120
|
+
_k32 = ctypes.WinDLL("kernel32", use_last_error=True)
|
|
121
|
+
_k32.CreateFileW.argtypes = [wintypes.LPCWSTR, wintypes.DWORD, wintypes.DWORD, wintypes.LPVOID,
|
|
122
|
+
wintypes.DWORD, wintypes.DWORD, wintypes.HANDLE]
|
|
123
|
+
_k32.CreateFileW.restype = wintypes.HANDLE
|
|
124
|
+
_k32.CloseHandle.argtypes = [wintypes.HANDLE]
|
|
125
|
+
_k32.FlushFileBuffers.argtypes = [wintypes.HANDLE]
|
|
126
|
+
_k32.LockFileEx.argtypes = [wintypes.HANDLE, wintypes.DWORD, wintypes.DWORD, wintypes.DWORD,
|
|
127
|
+
wintypes.DWORD, wintypes.LPVOID]
|
|
128
|
+
_k32.SetFileInformationByHandle.argtypes = [wintypes.HANDLE, ctypes.c_int, wintypes.LPVOID, wintypes.DWORD]
|
|
129
|
+
|
|
130
|
+
_GENERIC_READ, _GENERIC_WRITE, _DELETE = 0x80000000, 0x40000000, 0x00010000
|
|
131
|
+
_SHARE_ALL, _OPEN_EXISTING, _BACKUP_SEMANTICS = 0x7, 3, 0x02000000
|
|
132
|
+
_INVALID_HANDLE = wintypes.HANDLE(-1).value
|
|
133
|
+
_EXCLUSIVE_LOCK = 2
|
|
134
|
+
_FILE_RENAME_INFO_EX = 22
|
|
135
|
+
_REPLACE_IF_EXISTS, _POSIX_SEMANTICS = 1, 2
|
|
136
|
+
_ERROR_INVALID_FUNCTION, _ERROR_ACCESS_DENIED = 1, 5
|
|
137
|
+
_ERROR_NOT_SUPPORTED, _ERROR_INVALID_PARAMETER = 50, 87
|
|
138
|
+
_NO_DIRECTORY_FLUSH = (_ERROR_INVALID_FUNCTION, _ERROR_ACCESS_DENIED, _ERROR_NOT_SUPPORTED)
|
|
139
|
+
_TRANSIENT = PermissionError
|
|
140
|
+
|
|
141
|
+
def _native(path):
|
|
142
|
+
p = os.path.abspath(os.fspath(path))
|
|
143
|
+
if len(p) + _DERIVED < _MAX_PATH or p.startswith("\\\\?\\"):
|
|
144
|
+
return p
|
|
145
|
+
return "\\\\?\\UNC" + p[1:] if p.startswith("\\\\") else "\\\\?\\" + p
|
|
146
|
+
|
|
147
|
+
def _open_shared(path, access, flags=0):
|
|
148
|
+
h = _k32.CreateFileW(path, access, _SHARE_ALL, None, _OPEN_EXISTING, flags, None)
|
|
149
|
+
if h == _INVALID_HANDLE:
|
|
150
|
+
raise ctypes.WinError(ctypes.get_last_error())
|
|
151
|
+
return h
|
|
152
|
+
|
|
153
|
+
def _read(path):
|
|
154
|
+
fd = msvcrt.open_osfhandle(_open_shared(path, _GENERIC_READ), os.O_RDONLY)
|
|
155
|
+
with os.fdopen(fd, "rb") as f:
|
|
156
|
+
return f.read()
|
|
157
|
+
|
|
158
|
+
def _flock(fd):
|
|
159
|
+
overlapped = ctypes.create_string_buffer(32)
|
|
160
|
+
if not _k32.LockFileEx(msvcrt.get_osfhandle(fd), _EXCLUSIVE_LOCK, 0, 1, 0, overlapped):
|
|
161
|
+
raise ctypes.WinError(ctypes.get_last_error())
|
|
162
|
+
|
|
163
|
+
def _fsync_dir(directory):
|
|
164
|
+
try:
|
|
165
|
+
h = _open_shared(directory, _GENERIC_WRITE, _BACKUP_SEMANTICS)
|
|
166
|
+
except OSError as e:
|
|
167
|
+
if e.winerror in _NO_DIRECTORY_FLUSH:
|
|
168
|
+
return
|
|
169
|
+
raise
|
|
170
|
+
try:
|
|
171
|
+
if not _k32.FlushFileBuffers(h):
|
|
172
|
+
err = ctypes.get_last_error()
|
|
173
|
+
if err not in _NO_DIRECTORY_FLUSH:
|
|
174
|
+
raise ctypes.WinError(err)
|
|
175
|
+
finally:
|
|
176
|
+
_k32.CloseHandle(h)
|
|
177
|
+
|
|
178
|
+
def _rename_info(target):
|
|
179
|
+
units = len(target.encode("utf-16-le")) // 2
|
|
180
|
+
|
|
181
|
+
class Info(ctypes.Structure):
|
|
182
|
+
_fields_ = [("Flags", wintypes.DWORD), ("RootDirectory", wintypes.HANDLE),
|
|
183
|
+
("FileNameLength", wintypes.DWORD), ("FileName", wintypes.WCHAR * (units + 1))]
|
|
184
|
+
return Info(_REPLACE_IF_EXISTS | _POSIX_SEMANTICS, None, units * 2, target)
|
|
185
|
+
|
|
186
|
+
def _rename(tmp, native):
|
|
187
|
+
h = _open_shared(tmp, _DELETE)
|
|
188
|
+
try:
|
|
189
|
+
info = _rename_info(native)
|
|
190
|
+
err = 0 if _k32.SetFileInformationByHandle(h, _FILE_RENAME_INFO_EX, ctypes.byref(info),
|
|
191
|
+
ctypes.sizeof(info)) else ctypes.get_last_error()
|
|
192
|
+
finally:
|
|
193
|
+
_k32.CloseHandle(h)
|
|
194
|
+
if err in (_ERROR_NOT_SUPPORTED, _ERROR_INVALID_PARAMETER):
|
|
195
|
+
os.replace(tmp, native)
|
|
196
|
+
elif err:
|
|
197
|
+
raise ctypes.WinError(err)
|
|
198
|
+
|
|
199
|
+
else:
|
|
200
|
+
import errno
|
|
201
|
+
import fcntl
|
|
202
|
+
|
|
203
|
+
_TRANSIENT = ()
|
|
204
|
+
_NO_DIRECTORY_FLUSH = (errno.EINVAL, errno.ENOTSUP)
|
|
205
|
+
|
|
206
|
+
def _native(path):
|
|
207
|
+
return os.path.abspath(os.fspath(path))
|
|
208
|
+
|
|
209
|
+
def _read(path):
|
|
210
|
+
with open(path, "rb") as f:
|
|
211
|
+
return f.read()
|
|
212
|
+
|
|
213
|
+
def _flock(fd):
|
|
214
|
+
fcntl.flock(fd, fcntl.LOCK_EX)
|
|
215
|
+
|
|
216
|
+
def _fsync_dir(directory):
|
|
217
|
+
fd = os.open(directory, os.O_RDONLY)
|
|
218
|
+
try:
|
|
219
|
+
os.fsync(fd)
|
|
220
|
+
except OSError as e:
|
|
221
|
+
if e.errno not in _NO_DIRECTORY_FLUSH:
|
|
222
|
+
raise
|
|
223
|
+
finally:
|
|
224
|
+
os.close(fd)
|
|
225
|
+
|
|
226
|
+
def _rename(tmp, native):
|
|
227
|
+
os.replace(tmp, native)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "abstraction-cas"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Compare-and-set over a file. The floor under every service that keeps state in a file."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "Apache-2.0"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
requires-python = ">=3.9"
|
|
13
|
+
dependencies = []
|
|
14
|
+
|
|
15
|
+
[project.urls]
|
|
16
|
+
Source = "https://github.com/openabstractions/abstraction-cas"
|
|
17
|
+
|
|
18
|
+
[tool.setuptools]
|
|
19
|
+
py-modules = ["abstraction_cas"]
|