Skip to content
This repository has been archived by the owner on Dec 28, 2022. It is now read-only.

Commit

Permalink
in Nginx.File: getAccess() and setAccess()
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-leonov committed Jun 3, 2010
1 parent 1d9c13d commit ba868ab
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.markdown
Expand Up @@ -958,6 +958,24 @@ Checks if `path` directs to a file.

Return `null` if path does not exist at all, `false` if there is something but not a file and return `true` on an existent plain file.


### getAccess(fname)

Returns an access code of `fname`. If `fname` points to inexistent entry this mthod returns `null`.

var access = Nginx.File.getAccess('db.json')
if (access != 0644)
return Nginx.ERROR


### setAccess(fname, access)

Returns an access code of `fname`. As simple as it could to be.

if (Nginx.File.setAccess('db.json', 0644) == Nginx.ERROR)
return Nginx.HTTP_INTERNAL_SERVER_ERROR


Instance properties
-------------------

Expand Down
29 changes: 29 additions & 0 deletions js/tests/file.js
Expand Up @@ -136,6 +136,35 @@ NginxTests.file = function (r)
})


t.test('access', function (t)
{
var fname = prefix + 'nginx-file-access.txt'

var file = File.open(fname)
t.ok(file, 'create')

function checkAccess (access)
{
var rc = File.setAccess(fname, access)
t.ne(rc, File.ERROR, 'setAccess()')

var rc = File.getAccess(fname)
t.ne(rc, File.ERROR, 'getAccess()')

t.eq(rc, access, 'access')
}

checkAccess(0777)
checkAccess(0765)
checkAccess(0)
checkAccess(0123)
checkAccess(0666)

var rc = File.remove(fname)
t.ne(rc, File.ERROR, 'delete')
})


t.test('private protection', function (t)
{
var file = new File()
Expand Down
76 changes: 76 additions & 0 deletions src/classes/File.c
Expand Up @@ -228,6 +228,80 @@ method_exists(JSContext *cx, JSObject *self, uintN argc, jsval *argv, jsval *rva
return JS_TRUE;
}


static JSBool
method_getAccess(JSContext *cx, JSObject *self, uintN argc, jsval *argv, jsval *rval)
{
JSString *jss_name;
const char *name;
ngx_file_info_t fi;

TRACE_STATIC_METHOD();

E(argc == 1, "Nginx.File#getAccess takes 1 mandatory argument: name:String");

jss_name = JS_ValueToString(cx, argv[0]);
if (jss_name == NULL)
{
return JS_FALSE;
}

name = JS_GetStringBytes(jss_name);
if (name == NULL)
{
return JS_FALSE;
}

ngx_log_debug1(NGX_LOG_DEBUG_HTTP, js_log(), 0, "ngx_file_info(\"%s\")", name);
if (ngx_file_info((const char *) name, &fi) == NGX_FILE_ERROR)
{
*rval = JSVAL_NULL;
return JS_TRUE;
}

*rval = INT_TO_JSVAL(ngx_file_access(&fi));
return JS_TRUE;
}


static JSBool
method_setAccess(JSContext *cx, JSObject *self, uintN argc, jsval *argv, jsval *rval)
{
ngx_uint_t access;
JSString *jss_name;
jsdouble dp;
const char *name;

TRACE_STATIC_METHOD();

E(argc == 2, "Nginx.File#setAccess takes 2 mandatory arguments: name:String and access:Number");

jss_name = JS_ValueToString(cx, argv[0]);
if (jss_name == NULL)
{
return JS_FALSE;
}

name = JS_GetStringBytes(jss_name);
if (name == NULL)
{
return JS_FALSE;
}

if (!JS_ValueToNumber(cx, argv[1], &dp))
{
return JS_FALSE;
}

access = dp;

ngx_log_debug1(NGX_LOG_DEBUG_HTTP, js_log(), 0, "ngx_change_file_access(\"%s\", %d)", name, access);
*rval = INT_TO_JSVAL(ngx_change_file_access((const char *) name, access));

return JS_TRUE;
}


static JSBool
method_write(JSContext *cx, JSObject *self, uintN argc, jsval *argv, jsval *rval)
{
Expand Down Expand Up @@ -480,6 +554,8 @@ static JSFunctionSpec static_funcs[] =
JS_FS("open", method_open, 1, 0, 0),
JS_FS("remove", method_remove, 1, 0, 0),
JS_FS("exists", method_exists, 1, 0, 0),
JS_FS("getAccess", method_getAccess, 1, 0, 0),
JS_FS("setAccess", method_setAccess, 2, 0, 0),
JS_FS_END
};

Expand Down

0 comments on commit ba868ab

Please sign in to comment.