"Fossies" - the Fresh Open Source Software Archive

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

    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_rename()
   29  *  stress system by renames
   30  */
   31 static int stress_rename(const args_t *args)
   32 {
   33     char name1[PATH_MAX], name2[PATH_MAX];
   34     char *oldname = name1, *newname = name2, *tmpname;
   35     FILE *fp;
   36     uint64_t i = 0;
   37     const uint32_t inst1 = args->instance * 2;
   38     const uint32_t inst2 = inst1 + 1;
   39 
   40     if (stress_temp_dir_mk(args->name, args->pid, inst1) < 0)
   41         return EXIT_FAILURE;
   42     if (stress_temp_dir_mk(args->name, args->pid, inst2) < 0) {
   43         (void)stress_temp_dir_rm(args->name, args->pid, inst1);
   44         return EXIT_FAILURE;
   45     }
   46 restart:
   47     (void)stress_temp_filename(oldname, PATH_MAX,
   48         args->name, args->pid, inst1, i++);
   49 
   50     if ((fp = fopen(oldname, "w+")) == NULL) {
   51         int rc = exit_status(errno);
   52         pr_err("%s: fopen failed: errno=%d: (%s)\n",
   53             args->name, errno, strerror(errno));
   54         (void)stress_temp_dir_rm(args->name, args->pid, inst1);
   55         (void)stress_temp_dir_rm(args->name, args->pid, inst2);
   56         return rc;
   57     }
   58     (void)fclose(fp);
   59 
   60     for (;;) {
   61         (void)stress_temp_filename(newname, PATH_MAX,
   62             args->name, args->pid, inst2, i++);
   63         if (rename(oldname, newname) < 0) {
   64             (void)unlink(oldname);
   65             (void)unlink(newname);
   66             goto restart;
   67         }
   68 
   69         tmpname = oldname;
   70         oldname = newname;
   71         newname = tmpname;
   72         inc_counter(args);
   73         if (!keep_stressing())
   74             break;
   75 
   76         (void)stress_temp_filename(newname, PATH_MAX,
   77             args->name, args->pid, inst1, i++);
   78         if (rename(oldname, newname) < 0) {
   79             (void)unlink(oldname);
   80             (void)unlink(newname);
   81             goto restart;
   82         }
   83 
   84         tmpname = oldname;
   85         oldname = newname;
   86         newname = tmpname;
   87         inc_counter(args);
   88         if (!keep_stressing())
   89             break;
   90 
   91 #if defined(HAVE_RENAMEAT) && defined(O_DIRECTORY)
   92         {
   93             int oldfd;
   94 
   95             (void)stress_temp_filename(newname, PATH_MAX,
   96                 args->name, args->pid, inst1, i++);
   97 
   98             oldfd = open(".", O_RDONLY | O_DIRECTORY);
   99             if (oldfd < 0) {
  100                 (void)unlink(oldname);
  101                 (void)unlink(newname);
  102                 goto restart;
  103             }
  104             if (renameat(oldfd, oldname, AT_FDCWD, newname) < 0) {
  105                 (void)close(oldfd);
  106                 (void)unlink(oldname);
  107                 (void)unlink(newname);
  108                 goto restart;
  109             }
  110             (void)shim_fsync(oldfd);
  111             (void)close(oldfd);
  112             tmpname = oldname;
  113             oldname = newname;
  114             newname = tmpname;
  115 
  116             inc_counter(args);
  117             if (!keep_stressing())
  118                 break;
  119         }
  120 #endif
  121 
  122 #if defined(HAVE_RENAMEAT2) && defined(O_DIRECTORY) && defined(RENAME_NOREPLACE)
  123         {
  124             int oldfd;
  125 
  126             (void)stress_temp_filename(newname, PATH_MAX,
  127                 args->name, args->pid, inst1, i++);
  128 
  129             oldfd = open(".", O_RDONLY | O_DIRECTORY);
  130             if (oldfd < 0) {
  131                 (void)unlink(oldname);
  132                 (void)unlink(newname);
  133                 goto restart;
  134             }
  135             if (renameat2(oldfd, oldname, AT_FDCWD, newname, RENAME_NOREPLACE) < 0) {
  136                 (void)close(oldfd);
  137                 (void)unlink(oldname);
  138                 (void)unlink(newname);
  139                 goto restart;
  140             }
  141             (void)close(oldfd);
  142             tmpname = oldname;
  143             oldname = newname;
  144             newname = tmpname;
  145 
  146             inc_counter(args);
  147             if (!keep_stressing())
  148                 break;
  149         }
  150 #endif
  151     }
  152 
  153     (void)unlink(oldname);
  154     (void)unlink(newname);
  155     (void)stress_temp_dir_rm(args->name, args->pid, inst1);
  156     (void)stress_temp_dir_rm(args->name, args->pid, inst2);
  157 
  158     return EXIT_SUCCESS;
  159 }
  160 
  161 stressor_info_t stress_rename_info = {
  162     .stressor = stress_rename,
  163     .class = CLASS_FILESYSTEM | CLASS_OS,
  164 };