7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 * Copyright 2012 Joyent, Inc. All rights reserved.
25 *
26 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
27 */
28
29 /*
30 * lofiadm - administer lofi(7d). Very simple, add and remove file<->device
31 * associations, and display status. All the ioctls are private between
32 * lofi and lofiadm, and so are very simple - device information is
33 * communicated via a minor number.
34 */
35
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/lofi.h>
39 #include <sys/stat.h>
40 #include <sys/sysmacros.h>
41 #include <netinet/in.h>
42 #include <stdio.h>
43 #include <fcntl.h>
44 #include <locale.h>
45 #include <string.h>
46 #include <strings.h>
133 static int lzma_compress(void *src, size_t srclen, void *dst,
134 size_t *destlen, int level);
135
136 lofi_compress_info_t lofi_compress_table[LOFI_COMPRESS_FUNCTIONS] = {
137 {NULL, gzip_compress, 6, "gzip"}, /* default */
138 {NULL, gzip_compress, 6, "gzip-6"},
139 {NULL, gzip_compress, 9, "gzip-9"},
140 {NULL, lzma_compress, 0, "lzma"}
141 };
142
143 /* For displaying lofi mappings */
144 #define FORMAT "%-20s %-30s %s\n"
145
146 #define COMPRESS_ALGORITHM "gzip"
147 #define COMPRESS_THRESHOLD 2048
148 #define SEGSIZE 131072
149 #define BLOCK_SIZE 512
150 #define KILOBYTE 1024
151 #define MEGABYTE (KILOBYTE * KILOBYTE)
152 #define GIGABYTE (KILOBYTE * MEGABYTE)
153 #define LIBZ "libz.so"
154
155 static void
156 usage(const char *pname)
157 {
158 (void) fprintf(stderr, gettext(USAGE), pname, pname, pname,
159 pname, pname, pname, pname, pname, pname, pname);
160 exit(E_USAGE);
161 }
162
163 static int
164 gzip_compress(void *src, size_t srclen, void *dst, size_t *dstlen, int level)
165 {
166 static int (*compress2p)(void *, ulong_t *, void *, size_t, int) = NULL;
167 void *libz_hdl = NULL;
168
169 /*
170 * The first time we are called, attempt to dlopen()
171 * libz.so and get a pointer to the compress2() function
172 */
173 if (compress2p == NULL) {
174 if ((libz_hdl = openlib(LIBZ)) == NULL)
175 die(gettext("could not find %s. "
176 "gzip compression unavailable\n"), LIBZ);
177
178 if ((compress2p =
179 (int (*)(void *, ulong_t *, void *, size_t, int))
180 dlsym(libz_hdl, "compress2")) == NULL) {
181 closelib();
182 die(gettext("could not find the correct %s. "
183 "gzip compression unavailable\n"), LIBZ);
184 }
185 }
186
187 if ((*compress2p)(dst, (ulong_t *)dstlen, src, srclen, level) != 0)
188 return (-1);
189 return (0);
190 }
191
|
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 * Copyright 2012 Joyent, Inc. All rights reserved.
25 *
26 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
27 * Copyright (c) 2014 Gary Mills
28 */
29
30 /*
31 * lofiadm - administer lofi(7d). Very simple, add and remove file<->device
32 * associations, and display status. All the ioctls are private between
33 * lofi and lofiadm, and so are very simple - device information is
34 * communicated via a minor number.
35 */
36
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/lofi.h>
40 #include <sys/stat.h>
41 #include <sys/sysmacros.h>
42 #include <netinet/in.h>
43 #include <stdio.h>
44 #include <fcntl.h>
45 #include <locale.h>
46 #include <string.h>
47 #include <strings.h>
134 static int lzma_compress(void *src, size_t srclen, void *dst,
135 size_t *destlen, int level);
136
137 lofi_compress_info_t lofi_compress_table[LOFI_COMPRESS_FUNCTIONS] = {
138 {NULL, gzip_compress, 6, "gzip"}, /* default */
139 {NULL, gzip_compress, 6, "gzip-6"},
140 {NULL, gzip_compress, 9, "gzip-9"},
141 {NULL, lzma_compress, 0, "lzma"}
142 };
143
144 /* For displaying lofi mappings */
145 #define FORMAT "%-20s %-30s %s\n"
146
147 #define COMPRESS_ALGORITHM "gzip"
148 #define COMPRESS_THRESHOLD 2048
149 #define SEGSIZE 131072
150 #define BLOCK_SIZE 512
151 #define KILOBYTE 1024
152 #define MEGABYTE (KILOBYTE * KILOBYTE)
153 #define GIGABYTE (KILOBYTE * MEGABYTE)
154 #define LIBZ "libz.so.1"
155
156 static void
157 usage(const char *pname)
158 {
159 (void) fprintf(stderr, gettext(USAGE), pname, pname, pname,
160 pname, pname, pname, pname, pname, pname, pname);
161 exit(E_USAGE);
162 }
163
164 static int
165 gzip_compress(void *src, size_t srclen, void *dst, size_t *dstlen, int level)
166 {
167 static int (*compress2p)(void *, ulong_t *, void *, size_t, int) = NULL;
168 void *libz_hdl = NULL;
169
170 /*
171 * The first time we are called, attempt to dlopen()
172 * libz.so.1 and get a pointer to the compress2() function
173 */
174 if (compress2p == NULL) {
175 if ((libz_hdl = openlib(LIBZ)) == NULL)
176 die(gettext("could not find %s. "
177 "gzip compression unavailable\n"), LIBZ);
178
179 if ((compress2p =
180 (int (*)(void *, ulong_t *, void *, size_t, int))
181 dlsym(libz_hdl, "compress2")) == NULL) {
182 closelib();
183 die(gettext("could not find the correct %s. "
184 "gzip compression unavailable\n"), LIBZ);
185 }
186 }
187
188 if ((*compress2p)(dst, (ulong_t *)dstlen, src, srclen, level) != 0)
189 return (-1);
190 return (0);
191 }
192
|