1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Why is setxattr failing?

Discussion in 'Mac Programming' started by info1686, Jan 27, 2009.

  1. macrumors newbie

    #1
    This program has been written under Mac OS X 10.4

    Code:
    #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
     
  2. macrumors 6502

    #2
  3. macrumors 6502a

    Sayer

    #3
    The size parameter should probably be the size of the data you are actually passing in, not some arbitrary max. size.
     
  4. macrumors newbie

    #4
    in the setxattr call i changed size to xattr_size and now it works fine

    Thanks for the help....
     

Share This Page