summaryrefslogtreecommitdiff
path: root/tests/test-filesys.c
blob: aef4fc0b3f2157b4b56c69492b75c5b64b593aaf (plain)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* test-filesys.c
 *
 * Copyright 2001 Lutz Mueller <lutz@users.sf.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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 this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */
#include "config.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#ifdef HAVE_MCHECK_H
#include <mcheck.h>
#endif

#include <gphoto2/gphoto2-filesys.h>
#include <gphoto2/gphoto2-result.h>
#include <gphoto2/gphoto2-port-log.h>


#ifdef __GNUC__
#define __unused__ __attribute__((unused))
#else
#define __unused__
#endif


#define CHECK(r) {int ret = r; if (ret < 0) {printf ("Got error: %s\n", gp_result_as_string (ret)); return (1);}}

static void
log_func (GPLogLevel __unused__ level, const char __unused__ *domain,
	  const char *str, void __unused__ *data)
{
	printf ("%s\n", str);
}

static void
error_func (GPContext __unused__ *context, const char *str, void __unused__ *data)
{
	printf ("### %s\n", str);
}

static int
set_info_func (CameraFilesystem __unused__ *fs, const char __unused__ *folder,
	       const char __unused__ *file,
	       CameraFileInfo __unused__ info, void __unused__ *data,
	       GPContext __unused__ *context)
{
	printf ("  -> The camera will set the file info here.\n");

	return (GP_OK);
}

static int
get_info_func (CameraFilesystem __unused__ *fs, const char __unused__ *folder,
	       const char *file,
	       CameraFileInfo *info, void __unused__ *data, GPContext __unused__ *context)
{
	printf ("  -> The camera will get the file info here.\n");

	info->preview.fields = GP_FILE_INFO_NONE;
	info->file.fields    = GP_FILE_INFO_NONE;
	return (GP_OK);
}

static int
file_list_func (CameraFilesystem __unused__ *fs, const char *folder,
		CameraList *list,
		void __unused__ *data, GPContext __unused__ *context)
{
	printf ("### -> The camera will list the files in '%s' here.\n", folder);

	if (!strcmp (folder, "/whatever")) {
		gp_list_append (list, "file1", NULL);
		gp_list_append (list, "file2", NULL);
		gp_list_append (list, "file3", NULL);
		gp_list_append (list, "file4", NULL);
		gp_list_append (list, "file5", NULL);
		gp_list_append (list, "file6", NULL);
	}

	return (GP_OK);
}

static int
folder_list_func (CameraFilesystem __unused__ *fs, const char *folder,
		  CameraList *list,
		  void __unused__ *data, GPContext __unused__ *context)
{
	printf ("### -> The camera will list the folders in '%s' here.\n",
		folder);

	if (!strcmp (folder, "/")) {
		gp_list_append (list, "whatever", NULL);
		gp_list_append (list, "another", NULL);
	}

	if (!strcmp (folder, "/whatever")) {
		gp_list_append (list, "directory", NULL);
		gp_list_append (list, "dir", NULL);
	}

	if (!strcmp (folder, "/whatever/directory")) {
		gp_list_append (list, "my_special_folder", NULL);
	}

	return (GP_OK);
}

static int
delete_file_func (CameraFilesystem __unused__ *fs, const char *folder,
		  const char *file,
		  void __unused__ *data, GPContext __unused__ *context)
{
	printf ("Here we should delete %s from folder %s...\n", file, folder);

	return (GP_OK);
}

static CameraFilesystemFuncs fsfuncs = {
	.get_info_func = get_info_func,
	.set_info_func = set_info_func,
	.del_file_func = delete_file_func,
	.file_list_func = file_list_func,
	.folder_list_func = folder_list_func,
};
int
main ()
{
	CameraFilesystem *fs;
	CameraFileInfo info;
	CameraList *list;
	int x, count;
	const char *name;
	char *foldername;
	GPContext *context;
	int logid;

#ifdef HAVE_MCHECK_H
	mtrace();
#endif

	CHECK (gp_list_new(&list));

	logid = gp_log_add_func (GP_LOG_DEBUG, log_func, NULL);
	context = gp_context_new ();
	gp_context_set_error_func (context, error_func, NULL);

	printf ("*** Creating file system...\n");
	CHECK (gp_filesystem_new (&fs));

	printf ("*** Setting the callbacks...\n");
	CHECK (gp_filesystem_set_funcs (fs, &fsfuncs, NULL));

	printf ("*** Adding a file...\n");
	CHECK (gp_filesystem_append (fs, "/", "my.file", context));

	gp_filesystem_dump (fs);

	printf ("*** Removing this file...\n");
	CHECK (gp_filesystem_delete_file (fs, "/", "my.file", context));

	gp_filesystem_dump (fs);

	printf ("*** Resetting...\n");
	CHECK (gp_filesystem_reset (fs));

	gp_filesystem_dump (fs);

	printf ("*** Adding /...\n");
	CHECK (gp_filesystem_append (fs, "/", NULL, context));

	printf ("*** Adding /whatever ...\n");
	CHECK (gp_filesystem_append (fs, "/whatever", NULL, context));

	printf ("*** Adding /whatever/dir...\n");
	CHECK (gp_filesystem_append (fs, "/whatever/dir", NULL, context));

	printf ("*** Adding /whatever/dir/file1...\n");
	CHECK (gp_filesystem_append (fs, "/whatever/dir", "file1", context));

	gp_filesystem_dump (fs);

	printf ("*** Adding /whatever/dir/file2...\n");
	CHECK (gp_filesystem_append (fs, "/whatever/dir", "file2", context));
	CHECK (gp_filesystem_append (fs, "/whatever/dir", "file3", context));
	CHECK (gp_filesystem_append (fs, "/whatever/dir", "file4", context));
	CHECK (gp_filesystem_append (fs, "/whatever/dir", "file5", context));

	gp_filesystem_dump (fs);

	printf ("*** Deleting everything below root...\n");
	CHECK (gp_filesystem_delete_all (fs, "/", context));

	gp_filesystem_dump (fs);

	printf ("*** Appending root directory...\n");
	CHECK (gp_filesystem_append (fs, "/", NULL, context));

	printf ("*** Appending some directories...\n");
	CHECK (gp_filesystem_append (fs, "/whatever", NULL, context));
	CHECK (gp_filesystem_append (fs, "/whatever/directory", NULL, context));

	printf ("*** Adding some files...\n");
	CHECK (gp_filesystem_append (fs, "/whatever/directory",
				     "some.file", context));
	CHECK (gp_filesystem_append (fs, "/whatever/directory",
				     "some.file2", context));
	CHECK (gp_filesystem_append (fs, "/another/directory",
				     "another.file", context));

	gp_filesystem_dump (fs);

	printf ("*** Getting info about a file...\n");
	CHECK (gp_filesystem_get_info (fs, "/whatever/directory", "some.file",
				       &info, context));

	printf ("*** Getting info again (cache!)...\n");
	CHECK (gp_filesystem_get_info (fs, "/whatever/directory", "some.file",
				       &info, context));

	printf ("*** Set info about another file...\n");
	CHECK (gp_filesystem_set_info (fs, "/whatever/directory", "some.file2",
				       info, context));

	printf ("*** Getting info about this file (cache!)...\n");
	CHECK (gp_filesystem_get_info (fs, "/whatever/directory", "some.file2",
				       &info, context));

	printf ("*** Deleting a file...\n");
	CHECK (gp_filesystem_delete_file (fs, "/whatever/directory",
					  "some.file2", context));

	gp_filesystem_dump (fs);

	printf ("*** Resetting the filesystem...\n");
	CHECK (gp_filesystem_reset (fs));

	gp_filesystem_dump (fs);

	printf ("*** Getting file list for folder '/whatever/directory'...\n");
	CHECK (gp_filesystem_list_folders (fs, "/whatever/directory",
					   list, context));

	printf ("*** Getting file list for folder '/whatever/directory' "
		"again (cached!)...\n");
	CHECK (gp_filesystem_list_folders (fs, "/whatever/directory",
					   list, context));

	printf ("*** Counting the contents...\n");
	CHECK (count = gp_list_count (list));

	printf ("*** Listing the contents...\n");
	for (x = 0; x < count; x++) {
		CHECK (gp_list_get_name (list, x, &name));
		printf (" %i: '%s'\n", x, name);
	}

	printf ("*** Getting folder of 'file1'...\n");
	CHECK (gp_filesystem_get_folder (fs, "file1", &foldername, context));
	printf ("... found in '%s'.\n", foldername);
	free(foldername);

	printf ("*** Deleting a couple of files...\n");
	CHECK (gp_filesystem_delete_file (fs, "/whatever", "file5", context));
	CHECK (gp_filesystem_delete_file (fs, "/whatever", "file4", context));
	CHECK (gp_filesystem_delete_file (fs, "/whatever", "file3", context));

	gp_filesystem_dump (fs);

	printf ("*** Freeing file system...\n");
	CHECK (gp_filesystem_free (fs));

	gp_context_unref (context);

	CHECK (gp_list_free(list));

#ifdef HAVE_MCHECK_H
	muntrace();
#endif
	gp_log_remove_func (logid);

	return (0);
}

/*
 * Local variables:
 *  compile-command: "gcc -pedantic -Wstrict-prototypes -O2 -g test-filesys.c -o test-filesys `gphoto2-config --cflags --libs` && export MALLOC_TRACE=test-filesys.log && ./test-filesys && mtrace ./test-filesys test-filesys.log"
 * End:
 */