Thread: Why is setxattr failing?

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    22

    Why is setxattr failing?

    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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Does the manual page say anything about "Result too large" ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    your value is alloced as count+1 byte

    when you pass value to setattr I suppose the next parameter should specify the actual size of the value and not some artificial constant 4096 that has nothing to do with the value
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    22
    i replaced value with xattr_size and its working now

    thanks.......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 09-23-2008, 03:32 AM
  2. pointer comparison failing
    By Bleech in forum C Programming
    Replies: 4
    Last Post: 08-11-2007, 06:33 PM
  3. CreateDevice failing
    By MadCow257 in forum Game Programming
    Replies: 6
    Last Post: 03-14-2006, 09:03 PM
  4. My library is failing and I cannot figure out why...
    By DerelictDream in forum C++ Programming
    Replies: 7
    Last Post: 08-15-2005, 03:47 PM
  5. initializes all components of failing to false
    By romeoz in forum C++ Programming
    Replies: 21
    Last Post: 08-01-2003, 09:30 PM