You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by nw...@apache.org on 2017/03/05 15:43:39 UTC
[1/4] lucy git commit: Use Windows API to delete and rename files
Repository: lucy
Updated Branches:
refs/heads/master 291a32693 -> 7a64a288c
Use Windows API to delete and rename files
Delete files on Windows by calling CreateFile with
FILE_FLAG_DELETE_ON_CLOSE and closing the handle immediately. Unlike
DeleteFile, this allows files opened with FILE_SHARE_DELETE to be
(eventually) deleted.
Part of LUCY-324.
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/3b1b63e8
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/3b1b63e8
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/3b1b63e8
Branch: refs/heads/master
Commit: 3b1b63e8d8f7fcdf30d4c2af7c9ee519c662569a
Parents: de646e6
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Sun Mar 5 14:21:29 2017 +0100
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Sun Mar 5 16:29:44 2017 +0100
----------------------------------------------------------------------
core/Lucy/Store/FSFileHandle.c | 4 +-
core/Lucy/Store/FSFolder.c | 125 ++++++++++++++++++++++++++++--------
2 files changed, 102 insertions(+), 27 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucy/blob/3b1b63e8/core/Lucy/Store/FSFileHandle.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Store/FSFileHandle.c b/core/Lucy/Store/FSFileHandle.c
index fff9175..12769ec 100644
--- a/core/Lucy/Store/FSFileHandle.c
+++ b/core/Lucy/Store/FSFileHandle.c
@@ -388,15 +388,17 @@ S_init(FSFileHandleIVARS *ivars, String *path, uint32_t flags) {
DWORD desired_access = flags & FH_READ_ONLY
? GENERIC_READ
: GENERIC_WRITE;
+ DWORD share_mode = FILE_SHARE_READ;
DWORD creation_disposition = flags & FH_CREATE
? flags & FH_EXCLUSIVE
? CREATE_NEW
: OPEN_ALWAYS
: OPEN_EXISTING;
+ share_mode |= FILE_SHARE_DELETE;
char *path_ptr = Str_To_Utf8(path);
HANDLE handle
- = CreateFileA(path_ptr, desired_access, FILE_SHARE_READ, NULL,
+ = CreateFileA(path_ptr, desired_access, share_mode, NULL,
creation_disposition, FILE_ATTRIBUTE_NORMAL, NULL);
FREEMEM(path_ptr);
http://git-wip-us.apache.org/repos/asf/lucy/blob/3b1b63e8/core/Lucy/Store/FSFolder.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Store/FSFolder.c b/core/Lucy/Store/FSFolder.c
index d4d7f41..a1caf80 100644
--- a/core/Lucy/Store/FSFolder.c
+++ b/core/Lucy/Store/FSFolder.c
@@ -78,9 +78,17 @@ S_is_absolute(String *path);
static String*
S_absolutify(String *path);
+// Rename a directory or file.
+static bool
+S_rename(const char *from_path, const char *to_path);
+
// Create a hard link.
static bool
-S_hard_link(char *from_path, char *to_path);
+S_hard_link(const char *from_path, const char *to_path);
+
+// Delete a directory or file.
+static bool
+S_delete(const char *path);
FSFolder*
FSFolder_new(String *path) {
@@ -179,13 +187,10 @@ FSFolder_Local_Is_Directory_IMP(FSFolder *self, String *name) {
bool
FSFolder_Rename_IMP(FSFolder *self, String* from, String *to) {
+ // TODO: Update Folder entries.
char *from_path = S_fullpath_ptr(self, from);
char *to_path = S_fullpath_ptr(self, to);
- bool retval = !rename(from_path, to_path);
- if (!retval) {
- ErrMsg_set_with_errno("rename from '%s' to '%s' failed",
- from_path, to_path);
- }
+ bool retval = S_rename(from_path, to_path);
FREEMEM(from_path);
FREEMEM(to_path);
return retval;
@@ -194,27 +199,25 @@ FSFolder_Rename_IMP(FSFolder *self, String* from, String *to) {
bool
FSFolder_Hard_Link_IMP(FSFolder *self, String *from,
String *to) {
- char *from_path_ptr = S_fullpath_ptr(self, from);
- char *to_path_ptr = S_fullpath_ptr(self, to);
- bool retval = S_hard_link(from_path_ptr, to_path_ptr);
- FREEMEM(from_path_ptr);
- FREEMEM(to_path_ptr);
+ // TODO: Update Folder entries.
+ char *from_path = S_fullpath_ptr(self, from);
+ char *to_path = S_fullpath_ptr(self, to);
+ bool retval = S_hard_link(from_path, to_path);
+ FREEMEM(from_path);
+ FREEMEM(to_path);
return retval;
}
bool
FSFolder_Local_Delete_IMP(FSFolder *self, String *name) {
+ // TODO: Delete should only delete files. We should add RmDir for
+ // directories.
FSFolderIVARS *const ivars = FSFolder_IVARS(self);
-
- char *path_ptr = S_fullpath_ptr(self, name);
-#ifdef CHY_REMOVE_ZAPS_DIRS
- bool result = !remove(path_ptr);
-#else
- bool result = !rmdir(path_ptr) || !remove(path_ptr);
-#endif
+ char *path = S_fullpath_ptr(self, name);
+ bool retval = S_delete(path);
DECREF(Hash_Delete(ivars->entries, name));
- FREEMEM(path_ptr);
- return result;
+ FREEMEM(path);
+ return retval;
}
void
@@ -393,18 +396,63 @@ S_absolutify(String *path) {
}
static bool
-S_hard_link(char *from8, char *to8) {
- if (CreateHardLink(to8, from8, NULL)) {
+S_rename(const char *from_path, const char *to_path) {
+ // TODO: We should consider using MoveFileEx with
+ // MOVEFILE_REPLACE_EXISTING to better match POSIX semantics. But unlike
+ // POSIX, this allows to replace a file with a directory, breaking the
+ // tests.
+ if (MoveFileA(from_path, to_path) != 0) {
+ return true;
+ }
+ else {
+ ErrMsg_set_with_win_error("rename from '%s' to '%s' failed",
+ from_path, to_path);
+ return false;
+ }
+}
+
+static bool
+S_hard_link(const char *from_path, const char *to_path) {
+ if (CreateHardLinkA(to_path, from_path, NULL) != 0) {
return true;
}
else {
ErrMsg_set_with_win_error("CreateHardLink for new file '%s' "
"from '%s' failed",
- to8, from8);
+ to_path, from_path);
return false;
}
}
+static bool
+S_delete(const char *path) {
+ if (RemoveDirectoryA(path) != 0) {
+ return true;
+ }
+ if (GetLastError() != ERROR_DIRECTORY) {
+ ErrMsg_set_with_win_error("removing '%s' failed", path);
+ return false;
+ }
+
+ // Open file with FILE_FLAG_DELETE_ON_CLOSE and close it immediately.
+ // In contrast to DeleteFile, this allows files opened with
+ // FILE_SHARE_DELETE to be eventually deleted.
+ DWORD share_mode = FILE_SHARE_READ
+ | FILE_SHARE_WRITE
+ | FILE_SHARE_DELETE;
+ DWORD flags = FILE_FLAG_DELETE_ON_CLOSE;
+ DWORD attrs = FILE_ATTRIBUTE_NORMAL;
+ HANDLE handle = CreateFileA(path, DELETE, share_mode, NULL, OPEN_EXISTING,
+ flags | attrs, NULL);
+ if (handle == INVALID_HANDLE_VALUE) {
+ ErrMsg_set_with_win_error("removing '%s' failed", path);
+ return false;
+ }
+
+ CloseHandle(handle);
+ return true;
+}
+
#elif (defined(CHY_HAS_UNISTD_H))
static bool
@@ -432,10 +480,22 @@ S_absolutify(String *path) {
}
static bool
-S_hard_link(char *from8, char *to8) {
- if (-1 == link(from8, to8)) {
+S_rename(const char *from_path, const char *to_path) {
+ if (rename(from_path, to_path) != 0) {
+ ErrMsg_set_with_errno("rename from '%s' to '%s' failed",
+ from_path, to_path);
+ return false;
+ }
+ else {
+ return true;
+ }
+}
+
+static bool
+S_hard_link(const char *from_path, const char *to_path) {
+ if (link(from_path, to_path) != 0) {
ErrMsg_set_with_errno("hard link for new file '%s' from '%s' failed",
- to8, from8);
+ to_path, from_path);
return false;
}
else {
@@ -443,6 +503,19 @@ S_hard_link(char *from8, char *to8) {
}
}
+static bool
+S_delete(const char *path) {
+#ifdef CHY_REMOVE_ZAPS_DIRS
+ bool result = !remove(path);
+#else
+ bool result = !rmdir(path) || !remove(path);
+#endif
+ if (!result) {
+ ErrMsg_set_with_errno("removing '%s' failed", path);
+ }
+ return result;
+}
+
#else
#error "Need either windows.h or unistd.h"
#endif /* CHY_HAS_UNISTD_H vs. CHY_HAS_WINDOWS_H */
[4/4] lucy git commit: Create files with
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
Posted by nw...@apache.org.
Create files with FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
Might avoid problems with the Windows Search Service, although my
earlier testing suggests that the only reliable solution is to
exclude the folder via Indexing Options in Control Panel.
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/7a64a288
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/7a64a288
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/7a64a288
Branch: refs/heads/master
Commit: 7a64a288c36f7d3e3b30fd0f0605d7570e0c5a5a
Parents: 54d5093
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Sun Mar 5 16:31:36 2017 +0100
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Sun Mar 5 16:36:49 2017 +0100
----------------------------------------------------------------------
core/Lucy/Store/FSFileHandle.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucy/blob/7a64a288/core/Lucy/Store/FSFileHandle.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Store/FSFileHandle.c b/core/Lucy/Store/FSFileHandle.c
index 12769ec..f1536d1 100644
--- a/core/Lucy/Store/FSFileHandle.c
+++ b/core/Lucy/Store/FSFileHandle.c
@@ -399,7 +399,8 @@ S_init(FSFileHandleIVARS *ivars, String *path, uint32_t flags) {
char *path_ptr = Str_To_Utf8(path);
HANDLE handle
= CreateFileA(path_ptr, desired_access, share_mode, NULL,
- creation_disposition, FILE_ATTRIBUTE_NORMAL, NULL);
+ creation_disposition, FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,
+ NULL);
FREEMEM(path_ptr);
if (handle == INVALID_HANDLE_VALUE) {
[2/4] lucy git commit: Fix t/051-fsfile.t cleanup on Windows
Posted by nw...@apache.org.
Fix t/051-fsfile.t cleanup on Windows
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/de646e69
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/de646e69
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/de646e69
Branch: refs/heads/master
Commit: de646e69ebc74cde8536b2b4b1a85ee4d5a6c54c
Parents: 291a326
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Sun Mar 5 01:09:14 2017 +0100
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Sun Mar 5 16:29:44 2017 +0100
----------------------------------------------------------------------
perl/t/051-fsfile.t | 1 +
1 file changed, 1 insertion(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucy/blob/de646e69/perl/t/051-fsfile.t
----------------------------------------------------------------------
diff --git a/perl/t/051-fsfile.t b/perl/t/051-fsfile.t
index 6a7e34f..99c6dd5 100644
--- a/perl/t/051-fsfile.t
+++ b/perl/t/051-fsfile.t
@@ -97,6 +97,7 @@ undef $foo;
$dupe->read( $foo, 4 );
is( $foo, 'afoo', "reopened instream" );
+undef $instream;
# Trigger destruction.
undef $folder;
[3/4] lucy git commit: Make FSFolder always use Windows API
Posted by nw...@apache.org.
Make FSFolder always use Windows API
Fixes LUCY-324.
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/54d5093f
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/54d5093f
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/54d5093f
Branch: refs/heads/master
Commit: 54d5093f51b938e55fd734fbb1d3162f390452be
Parents: 3b1b63e
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Sun Mar 5 16:28:02 2017 +0100
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Sun Mar 5 16:29:50 2017 +0100
----------------------------------------------------------------------
core/Lucy/Store/FSFolder.c | 116 +++++++++++++++++++++++-----------------
1 file changed, 68 insertions(+), 48 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucy/blob/54d5093f/core/Lucy/Store/FSFolder.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Store/FSFolder.c b/core/Lucy/Store/FSFolder.c
index a1caf80..c2d7d36 100644
--- a/core/Lucy/Store/FSFolder.c
+++ b/core/Lucy/Store/FSFolder.c
@@ -19,26 +19,6 @@
#include "charmony.h"
-#include <ctype.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <sys/stat.h>
-
-#ifdef CHY_HAS_SYS_TYPES_H
- #include <sys/types.h>
-#endif
-
-// For rmdir, (hard) link.
-#ifdef CHY_HAS_UNISTD_H
- #include <unistd.h>
-#endif
-
-// For mkdir, rmdir.
-#ifdef CHY_HAS_DIRECT_H
- #include <direct.h>
-#endif
-
#include "Clownfish/CharBuf.h"
#include "Lucy/Store/ErrorMessage.h"
#include "Lucy/Store/FSFolder.h"
@@ -66,6 +46,10 @@ S_dir_ok(String *path);
static bool
S_create_dir(String *path);
+// Return true if the supplied path exists.
+static bool
+S_exists(const char *path);
+
// Return true unless the supplied path contains a slash.
static bool
S_is_local_entry(String *path);
@@ -157,12 +141,8 @@ FSFolder_Local_Exists_IMP(FSFolder *self, String *name) {
return false;
}
else {
- struct stat stat_buf;
char *fullpath_ptr = S_fullpath_ptr(self, name);
- bool retval = false;
- if (stat(fullpath_ptr, &stat_buf) != -1) {
- retval = true;
- }
+ bool retval = S_exists(fullpath_ptr);
FREEMEM(fullpath_ptr);
return retval;
}
@@ -327,13 +307,27 @@ S_fullpath_ptr(FSFolder *self, String *path) {
}
static bool
+S_is_local_entry(String *path) {
+ return !Str_Contains_Utf8(path, "/", 1);
+}
+
+/***************************************************************************/
+
+#if (defined(CHY_HAS_WINDOWS_H) && !defined(__CYGWIN__))
+
+// Windows.h defines INCREF and DECREF, so we include it only at the end of
+// this file and undef those symbols.
+#undef INCREF
+#undef DECREF
+
+#include <windows.h>
+
+static bool
S_dir_ok(String *path) {
- bool retval = false;
char *path_ptr = Str_To_Utf8(path);
- struct stat stat_buf;
- if (stat(path_ptr, &stat_buf) != -1) {
- if (stat_buf.st_mode & S_IFDIR) { retval = true; }
- }
+ DWORD attrs = GetFileAttributesA(path_ptr);
+ bool retval = attrs != INVALID_FILE_ATTRIBUTES
+ && (attrs & FILE_ATTRIBUTE_DIRECTORY);
FREEMEM(path_ptr);
return retval;
}
@@ -342,35 +336,26 @@ static bool
S_create_dir(String *path) {
bool retval = true;
char *path_ptr = Str_To_Utf8(path);
- if (-1 == chy_makedir(path_ptr, 0777)) {
- ErrMsg_set_with_errno("Couldn't create directory '%o'", path);
+ if (CreateDirectoryA(path_ptr, NULL) == 0) {
+ ErrMsg_set_with_win_error("Couldn't create directory '%o'", path);
retval = false;
}
+ SetFileAttributes(path_ptr, FILE_ATTRIBUTE_NOT_CONTENT_INDEXED);
FREEMEM(path_ptr);
return retval;
}
static bool
-S_is_local_entry(String *path) {
- return !Str_Contains_Utf8(path, "/", 1);
+S_exists(const char *path) {
+ return GetFileAttributesA(path) != INVALID_FILE_ATTRIBUTES;
}
-/***************************************************************************/
-
-#if (defined(CHY_HAS_WINDOWS_H) && !defined(__CYGWIN__))
-
-// Windows.h defines INCREF and DECREF, so we include it only at the end of
-// this file and undef those symbols.
-#undef INCREF
-#undef DECREF
-
-#include <windows.h>
-
static bool
S_is_absolute(String *path) {
int32_t code_point = Str_Code_Point_At(path, 0);
- if (isalpha(code_point)) {
+ if ((code_point >= 'A' && code_point <= 'Z')
+ || (code_point >= 'a' && code_point <= 'z')) {
code_point = Str_Code_Point_At(path, 1);
if (code_point != ':') { return false; }
code_point = Str_Code_Point_At(path, 2);
@@ -383,9 +368,9 @@ static String*
S_absolutify(String *path) {
if (S_is_absolute(path)) { return Str_Clone(path); }
- DWORD cwd_len = GetCurrentDirectory(0, NULL);
+ DWORD cwd_len = GetCurrentDirectoryA(0, NULL);
char *cwd = (char*)MALLOCATE(cwd_len);
- DWORD res = GetCurrentDirectory(cwd_len, cwd);
+ DWORD res = GetCurrentDirectoryA(cwd_len, cwd);
if (res == 0 || res > cwd_len) {
THROW(ERR, "GetCurrentDirectory failed");
}
@@ -455,6 +440,41 @@ S_delete(const char *path) {
#elif (defined(CHY_HAS_UNISTD_H))
+#include <errno.h>
+#include <stdio.h> // For rename, remove
+#include <sys/stat.h> // For stat, mkdir
+#include <unistd.h> // For getcwd, link, rmdir
+
+static bool
+S_dir_ok(String *path) {
+ bool retval = false;
+ char *path_ptr = Str_To_Utf8(path);
+ struct stat stat_buf;
+ if (stat(path_ptr, &stat_buf) != -1) {
+ if (stat_buf.st_mode & S_IFDIR) { retval = true; }
+ }
+ FREEMEM(path_ptr);
+ return retval;
+}
+
+static bool
+S_create_dir(String *path) {
+ bool retval = true;
+ char *path_ptr = Str_To_Utf8(path);
+ if (mkdir(path_ptr, 0777) != 0) {
+ ErrMsg_set_with_errno("Couldn't create directory '%o'", path);
+ retval = false;
+ }
+ FREEMEM(path_ptr);
+ return retval;
+}
+
+static bool
+S_exists(const char *path) {
+ struct stat stat_buf;
+ return stat(path, &stat_buf) == 0;
+}
+
static bool
S_is_absolute(String *path) {
return Str_Starts_With_Utf8(path, CHY_DIR_SEP, 1);