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