Skip to content

Commit 7dd4367

Browse files
Fix locking of SEQUENTIAL, LINE SEQUENTIAL and RELATIVE (#714)
* fix: file locks of SEQUENTIAL, LINE SEQUENTIAL and RELATIVE * test: add basic tests * chore: rename `indexed-lock` to `file-lock`
1 parent b13ddd2 commit 7dd4367

26 files changed

+620
-305
lines changed

.github/workflows/coverage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141
- name: Run other tests
4242
working-directory: tests
4343
run: |
44-
tests=("command-line-options" "data-rep" "i18n_sjis" "jp-compat" "run" "syntax" "cobj-idx" "indexed-lock" "misc")
44+
tests=("command-line-options" "data-rep" "i18n_sjis" "jp-compat" "run" "syntax" "cobj-idx" "file-lock" "misc")
4545
for test in "${tests[@]}"; do
4646
./"$test" || true
4747
done

.github/workflows/pull-request.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ jobs:
5454
- "syntax"
5555
- "cobj-idx"
5656
- "misc"
57-
- "indexed-lock"
57+
- "file-lock"
5858
os: ["ubuntu:24.04", "almalinux:9", "amazonlinux:2023"]
5959
uses: ./.github/workflows/test-other.yml
6060
with:
@@ -75,7 +75,7 @@ jobs:
7575
- "run"
7676
- "syntax"
7777
- "cobj-idx"
78-
- "indexed-lock"
78+
- "file-lock"
7979
#- "misc"
8080
os: ["ubuntu:24.04", "almalinux:9", "amazonlinux:2023"]
8181
uses: ./.github/workflows/test-other.yml

.github/workflows/push.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ jobs:
5858
- "run"
5959
- "syntax"
6060
- "cobj-idx"
61-
- "indexed-lock"
61+
- "file-lock"
6262
- "misc"
6363
os: ["ubuntu:24.04"]
6464
uses: ./.github/workflows/test-other.yml
@@ -80,7 +80,7 @@ jobs:
8080
- "run"
8181
- "syntax"
8282
- "cobj-idx"
83-
- "indexed-lock"
83+
- "file-lock"
8484
#- "misc"
8585
os: ["ubuntu:24.04"]
8686
uses: ./.github/workflows/test-other.yml

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ tests/jp-compat
5454
tests/cobj-idx
5555
tests/misc
5656
tests/run
57-
tests/indexed-lock
57+
tests/file-lock
5858
tests/*.log
5959
tests/syntax
6060
tests/cobol85/*/*.class

libcobj/app/src/main/java/jp/osscons/opensourcecobol/libcobj/file/CobolFile.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,13 +1210,13 @@ public int open_(String filename, int mode, int sharing) throws IOException {
12101210
FileLock fl = null;
12111211
if (!filename.startsWith("/dev/")) {
12121212
try {
1213-
boolean lockFlag;
1213+
boolean isSharedLock;
12141214
if (sharing != 0 || mode == COB_OPEN_OUTPUT) {
1215-
lockFlag = false;
1215+
isSharedLock = false;
12161216
} else {
1217-
lockFlag = true;
1217+
isSharedLock = true;
12181218
}
1219-
fl = fp.tryLock(0L, Long.MAX_VALUE, lockFlag);
1219+
fl = fp.tryLock(0L, Long.MAX_VALUE, isSharedLock);
12201220
} catch (NonWritableChannelException e) {
12211221
fp.close();
12221222
return EBADF;

libcobj/app/src/main/java/jp/osscons/opensourcecobol/libcobj/file/CobolRelativeFile.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.nio.ByteBuffer;
2323
import java.nio.channels.*;
2424
import java.nio.file.Files;
25-
import java.nio.file.Path;
2625
import java.nio.file.Paths;
2726
import jp.osscons.opensourcecobol.libcobj.data.AbstractCobolField;
2827

@@ -140,12 +139,7 @@ public int open_(String filename, int mode, int sharing) throws IOException {
140139
this.fp.seek(0);
141140
break;
142141
case COB_OPEN_OUTPUT:
143-
Path path = Paths.get(filename);
144-
if (Files.exists(path)) {
145-
Files.delete(path);
146-
}
147142
this.fp = new RandomAccessFile(this.assign.fieldToString(), "rw");
148-
this.fp.seek(0);
149143
break;
150144
case COB_OPEN_I_O:
151145
this.fp = new RandomAccessFile(this.assign.fieldToString(), "rw");
@@ -175,13 +169,13 @@ public int open_(String filename, int mode, int sharing) throws IOException {
175169
FileLock fl = null;
176170
if (!filename.startsWith("/dev/")) {
177171
try {
178-
boolean lockFlag;
172+
boolean isSharedLock;
179173
if (sharing != 0 || mode == COB_OPEN_OUTPUT) {
180-
lockFlag = false;
174+
isSharedLock = false;
181175
} else {
182-
lockFlag = true;
176+
isSharedLock = true;
183177
}
184-
fl = ch.tryLock(0L, Long.MAX_VALUE, lockFlag);
178+
fl = ch.tryLock(0L, Long.MAX_VALUE, isSharedLock);
185179
} catch (NonWritableChannelException e) {
186180
this.fp.close();
187181
return EBADF;
@@ -198,6 +192,11 @@ public int open_(String filename, int mode, int sharing) throws IOException {
198192
}
199193
}
200194

195+
if(mode == COB_OPEN_OUTPUT) {
196+
this.fp.setLength(0);
197+
this.fp.seek(0);
198+
}
199+
201200
this.file.setRandomAccessFile(this.fp, fl);
202201
if ((this.flag_select_features & COB_SELECT_LINAGE) != 0) {
203202
if (this.file_linage_check()) {

libcobj/app/src/main/java/jp/osscons/opensourcecobol/libcobj/file/FileIO.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ void setRandomAccessFile(RandomAccessFile ra, FileLock fl) {
9090
this.useStdOut = false;
9191
this.useStdIn = false;
9292
this.fc = ra.getChannel();
93+
this.fl = fl;
9394
}
9495

9596
/**

tests/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ run~
66
syntax~
77
command-line-options~
88
cobj-idx~
9-
indexed-lock~
9+
file-lock~
1010
*.dir

tests/Makefile.am

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ TESTS = syntax \
3030
jp-compat \
3131
command-line-options \
3232
cobj-idx \
33-
indexed-lock \
33+
file-lock \
3434
misc
3535
else
3636
TESTS = syntax \
@@ -42,7 +42,7 @@ TESTS = syntax \
4242
jp-compat \
4343
command-line-options \
4444
cobj-idx \
45-
indexed-lock \
45+
file-lock \
4646
misc
4747
endif
4848

@@ -209,18 +209,18 @@ cobj_idx_DEPENDENCIES = \
209209
cobj-idx.src/misc.at
210210

211211
indexed_lock_DEPENDENCIES = \
212-
indexed-lock.at \
213-
indexed-lock.src/file-lock.at \
214-
indexed-lock.src/access-same-record.at \
215-
indexed-lock.src/access-different-record.at \
216-
indexed-lock.src/input-mode.at \
217-
indexed-lock.src/same-process.at \
218-
indexed-lock.src/open-start-write-rewrite.at \
219-
indexed-lock.src/release-lock.at \
220-
indexed-lock.src/open-input.at \
221-
indexed-lock.src/lock-mode-clause.at \
222-
indexed-lock.src/old-file.at \
223-
indexed-lock.src/lock-mode-automatic.at
212+
file-lock.at \
213+
file-lock.src/lock-file.at \
214+
file-lock.src/access-same-record.at \
215+
file-lock.src/access-different-record.at \
216+
file-lock.src/input-mode.at \
217+
file-lock.src/same-process.at \
218+
file-lock.src/open-start-write-rewrite.at \
219+
file-lock.src/release-lock.at \
220+
file-lock.src/open-input.at \
221+
file-lock.src/lock-mode-clause.at \
222+
file-lock.src/old-file.at \
223+
file-lock.src/lock-mode-automatic.at
224224

225225
misc_DEPENDENCIES = \
226226
misc.src/signed-comp3.at \
@@ -305,5 +305,5 @@ $(srcdir)/i18n_sjis: $(i18n_sjis_DEPENDENCIES)
305305
$(srcdir)/jp-compat: $(jp_compat_DEPENDENCIES)
306306
$(srcdir)/command-line-options: $(command_line_options_DEPENDENCIES)
307307
$(srcdir)/cobj-idx: $(cobj_idx_DEPENDENCIES)
308-
$(srcdir)/indexed-lock: $(indexed_lock_DEPENDENCIES)
308+
$(srcdir)/file-lock: $(indexed_lock_DEPENDENCIES)
309309
$(srcdir)/misc: $(misc_DEPENDENCIES)

tests/Makefile.in

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ SUBDIRS = cobol85
570570
@I18N_UTF8_FALSE@ jp-compat \
571571
@I18N_UTF8_FALSE@ command-line-options \
572572
@I18N_UTF8_FALSE@ cobj-idx \
573-
@I18N_UTF8_FALSE@ indexed-lock \
573+
@I18N_UTF8_FALSE@ file-lock \
574574
@I18N_UTF8_FALSE@ misc
575575

576576
@I18N_UTF8_TRUE@TESTS = syntax \
@@ -582,7 +582,7 @@ SUBDIRS = cobol85
582582
@I18N_UTF8_TRUE@ jp-compat \
583583
@I18N_UTF8_TRUE@ command-line-options \
584584
@I18N_UTF8_TRUE@ cobj-idx \
585-
@I18N_UTF8_TRUE@ indexed-lock \
585+
@I18N_UTF8_TRUE@ file-lock \
586586
@I18N_UTF8_TRUE@ misc
587587

588588
syntax_DEPENDENCIES = \
@@ -748,18 +748,18 @@ cobj_idx_DEPENDENCIES = \
748748
cobj-idx.src/misc.at
749749

750750
indexed_lock_DEPENDENCIES = \
751-
indexed-lock.at \
752-
indexed-lock.src/file-lock.at \
753-
indexed-lock.src/access-same-record.at \
754-
indexed-lock.src/access-different-record.at \
755-
indexed-lock.src/input-mode.at \
756-
indexed-lock.src/same-process.at \
757-
indexed-lock.src/open-start-write-rewrite.at \
758-
indexed-lock.src/release-lock.at \
759-
indexed-lock.src/open-input.at \
760-
indexed-lock.src/lock-mode-clause.at \
761-
indexed-lock.src/old-file.at \
762-
indexed-lock.src/lock-mode-automatic.at
751+
file-lock.at \
752+
file-lock.src/lock-file.at \
753+
file-lock.src/access-same-record.at \
754+
file-lock.src/access-different-record.at \
755+
file-lock.src/input-mode.at \
756+
file-lock.src/same-process.at \
757+
file-lock.src/open-start-write-rewrite.at \
758+
file-lock.src/release-lock.at \
759+
file-lock.src/open-input.at \
760+
file-lock.src/lock-mode-clause.at \
761+
file-lock.src/old-file.at \
762+
file-lock.src/lock-mode-automatic.at
763763

764764
misc_DEPENDENCIES = \
765765
misc.src/signed-comp3.at \
@@ -1159,9 +1159,9 @@ cobj-idx.log: cobj-idx
11591159
--log-file $$b.log --trs-file $$b.trs \
11601160
$(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
11611161
"$$tst" $(AM_TESTS_FD_REDIRECT)
1162-
indexed-lock.log: indexed-lock
1163-
@p='indexed-lock'; \
1164-
b='indexed-lock'; \
1162+
file-lock.log: file-lock
1163+
@p='file-lock'; \
1164+
b='file-lock'; \
11651165
$(am__check_pre) $(LOG_DRIVER) --test-name "$$f" \
11661166
--log-file $$b.log --trs-file $$b.trs \
11671167
$(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
@@ -1407,7 +1407,7 @@ $(srcdir)/i18n_sjis: $(i18n_sjis_DEPENDENCIES)
14071407
$(srcdir)/jp-compat: $(jp_compat_DEPENDENCIES)
14081408
$(srcdir)/command-line-options: $(command_line_options_DEPENDENCIES)
14091409
$(srcdir)/cobj-idx: $(cobj_idx_DEPENDENCIES)
1410-
$(srcdir)/indexed-lock: $(indexed_lock_DEPENDENCIES)
1410+
$(srcdir)/file-lock: $(indexed_lock_DEPENDENCIES)
14111411
$(srcdir)/misc: $(misc_DEPENDENCIES)
14121412

14131413
# Tell versions [3.59,3.63) of GNU make to not export all variables.

0 commit comments

Comments
 (0)