Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:31

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  COPYRIGHT (c) 1989-2011.
0005  *  On-Line Applications Research Corporation (OAR).
0006  *
0007  * Redistribution and use in source and binary forms, with or without
0008  * modification, are permitted provided that the following conditions
0009  * are met:
0010  * 1. Redistributions of source code must retain the above copyright
0011  *    notice, this list of conditions and the following disclaimer.
0012  * 2. Redistributions in binary form must reproduce the above copyright
0013  *    notice, this list of conditions and the following disclaimer in the
0014  *    documentation and/or other materials provided with the distribution.
0015  *
0016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0017  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0019  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0020  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0021  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0022  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0023  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0024  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0025  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0026  * POSSIBILITY OF SUCH DAMAGE.
0027  */
0028 
0029 #ifdef HAVE_CONFIG_H
0030 #include "config.h"
0031 #endif
0032 
0033 #include <sys/stat.h>
0034 #include <limits.h>
0035 #include <fcntl.h>
0036 #include <errno.h>
0037 #include <stdio.h>
0038 #include <stdint.h>
0039 #include <unistd.h>
0040 
0041 #include "fstest.h"
0042 #include "fs_config.h"
0043 #include <tmacros.h>
0044 
0045 const char rtems_test_name[] = "FSRENAMEMAXLINKS " FILESYSTEM;
0046 const RTEMS_TEST_STATE rtems_test_state = TEST_STATE;
0047 
0048 void test (void)
0049 {
0050   int status;
0051   int rv;
0052   int i;
0053 
0054   const char *dir01 = "dir01";
0055   const char *dir02 = "dir02";
0056 
0057   char path01[30];
0058 
0059   char link_name[10];
0060 
0061   mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
0062   const char *wd = __func__;
0063 
0064   struct stat statbuf;
0065 
0066   long LINK_MAX_val;
0067 
0068   /*
0069    * Create a new directory and change the current directory to this
0070    */
0071 
0072   status = mkdir (wd, mode);
0073   rtems_test_assert (status == 0);
0074   status = chdir (wd);
0075   rtems_test_assert (status == 0);
0076 
0077   /*
0078    * The new argument points to a non existant directory and
0079    * the old argument points to an existant directory at LINK_MAX.
0080    */
0081 
0082   status = mkdir (dir01, mode);
0083   rtems_test_assert (status == 0);
0084 
0085   LINK_MAX_val = pathconf (dir01, _PC_LINK_MAX);
0086   rtems_test_assert (LINK_MAX_val >= 0);
0087 
0088   status = stat (dir01, &statbuf);
0089   rtems_test_assert (status == 0);
0090 
0091   for(i = statbuf.st_nlink; i < LINK_MAX_val; i++)
0092   {
0093     rv = snprintf (link_name, sizeof(link_name), "%s/%d", dir01, i);
0094     rtems_test_assert (rv < sizeof(link_name));
0095 
0096     status = mkdir (link_name, mode);
0097     rtems_test_assert (status == 0);
0098   }
0099 
0100   status = mkdir (dir02, mode);
0101   rtems_test_assert (status == 0);
0102 
0103   rv = snprintf (path01, sizeof(path01), "%s/%s", dir01, dir01);
0104   rtems_test_assert (rv < sizeof(path01));
0105   EXPECT_ERROR (EMLINK, rename, dir02, path01);
0106 
0107   /*
0108    * Clear directory
0109    */
0110 
0111   for(i = statbuf.st_nlink; i < LINK_MAX_val; i++)
0112   {
0113     rv = snprintf (link_name, sizeof(link_name), "%s/%d", dir01, i);
0114     rtems_test_assert (rv < sizeof(link_name));
0115 
0116     status = rmdir (link_name);
0117     rtems_test_assert (status == 0);
0118   }
0119 
0120   EXPECT_EQUAL (-1, rmdir, path01);
0121   EXPECT_EQUAL (0, rmdir, dir02);
0122   EXPECT_EQUAL (0, rmdir, dir01);
0123 
0124   /*
0125    * Go back to parent directory
0126    */
0127 
0128   status = chdir ("..");
0129   rtems_test_assert (status == 0);
0130 
0131   /*
0132    * Remove test directory
0133    */
0134 
0135   status = rmdir (wd);
0136   rtems_test_assert (status == 0);
0137 }