lua-filesystem (1.5.0-2) permissions.patch

Summary

 src/lfs.c |   50 +++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 47 insertions(+), 3 deletions(-)

    
download this patch

Patch contents

Author: Enrico Tassi <gareuselesinge@debian.org>
Description: patch needed for luatex, see http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=441595
--- lua-filesystem-1.5.0.orig/src/lfs.c
+++ lua-filesystem-1.5.0/src/lfs.c
@@ -128,16 +128,19 @@
 ** If unable to get the current directory, it returns nil
 **  and a string describing the error
 */
+#ifndef PATH_MAX
+#define PATH_MAX 4096
+#endif
+
 static int get_dir (lua_State *L) {
-  char *path;
-  if ((path = getcwd(NULL, 0)) == NULL) {
+  char path[PATH_MAX];
+  if (getcwd((char *)path, PATH_MAX) == NULL) {
     lua_pushnil(L);
     lua_pushstring(L, getcwd_error);
     return 2;
   }
   else {
     lua_pushstring(L, path);
-    free(path);
     return 1;
   }
 }
@@ -666,6 +669,46 @@
 #endif
 }
 
+ /*
+** Convert the inode protection mode to a permission list.
+*/
+
+#ifdef _WIN32
+static const char *perm2string (unsigned short mode) {
+  static char perms[10] = "---------\0";
+  int i;
+  for (i=0;i<9;i++) perms[i]='-';
+  if (mode  & _S_IREAD)
+   { perms[0] = 'r'; perms[3] = 'r'; perms[6] = 'r'; }
+  if (mode  & _S_IWRITE)
+   { perms[1] = 'w'; perms[4] = 'w'; perms[7] = 'w'; }
+  if (mode  & _S_IEXEC)
+   { perms[2] = 'x'; perms[5] = 'x'; perms[8] = 'x'; }
+  return perms;
+}
+#else
+static const char *perm2string (mode_t mode) {
+  static char perms[10] = "---------\0";
+  int i;
+  for (i=0;i<9;i++) perms[i]='-';
+  if (mode & S_IRUSR) perms[0] = 'r';
+  if (mode & S_IWUSR) perms[1] = 'w';
+  if (mode & S_IXUSR) perms[2] = 'x';
+  if (mode & S_IRGRP) perms[3] = 'r';
+  if (mode & S_IWGRP) perms[4] = 'w';
+  if (mode & S_IXGRP) perms[5] = 'x';
+  if (mode & S_IROTH) perms[6] = 'r';
+  if (mode & S_IWOTH) perms[7] = 'w';
+  if (mode & S_IXOTH) perms[8] = 'x';
+  return perms;
+}
+#endif
+
+/* permssions string */
+static void push_st_perm (lua_State *L, STAT_STRUCT *info) {
+    lua_pushstring (L, perm2string (info->st_mode));
+}
+
 typedef void (*_push_function) (lua_State *L, STAT_STRUCT *info);
 
 struct _stat_members {
@@ -685,6 +728,7 @@
 	{ "modification", push_st_mtime },
 	{ "change",       push_st_ctime },
 	{ "size",         push_st_size },
+ 	{ "permissions",  push_st_perm },
 #ifndef _WIN32
 	{ "blocks",       push_st_blocks },
 	{ "blksize",      push_st_blksize },