lunarmodules / luafilesystem

LuaFileSystem is a Lua library developed to complement the set of functions related to file systems offered by the standard Lua distribution.
https://lunarmodules.github.io/luafilesystem/
MIT License
900 stars 291 forks source link

missing prototypes warnings when building on windows using msys2 #168

Open FractalU opened 1 year ago

FractalU commented 1 year ago

I've manually built luafilesystem on window using msys2, a unix like environment on windows similar to cygwin. From gcc I get the following warnings.

src/lfs.c:171:5: warning: no previous prototype for 'lfs_win32_pusherror' [-Wmissing-prototypes]
  171 | int lfs_win32_pusherror(lua_State * L)
      |
src/lfs.c:184:8: warning: no previous prototype for 'windowsToUnixTime' [-Wmissing-prototypes]
  184 | time_t windowsToUnixTime(FILETIME ft)
      |
src/lfs.c:192:5: warning: no previous prototype for 'lfs_win32_lstat' [-Wmissing-prototypes]
  192 | int lfs_win32_lstat(const char *path, STAT_STRUCT * buffer)
      |

I guess all the functions mentioned in the warnings should be declared as static. Below is the proposed change.

In lfs.c at line 169

#ifdef _WIN32

static int lfs_win32_pusherror(lua_State * L)
{
  int en = GetLastError();
  lua_pushnil(L);
  if (en == ERROR_FILE_EXISTS || en == ERROR_SHARING_VIOLATION)
    lua_pushstring(L, "File exists");
  else
    lua_pushstring(L, strerror(en));
  return 2;
}

#define TICKS_PER_SECOND 10000000
#define EPOCH_DIFFERENCE 11644473600LL
static time_t windowsToUnixTime(FILETIME ft)
{
  ULARGE_INTEGER uli;
  uli.LowPart = ft.dwLowDateTime;
  uli.HighPart = ft.dwHighDateTime;
  return (time_t) (uli.QuadPart / TICKS_PER_SECOND - EPOCH_DIFFERENCE);
}

static int lfs_win32_lstat(const char *path, STAT_STRUCT * buffer)
{
  WIN32_FILE_ATTRIBUTE_DATA win32buffer;
  if (GetFileAttributesEx(path, GetFileExInfoStandard, &win32buffer)) {
    if (!(win32buffer.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
      return STAT_FUNC(path, buffer);
    }
    buffer->st_mode = _S_IFLNK;
    buffer->st_dev = 0;
    buffer->st_ino = 0;
    buffer->st_nlink = 0;
    buffer->st_uid = 0;
    buffer->st_gid = 0;
    buffer->st_rdev = 0;
    buffer->st_atime = windowsToUnixTime(win32buffer.ftLastAccessTime);
    buffer->st_mtime = windowsToUnixTime(win32buffer.ftLastWriteTime);
    buffer->st_ctime = windowsToUnixTime(win32buffer.ftCreationTime);
    buffer->st_size = 0;
    return 0;
  } else {
    return 1;
  }
}

#endif

Declare all these functions as static.