Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in_tail: avoid locking files on Windows #1159

Merged
merged 1 commit into from
Mar 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions plugins/in_tail/tail_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@ static inline void consume_bytes(char *buf, int bytes, int length)
memmove(buf, buf + bytes, length - bytes);
}

#ifdef _MSC_VER
/*
* Open a file for reading. We need to use CreateFileA() instead of
* open(2) to avoid automatic file locking.
*/
static int open_file(const char *path)
{
HANDLE h;
h = CreateFileA(path,
GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
NULL, /* lpSecurityAttributes */
OPEN_EXISTING, /* dwCreationDisposition */
0, /* dwFlagsAndAttributes */
NULL); /* hTemplateFile */
if (h == INVALID_HANDLE_VALUE) {
return -1;
}
return _open_osfhandle((intptr_t) h, _O_RDONLY);
}
#endif

static int unpack_and_pack(msgpack_packer *pck, msgpack_object *root,
char *key, size_t key_len,
char *val, size_t val_len)
Expand Down Expand Up @@ -553,11 +575,7 @@ int flb_tail_file_append(char *path, struct stat *st, int mode,
}

#ifdef _MSC_VER
/*
* We need O_BINARY here in order to avoid count errors due to
* Windows treating '\r\n' as 1 byte in text mode.
*/
fd = _open(path, _O_RDONLY | _O_BINARY);
fd = open_file(path);
#else
fd = open(path, O_RDONLY);
#endif
Expand Down