c7204ed7c099444f33c90a6cfe3805a31962abd3
[util-vserver.git] / util-vserver / lib / fscompat_getiattr-v11.hc
1 // $Id$    --*- c -*--
2
3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 //  
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
8 //  
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //  
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "ioctl-getext2flags.hc"
24 #include "ioctl-getfilecontext.hc"
25 #include "ioctl-getxflg.hc"
26
27 static inline int
28 vc_get_iattr_compat_v11(char const *filename,
29                         dev_t dev, ino_t ino,
30                         xid_t    * /*@null@*/ xid,
31                         uint32_t * /*@null@*/ flags,
32                         uint32_t * mask)
33 {
34   int                   fd = open(filename, O_RDONLY);
35   struct stat           st;
36   int                   stat_rc;
37   int                   old_mask = *mask;
38     
39   if (fd==-1) return -1;
40   *mask = 0;
41
42   stat_rc = fstat(fd, &st);
43   if (stat_rc==-1) goto err;
44
45   if (st.st_dev!=dev || st.st_ino!=ino) {
46     errno = EINVAL;
47     goto err;
48   }
49     
50   if ( old_mask&VC_IATTR_XID ) {
51     *xid = vc_X_get_filecontext(fd);
52     if (*xid!=VC_NOCTX) *mask |= VC_IATTR_XID;
53   }
54
55   if ( old_mask&VC_IATTR_IUNLINK ) {
56     long                tmp;
57     int         rc = vc_X_get_ext2flags(fd, &tmp);
58
59     if (rc!=-1) {
60       *mask |= VC_IATTR_IUNLINK;
61       if (tmp & (VC_IMMUTABLE_FILE_FL|VC_IMMUTABLE_LINK_FL))
62         *flags |= VC_IATTR_IUNLINK;
63     }
64   }
65
66   if ( (old_mask&VC_IATTR_BARRIER) && S_ISDIR(st.st_mode)) {
67     *mask  |= VC_IATTR_BARRIER;
68     if ((st.st_mode&0777) == 0) *flags |= VC_IATTR_BARRIER;
69   }
70
71   if ( (old_mask&(VC_IATTR_WATCH|VC_IATTR_HIDE)) ){
72     long                tmp;
73     int         rc = vc_X_get_xflg(fd, &tmp);
74     if (rc!=-1) {
75       *mask |= (VC_IATTR_WATCH|VC_IATTR_HIDE);
76       if (tmp&1) *flags |= VC_IATTR_HIDE;
77       if (tmp&2) *flags |= VC_IATTR_WATCH;
78     }
79   }
80
81   close(fd);
82   return 0;
83   err:
84   {
85     int old_errno = errno;
86     close(fd);
87     errno = old_errno;
88     return -1;
89   } 
90 }