Skip to content

Commit dd454b2

Browse files
authored
Merge pull request DeusData#2111 from DeusData/fix/1839-1864-stage-orphans
fix(pipeline): sweep orphaned/nested .db.stage files and stop the false invalid_existing_db route (DeusData#1839, DeusData#1864)
2 parents b790be3 + 951b9ab commit dd454b2

5 files changed

Lines changed: 1089 additions & 34 deletions

File tree

‎src/foundation/compat_fs.c‎

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
#include <windows.h>
2424

2525
#include <aclapi.h>
26-
#include <direct.h> /* _wmkdir */
27-
#include <errno.h> /* errno for spawn-failure logging */
28-
#include <fcntl.h> /* _O_RDONLY */
29-
#include <io.h> /* _wunlink, _open_osfhandle, _close */
30-
#include <stdint.h> /* intptr_t */
26+
#include <direct.h> /* _wmkdir */
27+
#include <errno.h> /* errno for spawn-failure logging */
28+
#include <fcntl.h> /* _O_RDONLY */
29+
#include <io.h> /* _wunlink, _open_osfhandle, _close */
30+
#include <share.h> /* _SH_DENYRW */
31+
#include <sys/stat.h> /* _S_IREAD */
32+
#include <stdint.h> /* intptr_t */
3133
#include "foundation/log.h"
3234
#include "foundation/win_utf8.h"
3335

@@ -588,6 +590,26 @@ int cbm_rmdir(const char *path) {
588590
return ret;
589591
}
590592

593+
int cbm_lockfile_open(const char *path, bool create) {
594+
wchar_t *wpath = cbm_path_to_wide(path);
595+
if (!wpath) {
596+
errno = EINVAL;
597+
return -1;
598+
}
599+
int flags = _O_RDWR | _O_BINARY | _O_NOINHERIT | (create ? _O_CREAT : 0);
600+
/* _SH_DENYRW: every other open of this file, from any process, fails
601+
* with EACCES until this descriptor closes -- including at death. */
602+
int fd = _wsopen(wpath, flags, _SH_DENYRW, _S_IREAD | _S_IWRITE);
603+
free(wpath);
604+
return fd;
605+
}
606+
607+
void cbm_lockfile_close(int fd) {
608+
if (fd >= 0) {
609+
(void)_close(fd);
610+
}
611+
}
612+
591613
/* Build a properly-quoted Windows command line from an argv array.
592614
* Returns a heap-allocated wide string, or NULL on allocation failure.
593615
* Quoting follows the MSVC CRT convention: arguments containing spaces,
@@ -737,6 +759,7 @@ int cbm_exec_no_shell(const char *const *argv) {
737759
#include <dirent.h>
738760
#include <errno.h>
739761
#include <fcntl.h>
762+
#include <sys/file.h>
740763
#include <sys/stat.h>
741764
#include <sys/wait.h>
742765
#include <unistd.h>
@@ -1043,6 +1066,34 @@ int cbm_rmdir(const char *path) {
10431066
return rmdir(path);
10441067
}
10451068

1069+
int cbm_lockfile_open(const char *path, bool create) {
1070+
int flags = O_RDWR | O_CLOEXEC | O_NOFOLLOW | (create ? O_CREAT : 0);
1071+
int fd;
1072+
do {
1073+
fd = open(path, flags, S_IRUSR | S_IWUSR);
1074+
} while (fd < 0 && errno == EINTR);
1075+
if (fd < 0) {
1076+
return -1;
1077+
}
1078+
int rc;
1079+
do {
1080+
rc = flock(fd, LOCK_EX | LOCK_NB);
1081+
} while (rc != 0 && errno == EINTR);
1082+
if (rc != 0) {
1083+
int saved = errno;
1084+
(void)close(fd);
1085+
errno = saved;
1086+
return -1;
1087+
}
1088+
return fd;
1089+
}
1090+
1091+
void cbm_lockfile_close(int fd) {
1092+
if (fd >= 0) {
1093+
(void)close(fd);
1094+
}
1095+
}
1096+
10461097
int cbm_exec_no_shell(const char *const *argv) {
10471098
if (!argv || !argv[0]) {
10481099
return CBM_NOT_FOUND;

‎src/foundation/compat_fs.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ int cbm_canonical_path(const char *path, char *out, size_t out_sz);
113113
/* Delete an empty directory. Returns 0 on success. */
114114
int cbm_rmdir(const char *path);
115115

116+
/* Exclusive lock file. Opens `path` and takes an exclusive lock the KERNEL
117+
* releases on any process death -- POSIX open(O_CLOEXEC|O_NOFOLLOW) +
118+
* flock(LOCK_EX|LOCK_NB); Windows _wsopen with _SH_DENYRW (deny every other
119+
* open) and _O_NOINHERIT -- so ownership never outlives its holder and is
120+
* never inherited by a spawned child. `create` false never creates the file.
121+
* Returns the descriptor, or -1 with errno: EWOULDBLOCK/EAGAIN (POSIX) or
122+
* EACCES (Windows sharing violation) when another holder is live, ENOENT
123+
* when absent, otherwise the open error. Never use this on a SQLite file:
124+
* on macOS an flock conflicts with SQLite's own fcntl byte locks. */
125+
int cbm_lockfile_open(const char *path, bool create);
126+
void cbm_lockfile_close(int fd);
127+
116128
/* Open a file by UTF-8 path.
117129
* On Windows, converts to wide-char and calls _wfopen so paths with
118130
* non-ASCII characters (accents, CJK, etc.) are handled correctly.

0 commit comments

Comments
 (0)