1 /*
2  * Unsquash a squashfs filesystem.  This is a highly compressed read only
3  * filesystem.
4  *
5  * Copyright (c) 2010, 2012
6  * Phillip Lougher <phillip@squashfs.org.uk>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2,
11  * or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  *
22  * unsquashfs_xattr.c
23  */
24 
25 #include "unsquashfs.h"
26 #include "xattr.h"
27 
28 #include <sys/xattr.h>
29 
30 #define NOSPACE_MAX 10
31 
32 extern int root_process;
33 extern int user_xattrs;
34 
write_xattr(char * pathname,unsigned int xattr)35 void write_xattr(char *pathname, unsigned int xattr)
36 {
37 	unsigned int count;
38 	struct xattr_list *xattr_list;
39 	int i;
40 	static int nonsuper_error = FALSE;
41 	static int ignore_xattrs = FALSE;
42 	static int nospace_error = 0;
43 
44 	if(ignore_xattrs || xattr == SQUASHFS_INVALID_XATTR ||
45 			sBlk.s.xattr_id_table_start == SQUASHFS_INVALID_BLK)
46 		return;
47 
48 	xattr_list = get_xattr(xattr, &count, 1);
49 	if(xattr_list == NULL) {
50 		ERROR("Failed to read xattrs for file %s\n", pathname);
51 		return;
52 	}
53 
54 	for(i = 0; i < count; i++) {
55 		int prefix = xattr_list[i].type & SQUASHFS_XATTR_PREFIX_MASK;
56 
57 		if(user_xattrs && prefix != SQUASHFS_XATTR_USER)
58 			continue;
59 
60 		if(root_process || prefix == SQUASHFS_XATTR_USER) {
61 			int res = lsetxattr(pathname, xattr_list[i].full_name,
62 				xattr_list[i].value, xattr_list[i].vsize, 0);
63 
64 			if(res == -1) {
65 				if(errno == ENOTSUP) {
66 					/*
67 					 * If the destination filesystem cannot
68 					 * suppport xattrs, print error, and
69 					 * disable xattr output as this error is
70 					 * unlikely to go away, and printing
71 					 * screenfulls of the same error message
72 					 * is rather annoying
73 					 */
74 					ERROR("write_xattr: failed to write "
75 						"xattr %s for file %s because "
76 						"extended attributes are not "
77 						"supported by the destination "
78 						"filesystem\n",
79 						xattr_list[i].full_name,
80 						pathname);
81 					ERROR("Ignoring xattrs in "
82 								"filesystem\n");
83 					ERROR("To avoid this error message, "
84 						"specify -no-xattrs\n");
85 					ignore_xattrs = TRUE;
86 				} else if((errno == ENOSPC || errno == EDQUOT)
87 						&& nospace_error < NOSPACE_MAX) {
88 					/*
89 					 * Many filesystems like ext2/3/4 have
90 					 * limits on the amount of xattr
91 					 * data that can be stored per file
92 					 * (typically one block or 4K), so
93 					 * we shouldn't disable xattr ouput,
94 					 * as the error may be restriced to one
95 					 * file only.  If we get a lot of these
96 					 * then suppress the error messsage
97 					 */
98 					ERROR("write_xattr: failed to write "
99 						"xattr %s for file %s because "
100 						"no extended attribute space "
101 						"remaining (per file or "
102 						"filesystem limit)\n",
103 						xattr_list[i].full_name,
104 						pathname);
105 					if(++ nospace_error == NOSPACE_MAX)
106 						ERROR("%d of these errors "
107 							"printed, further error "
108 							"messages of this type "
109 							"are suppressed!\n",
110 							NOSPACE_MAX);
111 				} else
112 					ERROR("write_xattr: failed to write "
113 						"xattr %s for file %s because "
114 						"%s\n", xattr_list[i].full_name,
115 						pathname, strerror(errno));
116 			}
117 		} else if(nonsuper_error == FALSE) {
118 			/*
119 			 * if extract user xattrs only then
120 			 * error message is suppressed, if not
121 			 * print error, and then suppress further error
122 			 * messages to avoid possible screenfulls of the
123 			 * same error message!
124 			 */
125 			ERROR("write_xattr: could not write xattr %s "
126 					"for file %s because you're not "
127 					"superuser!\n",
128 					xattr_list[i].full_name, pathname);
129 			ERROR("write_xattr: to avoid this error message, either"
130 				" specify -user-xattrs, -no-xattrs, or run as "
131 				"superuser!\n");
132 			ERROR("Further error messages of this type are "
133 				"suppressed!\n");
134 			nonsuper_error = TRUE;
135 		}
136 	}
137 
138 	free_xattr(xattr_list, count);
139 }
140