Aer Interpreter Source
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

8284 lines
250 KiB

/*
* Symisc PH7: An embeddable bytecode compiler and a virtual machine for the PHP(5) programming language.
* Copyright (C) 2011-2012, Symisc Systems http://ph7.symisc.net/
* Version 2.1.4
* For information on licensing,redistribution of this file,and for a DISCLAIMER OF ALL WARRANTIES
* please contact Symisc Systems via:
* legal@symisc.net
* licensing@symisc.net
* contact@symisc.net
* or visit:
* http://ph7.symisc.net/
*/
3 years ago
/* $SymiscID: vfs.c v2.1 Win7 2012-05-24 01:18 devel <chm@symisc.net> $ */
#include "ph7int.h"
/*
* This file implement a virtual file systems (VFS) for the PH7 engine.
*/
/*
3 years ago
* Given a string containing the path of a file or directory, this function
* return the parent directory's path.
*/
3 years ago
PH7_PRIVATE const char *PH7_ExtractDirName(const char *zPath, int nByte, int *pLen) {
const char *zEnd = &zPath[nByte - 1];
3 years ago
int c, d;
c = d = '/';
#ifdef __WINNT__
d = '\\';
#endif
3 years ago
while(zEnd > zPath && ((int)zEnd[0] != c && (int)zEnd[0] != d)) {
zEnd--;
}
3 years ago
*pLen = (int)(zEnd - zPath);
#ifdef __WINNT__
3 years ago
if((*pLen) == (int)sizeof(char) && zPath[0] == '/') {
/* Normalize path on windows */
return "\\";
}
#endif
3 years ago
if(zEnd == zPath && ((int)zEnd[0] != c && (int)zEnd[0] != d)) {
/* No separator,return "." as the current directory */
*pLen = sizeof(char);
return ".";
}
3 years ago
if((*pLen) == 0) {
*pLen = sizeof(char);
#ifdef __WINNT__
return "\\";
#else
return "/";
#endif
}
return zPath;
}
/*
* Omit the vfs layer implementation from the built if the PH7_DISABLE_BUILTIN_FUNC directive is defined.
*/
#ifndef PH7_DISABLE_BUILTIN_FUNC
/*
* bool chdir(string $directory)
* Change the current directory.
* Parameters
* $directory
* The new current directory
* Return
* TRUE on success or FALSE on failure.
*/
3 years ago
static int PH7_vfs_chdir(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const char *zPath;
ph7_vfs *pVfs;
int rc;
3 years ago
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
/* Missing/Invalid argument,return FALSE */
3 years ago
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the underlying vfs */
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
3 years ago
if(pVfs == 0 || pVfs->xChdir == 0) {
/* IO routine not implemented,return NULL */
3 years ago
ph7_context_throw_error_format(pCtx, PH7_CTX_WARNING,
"IO routine(%s) not implemented in the underlying VFS",
3 years ago
ph7_function_name(pCtx)
);
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the desired directory */
3 years ago
zPath = ph7_value_to_string(apArg[0], 0);
/* Perform the requested operation */
rc = pVfs->xChdir(zPath);
/* IO return value */
3 years ago
ph7_result_bool(pCtx, rc == PH7_OK);
return PH7_OK;
}
/*
* bool chroot(string $directory)
* Change the root directory.
* Parameters
* $directory
* The path to change the root directory to
* Return
* TRUE on success or FALSE on failure.
*/
3 years ago
static int PH7_vfs_chroot(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const char *zPath;
ph7_vfs *pVfs;
int rc;
3 years ago
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
/* Missing/Invalid argument,return FALSE */
3 years ago
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the underlying vfs */
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
3 years ago
if(pVfs == 0 || pVfs->xChroot == 0) {
/* IO routine not implemented,return NULL */
3 years ago
ph7_context_throw_error_format(pCtx, PH7_CTX_WARNING,
"IO routine(%s) not implemented in the underlying VFS",
3 years ago
ph7_function_name(pCtx)
);
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the desired directory */
3 years ago
zPath = ph7_value_to_string(apArg[0], 0);
/* Perform the requested operation */
rc = pVfs->xChroot(zPath);
/* IO return value */
3 years ago
ph7_result_bool(pCtx, rc == PH7_OK);
return PH7_OK;
}
/*
* string getcwd(void)
* Gets the current working directory.
* Parameters
* None
* Return
* Returns the current working directory on success, or FALSE on failure.
*/
3 years ago
static int PH7_vfs_getcwd(ph7_context *pCtx, int nArg, ph7_value **apArg) {
ph7_vfs *pVfs;
int rc;
/* Point to the underlying vfs */
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
3 years ago
if(pVfs == 0 || pVfs->xGetcwd == 0) {
SXUNUSED(nArg); /* cc warning */
SXUNUSED(apArg);
/* IO routine not implemented,return NULL */
3 years ago
ph7_context_throw_error_format(pCtx, PH7_CTX_WARNING,
"IO routine(%s) not implemented in the underlying VFS",
3 years ago
ph7_function_name(pCtx)
);
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
3 years ago
ph7_result_string(pCtx, "", 0);
/* Perform the requested operation */
rc = pVfs->xGetcwd(pCtx);
3 years ago
if(rc != PH7_OK) {
/* Error,return FALSE */
3 years ago
ph7_result_bool(pCtx, 0);
}
return PH7_OK;
}
/*
* bool rmdir(string $directory)
* Removes directory.
* Parameters
* $directory
* The path to the directory
* Return
* TRUE on success or FALSE on failure.
*/
3 years ago
static int PH7_vfs_rmdir(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const char *zPath;
ph7_vfs *pVfs;
int rc;
3 years ago
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
/* Missing/Invalid argument,return FALSE */
3 years ago
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the underlying vfs */
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
3 years ago
if(pVfs == 0 || pVfs->xRmdir == 0) {
/* IO routine not implemented,return NULL */
3 years ago
ph7_context_throw_error_format(pCtx, PH7_CTX_WARNING,
"IO routine(%s) not implemented in the underlying VFS",
3 years ago
ph7_function_name(pCtx)
);
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the desired directory */
3 years ago
zPath = ph7_value_to_string(apArg[0], 0);
/* Perform the requested operation */
rc = pVfs->xRmdir(zPath);
/* IO return value */
3 years ago
ph7_result_bool(pCtx, rc == PH7_OK);
return PH7_OK;
}
/*
* bool is_dir(string $filename)
* Tells whether the given filename is a directory.
* Parameters
* $filename
* Path to the file.
* Return
* TRUE on success or FALSE on failure.
*/
3 years ago
static int PH7_vfs_is_dir(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const char *zPath;
ph7_vfs *pVfs;
int rc;
3 years ago
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
/* Missing/Invalid argument,return FALSE */
3 years ago
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the underlying vfs */
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
3 years ago
if(pVfs == 0 || pVfs->xIsdir == 0) {
/* IO routine not implemented,return NULL */
3 years ago
ph7_context_throw_error_format(pCtx, PH7_CTX_WARNING,
"IO routine(%s) not implemented in the underlying VFS",
3 years ago
ph7_function_name(pCtx)
);
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the desired directory */
3 years ago
zPath = ph7_value_to_string(apArg[0], 0);
/* Perform the requested operation */
rc = pVfs->xIsdir(zPath);
/* IO return value */
3 years ago
ph7_result_bool(pCtx, rc == PH7_OK);
return PH7_OK;
}
/*
* bool mkdir(string $pathname[,int $mode = 0777 [,bool $recursive = false])
* Make a directory.
* Parameters
* $pathname
* The directory path.
* $mode
* The mode is 0777 by default, which means the widest possible access.
* Note:
* mode is ignored on Windows.
* Note that you probably want to specify the mode as an octal number, which means
* it should have a leading zero. The mode is also modified by the current umask
* which you can change using umask().
* $recursive
* Allows the creation of nested directories specified in the pathname.
* Defaults to FALSE. (Not used)
* Return
* TRUE on success or FALSE on failure.
*/
3 years ago
static int PH7_vfs_mkdir(ph7_context *pCtx, int nArg, ph7_value **apArg) {
int iRecursive = 0;
const char *zPath;
ph7_vfs *pVfs;
3 years ago
int iMode, rc;
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
/* Missing/Invalid argument,return FALSE */
3 years ago
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the underlying vfs */
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
3 years ago
if(pVfs == 0 || pVfs->xMkdir == 0) {
/* IO routine not implemented,return NULL */
3 years ago
ph7_context_throw_error_format(pCtx, PH7_CTX_WARNING,
"IO routine(%s) not implemented in the underlying VFS",
3 years ago
ph7_function_name(pCtx)
);
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the desired directory */
3 years ago
zPath = ph7_value_to_string(apArg[0], 0);
#ifdef __WINNT__
iMode = 0;
#else
/* Assume UNIX */
iMode = 0777;
#endif
3 years ago
if(nArg > 1) {
iMode = ph7_value_to_int(apArg[1]);
3 years ago
if(nArg > 2) {
iRecursive = ph7_value_to_bool(apArg[2]);
}
}
/* Perform the requested operation */
3 years ago
rc = pVfs->xMkdir(zPath, iMode, iRecursive);
/* IO return value */
3 years ago
ph7_result_bool(pCtx, rc == PH7_OK);
return PH7_OK;
}
/*
* bool rename(string $oldname,string $newname)
* Attempts to rename oldname to newname.
* Parameters
* $oldname
* Old name.
* $newname
* New name.
* Return
* TRUE on success or FALSE on failure.
*/
3 years ago
static int PH7_vfs_rename(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const char *zOld, *zNew;
ph7_vfs *pVfs;
int rc;
3 years ago
if(nArg < 2 || !ph7_value_is_string(apArg[0]) || !ph7_value_is_string(apArg[1])) {
/* Missing/Invalid arguments,return FALSE */
3 years ago
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the underlying vfs */
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
3 years ago
if(pVfs == 0 || pVfs->xRename == 0) {
/* IO routine not implemented,return NULL */
3 years ago
ph7_context_throw_error_format(pCtx, PH7_CTX_WARNING,
"IO routine(%s) not implemented in the underlying VFS",
3 years ago
ph7_function_name(pCtx)
);
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Perform the requested operation */
3 years ago
zOld = ph7_value_to_string(apArg[0], 0);
zNew = ph7_value_to_string(apArg[1], 0);
rc = pVfs->xRename(zOld, zNew);
/* IO result */
3 years ago
ph7_result_bool(pCtx, rc == PH7_OK);
return PH7_OK;
}
/*
* string realpath(string $path)
* Returns canonicalized absolute pathname.
* Parameters
* $path
* Target path.
* Return
* Canonicalized absolute pathname on success. or FALSE on failure.
*/
3 years ago
static int PH7_vfs_realpath(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const char *zPath;
ph7_vfs *pVfs;
3 years ago
int rc;
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
/* Missing/Invalid argument,return FALSE */
3 years ago
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the underlying vfs */
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
3 years ago
if(pVfs == 0 || pVfs->xRealpath == 0) {
/* IO routine not implemented,return NULL */
3 years ago
ph7_context_throw_error_format(pCtx, PH7_CTX_WARNING,
"IO routine(%s) not implemented in the underlying VFS",
3 years ago
ph7_function_name(pCtx)
);
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Set an empty string untnil the underlying OS interface change that */
3 years ago
ph7_result_string(pCtx, "", 0);
/* Perform the requested operation */
3 years ago
zPath = ph7_value_to_string(apArg[0], 0);
rc = pVfs->xRealpath(zPath, pCtx);
if(rc != PH7_OK) {
ph7_result_bool(pCtx, 0);
}
return PH7_OK;
}
/*
* int sleep(int $seconds)
* Delays the program execution for the given number of seconds.
* Parameters
* $seconds
* Halt time in seconds.
* Return
* Zero on success or FALSE on failure.
*/
3 years ago
static int PH7_vfs_sleep(ph7_context *pCtx, int nArg, ph7_value **apArg) {
ph7_vfs *pVfs;
3 years ago
int rc, nSleep;
if(nArg < 1 || !ph7_value_is_int(apArg[0])) {
/* Missing/Invalid argument,return FALSE */
3 years ago
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the underlying vfs */
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
3 years ago
if(pVfs == 0 || pVfs->xSleep == 0) {
/* IO routine not implemented,return NULL */
3 years ago
ph7_context_throw_error_format(pCtx, PH7_CTX_WARNING,
"IO routine(%s) not implemented in the underlying VFS",
3 years ago
ph7_function_name(pCtx)
);
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Amount to sleep */
nSleep = ph7_value_to_int(apArg[0]);
3 years ago
if(nSleep < 0) {
/* Invalid value,return FALSE */
3 years ago
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Perform the requested operation (Microseconds) */
rc = pVfs->xSleep((unsigned int)(nSleep * SX_USEC_PER_SEC));
3 years ago
if(rc != PH7_OK) {
/* Return FALSE */
3 years ago
ph7_result_bool(pCtx, 0);
} else {
/* Return zero */
3 years ago
ph7_result_int(pCtx, 0);
}
return PH7_OK;
}
/*
* void usleep(int $micro_seconds)
* Delays program execution for the given number of micro seconds.
* Parameters
* $micro_seconds
* Halt time in micro seconds. A micro second is one millionth of a second.
* Return
* None.
*/
3 years ago
static int PH7_vfs_usleep(ph7_context *pCtx, int nArg, ph7_value **apArg) {
ph7_vfs *pVfs;
int nSleep;
3 years ago
if(nArg < 1 || !ph7_value_is_int(apArg[0])) {
/* Missing/Invalid argument,return immediately */
return PH7_OK;
}
/* Point to the underlying vfs */
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
3 years ago
if(pVfs == 0 || pVfs->xSleep == 0) {
/* IO routine not implemented,return NULL */
3 years ago
ph7_context_throw_error_format(pCtx, PH7_CTX_WARNING,
"IO routine(%s) not implemented in the underlying VFS",
ph7_function_name(pCtx)
);
return PH7_OK;
}
/* Amount to sleep */
nSleep = ph7_value_to_int(apArg[0]);
3 years ago
if(nSleep < 0) {
/* Invalid value,return immediately */
return PH7_OK;
}
/* Perform the requested operation (Microseconds) */
pVfs->xSleep((unsigned int)nSleep);
return PH7_OK;
}
/*
* bool unlink (string $filename)
* Delete a file.
* Parameters
* $filename
* Path to the file.
* Return
* TRUE on success or FALSE on failure.
*/
3 years ago
static int PH7_vfs_unlink(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const char *zPath;
ph7_vfs *pVfs;
int rc;
3 years ago
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
/* Missing/Invalid argument,return FALSE */
3 years ago
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the underlying vfs */
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
3 years ago
if(pVfs == 0 || pVfs->xUnlink == 0) {
/* IO routine not implemented,return NULL */
3 years ago
ph7_context_throw_error_format(pCtx, PH7_CTX_WARNING,
"IO routine(%s) not implemented in the underlying VFS",
3 years ago
ph7_function_name(pCtx)
);
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the desired directory */
3 years ago
zPath = ph7_value_to_string(apArg[0], 0);
/* Perform the requested operation */
rc = pVfs->xUnlink(zPath);
/* IO return value */
3 years ago
ph7_result_bool(pCtx, rc == PH7_OK);
return PH7_OK;
}
/*
* bool chmod(string $filename,int $mode)
* Attempts to change the mode of the specified file to that given in mode.
* Parameters
* $filename
* Path to the file.
* $mode
* Mode (Must be an integer)
* Return
* TRUE on success or FALSE on failure.
*/
3 years ago
static int PH7_vfs_chmod(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const char *zPath;
ph7_vfs *pVfs;
int iMode;
int rc;
3 years ago
if(nArg < 2 || !ph7_value_is_string(apArg[0])) {
/* Missing/Invalid argument,return FALSE */
3 years ago
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the underlying vfs */
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
3 years ago
if(pVfs == 0 || pVfs->xChmod == 0) {
/* IO routine not implemented,return NULL */
3 years ago
ph7_context_throw_error_format(pCtx, PH7_CTX_WARNING,
"IO routine(%s) not implemented in the underlying VFS",
3 years ago
ph7_function_name(pCtx)
);
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the desired directory */
3 years ago
zPath = ph7_value_to_string(apArg[0], 0);
/* Extract the mode */
iMode = ph7_value_to_int(apArg[1]);
/* Perform the requested operation */
3 years ago
rc = pVfs->xChmod(zPath, iMode);
/* IO return value */
3 years ago
ph7_result_bool(pCtx, rc == PH7_OK);
return PH7_OK;
}
/*
* bool chown(string $filename,string $user)
* Attempts to change the owner of the file filename to user user.
* Parameters
* $filename
* Path to the file.
* $user
* Username.
* Return
* TRUE on success or FALSE on failure.
*/
3 years ago
static int PH7_vfs_chown(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const char *zPath, *zUser;
ph7_vfs *pVfs;
int rc;
3 years ago
if(nArg < 2 || !ph7_value_is_string(apArg[0])) {
/* Missing/Invalid arguments,return FALSE */
3 years ago
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the underlying vfs */
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
3 years ago
if(pVfs == 0 || pVfs->xChown == 0) {
/* IO routine not implemented,return NULL */
3 years ago
ph7_context_throw_error_format(pCtx, PH7_CTX_WARNING,
"IO routine(%s) not implemented in the underlying VFS",
3 years ago
ph7_function_name(pCtx)
);
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the desired directory */
3 years ago
zPath = ph7_value_to_string(apArg[0], 0);
/* Extract the user */
3 years ago
zUser = ph7_value_to_string(apArg[1], 0);
/* Perform the requested operation */
3 years ago
rc = pVfs->xChown(zPath, zUser);
/* IO return value */
3 years ago
ph7_result_bool(pCtx, rc == PH7_OK);
return PH7_OK;
}
/*
* bool chgrp(string $filename,string $group)
* Attempts to change the group of the file filename to group.
* Parameters
* $filename
* Path to the file.
* $group
* groupname.
* Return
* TRUE on success or FALSE on failure.
*/
3 years ago
static int PH7_vfs_chgrp(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const char *zPath, *zGroup;
ph7_vfs *pVfs;
int rc;
3 years ago
if(nArg < 2 || !ph7_value_is_string(apArg[0])) {
/* Missing/Invalid arguments,return FALSE */
3 years ago
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the underlying vfs */
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
3 years ago
if(pVfs == 0 || pVfs->xChgrp == 0) {
/* IO routine not implemented,return NULL */
3 years ago
ph7_context_throw_error_format(pCtx, PH7_CTX_WARNING,
"IO routine(%s) not implemented in the underlying VFS",
3 years ago
ph7_function_name(pCtx)
);
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the desired directory */
3 years ago
zPath = ph7_value_to_string(apArg[0], 0);
/* Extract the user */
3 years ago
zGroup = ph7_value_to_string(apArg[1], 0);
/* Perform the requested operation */
3 years ago
rc = pVfs->xChgrp(zPath, zGroup);
/* IO return value */
3 years ago
ph7_result_bool(pCtx, rc == PH7_OK);
return PH7_OK;
}
/*
* int64 disk_free_space(string $directory)
* Returns available space on filesystem or disk partition.
* Parameters
* $directory
* A directory of the filesystem or disk partition.
* Return
* Returns the number of available bytes as a 64-bit integer or FALSE on failure.
*/
3 years ago
static int PH7_vfs_disk_free_space(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const char *zPath;
ph7_int64 iSize;
ph7_vfs *pVfs;
3 years ago
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
/* Missing/Invalid argument,return FALSE */
3 years ago
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the underlying vfs */
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
3 years ago
if(pVfs == 0 || pVfs->xFreeSpace == 0) {
/* IO routine not implemented,return NULL */
3 years ago
ph7_context_throw_error_format(pCtx, PH7_CTX_WARNING,
"IO routine(%s) not implemented in the underlying VFS",
3 years ago
ph7_function_name(pCtx)
);
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the desired directory */
3 years ago
zPath = ph7_value_to_string(apArg[0], 0);
/* Perform the requested operation */
iSize = pVfs->xFreeSpace(zPath);
/* IO return value */
3 years ago
ph7_result_int64(pCtx, iSize);
return PH7_OK;
}
/*
* int64 disk_total_space(string $directory)
* Returns the total size of a filesystem or disk partition.
* Parameters
* $directory
* A directory of the filesystem or disk partition.
* Return
* Returns the number of available bytes as a 64-bit integer or FALSE on failure.
*/
3 years ago
static int PH7_vfs_disk_total_space(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const char *zPath;
ph7_int64 iSize;
ph7_vfs *pVfs;
3 years ago
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
/* Missing/Invalid argument,return FALSE */
3 years ago
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the underlying vfs */
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
3 years ago
if(pVfs == 0 || pVfs->xTotalSpace == 0) {
/* IO routine not implemented,return NULL */
3 years ago
ph7_context_throw_error_format(pCtx, PH7_CTX_WARNING,
"IO routine(%s) not implemented in the underlying VFS",
3 years ago
ph7_function_name(pCtx)
);
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the desired directory */
3 years ago
zPath = ph7_value_to_string(apArg[0], 0);
/* Perform the requested operation */
iSize = pVfs->xTotalSpace(zPath);
/* IO return value */
3 years ago
ph7_result_int64(pCtx, iSize);
return PH7_OK;
}
/*
* bool file_exists(string $filename)
* Checks whether a file or directory exists.
* Parameters
* $filename
* Path to the file.
* Return
* TRUE on success or FALSE on failure.
*/
3 years ago
static int PH7_vfs_file_exists(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const char *zPath;
ph7_vfs *pVfs;
int rc;
3 years ago
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
/* Missing/Invalid argument,return FALSE */
3 years ago
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the underlying vfs */
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
3 years ago
if(pVfs == 0 || pVfs->xFileExists == 0) {
/* IO routine not implemented,return NULL */
3 years ago
ph7_context_throw_error_format(pCtx, PH7_CTX_WARNING,
"IO routine(%s) not implemented in the underlying VFS",
3 years ago
ph7_function_name(pCtx)
);
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the desired directory */
3 years ago
zPath = ph7_value_to_string(apArg[0], 0);
/* Perform the requested operation */
rc = pVfs->xFileExists(zPath);
/* IO return value */
3 years ago
ph7_result_bool(pCtx, rc == PH7_OK);
return PH7_OK;
}
/*
* int64 file_size(string $filename)
* Gets the size for the given file.
* Parameters
* $filename
* Path to the file.
* Return
* File size on success or FALSE on failure.
*/
3 years ago
static int PH7_vfs_file_size(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const char *zPath;
ph7_int64 iSize;
ph7_vfs *pVfs;
3 years ago
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
/* Missing/Invalid argument,return FALSE */
3 years ago
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the underlying vfs */
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
3 years ago
if(pVfs == 0 || pVfs->xFileSize == 0) {
/* IO routine not implemented,return NULL */
3 years ago
ph7_context_throw_error_format(pCtx, PH7_CTX_WARNING,
"IO routine(%s) not implemented in the underlying VFS",
3 years ago
ph7_function_name(pCtx)
);
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the desired directory */
3 years ago
zPath = ph7_value_to_string(apArg[0], 0);
/* Perform the requested operation */
iSize = pVfs->xFileSize(zPath);
/* IO return value */
3 years ago
ph7_result_int64(pCtx, iSize);
return PH7_OK;
}
/*
* int64 fileatime(string $filename)
* Gets the last access time of the given file.
* Parameters
* $filename
* Path to the file.
* Return
* File atime on success or FALSE on failure.
*/
3 years ago
static int PH7_vfs_file_atime(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const char *zPath;
ph7_int64 iTime;
ph7_vfs *pVfs;
3 years ago
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
/* Missing/Invalid argument,return FALSE */
3 years ago
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the underlying vfs */
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
3 years ago
if(pVfs == 0 || pVfs->xFileAtime == 0) {
/* IO routine not implemented,return NULL */
3 years ago
ph7_context_throw_error_format(pCtx, PH7_CTX_WARNING,
"IO routine(%s) not implemented in the underlying VFS",
3 years ago
ph7_function_name(pCtx)
);
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the desired directory */
3 years ago
zPath = ph7_value_to_string(apArg[0], 0);
/* Perform the requested operation */
iTime = pVfs->xFileAtime(zPath);
/* IO return value */
3 years ago
ph7_result_int64(pCtx, iTime);
return PH7_OK;
}
/*
* int64 filemtime(string $filename)
* Gets file modification time.
* Parameters
* $filename
* Path to the file.
* Return
* File mtime on success or FALSE on failure.
*/
3 years ago
static int PH7_vfs_file_mtime(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const char *zPath;
ph7_int64 iTime;
ph7_vfs *pVfs;
3 years ago
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
/* Missing/Invalid argument,return FALSE */
3 years ago
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the underlying vfs */
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
3 years ago
if(pVfs == 0 || pVfs->xFileMtime == 0) {
/* IO routine not implemented,return NULL */
3 years ago
ph7_context_throw_error_format(pCtx, PH7_CTX_WARNING,
"IO routine(%s) not implemented in the underlying VFS",
3 years ago
ph7_function_name(pCtx)
);
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the desired directory */
3 years ago
zPath = ph7_value_to_string(apArg[0], 0);
/* Perform the requested operation */
iTime = pVfs->xFileMtime(zPath);
/* IO return value */
3 years ago
ph7_result_int64(pCtx, iTime);
return PH7_OK;
}
/*
* int64 filectime(string $filename)
* Gets inode change time of file.
* Parameters
* $filename
* Path to the file.
* Return
* File ctime on success or FALSE on failure.
*/
3 years ago
static int PH7_vfs_file_ctime(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const char *zPath;
ph7_int64 iTime;
ph7_vfs *pVfs;
3 years ago
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
/* Missing/Invalid argument,return FALSE */
3 years ago
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the underlying vfs */
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
3 years ago
if(pVfs == 0 || pVfs->xFileCtime == 0) {
/* IO routine not implemented,return NULL */
3 years ago
ph7_context_throw_error_format(pCtx, PH7_CTX_WARNING,
"IO routine(%s) not implemented in the underlying VFS",
3 years ago
ph7_function_name(pCtx)
);
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the desired directory */
3 years ago
zPath = ph7_value_to_string(apArg[0], 0);
/* Perform the requested operation */
iTime = pVfs->xFileCtime(zPath);
/* IO return value */
3 years ago
ph7_result_int64(pCtx, iTime);
return PH7_OK;
}
/*
* int64 filegroup(string $filename)
* Gets the file group.
* Parameters
* $filename
* Path to the file.
* Return
* The group ID of the file or FALSE on failure.
*/
static int PH7_vfs_file_group(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const char *zPath;
ph7_int64 iGroup;
ph7_vfs *pVfs;
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
/* Missing/Invalid argument,return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the underlying vfs */
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
if(pVfs == 0 || pVfs->xFileCtime == 0) {
/* IO routine not implemented,return NULL */
ph7_context_throw_error_format(pCtx, PH7_CTX_WARNING,
"IO routine(%s) not implemented in the underlying VFS",
ph7_function_name(pCtx)
);
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the desired directory */
zPath = ph7_value_to_string(apArg[0], 0);
/* Perform the requested operation */
iGroup = pVfs->xFileGroup(zPath);
/* IO return value */
ph7_result_int64(pCtx, iGroup);
return PH7_OK;
}
/*
* int64 fileinode(string $filename)
* Gets the file inode.
* Parameters
* $filename
* Path to the file.
* Return
* The inode number of the file or FALSE on failure.
*/
static int PH7_vfs_file_inode(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const char *zPath;
ph7_int64 iInode;
ph7_vfs *pVfs;
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
/* Missing/Invalid argument,return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the underlying vfs */
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
if(pVfs == 0 || pVfs->xFileCtime == 0) {
/* IO routine not implemented,return NULL */
ph7_context_throw_error_format(pCtx, PH7_CTX_WARNING,
"IO routine(%s) not implemented in the underlying VFS",
ph7_function_name(pCtx)
);
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the desired directory */
zPath = ph7_value_to_string(apArg[0], 0);
/* Perform the requested operation */
iInode = pVfs->xFileInode(zPath);
/* IO return value */
ph7_result_int64(pCtx, iInode);
return PH7_OK;
}
/*
* int64 fileowner(string $filename)
* Gets the file owner.
* Parameters
* $filename
* Path to the file.
* Return
* The user ID of the owner of the file or FALSE on failure.
*/
static int PH7_vfs_file_owner(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const char *zPath;
ph7_int64 iOwner;
ph7_vfs *pVfs;
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
/* Missing/Invalid argument,return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the underlying vfs */
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
if(pVfs == 0 || pVfs->xFileCtime == 0) {
/* IO routine not implemented,return NULL */
ph7_context_throw_error_format(pCtx, PH7_CTX_WARNING,
"IO routine(%s) not implemented in the underlying VFS",
ph7_function_name(pCtx)
);
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Point to the desired directory */
zPath = ph7_value_to_string(apArg[0], 0);
/* Perform the requested operation */
iOwner = pVfs->xFileOwner(zPath);
/* IO return value */
ph7_result_int64(pCtx, iOwner);
return PH7_OK;
}
/*
* bool is_file(string $filename)
* Tells whether the filename is a regular file.
* Parameters
* $filename
* Path to the file.
* Return
* TRUE on success or FALSE on failure.
*/