readwrite.c 7.65 KB
Newer Older
1 2 3 4 5
#include <glib/glib.h>
#include <glib/gstdio.h>
#include <gio/gio.h>
#include <string.h>

6 7 8
#ifdef G_OS_UNIX
#include <unistd.h>
#endif
LRN's avatar
LRN committed
9 10 11
#ifdef G_OS_WIN32
#include <io.h> /* for close() */
#endif
12

13 14
static const char *original_data = "This is some test data that we can put in a file...";
static const char *new_data = "new data..";
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

static void
verify_pos (GIOStream *iostream, goffset expected_pos)
{
  goffset pos;

  pos = g_seekable_tell (G_SEEKABLE (iostream));
  g_assert_cmpint (pos, ==, expected_pos);

  pos = g_seekable_tell (G_SEEKABLE (g_io_stream_get_input_stream (iostream)));
  g_assert_cmpint (pos, ==, expected_pos);

  pos = g_seekable_tell (G_SEEKABLE (g_io_stream_get_output_stream (iostream)));
  g_assert_cmpint (pos, ==, expected_pos);
}

static void
verify_iostream (GFileIOStream *file_iostream)
{
  gboolean res;
  GIOStream *iostream;
  GError *error;
  GInputStream *in;
  GOutputStream *out;
  char buffer[1024];
  gsize n_bytes;
  char *modified_data;

  iostream = G_IO_STREAM (file_iostream);

  verify_pos (iostream, 0);

  in = g_io_stream_get_input_stream (iostream);
  out = g_io_stream_get_output_stream (iostream);

  res = g_input_stream_read_all (in, buffer, 20, &n_bytes, NULL, NULL);
  g_assert (res);
52
  g_assert_cmpmem (buffer, n_bytes, original_data, 20);
53 54 55 56 57 58 59 60 61 62 63

  verify_pos (iostream, 20);

  res = g_seekable_seek (G_SEEKABLE (iostream),
			 -10, G_SEEK_END,
			 NULL, NULL);
  g_assert (res);
  verify_pos (iostream, strlen (original_data) - 10);

  res = g_input_stream_read_all (in, buffer, 20, &n_bytes, NULL, NULL);
  g_assert (res);
64
  g_assert_cmpmem (buffer, n_bytes, original_data + strlen (original_data) - 10, 10);
65 66 67

  verify_pos (iostream, strlen (original_data));

68 69 70 71 72 73 74 75 76 77 78 79
  res = g_seekable_seek (G_SEEKABLE (iostream),
			 10, G_SEEK_SET,
			 NULL, NULL);

  res = g_input_stream_skip (in, 5, NULL, NULL);
  g_assert (res == 5);
  verify_pos (iostream, 15);

  res = g_input_stream_skip (in, 10000, NULL, NULL);
  g_assert (res == strlen (original_data) - 15);
  verify_pos (iostream, strlen (original_data));

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
  res = g_seekable_seek (G_SEEKABLE (iostream),
			 10, G_SEEK_SET,
			 NULL, NULL);

  verify_pos (iostream, 10);

  res = g_output_stream_write_all (out, new_data, strlen (new_data),
				   &n_bytes, NULL, NULL);
  g_assert (res);
  g_assert_cmpint (n_bytes, ==, strlen (new_data));

  verify_pos (iostream, 10 + strlen (new_data));

  res = g_seekable_seek (G_SEEKABLE (iostream),
			 0, G_SEEK_SET,
			 NULL, NULL);
  g_assert (res);
  verify_pos (iostream, 0);

  res = g_input_stream_read_all (in, buffer, strlen (original_data), &n_bytes, NULL, NULL);
  g_assert (res);
  g_assert_cmpint ((int)n_bytes, ==, strlen (original_data));
  buffer[n_bytes] = 0;

  modified_data = g_strdup (original_data);
  memcpy (modified_data + 10, new_data, strlen (new_data));
  g_assert_cmpstr (buffer, ==, modified_data);

  verify_pos (iostream, strlen (original_data));

  res = g_seekable_seek (G_SEEKABLE (iostream),
			 0, G_SEEK_SET,
			 NULL, NULL);
  g_assert (res);
  verify_pos (iostream, 0);

  res = g_output_stream_close (out, NULL, NULL);
  g_assert (res);

  res = g_input_stream_read_all (in, buffer, 15, &n_bytes, NULL, NULL);
  g_assert (res);
121
  g_assert_cmpmem (buffer, n_bytes, modified_data, 15);
122 123 124 125 126

  error = NULL;
  res = g_output_stream_write_all (out, new_data, strlen (new_data),
				   &n_bytes, NULL, &error);
  g_assert (!res);
127 128
  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
  g_error_free (error);
129 130 131

  error = NULL;
  res = g_io_stream_close (iostream, NULL, &error);
132 133
  g_assert (res);
  g_assert_no_error (error);
134 135 136 137 138 139 140

  g_free (modified_data);
}

static void
test_g_file_open_readwrite (void)
{
141
  char *tmp_file;
142 143 144 145 146 147 148 149
  int fd;
  gboolean res;
  GFileIOStream *file_iostream;
  char *path;
  GFile *file;
  GError *error;

  fd = g_file_open_tmp ("readwrite_XXXXXX",
150
			&tmp_file, NULL);
151 152 153
  g_assert (fd != -1);
  close (fd);

154
  res = g_file_set_contents (tmp_file,
155 156 157
			     original_data, -1, NULL);
  g_assert (res);

158
  path = g_build_filename (g_get_tmp_dir (), "g-a-nonexisting-file", NULL);
159 160 161 162 163 164 165 166 167
  file = g_file_new_for_path (path);
  g_free (path);
  error = NULL;
  file_iostream = g_file_open_readwrite (file, NULL, &error);
  g_assert (file_iostream == NULL);
  g_assert (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND));
  g_error_free (error);
  g_object_unref (file);

168
  file = g_file_new_for_path (tmp_file);
169 170 171
  error = NULL;
  file_iostream = g_file_open_readwrite (file, NULL, &error);
  g_assert (file_iostream != NULL);
172
  g_object_unref (file);
173 174 175 176 177

  verify_iostream (file_iostream);

  g_object_unref (file_iostream);

178
  g_unlink (tmp_file);
179
  g_free (tmp_file);
180 181 182 183 184
}

static void
test_g_file_create_readwrite (void)
{
185
  char *tmp_file;
186 187 188 189 190 191 192 193 194
  int fd;
  gboolean res;
  GFileIOStream *file_iostream;
  GOutputStream *out;
  GFile *file;
  GError *error;
  gsize n_bytes;

  fd = g_file_open_tmp ("readwrite_XXXXXX",
195
			&tmp_file, NULL);
196 197 198
  g_assert (fd != -1);
  close (fd);

199
  file = g_file_new_for_path (tmp_file);
200 201 202
  error = NULL;
  file_iostream = g_file_create_readwrite (file, 0, NULL, &error);
  g_assert (file_iostream == NULL);
203 204
  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
  g_error_free (error);
205

206
  g_unlink (tmp_file);
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
  file_iostream = g_file_create_readwrite (file, 0, NULL, &error);
  g_assert (file_iostream != NULL);

  out = g_io_stream_get_output_stream (G_IO_STREAM (file_iostream));
  res = g_output_stream_write_all (out, original_data, strlen (original_data),
				   &n_bytes, NULL, NULL);
  g_assert (res);
  g_assert_cmpint (n_bytes, ==, strlen (original_data));

  res = g_seekable_seek (G_SEEKABLE (file_iostream),
			 0, G_SEEK_SET,
			 NULL, NULL);
  g_assert (res);

  verify_iostream (file_iostream);

  g_object_unref (file_iostream);
224
  g_object_unref (file);
225

226 227
  g_unlink (tmp_file);
  g_free (tmp_file);
228 229 230 231 232
}

static void
test_g_file_replace_readwrite (void)
{
233
  char *tmp_file, *backup, *data;
234 235 236 237 238 239 240 241 242 243 244
  int fd;
  gboolean res;
  GFileIOStream *file_iostream;
  GInputStream *in;
  GOutputStream *out;
  GFile *file;
  GError *error;
  char buffer[1024];
  gsize n_bytes;

  fd = g_file_open_tmp ("readwrite_XXXXXX",
245
			&tmp_file, NULL);
246 247 248
  g_assert (fd != -1);
  close (fd);

249
  res = g_file_set_contents (tmp_file,
250 251 252
			     new_data, -1, NULL);
  g_assert (res);

253
  file = g_file_new_for_path (tmp_file);
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
  error = NULL;
  file_iostream = g_file_replace_readwrite (file, NULL,
					    TRUE, 0, NULL, &error);
  g_assert (file_iostream != NULL);

  in = g_io_stream_get_input_stream (G_IO_STREAM (file_iostream));

  /* Ensure its empty */
  res = g_input_stream_read_all (in, buffer, sizeof buffer, &n_bytes, NULL, NULL);
  g_assert (res);
  g_assert_cmpint ((int)n_bytes, ==, 0);

  out = g_io_stream_get_output_stream (G_IO_STREAM (file_iostream));
  res = g_output_stream_write_all (out, original_data, strlen (original_data),
				   &n_bytes, NULL, NULL);
  g_assert (res);
  g_assert_cmpint (n_bytes, ==, strlen (original_data));

  res = g_seekable_seek (G_SEEKABLE (file_iostream),
			 0, G_SEEK_SET,
			 NULL, NULL);
  g_assert (res);

  verify_iostream (file_iostream);

  g_object_unref (file_iostream);
280
  g_object_unref (file);
281

282
  backup = g_strconcat (tmp_file, "~", NULL);
283 284 285 286 287 288 289 290 291
  res = g_file_get_contents (backup,
			     &data,
			     NULL, NULL);
  g_assert (res);
  g_assert_cmpstr (data, ==, new_data);
  g_free (data);
  g_unlink (backup);
  g_free (backup);

292 293
  g_unlink (tmp_file);
  g_free (tmp_file);
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
}


int
main (int   argc,
      char *argv[])
{
  g_test_init (&argc, &argv, NULL);

  g_test_add_func ("/readwrite/test_g_file_open_readwrite",
		   test_g_file_open_readwrite);
  g_test_add_func ("/readwrite/test_g_file_create_readwrite",
		   test_g_file_create_readwrite);
  g_test_add_func ("/readwrite/test_g_file_replace_readwrite",
		   test_g_file_replace_readwrite);

  return g_test_run();
}