cgi-bin/tek/os/posix.c
author Timm S. Mueller <tmueller@neoscientists.org>
Fri, 02 May 2008 14:42:18 +0200
changeset 241 c6c81629f54e
parent 198 87a4de7c7457
permissions -rw-r--r--
Markup: merged in latest changes (bugfix in preformatted layout, support for
numbers in headnode Ids), source code documentation added, format description
slightly cleaned up; Makefiles: markup class installation path corrected;
luahtml parser: corrected to no longer bail out with "too many syntax levels"
when >200 variables were included; posix: minor source code cleanup
tmueller@193
     1
tmueller@193
     2
/*
tmueller@193
     3
**	tek.os.posix - tek POSIX library
tmueller@193
     4
**	Written by Timm S. Mueller <tmueller at neoscientists.org>
tmueller@193
     5
**	See copyright notice in COPYRIGHT
tmueller@193
     6
*/
tmueller@0
     7
tmueller@0
     8
#include <stdlib.h>
tmueller@0
     9
#include <string.h>
tmueller@0
    10
#include <sys/types.h>
tmueller@0
    11
#include <sys/stat.h>
tmueller@0
    12
#include <unistd.h>
tmueller@0
    13
#include <dirent.h>
tmueller@0
    14
#include <errno.h>
tmueller@172
    15
#include <math.h>
tmueller@172
    16
#include <time.h>
tmueller@0
    17
#include <lua.h>
tmueller@0
    18
#include <lualib.h>
tmueller@0
    19
#include <lauxlib.h>
tmueller@0
    20
tmueller@0
    21
tmueller@0
    22
#define DIRCLASSNAME "dir*"
tmueller@0
    23
tmueller@0
    24
tmueller@0
    25
static void
tmueller@0
    26
setfield(lua_State *L, const char *attr, const char *key, lua_Integer n)
tmueller@0
    27
{
tmueller@0
    28
	if (attr == NULL || strcmp(attr, key) == 0)
tmueller@0
    29
		lua_pushinteger(L, n);
tmueller@0
    30
	if (attr == NULL)
tmueller@0
    31
		lua_setfield(L, -2, key);
tmueller@0
    32
}
tmueller@0
    33
tmueller@18
    34
tmueller@0
    35
static int
tmueller@0
    36
setstat(lua_State *L, int result, struct stat *s, const char *attr)
tmueller@0
    37
{
tmueller@0
    38
	if (result != 0)
tmueller@0
    39
	{
tmueller@0
    40
		lua_pushnil(L);
tmueller@0
    41
		lua_pushstring(L, strerror(errno));
tmueller@0
    42
		return 2;
tmueller@0
    43
	}
tmueller@0
    44
tmueller@0
    45
	if (attr == NULL)
tmueller@0
    46
		lua_newtable(L);
tmueller@198
    47
tmueller@0
    48
	setfield(L, attr, "dev", s->st_dev);
tmueller@0
    49
	setfield(L, attr, "ino", s->st_ino);
tmueller@198
    50
tmueller@0
    51
	if (attr == NULL || strcmp(attr, "mode") == 0)
tmueller@0
    52
	{
tmueller@0
    53
		if (S_ISREG(s->st_mode))
tmueller@0
    54
			lua_pushstring(L, "file");
tmueller@0
    55
		else if (S_ISDIR(s->st_mode))
tmueller@0
    56
			lua_pushstring(L, "directory");
tmueller@0
    57
		else if (S_ISLNK(s->st_mode))
tmueller@0
    58
			lua_pushstring(L, "link");
tmueller@0
    59
		else if (S_ISSOCK(s->st_mode))
tmueller@0
    60
			lua_pushstring(L, "socket");
tmueller@0
    61
		else if (S_ISFIFO(s->st_mode))
tmueller@0
    62
			lua_pushstring(L, "named pipe");
tmueller@0
    63
		else if (S_ISCHR(s->st_mode))
tmueller@0
    64
			lua_pushstring(L, "char device");
tmueller@0
    65
		else if (S_ISBLK(s->st_mode))
tmueller@0
    66
			lua_pushstring(L, "block device");
tmueller@0
    67
		else
tmueller@0
    68
			lua_pushstring(L, "other");
tmueller@0
    69
		if (attr == NULL)
tmueller@0
    70
			lua_setfield(L, -2, "mode");
tmueller@0
    71
	}
tmueller@198
    72
tmueller@0
    73
	setfield(L, attr, "nlink", s->st_nlink);
tmueller@0
    74
	setfield(L, attr, "uid", s->st_uid);
tmueller@0
    75
	setfield(L, attr, "gid", s->st_gid);
tmueller@0
    76
	setfield(L, attr, "rdev", s->st_rdev);
tmueller@0
    77
	setfield(L, attr, "access", s->st_atime);
tmueller@0
    78
	setfield(L, attr, "modifications", s->st_mtime);
tmueller@0
    79
	setfield(L, attr, "change", s->st_ctime);
tmueller@165
    80
	setfield(L, attr, "size", s->st_size);
tmueller@0
    81
	setfield(L, attr, "blocks", s->st_blocks);
tmueller@0
    82
	setfield(L, attr, "blksize", s->st_blksize);
tmueller@198
    83
tmueller@0
    84
	return 1;
tmueller@0
    85
}
tmueller@0
    86
tmueller@0
    87
tmueller@198
    88
static int
tmueller@0
    89
posix_stat(lua_State *L)
tmueller@0
    90
{
tmueller@0
    91
	const char *path = luaL_checkstring(L, 1);
tmueller@0
    92
	const char *attr = luaL_optstring(L, 2, NULL);
tmueller@0
    93
	struct stat s;
tmueller@0
    94
	return setstat(L, stat(path, &s), &s, attr);
tmueller@198
    95
}
tmueller@0
    96
tmueller@0
    97
tmueller@198
    98
static int
tmueller@0
    99
posix_lstat(lua_State *L)
tmueller@0
   100
{
tmueller@0
   101
	const char *path = luaL_checkstring(L, 1);
tmueller@0
   102
	const char *attr = luaL_optstring(L, 2, NULL);
tmueller@0
   103
	struct stat s;
tmueller@0
   104
	return setstat(L, lstat(path, &s), &s, attr);
tmueller@0
   105
}
tmueller@0
   106
tmueller@0
   107
tmueller@198
   108
static int
tmueller@0
   109
posix_opendir(lua_State *L)
tmueller@0
   110
{
tmueller@0
   111
	const char *path = luaL_checkstring(L, 1);
tmueller@0
   112
	DIR **pdir = lua_newuserdata(L, sizeof(void *));
tmueller@0
   113
	*pdir = NULL;
tmueller@0
   114
	lua_pushvalue(L, lua_upvalueindex(2)); /* class metatable */
tmueller@0
   115
	/* attach metatable to the userdata object */
tmueller@0
   116
	lua_setmetatable(L, -2); /* s: udata */
tmueller@0
   117
	/* create class instance */
tmueller@0
   118
	*pdir = opendir(path);
tmueller@0
   119
	if (*pdir == NULL)
tmueller@0
   120
	{
tmueller@0
   121
		lua_pushnil(L);
tmueller@0
   122
		lua_pushstring(L, strerror(errno));
tmueller@0
   123
		return 2;
tmueller@0
   124
	}
tmueller@0
   125
	return 1;
tmueller@0
   126
}
tmueller@0
   127
tmueller@0
   128
tmueller@0
   129
static DIR **
tmueller@0
   130
getinstptr(lua_State *L, int narg, const char *classname)
tmueller@0
   131
{
tmueller@0
   132
	DIR **pinst = luaL_checkudata(L, narg, classname);
tmueller@0
   133
	if (*pinst) return pinst;
tmueller@0
   134
	luaL_argerror(L, narg, "Closed handle");
tmueller@0
   135
	return NULL;
tmueller@0
   136
}
tmueller@0
   137
tmueller@0
   138
tmueller@198
   139
static int
tmueller@0
   140
posix_closedir(lua_State *L)
tmueller@0
   141
{
tmueller@0
   142
	DIR **pdir = getinstptr(L, 1, DIRCLASSNAME);
tmueller@0
   143
	closedir(*pdir);
tmueller@0
   144
	*pdir = NULL;
tmueller@0
   145
	return 0;
tmueller@0
   146
}
tmueller@0
   147
tmueller@0
   148
tmueller@198
   149
static int
tmueller@0
   150
posix_readdir(lua_State *L)
tmueller@0
   151
{
tmueller@0
   152
	DIR *dir = *getinstptr(L, 1, DIRCLASSNAME);
tmueller@0
   153
	struct dirent *de = readdir(dir);
tmueller@0
   154
	if (de)
tmueller@0
   155
		lua_pushstring(L, de->d_name);
tmueller@0
   156
	else
tmueller@0
   157
		lua_pushnil(L);
tmueller@0
   158
	return 1;
tmueller@0
   159
}
tmueller@0
   160
tmueller@0
   161
tmueller@198
   162
static int
tmueller@0
   163
posix_readlink(lua_State *L)
tmueller@0
   164
{
tmueller@0
   165
	const char *path = luaL_checkstring(L, 1);
tmueller@0
   166
	char buf[PATH_MAX + 1];
tmueller@0
   167
	ssize_t len = readlink(path, buf, sizeof(buf) - 1);
tmueller@0
   168
	if (len < 0)
tmueller@0
   169
	{
tmueller@0
   170
		lua_pushnil(L);
tmueller@0
   171
		lua_pushstring(L, strerror(errno));
tmueller@0
   172
		return 2;
tmueller@0
   173
	}
tmueller@0
   174
	buf[len] = 0;
tmueller@0
   175
	lua_pushstring(L, buf);
tmueller@0
   176
	return 1;
tmueller@0
   177
}
tmueller@0
   178
tmueller@0
   179
tmueller@198
   180
static int
tmueller@0
   181
posix_mkdir(lua_State *L)
tmueller@0
   182
{
tmueller@0
   183
	const char *path = luaL_checkstring(L, 1);
tmueller@0
   184
	if (mkdir(path, 0775) == 0)
tmueller@0
   185
	{
tmueller@0
   186
		lua_pushboolean(L, 1);
tmueller@0
   187
		return 1;
tmueller@0
   188
	}
tmueller@0
   189
	lua_pushnil(L);
tmueller@0
   190
	lua_pushstring(L, strerror(errno));
tmueller@0
   191
	return 2;
tmueller@0
   192
}
tmueller@0
   193
tmueller@0
   194
tmueller@198
   195
static int
tmueller@0
   196
posix_symlink(lua_State *L)
tmueller@0
   197
{
tmueller@0
   198
	const char *oldpath = luaL_checkstring(L, 1);
tmueller@0
   199
	const char *newpath = luaL_checkstring(L, 2);
tmueller@0
   200
	if (symlink(oldpath, newpath) == 0)
tmueller@0
   201
	{
tmueller@0
   202
		lua_pushboolean(L, 1);
tmueller@0
   203
		return 1;
tmueller@0
   204
	}
tmueller@0
   205
	lua_pushnil(L);
tmueller@0
   206
	lua_pushstring(L, strerror(errno));
tmueller@0
   207
	return 2;
tmueller@0
   208
}
tmueller@0
   209
tmueller@0
   210
tmueller@0
   211
static int
tmueller@0
   212
resolvepath(const char *src, char *dest)
tmueller@0
   213
{
tmueller@0
   214
	int len = strlen(src);
tmueller@0
   215
	const char *sp = src + len;
tmueller@0
   216
	char *dp = dest;
tmueller@0
   217
	int dc = 0, slc = 0, eac = 0, wc = 0;
tmueller@0
   218
	int i, c;
tmueller@198
   219
tmueller@198
   220
	while (len--)
tmueller@0
   221
	{
tmueller@0
   222
		c = *(--sp);
tmueller@0
   223
		switch (c)
tmueller@0
   224
		{
tmueller@0
   225
			case '/':
tmueller@0
   226
				if (dc == 2)
tmueller@0
   227
					eac++;
tmueller@0
   228
				dc = 0;
tmueller@0
   229
				slc = 1;
tmueller@0
   230
				wc = 0;
tmueller@0
   231
				break;
tmueller@0
   232
tmueller@0
   233
			case '.':
tmueller@0
   234
				if (slc)
tmueller@0
   235
				{
tmueller@0
   236
					dc++;
tmueller@0
   237
					break;
tmueller@0
   238
				}
tmueller@0
   239
				/* fallthru: */
tmueller@0
   240
tmueller@0
   241
			default:
tmueller@0
   242
				if (wc)
tmueller@0
   243
					break;
tmueller@198
   244
tmueller@0
   245
				if (slc)
tmueller@0
   246
				{
tmueller@0
   247
					slc = 0;
tmueller@198
   248
tmueller@0
   249
					if (eac > 0)
tmueller@0
   250
					{
tmueller@0
   251
						/* resolve one eatcount */
tmueller@0
   252
						eac--;
tmueller@0
   253
						/* now wait for next path part */
tmueller@0
   254
						wc = 1;
tmueller@0
   255
						break;
tmueller@0
   256
					}
tmueller@198
   257
tmueller@0
   258
					*dp++ = '/';
tmueller@0
   259
				}
tmueller@198
   260
tmueller@0
   261
				while (dc == 2 || dc == 1)
tmueller@0
   262
				{
tmueller@0
   263
					*dp++ = '.';
tmueller@0
   264
					dc--;
tmueller@0
   265
				}
tmueller@0
   266
				dc = 0;
tmueller@198
   267
tmueller@0
   268
				*dp++ = c;
tmueller@0
   269
				break;
tmueller@0
   270
		}
tmueller@0
   271
	}
tmueller@198
   272
tmueller@0
   273
	/* unresolved eatcount */
tmueller@0
   274
	if (eac)
tmueller@0
   275
		return 0;
tmueller@198
   276
tmueller@0
   277
	/* resolve remaining slash */
tmueller@0
   278
	if (slc)
tmueller@0
   279
		*dp++ = '/';
tmueller@198
   280
tmueller@0
   281
	*dp = 0;
tmueller@198
   282
tmueller@0
   283
	len = dp - dest;
tmueller@0
   284
	for (i = 0; i < len / 2; ++i)
tmueller@0
   285
	{
tmueller@0
   286
		char t = dest[i];
tmueller@198
   287
		dest[i] = dest[len - i - 1];
tmueller@0
   288
		dest[len - i - 1] = t;
tmueller@0
   289
	}
tmueller@0
   290
tmueller@0
   291
	return 1;
tmueller@0
   292
}
tmueller@0
   293
tmueller@0
   294
tmueller@198
   295
static int
tmueller@0
   296
posix_abspath(lua_State *L)
tmueller@0
   297
{
tmueller@0
   298
	const char *path = luaL_checkstring(L, 1);
tmueller@0
   299
	char *pwd = NULL;
tmueller@0
   300
	size_t pwdsize = 16;
tmueller@198
   301
	size_t len1, len2;
tmueller@198
   302
	char *srcpath, *dstpath;
tmueller@198
   303
tmueller@0
   304
	for (;;)
tmueller@0
   305
	{
tmueller@198
   306
		if (path[0] == '/')
tmueller@0
   307
		{
tmueller@198
   308
			pwd = malloc(2);
tmueller@198
   309
			if (pwd == NULL)
tmueller@198
   310
				break;
tmueller@198
   311
			pwd[0] = '/';
tmueller@198
   312
			pwd[1] = 0;
tmueller@198
   313
		}
tmueller@198
   314
		else
tmueller@198
   315
		{
tmueller@198
   316
			char *newpwd = realloc(pwd, pwdsize);
tmueller@198
   317
			if (newpwd == NULL)
tmueller@198
   318
				break;
tmueller@198
   319
			pwd = newpwd;
tmueller@198
   320
tmueller@198
   321
			if (!getcwd(pwd, pwdsize))
tmueller@0
   322
			{
tmueller@198
   323
				if (errno == ERANGE)
tmueller@0
   324
				{
tmueller@198
   325
					pwdsize <<= 1;
tmueller@198
   326
					continue;
tmueller@0
   327
				}
tmueller@0
   328
				lua_pushnil(L);
tmueller@198
   329
				lua_pushstring(L, strerror(errno));
tmueller@0
   330
				return 2;
tmueller@0
   331
			}
tmueller@198
   332
		}
tmueller@198
   333
tmueller@198
   334
		len1 = strlen(pwd);
tmueller@198
   335
		len2 = strlen(path);
tmueller@198
   336
		srcpath = malloc(len1 + 1 + len2 + 1);
tmueller@198
   337
		dstpath = malloc(len1 + 1 + len2 + 1);
tmueller@198
   338
tmueller@198
   339
		if (srcpath && dstpath)
tmueller@198
   340
		{
tmueller@198
   341
			int res;
tmueller@198
   342
tmueller@198
   343
			strcpy(srcpath, pwd);
tmueller@198
   344
			free(pwd);
tmueller@198
   345
			srcpath[len1] = '/';
tmueller@198
   346
			strcpy(srcpath + len1 + 1, path);
tmueller@198
   347
			res = resolvepath(srcpath, dstpath);
tmueller@0
   348
			free(srcpath);
tmueller@198
   349
tmueller@198
   350
			if (res)
tmueller@198
   351
			{
tmueller@198
   352
				lua_pushstring(L, dstpath);
tmueller@198
   353
				free(dstpath);
tmueller@198
   354
				return 1;
tmueller@198
   355
			}
tmueller@198
   356
tmueller@0
   357
			free(dstpath);
tmueller@198
   358
			lua_pushnil(L);
tmueller@198
   359
			lua_pushstring(L, "Not a valid path");
tmueller@198
   360
			return 2;
tmueller@0
   361
		}
tmueller@198
   362
tmueller@198
   363
		free(srcpath);
tmueller@198
   364
		free(dstpath);
tmueller@198
   365
		break;
tmueller@0
   366
	}
tmueller@198
   367
tmueller@0
   368
	free(pwd);
tmueller@0
   369
	luaL_error(L, "Out of memory");
tmueller@0
   370
	return 0;
tmueller@0
   371
}
tmueller@0
   372
tmueller@0
   373
tmueller@198
   374
static int
tmueller@172
   375
posix_nanosleep(lua_State *L)
tmueller@172
   376
{
tmueller@172
   377
	struct timespec req;
tmueller@172
   378
	lua_Number d = luaL_checknumber(L, 1);
tmueller@198
   379
tmueller@172
   380
	req.tv_sec = (time_t) d;
tmueller@172
   381
	req.tv_nsec = (long) ((d - req.tv_sec) * 1000000000);
tmueller@198
   382
tmueller@172
   383
	if (nanosleep(&req, NULL) == 0)
tmueller@172
   384
	{
tmueller@172
   385
		lua_pushboolean(L, 1);
tmueller@172
   386
		return 1;
tmueller@172
   387
	}
tmueller@198
   388
tmueller@172
   389
	lua_pushnil(L);
tmueller@172
   390
	lua_pushstring(L, strerror(errno));
tmueller@172
   391
	return 2;
tmueller@172
   392
}
tmueller@172
   393
tmueller@172
   394
tmueller@0
   395
static const luaL_Reg lib[] =
tmueller@0
   396
{
tmueller@0
   397
	{ "stat", posix_stat },
tmueller@0
   398
	{ "lstat", posix_lstat },
tmueller@0
   399
	{ "opendir", posix_opendir },
tmueller@0
   400
	{ "closedir", posix_closedir },
tmueller@0
   401
	{ "readdir", posix_readdir },
tmueller@0
   402
	{ "mkdir", posix_mkdir },
tmueller@0
   403
	{ "readlink", posix_readlink },
tmueller@0
   404
	{ "symlink", posix_symlink },
tmueller@0
   405
	{ "abspath", posix_abspath },
tmueller@172
   406
	{ "nanosleep", posix_nanosleep },
tmueller@0
   407
	{ NULL, NULL }
tmueller@0
   408
};
tmueller@0
   409
tmueller@0
   410
tmueller@0
   411
static const luaL_Reg methods[] =
tmueller@0
   412
{
tmueller@0
   413
	{"read", posix_readdir },
tmueller@0
   414
	{"close", posix_closedir },
tmueller@0
   415
	{"__gc", posix_closedir },
tmueller@0
   416
	{NULL, NULL}
tmueller@0
   417
};
tmueller@0
   418
tmueller@0
   419
tmueller@0
   420
static void
tmueller@0
   421
addclass(lua_State *L, const char *libname, const char *classname,
tmueller@241
   422
	const luaL_Reg *functions, const luaL_Reg *methods, void *userdata)
tmueller@0
   423
{
tmueller@18
   424
	luaL_newmetatable(L, classname); /* classtab */
tmueller@18
   425
	lua_pushliteral(L, "__index"); /* classtab, "__index" */
tmueller@0
   426
tmueller@0
   427
	/* insert self: classtab.__index = classtab */
tmueller@18
   428
	lua_pushvalue(L, -2); /* classtab, "__index", classtab */
tmueller@18
   429
	lua_rawset(L, -3); /* classtab */
tmueller@0
   430
tmueller@0
   431
	/* insert methods. consume 1 userdata. do not create a new tab */
tmueller@18
   432
	lua_pushlightuserdata(L, userdata); /* classtab, userdata */
tmueller@18
   433
	luaL_openlib(L, NULL, methods, 1); /* classtab */
tmueller@0
   434
tmueller@0
   435
	/* first upvalue: userdata */
tmueller@18
   436
	lua_pushlightuserdata(L, userdata); /* classtab, userdata */
tmueller@0
   437
tmueller@0
   438
	/* duplicate table argument to be used as second upvalue for cclosure */
tmueller@18
   439
	lua_pushvalue(L, -2); /* classtab, userdata, classtab */
tmueller@0
   440
tmueller@0
   441
	/* insert functions */
tmueller@0
   442
	luaL_openlib(L, libname, functions, 2);	/* classtab, libtab */
tmueller@0
   443
tmueller@18
   444
	/* adjust stack */
tmueller@198
   445
	lua_pop(L, 2);
tmueller@0
   446
}
tmueller@0
   447
tmueller@0
   448
tmueller@188
   449
int luaopen_tek_os_posix(lua_State *L)
tmueller@0
   450
{
tmueller@241
   451
	addclass(L, "tek.os.posix", DIRCLASSNAME, lib, methods, NULL);
tmueller@0
   452
	return 0;
tmueller@0
   453
}