diff --git a/libcobj/app/src/main/java/jp/osscons/opensourcecobol/libcobj/file/CobolFile.java b/libcobj/app/src/main/java/jp/osscons/opensourcecobol/libcobj/file/CobolFile.java index 9877b5b4..1236d37c 100755 --- a/libcobj/app/src/main/java/jp/osscons/opensourcecobol/libcobj/file/CobolFile.java +++ b/libcobj/app/src/main/java/jp/osscons/opensourcecobol/libcobj/file/CobolFile.java @@ -285,44 +285,50 @@ public class CobolFile { /** TODO: 準備中 */ protected static final int COB_STATUS_91_NOT_AVAILABLE = 91; + // ============================================== + // The following constants must not be equal + // to any of the above constants `COB_STATUS_*` + /** TODO: 準備中 */ - protected static final int COB_LINAGE_INVALID = 16384; + protected static final int ENOENT = 1002; /** TODO: 準備中 */ - protected static final int COB_NOT_CONFIGURED = 32768; + protected static final int EBADF = 1009; /** TODO: 準備中 */ - public static final int COB_SELECT_FILE_STATUS = 0x01; + protected static final int EACCESS = 1013; /** TODO: 準備中 */ - public static final int COB_SELECT_EXTERNAL = 0x02; + protected static final int EISDIR = 1021; /** TODO: 準備中 */ - public static final int COB_SELECT_LINAGE = 0x04; + protected static final int EROFS = 1030; /** TODO: 準備中 */ - public static final int COB_SELECT_SPLITKEY = 0x08; + protected static final int EAGAIN = 1011; + + // ============================================== /** TODO: 準備中 */ - protected static final int FNSTATUSSIZE = 3; + protected static final int COB_LINAGE_INVALID = 16384; /** TODO: 準備中 */ - protected static final int ENOENT = 2; + protected static final int COB_NOT_CONFIGURED = 32768; /** TODO: 準備中 */ - protected static final int EBADF = 9; + public static final int COB_SELECT_FILE_STATUS = 0x01; /** TODO: 準備中 */ - protected static final int EACCESS = 13; + public static final int COB_SELECT_EXTERNAL = 0x02; /** TODO: 準備中 */ - protected static final int EISDIR = 21; + public static final int COB_SELECT_LINAGE = 0x04; /** TODO: 準備中 */ - protected static final int EROFS = 30; + public static final int COB_SELECT_SPLITKEY = 0x08; /** TODO: 準備中 */ - protected static final int EAGAIN = 11; + protected static final int FNSTATUSSIZE = 3; /** TODO: 準備中 */ public static CobolFile errorFile; diff --git a/libcobj/app/src/main/java/jp/osscons/opensourcecobol/libcobj/file/CobolIndexedFile.java b/libcobj/app/src/main/java/jp/osscons/opensourcecobol/libcobj/file/CobolIndexedFile.java index f23aec60..582f6d92 100644 --- a/libcobj/app/src/main/java/jp/osscons/opensourcecobol/libcobj/file/CobolIndexedFile.java +++ b/libcobj/app/src/main/java/jp/osscons/opensourcecobol/libcobj/file/CobolIndexedFile.java @@ -208,17 +208,27 @@ public int open_(String filename, int mode, int sharing) { boolean fileExists = new java.io.File(filename).exists(); + if (mode == COB_OPEN_INPUT && !fileExists) { + return ENOENT; + } + p.connection = null; try { p.connection = DriverManager.getConnection("jdbc:sqlite:" + filename, config.toProperties()); p.connection.setAutoCommit(false); + + // Check if the file is accessible + try (Statement st = p.connection.createStatement()) { + st.execute("select 1"); + } + p.connection.commit(); } catch (SQLException e) { int errorCode = e.getErrorCode(); if (errorCode == SQLiteErrorCode.SQLITE_BUSY.code) { return COB_STATUS_61_FILE_SHARING; } else { - return ENOENT; + return COB_STATUS_30_PERMANENT_ERROR; } } catch (Exception e) { return COB_STATUS_30_PERMANENT_ERROR; diff --git a/tests/cobj-idx.src/load.at b/tests/cobj-idx.src/load.at index e0754e98..5c7f4b01 100644 --- a/tests/cobj-idx.src/load.at +++ b/tests/cobj-idx.src/load.at @@ -13,7 +13,8 @@ AT_DATA([load.cbl], alternate record key is alt-key-1 alternate record key is alt-key-2 alternate record key is alt-key-dup-1 with duplicates - alternate record key is alt-key-dup-2 with duplicates. + alternate record key is alt-key-dup-2 with duplicates + file status is file-status. data division. file section. fd f. @@ -25,9 +26,13 @@ AT_DATA([load.cbl], 03 alt-key-dup-2 pic x(5). 03 rec-value pic x(5). working-storage section. + 01 file-status pic xx. procedure division. main-proc. open input f. + if file-status not = '00' + stop run + end-if. perform forever read f next at end diff --git a/tests/run.src/miscellaneous.at b/tests/run.src/miscellaneous.at index 0c18e283..4a91dc2d 100644 --- a/tests/run.src/miscellaneous.at +++ b/tests/run.src/miscellaneous.at @@ -2095,3 +2095,43 @@ AT_CHECK([java prog], [1], [], ]) AT_CLEANUP + +AT_SETUP([Open an invalid formatted indexed file]) + +AT_CHECK([echo invalid-data > invalid-formatted-file]) + +AT_DATA([prog.cbl], +[ + IDENTIFICATION DIVISION. + PROGRAM-ID. prog. + + ENVIRONMENT DIVISION. + INPUT-OUTPUT SECTION. + FILE-CONTROL. + SELECT F ASSIGN TO "invalid-formatted-file" + ORGANIZATION IS INDEXED + ACCESS MODE IS DYNAMIC + RECORD KEY IS REC-KEY + FILE STATUS IS FILE-STATUS. + + DATA DIVISION. + FILE SECTION. + FD f. + 01 REC. + 05 REC-KEY PIC X(5). + + WORKING-STORAGE SECTION. + 01 FILE-STATUS PIC XX. + PROCEDURE DIVISION. + MAIN-PROCEDURE. + + OPEN INPUT f. + DISPLAY FILE-STATUS. + CLOSE f. +]) + +AT_CHECK([${COMPILE} prog.cbl]) +AT_CHECK([java prog], [0], +[30 +]) +AT_CLEANUP