"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-utime.c" (15 Mar 2019, 4354 Bytes) of package /linux/privat/stress-ng-0.09.56.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "stress-utime.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.09.50_vs_0.09.51.

    1 /*
    2  * Copyright (C) 2013-2019 Canonical, Ltd.
    3  *
    4  * This program is free software; you can redistribute it and/or
    5  * modify it under the terms of the GNU General Public License
    6  * as published by the Free Software Foundation; either version 2
    7  * of the License, or (at your option) any later version.
    8  *
    9  * This program is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  * GNU General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License
   15  * along with this program; if not, write to the Free Software
   16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
   17  *
   18  * This code is a complete clean re-write of the stress tool by
   19  * Colin Ian King <colin.king@canonical.com> and attempts to be
   20  * backwardly compatible with the stress tool by Amos Waterland
   21  * <apw@rossby.metr.ou.edu> but has more stress tests and more
   22  * functionality.
   23  *
   24  */
   25 #include "stress-ng.h"
   26 
   27 /*
   28  *  stress_utime()
   29  *  stress system by setting file utime
   30  */
   31 static int stress_utime(const args_t *args)
   32 {
   33     char filename[PATH_MAX];
   34     int ret, fd;
   35 
   36     ret = stress_temp_dir_mk_args(args);
   37     if (ret < 0)
   38         return exit_status(-ret);
   39 
   40     (void)stress_temp_filename_args(args,
   41         filename, sizeof(filename), mwc32());
   42     if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
   43         ret = exit_status(errno);
   44         pr_err("%s: open failed: errno=%d: (%s)\n",
   45             args->name, errno, strerror(errno));
   46         (void)stress_temp_dir_rm_args(args);
   47         return ret;
   48     }
   49 
   50     do {
   51         struct timeval timevals[2];
   52 #if (defined(HAVE_FUTIMENS) || defined(HAVE_UTIMENSAT)) && \
   53     (defined(UTIME_NOW) || defined(UTIME_OMIT))
   54         struct timespec ts[2];
   55 #endif
   56 
   57         if (gettimeofday(&timevals[0], NULL) == 0) {
   58             timevals[1] = timevals[0];
   59             if (utimes(filename, timevals) < 0) {
   60                 pr_dbg("%s: utimes failed: errno=%d: (%s)\n",
   61                     args->name, errno, strerror(errno));
   62                 break;
   63             }
   64         }
   65         if (utimes(filename, NULL) < 0) {
   66             pr_dbg("%s: utimes failed: errno=%d: (%s)\n",
   67                 args->name, errno, strerror(errno));
   68             break;
   69         }
   70 #if defined(HAVE_FUTIMENS)
   71         if (futimens(fd, NULL) < 0) {
   72             pr_dbg("%s: futimens failed: errno=%d: (%s)\n",
   73                 args->name, errno, strerror(errno));
   74             break;
   75         }
   76 
   77 #if defined(UTIME_NOW)
   78         ts[0].tv_sec = UTIME_NOW;
   79         ts[0].tv_nsec = UTIME_NOW;
   80         ts[1].tv_sec = UTIME_NOW;
   81         ts[1].tv_nsec = UTIME_NOW;
   82         if (futimens(fd, &ts[0]) < 0) {
   83             pr_dbg("%s: futimens failed: errno=%d: (%s)\n",
   84                 args->name, errno, strerror(errno));
   85             break;
   86         }
   87 #endif
   88 
   89 #if defined(UTIME_OMIT)
   90         ts[0].tv_sec = UTIME_OMIT;
   91         ts[0].tv_nsec = UTIME_OMIT;
   92         if (futimens(fd, &ts[0]) < 0) {
   93             pr_dbg("%s: futimens failed: errno=%d: (%s)\n",
   94                 args->name, errno, strerror(errno));
   95             break;
   96         }
   97 #endif
   98 #endif
   99 
  100 #if defined(HAVE_UTIMENSAT)
  101 
  102 #if defined(UTIME_NOW)
  103         ts[0].tv_sec = UTIME_NOW;
  104         ts[0].tv_nsec = UTIME_NOW;
  105 
  106         ts[1].tv_sec = UTIME_NOW;
  107         ts[1].tv_nsec = UTIME_NOW;
  108 
  109         (void)utimensat(AT_FDCWD, filename, ts, 0);
  110 #endif
  111 
  112 #if defined(UTIME_OMIT)
  113         ts[1].tv_nsec = UTIME_OMIT;
  114         (void)utimensat(AT_FDCWD, filename, ts, 0);
  115 #endif
  116 
  117 #if defined(AT_SYMLINK_NOFOLLOW)
  118 #if defined(UTIME_NOW)
  119         ts[1].tv_nsec = UTIME_NOW;
  120         (void)utimensat(AT_FDCWD, filename, ts, AT_SYMLINK_NOFOLLOW);
  121 #endif
  122 #if defined(UTIME_OMIT)
  123         ts[1].tv_nsec = UTIME_OMIT;
  124         (void)utimensat(AT_FDCWD, filename, ts, AT_SYMLINK_NOFOLLOW);
  125 #endif
  126 #endif
  127 #endif
  128         if (g_opt_flags & OPT_FLAGS_UTIME_FSYNC)
  129             (void)shim_fsync(fd);
  130 
  131 #if defined(HAVE_UTIME_H)
  132         {
  133             struct utimbuf utbuf;
  134 
  135             utbuf.actime = (time_t)time_now();
  136             utbuf.modtime = utbuf.actime;
  137 
  138             if (utime(filename, &utbuf) < 0) {
  139                 pr_dbg("%s: utime failed: errno=%d: (%s)\n",
  140                     args->name, errno, strerror(errno));
  141                 break;
  142             }
  143             if (utime(filename, NULL) < 0) {
  144                 pr_dbg("%s: utime failed: errno=%d: (%s)\n",
  145                     args->name, errno, strerror(errno));
  146                 break;
  147             }
  148         }
  149 #endif
  150         /* forces metadata writeback */
  151         if (g_opt_flags & OPT_FLAGS_UTIME_FSYNC)
  152             (void)shim_fsync(fd);
  153         inc_counter(args);
  154     } while (keep_stressing());
  155 
  156     (void)close(fd);
  157     (void)unlink(filename);
  158     (void)stress_temp_dir_rm_args(args);
  159 
  160     return EXIT_SUCCESS;
  161 }
  162 
  163 stressor_info_t stress_utime_info = {
  164     .stressor = stress_utime,
  165     .class = CLASS_FILESYSTEM | CLASS_OS
  166 };