From 5733cc4bcb436bbb7c676173c72db182ae00fab9 Mon Sep 17 00:00:00 2001 From: Raghavendra Bhat Date: Fri, 29 Jul 2011 12:56:59 +0530 Subject: programs to truncate a file based on path as well as fd. Can be enhanced to write some truncate based application. --- INFO | 2 ++ c_pgms/truncate/fd_truncate.c | 30 ++++++++++++++++++++++++++++++ c_pgms/truncate/truncate_file.c | 26 ++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 c_pgms/truncate/fd_truncate.c create mode 100644 c_pgms/truncate/truncate_file.c diff --git a/INFO b/INFO index 0da07cb..0120f94 100644 --- a/INFO +++ b/INFO @@ -21,6 +21,8 @@ c_pgms/threaded_io ---> program for opening a file from multiple threads and eac c_pgms/create_bench(create.c) ---> benchmarking application for taking average time duration needed for the creation of different types of files such as regular file, directory, FIFO, character and block device special files c_pgms/inotify.c ---> program which monitors a directory and sends mail whenever some files are created under it. Used for sending nightly sanity results. + +c_pgms/trucate ---> programs which truncates a file based on both path as well as fd on a file. Can be enhanced in future to write some truncate based applications. Used to verify bug 3077. ============================================================================================================================================ legacy/performance ---> legacy scripts (not currently in use) for getting performance statistics diff --git a/c_pgms/truncate/fd_truncate.c b/c_pgms/truncate/fd_truncate.c new file mode 100644 index 0000000..1425ed0 --- /dev/null +++ b/c_pgms/truncate/fd_truncate.c @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +int main () +{ + int ret = -1; + int fd = -1; + char *str = "This is a string"; + char pwd[255] = {0, }; + + fd = open ("test_file", O_RDWR); + if (fd == -1) { + fprintf (stderr, "ERROR: open failed on the file test_file (%s)", + strerror (errno)); + } + ret = ftruncate (fd, 10); + if (ret == -1) { + fprintf (stderr, "ERROR: truncate failed on the file %s (%s)", + pwd, strerror (errno)); + } + + if (fd > 0) + close (fd); +} diff --git a/c_pgms/truncate/truncate_file.c b/c_pgms/truncate/truncate_file.c new file mode 100644 index 0000000..d6f0b60 --- /dev/null +++ b/c_pgms/truncate/truncate_file.c @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +int main () +{ + int ret = -1; + int fd = -1; + char *str = "This is a string"; + char pwd[255] = {0, }; + + getcwd (pwd, sizeof (pwd)); + + strcat (pwd, "/test_file"); + + ret = truncate (pwd, 10); + if (ret == -1) { + fprintf (stderr, "ERROR: truncate failed on the file %s (%s)", + pwd, strerror (errno)); + } +} -- cgit