mce-io.h 5.59 KB
Newer Older
1 2 3 4
/**
 * @file mce-io.h
 * Headers for the generic I/O functionality for the Mode Control Entity
 * <p>
Santtu Lakkala's avatar
Santtu Lakkala committed
5
 * Copyright © 2007, 2009-2011 Nokia Corporation and/or its subsidiary(-ies).
6
 * Copyright (C) 2012-2019 Jolla Ltd.
7 8
 * <p>
 * @author David Weinehall <david.weinehall@nokia.com>
9 10 11
 * @author Santtu Lakkala <ext-santtu.1.lakkala@nokia.com>
 * @author Jukka Turunen <ext-jukka.t.turunen@nokia.com>
 * @author Simo Piiroinen <simo.piiroinen@jollamobile.com>
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * mce is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * mce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with mce.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef _MCE_IO_H_
#define _MCE_IO_H_

28 29
#include <sys/stat.h>

30
#include <stdio.h>
31
#include <stdbool.h>
32

33 34
#include <glib.h>

35 36 37 38 39 40 41 42 43 44
/** Error policies for mce-io */
typedef enum {
	/** Exit on error */
	MCE_IO_ERROR_POLICY_EXIT,
	/** Warn about errors */
	MCE_IO_ERROR_POLICY_WARN,
	/** Silently ignore errors */
	MCE_IO_ERROR_POLICY_IGNORE
} error_policy_t;

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
/** Control structure for updating output files */
typedef struct {
	/* static configuration */

	/** descriptive context information used for identifying the
	 *  purpose of the output file even if no valid output path
	 *  is available */
	const gchar *context;

	/** TRUE to truncate the file before writing,
	 *  FALSE to append to the end of the file */
	gboolean truncate_file;

	/** TRUE to close the file on exit
	 *  [from mce_write_number_string_to_file() function],
	 *  FALSE to leave the file open */
	gboolean close_on_exit;

	/* runtime configuration */

	/** Path to the file, or NULL (in which case one misconfiguration
	 *  error message will be logged if write helpers are called) */
	const char *path;

	/* dynamic state */

	/** Cached output stream, use mce_close_output() to close */
	FILE *file;

	/** TRUE if missing path configuration error has already been
	 *  written for this file */
	gboolean invalid_config_reported;
77 78 79

	/** For suppressing reporting of repeated errors */
	int reported_errno;
80 81
} output_state_t;

82
typedef struct mce_io_mon_t mce_io_mon_t;
83

84
/** Callback function type for I/O monitor input notifications */
85
typedef gboolean (*mce_io_mon_notify_cb)(mce_io_mon_t *iomon, gpointer data, gsize bytes_read);
86

87
/** Callback function type for I/O monitor delete notifications */
88
typedef void (*mce_io_mon_delete_cb)(mce_io_mon_t *iomon);
89

90 91 92
/** Callback function type for releasing I/O monitor user data block */
typedef void (*mce_io_mon_free_cb)(void *user_data);

93 94
/* iomon functions */

95 96 97 98 99 100
mce_io_mon_t *mce_io_mon_register_string(const gint fd,
					 const gchar *const file,
					 error_policy_t error_policy,
					 gboolean rewind_policy,
					 mce_io_mon_notify_cb callback,
					 mce_io_mon_delete_cb delete_cb);
101

102 103 104 105 106 107 108
mce_io_mon_t *mce_io_mon_register_chunk(const gint fd,
					const gchar *const file,
					error_policy_t error_policy,
					gboolean rewind_policy,
					mce_io_mon_notify_cb callback,
					mce_io_mon_delete_cb delete_cb,
					gulong chunk_size);
109

110
void mce_io_mon_unregister(mce_io_mon_t *iomon);
111

112
void mce_io_mon_unregister_list(GSList *list);
113

114
void mce_io_mon_unregister_at_path(const char *path);
115

116
void mce_io_mon_suspend(mce_io_mon_t *iomon);
117

118
void mce_io_mon_resume(mce_io_mon_t *iomon);
119

120
const gchar *mce_io_mon_get_path(const mce_io_mon_t *iomon);
121

122
int mce_io_mon_get_fd(const mce_io_mon_t *iomon);
123

124 125 126 127 128 129
void mce_io_mon_set_user_data(mce_io_mon_t *iomon,
			      void *user_data,
			      mce_io_mon_free_cb free_cb);

void *mce_io_mon_get_user_data(const mce_io_mon_t *iomon);

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
/* output_state_t funtions */

void mce_close_output(output_state_t *output);

gboolean mce_write_number_string_to_file(output_state_t *output, const gulong number);

/* misc utils */

gboolean mce_close_file(const gchar *const file, FILE **fp);

gboolean mce_read_chunk_from_file(const gchar *const file, void **data,
				  gssize *len, int flags);

gboolean mce_read_string_from_file(const gchar *const file, gchar **string);

gboolean mce_read_number_string_from_file(const gchar *const file,
					  gulong *number, FILE **fp,
					  gboolean rewind,
					  gboolean close_on_exit);

gboolean mce_write_string_to_file(const gchar *const file,
				  const gchar *const string);

gboolean mce_write_number_string_to_file_atomic(const gchar *const file,
						const gulong number);

Santtu Lakkala's avatar
Santtu Lakkala committed
156
gboolean mce_are_settings_locked(void);
157

Santtu Lakkala's avatar
Santtu Lakkala committed
158
gboolean mce_unlock_settings(void);
159

160
void *mce_io_load_file(const char *path, size_t *psize);
161

spiiroin's avatar
spiiroin committed
162
void *mce_io_load_file_until_eof(const char *path, size_t *psize);
163

164 165 166
gboolean mce_io_save_file(const char *path,
			  const void *data, size_t size,
			  mode_t mode);
167

spiiroin's avatar
spiiroin committed
168 169
gboolean mce_io_save_to_existing_file(const char *path,
				      const void *data, size_t size);
170

171 172 173
gboolean mce_io_save_file_atomic(const char *path,
				 const void *data, size_t size,
				 mode_t mode, gboolean keep_backup);
174

175 176 177 178
gboolean mce_io_update_file_atomic(const char *path,
				   const void *data, size_t size,
				   mode_t mode, gboolean keep_backup);

179 180 181 182
/* Resume from suspend detection */
void mce_io_init_resume_timer(void);
void mce_io_quit_resume_timer(void);

183 184 185 186
guint mce_io_add_watch(int fd, bool close_on_unref, GIOCondition cnd, GIOFunc io_cb, gpointer aptr);
const char *mce_io_condition_repr (GIOCondition cond);
const char *mce_io_status_name    (GIOStatus io_status);

187
#endif /* _MCE_IO_H_ */