unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
blob 78e57b6e7d5369ea71ea2a204c07a442650e0074 5212 bytes (raw)
name: contrib/notmuch-deliver/test/notmuch-lock.c 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 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
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
 
/* vim: set cino=t0 fo=croql sw=4 ts=4 sts=0 noet cin fdm=syntax nolist : */

#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#include <glib.h>

#include <notmuch.h>


#define MIN_UWAIT 1000        // 1 millisecond
#define MAX_UWAIT 600000000   // 10 minutes


typedef struct ChildFinishedData_
{
	GMainLoop *main_loop;
	int return_val;
} ChildFinishedData;

typedef struct SpawnChildData_
{
	char** new_argv;
	ChildFinishedData *child_finidshed_data;
	GMainLoop *main_loop;
	int return_val;
} SpawnChildData;

static gint sleep_option = -1;
static GOptionEntry entries[] = {
	{ "sleep", 's', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_INT, &sleep_option,
		"Sleep for N microseconds, while holding the lock to the database", "N" },
	{NULL, 0, 0, 0, NULL, NULL, NULL},
};



static gboolean
load_keyfile(const gchar *path, gchar **db_path, gchar ***tags)
{
	GKeyFile *fd;
	GError *error;

	fd = g_key_file_new();
	error = NULL;
	if (!g_key_file_load_from_file(fd, path, G_KEY_FILE_NONE,
				&error)) {
		g_printerr("Failed to parse `%s': %s", path,
				error->message);
		g_error_free(error);
		g_key_file_free(fd);
		return FALSE;
	}

	*db_path = g_key_file_get_string(fd, "database",
			"path", &error);
	if (*db_path == NULL) {
		g_critical("Failed to parse database.path from `%s': %s", path,
				error->message);
		g_error_free(error);
		g_key_file_free(fd);
		return FALSE;
	}

	*tags = g_key_file_get_string_list(fd, "new",
			"tags", NULL, NULL);

	g_key_file_free(fd);
	return TRUE;
}



static gchar *
get_db_path(void)
{
	gchar *conf_path, *db_path;

	// Get location of notmuch config
	if (g_getenv("NOTMUCH_CONFIG"))
		conf_path = g_strdup(g_getenv("NOTMUCH_CONFIG"));
	else if (g_getenv("HOME"))
		conf_path = g_build_filename(g_getenv("HOME"),
				".notmuch-config", NULL);
	else {
		g_critical("Neither NOTMUCH_CONFIG nor HOME set");
		return NULL;
	}

	// Get location of notmuch database
	gchar **conf_tags = NULL;
	g_print("Parsing configuration from `%s'\n", conf_path);
	if (!load_keyfile(conf_path, &db_path, &conf_tags)) {
		g_free(conf_path);
		return NULL;
	}

	g_free(conf_path);
	return db_path;
}

static void
child_finished_cb(GPid pid, gint status, gpointer data)
{
	ChildFinishedData *child_finidshed_data = (ChildFinishedData *)data;

	g_printerr("Called child_finished_cb()\n");

	if WIFEXITED(status) {
		child_finidshed_data->return_val = WEXITSTATUS(status);
		g_printerr("PID %d exited normally with exit code %d\n", pid, WEXITSTATUS(status));
	}

	g_main_loop_quit(child_finidshed_data->main_loop);
}


static gboolean
spawn_child_cb(gpointer data)
{
	SpawnChildData *spawn_child_data = (SpawnChildData *)data;

	g_printerr("Called spawn_child_cb()\n");

	gboolean spawn_success = FALSE;
	GPid child_pid = 0;
	spawn_success = g_spawn_async(
			g_get_current_dir(),
			spawn_child_data->new_argv,
			NULL,
			G_SPAWN_SEARCH_PATH | G_SPAWN_CHILD_INHERITS_STDIN | G_SPAWN_DO_NOT_REAP_CHILD,
			NULL,
			NULL,
			&child_pid,
			NULL
			);
	if (spawn_success) {
		g_child_watch_add(child_pid, &child_finished_cb, spawn_child_data->child_finidshed_data);
	}
	else {
		g_printerr("faild to spawn child\n");
	}


	return FALSE;
}

static gboolean
close_db_cb(gpointer data)
{
	notmuch_database_t *db = (notmuch_database_t*)data;

	g_printerr("Called close_db_cb()\n");

	// Close database again
	notmuch_database_close(db);

	return FALSE;
}


int
main(int argc, char *argv[])
{
	gchar *db_path;
	gint32 sleep_time = 0;
	notmuch_database_t *db;
	GError *error = NULL;
	GOptionContext *context;

	// parse command line options
	gboolean option_parse_success = FALSE;
	context = g_option_context_new( "COMMAND [ARGS...]");
	g_option_context_add_main_entries(context, entries, NULL);
	g_option_context_set_summary(context,
			"Utility to test behaviour of programs while the notmuch database is locked");
	option_parse_success = g_option_context_parse(context, &argc, &argv, &error);
	g_option_context_free(context);
	if (! option_parse_success) {
		g_printerr("option parsing failed: %s\n", error->message);
		return EXIT_FAILURE;
	}

	// Check new program args
	if ((argc - 1) <= 0) {
		g_printerr("no command supplied\n");
		return EXIT_FAILURE;
	}
	char **new_argv = argv + 1;
	if (g_strcmp0(new_argv[0], "--") == 0)
		new_argv++;

	// Get notmuch database path
	db_path = get_db_path();
	if (db_path == NULL)
		return EXIT_FAILURE;

	// Open notmuch database
	g_printerr("Opening notmuch database `%s'\n", db_path);
	db = notmuch_database_open(db_path, NOTMUCH_DATABASE_MODE_READ_WRITE);
	g_free(db_path);
	if (db == NULL)
		return EXIT_FAILURE;

	// Sleep for some time
	if (sleep_option >= 0) {
		sleep_time = sleep_option;
	} else {
		sleep_time = g_random_int_range(MIN_UWAIT, MAX_UWAIT);
	}
	g_printerr("Sleeping for %f secs\n", ((double)sleep_time)/(1000*1000));

	GMainLoop *main_loop = g_main_loop_new(NULL, FALSE);

	ChildFinishedData child_finidshed_data = {main_loop, 0};
	SpawnChildData spawn_child_data = {new_argv, &child_finidshed_data, main_loop, 0};

	g_idle_add(&spawn_child_cb, &spawn_child_data);
	g_timeout_add(sleep_time / 1000, &close_db_cb, db);
	g_main_loop_run(main_loop);


	return child_finidshed_data.return_val;
}

debug log:

solving 78e57b6 ...
found 78e57b6 in https://yhetil.org/notmuch/1320578584-6642-4-git-send-email-davrieb@liegesta.at/
found 2303843 in https://yhetil.org/notmuch/1320578584-6642-3-git-send-email-davrieb@liegesta.at/

applying [1/2] https://yhetil.org/notmuch/1320578584-6642-3-git-send-email-davrieb@liegesta.at/
diff --git a/contrib/notmuch-deliver/test/notmuch-lock.c b/contrib/notmuch-deliver/test/notmuch-lock.c
new file mode 100644
index 0000000..2303843


applying [2/2] https://yhetil.org/notmuch/1320578584-6642-4-git-send-email-davrieb@liegesta.at/
diff --git a/contrib/notmuch-deliver/test/notmuch-lock.c b/contrib/notmuch-deliver/test/notmuch-lock.c
index 2303843..78e57b6 100644

Checking patch contrib/notmuch-deliver/test/notmuch-lock.c...
Applied patch contrib/notmuch-deliver/test/notmuch-lock.c cleanly.
Checking patch contrib/notmuch-deliver/test/notmuch-lock.c...
Applied patch contrib/notmuch-deliver/test/notmuch-lock.c cleanly.

index at:
100644 78e57b6e7d5369ea71ea2a204c07a442650e0074	contrib/notmuch-deliver/test/notmuch-lock.c

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).