"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-link.c" (15 Mar 2019, 3977 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-link.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.09.52_vs_0.09.54.

    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_link_unlink()
   29  *  remove all links
   30  */
   31 static void stress_link_unlink(
   32     const args_t *args,
   33     const uint64_t n)
   34 {
   35     uint64_t i;
   36 
   37     for (i = 0; i < n; i++) {
   38         char path[PATH_MAX];
   39 
   40         (void)stress_temp_filename_args(args,
   41             path, sizeof(path), i);
   42         (void)unlink(path);
   43     }
   44     (void)sync();
   45 }
   46 
   47 /*
   48  *  stress_link_generic
   49  *  stress links, generic case
   50  */
   51 static int stress_link_generic(
   52     const args_t *args,
   53     int (*linkfunc)(const char *oldpath, const char *newpath),
   54     const char *funcname)
   55 {
   56     int rc, ret, fd;
   57     char oldpath[PATH_MAX];
   58     size_t oldpathlen;
   59     bool symlink_func = (linkfunc == symlink);
   60 
   61     ret = stress_temp_dir_mk_args(args);
   62     if (ret < 0)
   63         return exit_status(-ret);
   64     (void)stress_temp_filename_args(args, oldpath, sizeof(oldpath), ~0);
   65     if ((fd = open(oldpath, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) < 0) {
   66         ret = exit_status(errno);
   67         pr_fail_err("open");
   68         (void)stress_temp_dir_rm_args(args);
   69         return ret;
   70     }
   71     (void)close(fd);
   72 
   73     oldpathlen = strlen(oldpath);
   74 
   75     rc = EXIT_SUCCESS;
   76     do {
   77         uint64_t i, n = DEFAULT_LINKS;
   78 
   79         for (i = 0; i < n; i++) {
   80             char newpath[PATH_MAX];
   81             struct stat stbuf;
   82 
   83             (void)stress_temp_filename_args(args,
   84                 newpath, sizeof(newpath), i);
   85             if (linkfunc(oldpath, newpath) < 0) {
   86                 rc = exit_status(errno);
   87                 pr_fail_err(funcname);
   88                 n = i;
   89                 break;
   90             }
   91             if (symlink_func) {
   92                 char buf[PATH_MAX];
   93                 ssize_t rret;
   94 
   95                 rret = readlink(newpath, buf, sizeof(buf) - 1);
   96                 if (rret < 0) {
   97                     rc = exit_status(errno);
   98                     pr_fail_err("readlink");
   99                 } else {
  100                     newpath[rret] = '\0';
  101                     if ((size_t)rret != oldpathlen)
  102                         pr_fail_err("readlink length error");
  103                     else
  104                         if (strncmp(oldpath, buf, rret))
  105                             pr_fail_err("readlink path error");
  106                 }
  107             }
  108             if (lstat(newpath, &stbuf) < 0) {
  109                 rc = exit_status(errno);
  110                 pr_fail_err("lstat");
  111             }
  112 
  113             if (!keep_stressing())
  114                 goto abort;
  115 
  116             inc_counter(args);
  117         }
  118         stress_link_unlink(args, n);
  119     } while (keep_stressing());
  120 
  121 abort:
  122     /* force unlink of all files */
  123     pr_tidy("%s: removing %" PRIu32" entries\n", args->name, DEFAULT_LINKS);
  124     stress_link_unlink(args, DEFAULT_LINKS);
  125     (void)unlink(oldpath);
  126     (void)stress_temp_dir_rm_args(args);
  127 
  128     return rc;
  129 }
  130 
  131 #if !defined(__HAIKU__)
  132 /*
  133  *  stress_link
  134  *  stress hard links
  135  */
  136 static int stress_link(const args_t *args)
  137 {
  138     return stress_link_generic(args, link, "link");
  139 }
  140 #endif
  141 
  142 /*
  143  *  stress_symlink
  144  *  stress symbolic links
  145  */
  146 static int stress_symlink(const args_t *args)
  147 {
  148     return stress_link_generic(args, symlink, "symlink");
  149 }
  150 
  151 #if !defined(__HAIKU__)
  152 stressor_info_t stress_link_info = {
  153     .stressor = stress_link,
  154     .class = CLASS_FILESYSTEM | CLASS_OS
  155 };
  156 #else
  157 stressor_info_t stress_link_info = {
  158     .stressor = stress_not_implemented,
  159     .class = CLASS_FILESYSTEM | CLASS_OS
  160 };
  161 #endif
  162 
  163 stressor_info_t stress_symlink_info = {
  164     .stressor = stress_symlink,
  165     .class = CLASS_FILESYSTEM | CLASS_OS
  166 };