Repository navigation
Expand file tree
/
Copy pathOSFileLock.class.st
More file actions
172 lines (132 loc) · 3.74 KB
/
Copy pathOSFileLock.class.st
File metadata and controls
172 lines (132 loc) · 3.74 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
"
I describe the region representing the entire addressable space of an external file, including regions that have not yet been allocated for use by the file. On platforms that support file locking, an OSFileLock is used to describe a lock on the entire file.
"
Class {
#name : #OSFileLock,
#superclass : #Object,
#instVars : [
'fileStream',
'exclusive'
],
#category : #'OSProcess-Base'
}
{ #category : #'instance creation' }
OSFileLock class >> onFile: aFileStream exclusive: writeLockFlag [
"A region representing the whole file"
^ super new onFile: aFileStream exclusive: writeLockFlag
]
{ #category : #comparing }
OSFileLock >> = aFileLock [
^ ((self species = aFileLock species)
and: [self fileStream == aFileLock fileStream])
and: [self exclusive == aFileLock exclusive]
]
{ #category : #'conflict testing' }
OSFileLock >> conflictsWith: otherFileLock [
^ (self isExclusive or: [otherFileLock isExclusive])
and: [self overlaps: otherFileLock]
]
{ #category : #accessing }
OSFileLock >> exclusive [
"Answer the value of exclusive. Default is true, indicating a read-write lock
as opposed to a shared read lock."
^ exclusive ifNil: [exclusive := true].
]
{ #category : #accessing }
OSFileLock >> exclusive: anObject [
"Set the value of exclusive"
exclusive := anObject
]
{ #category : #accessing }
OSFileLock >> fileStream [
"Answer the value of fileStream"
^ fileStream
]
{ #category : #accessing }
OSFileLock >> fileStream: anObject [
"Set the value of fileStream"
fileStream := anObject
]
{ #category : #comparing }
OSFileLock >> hash [
^ (self fileStream hash + self exclusive hash) hashMultiply
]
{ #category : #testing }
OSFileLock >> isActive [
^ self fileStream closed not
]
{ #category : #testing }
OSFileLock >> isExclusive [
^ self exclusive
]
{ #category : #private }
OSFileLock >> length [
^ 0
]
{ #category : #'system locking' }
OSFileLock >> lock [
"Answer true on success"
^ self lockIfFail: [false]
]
{ #category : #'system locking' }
OSFileLock >> lockIfFail: failBlock [
| handle result |
handle := ThisOSProcess accessor handleFromFileStream: self fileStream.
result := ThisOSProcess accessor
primLockFileRegion: handle
offset: self offset
length: self length
exclusive: self isExclusive.
result == 0
ifTrue: [^ true]
ifFalse: [^ failBlock value]
]
{ #category : #private }
OSFileLock >> offset [
^ 0
]
{ #category : #'initialize-release' }
OSFileLock >> onFile: aFileStream exclusive: writeLockFlag [
self fileStream: aFileStream.
self exclusive: writeLockFlag
]
{ #category : #comparing }
OSFileLock >> overlaps: aFileLock [
"Answer true if the receiver represents an addressable region that overlaps aFileLock"
^ aFileLock fileStream fullName = self fileStream fullName
]
{ #category : #comparing }
OSFileLock >> overlapsRegion: aFileRegionLock [
^ aFileRegionLock fileStream fullName = self fileStream fullName
]
{ #category : #'system locking' }
OSFileLock >> test [
"Answer true if this is a lockable file or region"
| handle result |
handle := ThisOSProcess accessor handleFromFileStream: self fileStream.
result := ThisOSProcess accessor
primTestLockableFileRegion: handle
offset: self offset
length: self length
exclusive: self isExclusive.
^ (result == -1 or: [result isNil])
ifTrue: [false]
ifFalse: [result first]
]
{ #category : #'system locking' }
OSFileLock >> unlock [
"Answer true on success"
^ self unlockIfFail: [false]
]
{ #category : #'system locking' }
OSFileLock >> unlockIfFail: failBlock [
| handle result |
handle := ThisOSProcess accessor handleFromFileStream: self fileStream.
result := ThisOSProcess accessor
primUnlockFileRegion: handle
offset: self offset
length: self length.
result == 0
ifTrue: [^ true]
ifFalse: [^ failBlock value]
]