PDA

View Full Version : Why is setxattr failing?




info1686
Jan 27, 2009, 10:56 PM
This program has been written under Mac OS X 10.4


#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<errno.h>
#include<sys/syslimits.h>
#include<sys/xattr.h>
#include<stdlib.h>

int main(int argc,char *argv[])
{
struct stat statbuf ;
int ret ;
char buf[PATH_MAX + 1] ;
char *name = "com.apple.FinderInfo" ;
size_t size = 4096 ;
void *value ;
ssize_t count , xattr_size ;
if(argc == 3)
{
ret = lstat(argv[1] , &statbuf) ;
if(ret != 0)
{
perror("lstat failed\n");
return 0;
}
if(S_ISLNK(statbuf.st_mode))
printf("%s is a symbolic link\n",argv[1]) ;
ret = readlink(argv[1] , buf , PATH_MAX) ;
if(ret == -1)
{
perror("readlink failed\n");
return 0;
}
else
printf("target of %s = %s\n",argv[1],buf) ;

count = getxattr(argv[1],name,NULL,size,0,XATTR_NOFOLLOW) ;
if(count == -1)
printf("getxattr failed\n") ;
else
{
printf("count = %u\n",count) ;
value=malloc(count + 1) ;
xattr_size = getxattr(argv[1],name,value,count+1,0,XATTR_NOFOLLOW) ;
if(xattr_size == -1)
{
printf("getxattr failed\n");
return 0;
}
}

ret = symlink(buf , argv[2]) ;
if(ret != 0)
{
perror("symlink failed\n");
return 0;
}
else
{
printf("symbolic link created\n") ;
ret = setxattr(argv[2],name,value,size,0,XATTR_NOFOLLOW) ;
if(ret == 0)
printf("setxattr success\n");
else
perror("setxattr failed\n");
}


}
return 0;
}


when setxattr fails it gives this output :

setxattr failed
: Result too large



Sander
Jan 28, 2009, 02:48 AM
According to http://developer.apple.com/documentation/Darwin/Reference/Manpages/man2/setxattr.2.html, "some attributes have size restrictions". Perhaps you're trying to stuff too much data in such an attribute? Are you sure the FinderInfo attribute can fit 4096 bytes?

Sayer
Jan 28, 2009, 03:41 PM
The size parameter should probably be the size of the data you are actually passing in, not some arbitrary max. size.

info1686
Jan 29, 2009, 03:03 AM
in the setxattr call i changed size to xattr_size and now it works fine

Thanks for the help....