Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

remove, list changes, get,

  • Loading branch information...
commit 2d7c569a1c31d940293c5450a456c699bb92e46f 1 parent abbf340
@cinterloper cinterloper authored
Showing with 100 additions and 14 deletions.
  1. +8 −4 README.md
  2. +91 −8 src/node_xattr.cc
  3. +1 −1  test/test.js
  4. +0 −1  test/utils.js
View
12 README.md
@@ -2,10 +2,6 @@
node-xattr is a native module wrapping xattr to read and set extended attributes on files.
-## Install
-
-npm install xattr
-
## Build
node-waf configure && node-waf build && node-waf install
@@ -16,11 +12,19 @@ node-waf configure && node-waf build && node-waf install
Get all the extended attributes on a file, returns obj
+ var attrs = xattr.glist("/path/to/file");
+
+Get just the extended attribute names on a file, returns array obj
+
var attrs = xattr.list("/path/to/file");
Set an extended attribute on a file. Note at thie moment you can only set string values.
xattr.set("/path/to/file", "someAttribute", "someValue");
+Remove xattr on a file, returns bool
+
+ xattr.remove("/path/to/file", "user.attribute");
+
View
99 src/node_xattr.cc
@@ -74,7 +74,7 @@ static Handle<Value> set(const Arguments& args) {
return Boolean::New(true);
}
-static Handle<Value> list(const Arguments& args) {
+static Handle<Value> GetList(const Arguments& args) {
HandleScope scope;
char list[XATTR_SIZE],value[XATTR_SIZE];
const char *filename;
@@ -94,10 +94,6 @@ static Handle<Value> list(const Arguments& args) {
// create obj for return
Handle<Object> result = Object::New();
- if (listLen<1){
- return result;
- }
-
//for each of the attrs, do getxattr and add them as key/val to the obj
for (ns=0; ns<listLen; ns+= strlen(&list[ns])+1){
#ifdef __APPLE__
@@ -105,19 +101,106 @@ static Handle<Value> list(const Arguments& args) {
#else
valueLen = getxattr(filename, &list[ns],value, XATTR_SIZE);
#endif
- //if (valueLen > 0){
+ if (valueLen > 0){
result->Set(String::New(&list[ns]),String::New(value, valueLen));
- //}
+ }
}
return result;
}
+static Handle<Value> get(const Arguments& args) {
+ HandleScope scope;
+ char *attr,value[XATTR_SIZE];
+ const char *filename;
+ const char *attribute;
+
+ ssize_t valueLen;
+ int ns;
+
+ //make sure we were passed a string
+ REQ_STR_ARG(0, s1);
+ filename= ObjectToString(s1).c_str();
+ REQ_STR_ARG(1, s2);
+ attribute= ObjectToString(s2).c_str();
+
+
+ // create obj for return
+ Handle<Object> result = Object::New();
+
+ //for each of the attrs, do getxattr and add them as key/val to the obj
+#ifdef __APPLE__
+ valueLen = getxattr(filename, attribute,value, XATTR_SIZE, 0, 0);
+#else
+ valueLen = getxattr(filename, attribute,value, XATTR_SIZE);
+#endif
+ result->Set(String::New(attribute),String::New(value));
+
+ return result;
+}
+
+static Handle<Value> SimpleList(const Arguments& args) {
+ HandleScope scope;
+ char list[XATTR_SIZE];
+ const char *filename;
+ ssize_t listLen;
+ int ns;
+
+ //make sure we were passed a string
+ REQ_STR_ARG(0, s);
+ filename= ObjectToString(s).c_str();
+
+ //get all the extended attributes on filename
+#ifdef __APPLE__
+ listLen = listxattr(filename,list,XATTR_SIZE,0);
+#else
+ listLen = listxattr(filename,list,XATTR_SIZE);
+#endif
+ // create obj for return
+ Handle<Object> result = Object::New();
+
+ //for each of the attrs, do getxattr and add them as key/val to the obj
+ int nc=0;
+ for (ns=0; ns<listLen; ns+= strlen(&list[ns])+1){
+ nc++;
+ result->Set(nc,String::New(&list[ns]));
+ }
+ return result;
+}
+static Handle<Value> remove(const Arguments& args) {
+ HandleScope scope;
+ ssize_t res;
+ int valLen;
+
+ REQ_ASCII_ARG(0,filename);
+ REQ_ASCII_ARG(1,attribute);
+#ifdef __APPLE__
+ res = removexattr(*filename, *attribute,0);
+#else
+ res = removexattr(*filename, *attribute);
+#endif
+ //printf("Setting file: %s, attribute: %s, value: %s, length: %d\n", *filename, *attribute, *val,val.length());
+
+
+ //Error
+ if (res == -1){
+ return ThrowException(Exception::TypeError( \
+ String::Concat(String::New("Error removing extended attribue: "), String::New(strerror(errno)))));
+ }
+
+ return Boolean::New(true);
+}
+
+
+
extern "C" {
void init (Handle<Object> target)
{
- NODE_SET_METHOD(target, "list", list);
+ NODE_SET_METHOD(target, "list", SimpleList);
+ NODE_SET_METHOD(target, "glist", GetList);
NODE_SET_METHOD(target, "set", set);
+ NODE_SET_METHOD(target, "get", get);
+ NODE_SET_METHOD(target, "remove", remove);
}
NODE_MODULE(xattr, init);
View
2  test/test.js
@@ -41,7 +41,7 @@ function runTest(runs){
function check() {
- var data = xattr.list(filename);
+ var data = xattr.glist(filename);
for (var i = 0; i<4; i++){
if (data["user.p"+i] != vals["v" + i]){
View
1  test/utils.js
@@ -6,7 +6,6 @@ var filename = "/tmp/xattr.test";
//var filename = "/opt/solr_index/perfTest/xattr.test";
//var filename = "/var/glusterfs/galaxy/galaxy_0/testDir/xattr.test";
//var filename = "/var/glusterfs/polyomic/polyomic_0/testDir/xattr.test";
-var filename = "/storage/testDir/xattr.test";
exports.createFile=function(index, cb){
if (index){
Please sign in to comment.
Something went wrong with that request. Please try again.