Whamcloud - gitweb
LU-4970 tests: turn up debugging for sanity-lfsck/14
[fs/lustre-release.git] / lustre / liblustre / tests / test_common.c
index a216d07..1395df3 100644 (file)
@@ -1,3 +1,37 @@
+/*
+ * GPL HEADER START
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
+ *
+ * This program 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
+ * General Public License version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; If not, see
+ * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ *
+ * GPL HEADER END
+ */
+/*
+ * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Use is subject to license terms.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
+ */
+
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
@@ -7,6 +41,10 @@
 #include <string.h>
 #include <errno.h>
 #include <dirent.h>
+#include <utime.h>
+#include <stdarg.h>
+
+#include <liblustre.h>
 
 #include "test_common.h"
 
@@ -16,6 +54,10 @@ int exit_on_err = 1;
  * util functions
  ******************************************************************/
 
+#ifdef EXIT
+#undef EXIT
+#endif
+
 #define EXIT(err)                                      \
        do {                                            \
                if (exit_on_err)                        \
@@ -89,7 +131,7 @@ void t_mkdir(const char *path)
 {
         int rc;
 
-        rc = mkdir(path, 00644);
+        rc = mkdir(path, 00755);
         if (rc < 0) {
                 printf("mkdir(%s) error: %s\n", path, strerror(errno));
                 EXIT(1);
@@ -173,7 +215,7 @@ int t_open(const char *path)
 {
         int fd;
 
-        fd = open(path, O_RDWR);
+        fd = open(path, O_RDWR | O_LARGEFILE);
         if (fd < 0) {
                 printf("open(%s) error: %s\n", path, strerror(errno));
                 EXIT_RET(fd);
@@ -181,6 +223,27 @@ int t_open(const char *path)
         return fd;
 }
 
+int t_chdir(const char *path)
+{
+        int rc = chdir(path);
+        if (rc < 0) {
+                printf("chdir(%s) error: %s\n", path, strerror(errno));
+                EXIT_RET(rc);
+        }
+        return rc;
+}
+
+int t_utime(const char *path, const struct utimbuf *buf)
+{
+        int rc = utime(path, buf);
+        if (rc < 0) {
+                printf("utime(%s, %p) error: %s\n", path, buf,
+                       strerror(errno));
+                EXIT_RET(rc);
+        }
+        return rc;
+}
+
 int t_opendir(const char *path)
 {
         int fd;
@@ -209,6 +272,8 @@ int t_check_stat(const char *name, struct stat *buf)
        struct stat stat;
         int rc;
 
+        memset(&stat, 0, sizeof(stat));
+
        rc = lstat(name, &stat);
         if (rc) {
                printf("error %d stat %s\n", rc, name);
@@ -216,6 +281,10 @@ int t_check_stat(const char *name, struct stat *buf)
        }
         if (buf)
                 memcpy(buf, &stat, sizeof(*buf));
+        if (stat.st_blksize == 0) {
+                printf("error: blksize is 0\n");
+                EXIT_RET(-EINVAL);
+        }
 
        return 0;
 }
@@ -302,7 +371,7 @@ void t_ls(int fd, char *buf, int size)
        while ((rc = getdirentries64(fd, buf, size, &base)) > 0) {
                pos = 0;
                while (pos < rc) {
-                       ent = (struct dirent64 *) ((char*) buf + pos);
+                       ent = (struct dirent64 *)((char *)buf + pos);
                        printf("%s\n", ent->d_name);
                        pos += ent->d_reclen;
                }
@@ -314,6 +383,79 @@ void t_ls(int fd, char *buf, int size)
        }
 }
 
+int t_fcntl(int fd, int cmd, ...)
+{
+       va_list ap;
+       long arg;
+       struct flock *lock;
+       int rc = -1;
+
+       va_start(ap, cmd);
+       switch (cmd) {
+       case F_GETFL:
+               va_end(ap);
+               rc = fcntl(fd, cmd);
+               if (rc == -1) {
+                       printf("fcntl GETFL failed: %s\n",
+                                strerror(errno));
+                       EXIT(1);
+               }
+               break;
+       case F_SETFL:
+               arg = va_arg(ap, long);
+               va_end(ap);
+               rc = fcntl(fd, cmd, arg);
+               if (rc == -1) {
+                       printf("fcntl SETFL %ld failed: %s\n",
+                                arg, strerror(errno));
+                       EXIT(1);
+               }
+               break;
+       case F_GETLK:
+#ifdef F_GETLK64
+#if F_GETLK64 != F_GETLK
+        case F_GETLK64:
+#endif
+#endif
+       case F_SETLK:
+#ifdef F_SETLK64
+#if F_SETLK64 != F_SETLK
+        case F_SETLK64:
+#endif
+#endif
+       case F_SETLKW:
+#ifdef F_SETLKW64
+#if F_SETLKW64 != F_SETLKW
+        case F_SETLKW64:
+#endif
+#endif
+               lock = va_arg(ap, struct flock *);
+               va_end(ap);
+               rc = fcntl(fd, cmd, lock);
+               if (rc == -1) {
+                       printf("fcntl cmd %d failed: %s\n",
+                                cmd, strerror(errno));
+                       EXIT(1);
+               }
+               break;
+       case F_DUPFD:
+               arg = va_arg(ap, long);
+               va_end(ap);
+               rc = fcntl(fd, cmd, arg);
+               if (rc == -1) {
+                       printf("fcntl F_DUPFD %d failed: %s\n",
+                                (int)arg, strerror(errno));
+                       EXIT(1);
+               }
+               break;
+       default:
+               va_end(ap);
+               printf("fcntl cmd %d not supported\n", cmd);
+               EXIT(1);
+       }
+       return rc;
+}
+
 char *safe_strncpy(char *dst, char *src, int max_size)
 {
        int src_size;