about summary refs log tree commit diff
path: root/libio/tst-ftell-active-handler.c
diff options
context:
space:
mode:
authorSiddhesh Poyarekar <siddhesh@redhat.com>2014-03-17 18:42:53 +0530
committerSiddhesh Poyarekar <siddhesh@redhat.com>2014-03-17 21:24:02 +0530
commitae42bbc55a9e05976269026ddabcfb917f6e922f (patch)
tree3d5c26ff77263cada4610720a1ea591c63185b1d /libio/tst-ftell-active-handler.c
parentea33158c96c53a64402a772186956c1f5cb556ae (diff)
downloadglibc-ae42bbc55a9e05976269026ddabcfb917f6e922f.tar.gz
glibc-ae42bbc55a9e05976269026ddabcfb917f6e922f.tar.xz
glibc-ae42bbc55a9e05976269026ddabcfb917f6e922f.zip
Change offset in fdopen only if setting O_APPEND
fdopen should only be allowed to change the offset in the file it
attaches to if it is setting O_APPEND.  If O_APPEND is already set, it
should not change the state of the handle.
Diffstat (limited to 'libio/tst-ftell-active-handler.c')
-rw-r--r--libio/tst-ftell-active-handler.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/libio/tst-ftell-active-handler.c b/libio/tst-ftell-active-handler.c
index 40ca58c9e2..e9dc7b3d52 100644
--- a/libio/tst-ftell-active-handler.c
+++ b/libio/tst-ftell-active-handler.c
@@ -414,6 +414,61 @@ do_append_test (const char *filename)
 	}
     }
 
+  /* 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 %ld, should be %ld",
+	      new_seek_ret, seek_ret);
+      ret |= 1;
+    }
+  else
+    printf ("retained current file offset %ld", seek_ret);
+
+  new_seek_ret = ftello (fp);
+
+  if (seek_ret != new_seek_ret)
+    {
+      printf (", ftello reported incorrect offset %ld, should be %ld\n",
+	      new_seek_ret, seek_ret);
+      ret |= 1;
+    }
+  else
+    printf (", ftello reported correct offset %ld\n", seek_ret);
+
+  fclose (fp);
+
   return ret;
 }