xref: /aosp_15_r20/external/selinux/libsemanage/src/compressed_file.h (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1*2d543d20SAndroid Build Coastguard Worker /* Author: Jason Tang	  <[email protected]>
2*2d543d20SAndroid Build Coastguard Worker  *         Christopher Ashworth <[email protected]>
3*2d543d20SAndroid Build Coastguard Worker  *         Ondrej Mosnacek <[email protected]>
4*2d543d20SAndroid Build Coastguard Worker  *
5*2d543d20SAndroid Build Coastguard Worker  * Copyright (C) 2004-2006 Tresys Technology, LLC
6*2d543d20SAndroid Build Coastguard Worker  * Copyright (C) 2005-2021 Red Hat, Inc.
7*2d543d20SAndroid Build Coastguard Worker  *
8*2d543d20SAndroid Build Coastguard Worker  *  This library is free software; you can redistribute it and/or
9*2d543d20SAndroid Build Coastguard Worker  *  modify it under the terms of the GNU Lesser General Public
10*2d543d20SAndroid Build Coastguard Worker  *  License as published by the Free Software Foundation; either
11*2d543d20SAndroid Build Coastguard Worker  *  version 2.1 of the License, or (at your option) any later version.
12*2d543d20SAndroid Build Coastguard Worker  *
13*2d543d20SAndroid Build Coastguard Worker  *  This library is distributed in the hope that it will be useful,
14*2d543d20SAndroid Build Coastguard Worker  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15*2d543d20SAndroid Build Coastguard Worker  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16*2d543d20SAndroid Build Coastguard Worker  *  Lesser General Public License for more details.
17*2d543d20SAndroid Build Coastguard Worker  *
18*2d543d20SAndroid Build Coastguard Worker  *  You should have received a copy of the GNU Lesser General Public
19*2d543d20SAndroid Build Coastguard Worker  *  License along with this library; if not, write to the Free Software
20*2d543d20SAndroid Build Coastguard Worker  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21*2d543d20SAndroid Build Coastguard Worker  */
22*2d543d20SAndroid Build Coastguard Worker 
23*2d543d20SAndroid Build Coastguard Worker #ifndef _SEMANAGE_CIL_FILE_H_
24*2d543d20SAndroid Build Coastguard Worker #define _SEMANAGE_CIL_FILE_H_
25*2d543d20SAndroid Build Coastguard Worker 
26*2d543d20SAndroid Build Coastguard Worker #include <sys/mman.h>
27*2d543d20SAndroid Build Coastguard Worker #include <sys/types.h>
28*2d543d20SAndroid Build Coastguard Worker 
29*2d543d20SAndroid Build Coastguard Worker #include "handle.h"
30*2d543d20SAndroid Build Coastguard Worker 
31*2d543d20SAndroid Build Coastguard Worker struct file_contents {
32*2d543d20SAndroid Build Coastguard Worker 	void *data; /** file contents (uncompressed) */
33*2d543d20SAndroid Build Coastguard Worker 	size_t len; /** length of contents */
34*2d543d20SAndroid Build Coastguard Worker 	int compressed; /** whether file was compressed */
35*2d543d20SAndroid Build Coastguard Worker };
36*2d543d20SAndroid Build Coastguard Worker 
37*2d543d20SAndroid Build Coastguard Worker /**
38*2d543d20SAndroid Build Coastguard Worker  * Map/read a possibly-compressed file into memory.
39*2d543d20SAndroid Build Coastguard Worker  *
40*2d543d20SAndroid Build Coastguard Worker  * If the file is bzip compressed map_file will uncompress the file into
41*2d543d20SAndroid Build Coastguard Worker  * @p contents. The caller is responsible for calling
42*2d543d20SAndroid Build Coastguard Worker  * @ref unmap_compressed_file on @p contents on success.
43*2d543d20SAndroid Build Coastguard Worker  *
44*2d543d20SAndroid Build Coastguard Worker  * @param sh        semanage handle
45*2d543d20SAndroid Build Coastguard Worker  * @param path      path to the file
46*2d543d20SAndroid Build Coastguard Worker  * @param contents  pointer to struct file_contents, which will be
47*2d543d20SAndroid Build Coastguard Worker  *   populated with data pointer, size, and an indication whether
48*2d543d20SAndroid Build Coastguard Worker  *   the file was compressed or not
49*2d543d20SAndroid Build Coastguard Worker  *
50*2d543d20SAndroid Build Coastguard Worker  * @return 0 on success, -1 otherwise.
51*2d543d20SAndroid Build Coastguard Worker  */
52*2d543d20SAndroid Build Coastguard Worker int map_compressed_file(semanage_handle_t *sh, const char *path,
53*2d543d20SAndroid Build Coastguard Worker 			struct file_contents *contents);
54*2d543d20SAndroid Build Coastguard Worker 
55*2d543d20SAndroid Build Coastguard Worker /**
56*2d543d20SAndroid Build Coastguard Worker  * Destroy a previously mapped possibly-compressed file.
57*2d543d20SAndroid Build Coastguard Worker  *
58*2d543d20SAndroid Build Coastguard Worker  * If all fields of @p contents are zero/NULL, the function is
59*2d543d20SAndroid Build Coastguard Worker  * guaranteed to do nothing.
60*2d543d20SAndroid Build Coastguard Worker  *
61*2d543d20SAndroid Build Coastguard Worker  * @param contents  pointer to struct file_contents to destroy
62*2d543d20SAndroid Build Coastguard Worker  */
63*2d543d20SAndroid Build Coastguard Worker void unmap_compressed_file(struct file_contents *contents);
64*2d543d20SAndroid Build Coastguard Worker 
65*2d543d20SAndroid Build Coastguard Worker /**
66*2d543d20SAndroid Build Coastguard Worker  * Write bytes into a file, using compression if configured.
67*2d543d20SAndroid Build Coastguard Worker  *
68*2d543d20SAndroid Build Coastguard Worker  * @param sh    semanage handle
69*2d543d20SAndroid Build Coastguard Worker  * @param path  path to the file
70*2d543d20SAndroid Build Coastguard Worker  * @param data  pointer to the data
71*2d543d20SAndroid Build Coastguard Worker  * @param len   length of the data
72*2d543d20SAndroid Build Coastguard Worker  *
73*2d543d20SAndroid Build Coastguard Worker  * @return 0 on success, -1 otherwise.
74*2d543d20SAndroid Build Coastguard Worker  */
75*2d543d20SAndroid Build Coastguard Worker int write_compressed_file(semanage_handle_t *sh, const char *path,
76*2d543d20SAndroid Build Coastguard Worker 			  void *data, size_t len);
77*2d543d20SAndroid Build Coastguard Worker 
78*2d543d20SAndroid Build Coastguard Worker #endif
79