aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEroica <seba79@mailbox.org>2017-03-14 14:19:19 +0100
committerHisham Muhammad <hisham@gobolinux.org>2020-04-21 13:45:50 -0300
commit53d2acc5173e20a843d26d88ffa4767b01868e02 (patch)
tree06a10b5e0d16a68b69f908edafa93eaa5d7bdd2e
parent6b11508ea9e03077f805bf619b558dbed893c7d2 (diff)
downloadluafilesystem-53d2acc5173e20a843d26d88ffa4767b01868e02.tar.gz
luafilesystem-53d2acc5173e20a843d26d88ffa4767b01868e02.tar.bz2
luafilesystem-53d2acc5173e20a843d26d88ffa4767b01868e02.zip
Add Windows compatibility for lfs.link
Co-Authored-By: Hisham Muhammad <hisham@gobolinux.org>
-rw-r--r--src/lfs.c106
1 files changed, 65 insertions, 41 deletions
diff --git a/src/lfs.c b/src/lfs.c
index cca4411..ac63947 100644
--- a/src/lfs.c
+++ b/src/lfs.c
@@ -29,6 +29,10 @@
29#endif 29#endif
30#endif 30#endif
31 31
32#ifdef _WIN32
33 #define _WIN32_WINNT 0x600
34#endif
35
32#ifndef LFS_DO_NOT_USE_LARGE_FILE 36#ifndef LFS_DO_NOT_USE_LARGE_FILE
33#define _LARGEFILE64_SOURCE 37#define _LARGEFILE64_SOURCE
34#endif 38#endif
@@ -117,15 +121,42 @@ typedef struct dir_data {
117 #define lfs_setmode(file, m) (_setmode(_fileno(file), m)) 121 #define lfs_setmode(file, m) (_setmode(_fileno(file), m))
118 #define STAT_STRUCT struct _stati64 122 #define STAT_STRUCT struct _stati64
119 #endif 123 #endif
120#define STAT_FUNC _stati64 124
121#define LSTAT_FUNC STAT_FUNC 125 #ifndef _S_IFLNK
126 #define _S_IFLNK 0x400
127 #endif
128
129 #ifndef S_ISDIR
130 #define S_ISDIR(mode) (mode&_S_IFDIR)
131 #endif
132 #ifndef S_ISREG
133 #define S_ISREG(mode) (mode&_S_IFREG)
134 #endif
135 #ifndef S_ISLNK
136 #define S_ISLNK(mode) (mode&_S_IFLNK)
137 #endif
138 #ifndef S_ISSOCK
139 #define S_ISSOCK(mode) (0)
140 #endif
141 #ifndef S_ISFIFO
142 #define S_ISFIFO(mode) (0)
143 #endif
144 #ifndef S_ISCHR
145 #define S_ISCHR(mode) (mode&_S_IFCHR)
146 #endif
147 #ifndef S_ISBLK
148 #define S_ISBLK(mode) (0)
149 #endif
150
151 #define STAT_FUNC _stati64
152 #define LSTAT_FUNC lfs_win32_lstat
122#else 153#else
123#define _O_TEXT 0 154 #define _O_TEXT 0
124#define _O_BINARY 0 155 #define _O_BINARY 0
125#define lfs_setmode(file, m) ((void)file, (void)m, 0) 156 #define lfs_setmode(file, m) ((void)file, (void)m, 0)
126#define STAT_STRUCT struct stat 157 #define STAT_STRUCT struct stat
127#define STAT_FUNC stat 158 #define STAT_FUNC stat
128#define LSTAT_FUNC lstat 159 #define LSTAT_FUNC lstat
129#endif 160#endif
130 161
131#ifdef _WIN32 162#ifdef _WIN32
@@ -440,20 +471,36 @@ static int file_unlock (lua_State *L) {
440** @param #2 Name of link. 471** @param #2 Name of link.
441** @param #3 True if link is symbolic (optional). 472** @param #3 True if link is symbolic (optional).
442*/ 473*/
443static int make_link (lua_State *L) { 474static int make_link(lua_State *L)
444#ifndef _WIN32 475{
445 const char *oldpath = luaL_checkstring(L, 1); 476 const char *oldpath = luaL_checkstring(L, 1);
446 const char *newpath = luaL_checkstring(L, 2); 477 const char *newpath = luaL_checkstring(L, 2);
447 int res = (lua_toboolean(L,3) ? symlink : link)(oldpath, newpath); 478#ifndef _WIN32
448 if (res == -1) { 479 return pushresult(L,
449 return pusherror(L, NULL); 480 (lua_toboolean(L, 3) ? symlink : link)(oldpath, newpath), NULL);
481#else
482 int symbolic = lua_toboolean(L, 3);
483 STAT_STRUCT oldpathinfo;
484 int is_dir = 0;
485 if (STAT_FUNC(oldpath, &oldpathinfo) == 0) {
486 is_dir = S_ISDIR(oldpathinfo.st_mode) != 0;
487 }
488 if (!symbolic && is_dir) {
489 lua_pushnil(L);
490 lua_pushstring(L, "hard links to directories are not supported on Windows");
491 return 2;
492 }
493
494 int result = symbolic ? CreateSymbolicLink(newpath, oldpath, is_dir)
495 : CreateHardLink(newpath, oldpath, NULL);
496
497 if (result) {
498 return pushresult(L, result, NULL);
450 } else { 499 } else {
451 lua_pushinteger(L, 0); 500 lua_pushnil(L); lua_pushstring(L, symbolic ? "make_link CreateSymbolicLink() failed"
452 return 1; 501 : "make_link CreateHardLink() failed");
502 return 2;
453 } 503 }
454#else
455 errno = ENOSYS; /* = "Function not implemented" */
456 return pushresult(L, -1, "make_link is not supported on Windows");
457#endif 504#endif
458} 505}
459 506
@@ -610,29 +657,6 @@ static int lock_create_meta (lua_State *L) {
610} 657}
611 658
612 659
613#ifdef _WIN32
614 #ifndef S_ISDIR
615 #define S_ISDIR(mode) (mode&_S_IFDIR)
616 #endif
617 #ifndef S_ISREG
618 #define S_ISREG(mode) (mode&_S_IFREG)
619 #endif
620 #ifndef S_ISLNK
621 #define S_ISLNK(mode) (0)
622 #endif
623 #ifndef S_ISSOCK
624 #define S_ISSOCK(mode) (0)
625 #endif
626 #ifndef S_ISFIFO
627 #define S_ISFIFO(mode) (0)
628 #endif
629 #ifndef S_ISCHR
630 #define S_ISCHR(mode) (mode&_S_IFCHR)
631 #endif
632 #ifndef S_ISBLK
633 #define S_ISBLK(mode) (0)
634 #endif
635#endif
636/* 660/*
637** Convert the inode protection mode to a string. 661** Convert the inode protection mode to a string.
638*/ 662*/