summary refs log tree commit diff
path: root/libio/tst-ftell-active-handler.c
blob: 5cc013da11e3ad259b790196e6d00f2d191c0020 (plain) (blame)
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
/* Verify that ftell returns the correct value at various points before and
   after the handler on which it is called becomes active.
   Copyright (C) 2014-2017 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C 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.1 of the License, or (at your option) any later version.

   The GNU C 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 the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <locale.h>
#include <wchar.h>

static int do_test (void);

#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"

#define get_handles_fdopen(filename, fd, fp, fd_mode, mode) \
({									      \
  int ret = 0;								      \
  (fd) = open ((filename), (fd_mode), 0);				      \
  if ((fd) == -1)							      \
    {									      \
      printf ("open failed: %m\n");					      \
      ret = 1;								      \
    }									      \
  else									      \
    {									      \
      (fp) = fdopen ((fd), (mode));					      \
      if ((fp) == NULL)							      \
	{								      \
	  printf ("fdopen failed: %m\n");				      \
	  close (fd);							      \
	  ret = 1;							      \
	}								      \
    }									      \
  ret;									      \
})

#define get_handles_fopen(filename, fd, fp, mode) \
({									      \
  int ret = 0;								      \
  (fp) = fopen ((filename), (mode));					      \
  if ((fp) == NULL)							      \
    {									      \
      printf ("fopen failed: %m\n");					      \
      ret = 1;								      \
    }									      \
  else									      \
    {									      \
      (fd) = fileno (fp);						      \
      if ((fd) == -1)							      \
	{								      \
	  printf ("fileno failed: %m\n");				      \
	  ret = 1;							      \
	}								      \
    }									      \
  ret;									      \
})

/* data points to either char_data or wide_data, depending on whether we're
   testing regular file mode or wide mode respectively.  Similarly,
   fputs_func points to either fputs or fputws.  data_len keeps track of the
   length of the current data and file_len maintains the current file
   length.  */
static const void *data;
static const char *char_data = "abcdef";
static const wchar_t *wide_data = L"abcdef";
static size_t data_len;
static size_t file_len;

typedef int (*fputs_func_t) (const void *data, FILE *fp);
typedef void *(*fgets_func_t) (void *ws, int n, FILE *fp);
fputs_func_t fputs_func;
fgets_func_t fgets_func;

/* This test verifies that the offset reported by ftell is correct after the
   file is truncated using ftruncate.  ftruncate does not change the file
   offset on truncation and hence, SEEK_CUR should continue to point to the
   old offset and not be changed to the new offset.  */
static int
do_ftruncate_test (const char *filename)
{
  FILE *fp = NULL;
  int fd;
  int ret = 0;
  struct test
    {
      const char *mode;
      int fd_mode;
    } test_modes[] = {
	  {"r+", O_RDWR},
	  {"w", O_WRONLY | O_TRUNC},
	  {"w+", O_RDWR | O_TRUNC},
	  {"a", O_WRONLY},
	  {"a+", O_RDWR}
    };

  for (int j = 0; j < 2; j++)
    {
      for (int i = 0; i < sizeof (test_modes) / sizeof (struct test); i++)
	{
	  int fileret;
	  printf ("\tftruncate: %s (file, \"%s\"): ",
		  j == 0 ? "fopen" : "fdopen",
		  test_modes[i].mode);

	  if (j == 0)
	    fileret = get_handles_fopen (filename, fd, fp, test_modes[i].mode);
	  else
	    fileret = get_handles_fdopen (filename, fd, fp,
					  test_modes[i].fd_mode,
					  test_modes[i].mode);

	  if (fileret != 0)
	    return fileret;

	  /* Write some data.  */
	  size_t written = fputs_func (data, fp);

	  if (written == EOF)
	    {
	      printf ("fputs[1] failed to write data\n");
	      ret |= 1;
	    }

	  /* Record the offset.  */
	  long offset = ftell (fp);

	  /* Flush data to allow switching active handles.  */
	  if (fflush (fp))
	    {
	      printf ("Flush failed: %m\n");
	      ret |= 1;
	    }

	  /* Now truncate the file.  */
	  if (ftruncate (fd, 0) != 0)
	    {
	      printf ("Failed to truncate file: %m\n");
	      ret |= 1;
	    }

	  /* ftruncate does not change the offset, so there is no need to call
	     anything to be able to switch active handles.  */
	  long new_offset = ftell (fp);

	  /* The offset should remain unchanged since ftruncate does not update
	     it.  */
	  if (offset != new_offset)
	    {
	      printf ("Incorrect offset.  Expected %ld, but got %ld\n",
		      offset, new_offset);

	      ret |= 1;
	    }
	  else
	    printf ("offset = %ld\n", offset);

	  fclose (fp);
	}
    }

  return ret;
}
/* Test that ftell output after a rewind is correct.  */
static int
do_rewind_test (const char *filename)
{
  int ret = 0;
  struct test
    {
      const char *mode;
      int fd_mode;
      size_t old_off;
      size_t new_off;
    } test_modes[] = {
	  {"w", O_WRONLY | O_TRUNC, 0, data_len},
	  {"w+", O_RDWR | O_TRUNC, 0, data_len},
	  {"r+", O_RDWR, 0, data_len},
	  /* The new offsets for 'a' and 'a+' modes have to factor in the
	     previous writes since they always append to the end of the
	     file.  */
	  {"a", O_WRONLY, 0, 3 * data_len},
	  {"a+", O_RDWR, 0, 4 * data_len},
    };

  /* Empty the file before the test so that our offsets are simple to
     calculate.  */
  FILE *fp = fopen (filename, "w");
  if (fp == NULL)
    {
      printf ("Failed to open file for emptying\n");
      return 1;
    }
  fclose (fp);

  for (int j = 0; j < 2; j++)
    {
      for (int i = 0; i < sizeof (test_modes) / sizeof (struct test); i++)
	{
	  FILE *fp;
	  int fd;
	  int fileret;

	  printf ("\trewind: %s (file, \"%s\"): ", j == 0 ? "fdopen" : "fopen",
		  test_modes[i].mode);

	  if (j == 0)
	    fileret = get_handles_fdopen (filename, fd, fp,
					  test_modes[i].fd_mode,
					  test_modes[i].mode);
	  else
	    fileret = get_handles_fopen (filename, fd, fp, test_modes[i].mode);

	  if (fileret != 0)
	    return fileret;

	  /* Write some content to the file, rewind and ensure that the ftell
	     output after the rewind is 0.  POSIX does not specify what the
	     behavior is when a file is rewound in 'a' mode, so we retain
	     current behavior, which is to keep the 0 offset.  */
	  size_t written = fputs_func (data, fp);

	  if (written == EOF)
	    {
	      printf ("fputs[1] failed to write data\n");
	      ret |= 1;
	    }

	  rewind (fp);
	  long offset = ftell (fp);

	  if (offset != test_modes[i].old_off)
	    {
	      printf ("Incorrect old offset.  Expected %zu, but got %ld, ",
		      test_modes[i].old_off, offset);
	      ret |= 1;
	    }
	  else
	    printf ("old offset = %ld, ", offset);

	  written = fputs_func (data, fp);

	  if (written == EOF)
	    {
	      printf ("fputs[1] failed to write data\n");
	      ret |= 1;
	    }

	  /* After this write, the offset in append modes should factor in the
	     implicit lseek to the end of file.  */
	  offset = ftell (fp);
	  if (offset != test_modes[i].new_off)
	    {
	      printf ("Incorrect new offset.  Expected %zu, but got %ld\n",
		      test_modes[i].new_off, offset);
	      ret |= 1;
	    }
	  else
	    printf ("new offset = %ld\n", offset);
	}
    }
  return ret;
}

/* Test that the value of ftell is not cached when the stream handle is not
   active.  */
static int
do_ftell_test (const char *filename)
{
  int ret = 0;
  struct test
    {
      const char *mode;
      int fd_mode;
      size_t old_off;
      size_t new_off;
      size_t eof_off;
    } test_modes[] = {
	  /* In w, w+ and r+ modes, the file position should be at the
	     beginning of the file.  After the write, the offset should be
	     updated to data_len.  We don't use eof_off in w and a modes since
	     they don't allow reading.  */
	  {"w", O_WRONLY | O_TRUNC, 0, data_len, 0},
	  {"w+", O_RDWR | O_TRUNC, 0, data_len, 2 * data_len},
	  {"r+", O_RDWR, 0, data_len, 3 * data_len},
	  /* For the 'a' mode, the initial file position should be the
	     current end of file. After the write, the offset has data_len
	     added to the old value.  For a+ mode however, the initial file
	     position is the file position of the underlying file descriptor,
	     since it is initially assumed to be in read mode.  */
	  {"a", O_WRONLY, 3 * data_len, 4 * data_len, 5 * data_len},
	  {"a+", O_RDWR, 0, 5 * data_len, 6 * data_len},
    };
  for (int j = 0; j < 2; j++)
    {
      for (int i = 0; i < sizeof (test_modes) / sizeof (struct test); i++)
	{
	  FILE *fp;
	  int fd;
	  int fileret;

	  printf ("\tftell: %s (file, \"%s\"): ", j == 0 ? "fdopen" : "fopen",
		  test_modes[i].mode);

	  if (j == 0)
	    fileret = get_handles_fdopen (filename, fd, fp,
					  test_modes[i].fd_mode,
					  test_modes[i].mode);
	  else
	    fileret = get_handles_fopen (filename, fd, fp, test_modes[i].mode);

	  if (fileret != 0)
	    return fileret;

	  long off = ftell (fp);
	  if (off != test_modes[i].old_off)
	    {
	      printf ("Incorrect old offset.  Expected %zu but got %ld, ",
		      test_modes[i].old_off, off);
	      ret |= 1;
	    }
	  else
	    printf ("old offset = %ld, ", off);

	  /* The effect of this write on the offset should be seen in the ftell
	     call that follows it.  */
	  int write_ret = write (fd, data, data_len);
	  if (write_ret != data_len)
	    {
	      printf ("write failed (%m)\n");
	      ret |= 1;
	    }
	  off = ftell (fp);

	  if (off != test_modes[i].new_off)
	    {
	      printf ("Incorrect new offset.  Expected %zu but got %ld",
		      test_modes[i].new_off, off);
	      ret |= 1;
	    }
	  else
	    printf ("new offset = %ld", off);

	  /* Read to the end, write some data to the fd and check if ftell can
	     see the new ofset.  Do this test only for files that allow
	     reading.  */
	  if (test_modes[i].fd_mode != O_WRONLY)
	    {
	      wchar_t tmpbuf[data_len];

	      rewind (fp);

	      while (fgets_func (tmpbuf, data_len, fp) && !feof (fp));

	      write_ret = write (fd, data, data_len);
	      if (write_ret != data_len)
		{
		  printf ("write failed (%m)\n");
		  ret |= 1;
		}
	      off = ftell (fp);

	      if (off != test_modes[i].eof_off)
		{
		  printf (", Incorrect offset after read EOF.  "
			  "Expected %zu but got %ld\n",
			  test_modes[i].eof_off, off);
		  ret |= 1;
		}
	      else
		printf (", offset after EOF = %ld\n", off);
	    }
	  else
	    putc ('\n', stdout);

	  fclose (fp);
	}
    }

  return ret;
}

/* This test opens the file for writing, moves the file offset of the
   underlying file, writes out data and then checks if ftell trips on it.  */
static int
do_write_test (const char *filename)
{
  FILE *fp = NULL;
  int fd;
  int ret = 0;
  struct test
    {
      const char *mode;
      int fd_mode;
    } test_modes[] = {
	  {"w", O_WRONLY | O_TRUNC},
	  {"w+", O_RDWR | O_TRUNC},
	  {"r+", O_RDWR}
    };

  for (int j = 0; j < 2; j++)
    {
      for (int i = 0; i < sizeof (test_modes) / sizeof (struct test); i++)
	{
	  int fileret;
	  printf ("\twrite: %s (file, \"%s\"): ", j == 0 ? "fopen" : "fdopen",
		  test_modes[i].mode);

	  if (j == 0)
	    fileret = get_handles_fopen (filename, fd, fp, test_modes[i].mode);
	  else
	    fileret = get_handles_fdopen (filename, fd, fp,
					  test_modes[i].fd_mode,
					  test_modes[i].mode);

	  if (fileret != 0)
	    return fileret;

	  /* Move offset to just before the end of the file.  */
	  off_t seek_ret = lseek (fd, file_len - 1, SEEK_SET);
	  if (seek_ret == -1)
	    {
	      printf ("lseek failed: %m\n");
	      ret |= 1;
	    }

	  /* Write some data.  */
	  size_t written = fputs_func (data, fp);

	  if (written == EOF)
	    {
	      printf ("fputs[1] failed to write data\n");
	      ret |= 1;
	    }

	  /* Verify that the offset points to the end of the file.  The length
	     of the file would be the original length + the length of data
	     written to it - the amount by which we moved the offset using
	     lseek.  */
	  long offset = ftell (fp);
	  file_len = file_len - 1 + data_len;

	  if (offset != file_len)
	    {
	      printf ("Incorrect offset.  Expected %zu, but got %ld\n",
		      file_len, offset);

	      ret |= 1;
	    }

	  printf ("offset = %ld\n", offset);
	  fclose (fp);
	}
    }

  return ret;
}

/* This test opens a file in append mode, writes some data, and then verifies
   that ftell does not trip over it.  */
static int
do_append_test (const char *filename)
{
  FILE *fp = NULL;
  int ret = 0;
  int fd;

  struct test
    {
      const char *mode;
      int fd_mode;
    } test_modes[] = {
	  {"a", O_WRONLY},
	  {"a+", O_RDWR}
    };

  for (int j = 0; j < 2; j++)
    {
      for (int i = 0; i < sizeof (test_modes) / sizeof (struct test); i++)
	{
	  int fileret;

	  printf ("\tappend: %s (file, \"%s\"): ", j == 0 ? "fopen" : "fdopen",
		  test_modes[i].mode);

	  if (j == 0)
	    fileret = get_handles_fopen (filename, fd, fp, test_modes[i].mode);
	  else
	    fileret = get_handles_fdopen (filename, fd, fp,
					  test_modes[i].fd_mode,
					  test_modes[i].mode);

	  if (fileret != 0)
	    return fileret;

	  /* Write some data.  */
	  size_t written = fputs_func (data, fp);

	  if (written == EOF)
	    {
	      printf ("fputs[1] failed to write all data\n");
	      ret |= 1;
	    }

	  /* Verify that the offset points to the end of the file.  The file
	     len is maintained by adding data_len each time to reflect the data
	     written to it.  */
	  long offset = ftell (fp);
	  file_len += data_len;

	  if (offset != file_len)
	    {
	      printf ("Incorrect offset.  Expected %zu, but got %ld\n",
		      file_len, offset);

	      ret |= 1;
	    }

	  printf ("offset = %ld\n", offset);
	  fclose (fp);
	}
    }

  /* For fdopen in 'a' mode, the file descriptor should not change if the file
     is already open with the O_APPEND flag set.  */
  fd = open (filename, O_WRONLY | O_APPEND, 0);
  if (fd == -1)
    {
      printf ("open(O_APPEND) failed: %m\n");
      return 1;
    }

  off_t seek_ret = lseek (fd, file_len - 1, SEEK_SET);
  if (seek_ret == -1)
    {
      printf ("lseek[O_APPEND][0] failed: %m\n");
      ret |= 1;
    }

  fp = fdopen (fd, "a");
  if (fp == NULL)
    {
      printf ("fdopen(O_APPEND) failed: %m\n");
      close (fd);
      return 1;
    }

  off_t new_seek_ret = lseek (fd, 0, SEEK_CUR);
  if (seek_ret == -1)
    {
      printf ("lseek[O_APPEND][1] failed: %m\n");
      ret |= 1;
    }

  printf ("\tappend: fdopen (file, \"a\"): O_APPEND: ");

  if (seek_ret != new_seek_ret)
    {
      printf ("incorrectly modified file offset to %jd, should be %jd",
	      (intmax_t)  new_seek_ret, (intmax_t) seek_ret);
      ret |= 1;
    }
  else
    printf ("retained current file offset %jd", (intmax_t) seek_ret);

  new_seek_ret = ftello (fp);

  if (seek_ret != new_seek_ret)
    {
      printf (", ftello reported incorrect offset %jd, should be %jd\n",
	      (intmax_t) new_seek_ret, (intmax_t) seek_ret);
      ret |= 1;
    }
  else
    printf (", ftello reported correct offset %jd\n", (intmax_t) seek_ret);

  fclose (fp);

  return ret;
}

static int
do_one_test (const char *filename)
{
  int ret = 0;

  ret |= do_ftell_test (filename);
  ret |= do_write_test (filename);
  ret |= do_append_test (filename);
  ret |= do_rewind_test (filename);
  ret |= do_ftruncate_test (filename);

  return ret;
}

/* Run a set of tests for ftell for regular files and wide mode files.  */
static int
do_test (void)
{
  int ret = 0;
  FILE *fp = NULL;
  char *filename;
  size_t written;
  int fd = create_temp_file ("tst-active-handler-tmp.", &filename);

  if (fd == -1)
    {
      printf ("create_temp_file: %m\n");
      return 1;
    }

  fp = fdopen (fd, "w");
  if (fp == NULL)
    {
      printf ("fdopen[0]: %m\n");
      close (fd);
      return 1;
    }

  data = char_data;
  data_len = strlen (char_data);
  file_len = strlen (char_data);
  written = fputs (data, fp);

  if (written == EOF)
    {
      printf ("fputs[1] failed to write data\n");
      ret = 1;
    }

  fclose (fp);
  if (ret)
    return ret;

  /* Tests for regular files.  */
  puts ("Regular mode:");
  fputs_func = (fputs_func_t) fputs;
  fgets_func = (fgets_func_t) fgets;
  data = char_data;
  data_len = strlen (char_data);
  ret |= do_one_test (filename);

  /* Truncate the file before repeating the tests in wide mode.  */
  fp = fopen (filename, "w");
  if (fp == NULL)
    {
      printf ("fopen failed %m\n");
      return 1;
    }
  fclose (fp);

  /* Tests for wide files.  */
  puts ("Wide mode:");
  if (setlocale (LC_ALL, "en_US.UTF-8") == NULL)
    {
      printf ("Cannot set en_US.UTF-8 locale.\n");
      return 1;
    }
  fputs_func = (fputs_func_t) fputws;
  fgets_func = (fgets_func_t) fgetws;
  data = wide_data;
  data_len = wcslen (wide_data);
  ret |= do_one_test (filename);

  return ret;
}