use ENSC_FIX_IOCTL() to fix dietlibc's ioctl() return values
[util-vserver.git] / util-vserver / lib / fscompat_setiattr-fscompat.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-setext2flags.hc"
24 #include "ioctl-setfilecontext.hc"
25 #include "ioctl-setxflg.hc"
26 #include "ioctl-getxflg.hc"
27
28 static inline ALWAYSINLINE int
29 vc_set_iattr_compat_fscompat(char const *filename,
30                              dev_t dev, ino_t ino, xid_t xid,
31                              uint32_t flags, uint32_t mask,
32                              mode_t const *mode)
33 {
34   int                   fd;
35   struct stat           st;
36   int                   stat_rc;
37
38   if (mode!=0) st.st_mode = *mode;
39   if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode)) return 0;
40   
41   fd = open(filename, O_RDONLY);
42   if (fd==-1) return -1;
43     
44   stat_rc = fstat(fd, &st);
45   if (stat_rc==-1) goto err;
46
47   if (st.st_dev!=dev || st.st_ino!=ino) {
48     errno = EINVAL;
49     goto err;
50   }
51
52   if ( (mask&VC_IATTR_IUNLINK) ) {
53     int const           tmp = VC_IMMUTABLE_FILE_FL|VC_IMMUTABLE_LINK_FL;
54     if (vc_X_set_ext2flags(fd,
55                            (flags&VC_IATTR_IUNLINK) ? tmp : 0,
56                            (flags&VC_IATTR_IUNLINK) ? 0   : tmp)==-1)
57       goto err;
58   }
59
60   if ( (mask&VC_IATTR_BARRIER) ) {
61     if (fchmod(fd, (flags&VC_IATTR_BARRIER) ? 0 : (st.st_mode|0500))==-1)
62       goto err;
63   }
64
65   if ( (mask&VC_IATTR_XID) &&
66        vc_X_set_filecontext(fd, xid)==-1)
67     goto err;
68
69   if ( (mask&(VC_IATTR_HIDE|VC_IATTR_WATCH)) ) {
70     long                tmp;
71     if (vc_X_get_xflg(fd, &tmp)==-1) goto err;
72
73     tmp &= ~( ((mask&VC_IATTR_HIDE)   ? 1 : 0) |
74               ((mask&VC_IATTR_WATCH) ? 2 : 0) );
75     tmp |= ( ((flags&VC_IATTR_HIDE)  ? 1 : 0) |
76              ((flags&VC_IATTR_WATCH) ? 2 : 0) );
77
78     if (vc_X_set_xflg(fd, tmp)==-1) goto err;
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 }