422ec805757027d3e855acf921a9f70d78000f9c
[util-vserver.git] / lib / syscall_setiattr-fscompat.hc
1 // $Id: syscall_setiattr-fscompat.hc,v 1.3 2005/07/15 18:06:27 ensc Exp $    --*- 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 #include <fcntl.h>
29
30 static inline ALWAYSINLINE int
31 vc_set_iattr_fscompat(char const *filename,
32                       xid_t xid,
33                       uint32_t flags, uint32_t mask)
34 {
35   int                   fd;
36   struct stat           st;
37   int                   stat_rc;
38
39   fd = open(filename, O_RDONLY|O_NONBLOCK);
40   if (fd==-1) return -1;
41     
42   stat_rc = fstat(fd, &st);
43   if (stat_rc==-1) goto err;
44
45   if ( (mask&VC_IATTR_IUNLINK) ) {
46     unsigned int const  tmp = VC_IMMUTABLE_FILE_FL|VC_IMMUTABLE_LINK_FL;
47     if (vc_X_set_ext2flags(fd,
48                            (flags&VC_IATTR_IUNLINK) ? tmp : 0,
49                            (flags&VC_IATTR_IUNLINK) ? 0   : tmp)==-1)
50       goto err;
51   }
52
53   if ( (mask&VC_IATTR_BARRIER) ) {
54     if ((flags&VC_IATTR_BARRIER)) {
55       if (vc_X_set_ext2flags(fd, VC_IMMUTABLE_LINK_FL, 0)==-1 ||
56           fchmod(fd, 0))
57         goto err;
58     }
59     else {
60       if (vc_X_set_ext2flags(fd, 0, VC_IMMUTABLE_LINK_FL)==-1 ||
61           fchmod(fd, 0500))
62         goto err;
63     }      
64   }
65
66   if ( (mask&VC_IATTR_XID) &&
67        vc_X_set_filecontext(fd, xid)==-1)
68     goto err;
69
70   if ( (mask&(VC_IATTR_HIDE|VC_IATTR_WATCH)) ) {
71     long                tmp;
72     if (vc_X_get_xflg(fd, &tmp)==-1) goto err;
73
74     tmp &= ~( ((mask&VC_IATTR_HIDE)   ? 1 : 0) |
75               ((mask&VC_IATTR_WATCH) ? 2 : 0) );
76     tmp |= ( ((flags&VC_IATTR_HIDE)  ? 1 : 0) |
77              ((flags&VC_IATTR_WATCH) ? 2 : 0) );
78
79     if (vc_X_set_xflg(fd, tmp)==-1) goto err;
80   }
81
82   close(fd);
83   return 0;
84   err:
85   {
86     int old_errno = errno;
87     close(fd);
88     errno = old_errno;
89     return -1;
90   }
91 }