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[] = "FSRENAMEEXISTING " FILESYSTEM;
0046 const RTEMS_TEST_STATE rtems_test_state = TEST_STATE;
0047 
0048 static void rename_error (void)
0049 {
0050 
0051   int fd;
0052   int status;
0053   char *name01 = "name01";
0054   char *name02 = "name02";
0055   char *name03 = "name03";
0056   char *nonexistence = "name04";
0057 
0058   char name[20];
0059 
0060   mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
0061   const char *wd = __func__;
0062 
0063   /*
0064    *Create a new directory and change the current directory to  this
0065    */
0066 
0067   status = mkdir (wd, mode);
0068   rtems_test_assert (status == 0);
0069   status = chdir (wd);
0070   rtems_test_assert (status == 0);
0071 
0072 
0073   /*
0074    * Create a new directory and a new directory in it
0075    */
0076 
0077   status = mkdir (name01, mode);
0078   rtems_test_assert (status == 0);
0079   status = mkdir (name02, mode);
0080   rtems_test_assert (status == 0);
0081   sprintf (name, "%s/%s", name01, name03);
0082   status = mkdir (name, mode);
0083   rtems_test_assert (status == 0);
0084 
0085   /*
0086    * The new directory pathname contains a path prefix
0087    *  that names the old directory.
0088    */
0089   EXPECT_ERROR (EINVAL, rename, name01, name);
0090   /*
0091    * The new argument points to a directory and
0092    *  the old argument points to a file that is not a directory.
0093    */
0094   fd = creat (name03, mode);
0095   status = close (fd);
0096   rtems_test_assert (status == 0);
0097   EXPECT_ERROR (EISDIR, rename, name03, name02);
0098 
0099   /*
0100    * The link named by old does not name an existing file,
0101    *    or either old or new points to an empty string.
0102    */
0103 
0104   EXPECT_ERROR (ENOENT, rename, nonexistence, name01);
0105   EXPECT_ERROR (ENOENT, rename, "", name01);
0106 
0107   /*
0108    * A component of either path prefix is not a directory;
0109    * or the old argument names a directory and new argument names
0110    *  a non-directory file.
0111    */
0112 
0113   sprintf (name, "%s/%s", name03, name01);
0114   EXPECT_ERROR (ENOTDIR, rename, name, name03);
0115   EXPECT_ERROR (ENOTDIR, rename, name02, name03);
0116 
0117   /*
0118    * Go back to parent directory
0119    */
0120   status = chdir ("..");
0121   rtems_test_assert (status == 0);
0122 }
0123 
0124 static void same_file_test (void)
0125 {
0126   int fd;
0127   int status;
0128 
0129   const char *name01 = "name01";
0130 
0131   mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
0132   const char *wd = __func__;
0133 
0134   /*
0135    * Create a new directory and change the current directory to this
0136    */
0137 
0138   status = mkdir (wd, mode);
0139   rtems_test_assert (status == 0);
0140   status = chdir (wd);
0141   rtems_test_assert (status == 0);
0142 
0143   /*
0144    * The new argument points to a file and
0145    * the old argument points to the same file on the same directory.
0146    */
0147 
0148   puts ("\nRename file with itself\n");
0149 
0150   fd = creat (name01, mode);
0151   rtems_test_assert (fd >= 0);
0152   status = close (fd);
0153   rtems_test_assert (status == 0);
0154 
0155   EXPECT_EQUAL (0, rename, name01, name01);
0156 
0157   /*
0158    * Clear directory
0159    */
0160 
0161   EXPECT_EQUAL (0, unlink, name01);
0162 
0163   /*
0164    * Go back to parent directory
0165    */
0166 
0167   status = chdir ("..");
0168   rtems_test_assert (status == 0);
0169 
0170   /*
0171    * Remove test directory
0172    */
0173 
0174   status = rmdir (wd);
0175   rtems_test_assert (status == 0);
0176 }
0177 
0178 
0179 static void directory_test (void)
0180 {
0181   int fd;
0182   int status;
0183   int rv;
0184 
0185   const char *name01 = "name01";
0186 
0187   const char *dir01 = "dir01";
0188   const char *dir02 = "dir02";
0189 
0190   char path01[30];
0191 
0192   mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
0193   const char *wd = __func__;
0194 
0195   /*
0196    * Create a new directory and change the current directory to this
0197    */
0198 
0199   status = mkdir (wd, mode);
0200   rtems_test_assert (status == 0);
0201   status = chdir (wd);
0202   rtems_test_assert (status == 0);
0203 
0204   /*
0205    * The new argument points to a file and
0206    * the old argument points to a directory.
0207    */
0208 
0209   puts ("\nRename directory with file\n");
0210 
0211   fd = creat (name01, mode);
0212   rtems_test_assert (fd >= 0);
0213   status = close (fd);
0214   rtems_test_assert (status == 0);
0215 
0216   status = mkdir (dir01, mode);
0217   rtems_test_assert (status == 0);
0218 
0219   EXPECT_ERROR (ENOTDIR, rename, dir01, name01);
0220 
0221   /*
0222    * Clear directory
0223    */
0224 
0225   EXPECT_EQUAL (0, unlink, name01);
0226   EXPECT_EQUAL (0, rmdir, dir01);
0227 
0228   /*
0229    * The new argument points to a directory and
0230    * the old argument points to a file.
0231    */
0232 
0233   puts ("\nRename file with directory\n");
0234 
0235   fd = creat (name01, mode);
0236   rtems_test_assert (fd >= 0);
0237   status = close (fd);
0238   rtems_test_assert (status == 0);
0239 
0240   status = mkdir (dir01, mode);
0241   rtems_test_assert (status == 0);
0242 
0243   EXPECT_ERROR (EISDIR, rename, name01, dir01);
0244 
0245   /*
0246    * Clear directory
0247    */
0248 
0249   EXPECT_EQUAL (0, unlink, name01);
0250   EXPECT_EQUAL (0, rmdir, dir01);
0251 
0252   /*
0253    * The new argument points to an empty directory and
0254    * the old argument points to an ancestor directory of new.
0255    */
0256 
0257   puts ("\nRename directory with ancestor directory\n");
0258 
0259   status = mkdir (dir02, mode);
0260   rtems_test_assert (status == 0);
0261 
0262   rv = snprintf (path01, sizeof(path01), "%s/%s", dir02, dir01);
0263   rtems_test_assert (rv < sizeof(path01));
0264   status = mkdir (path01, mode);
0265   rtems_test_assert (status == 0);
0266 
0267   EXPECT_ERROR (EINVAL, rename, dir02, path01);
0268 
0269   /*
0270    * Clear directory
0271    */
0272 
0273   EXPECT_EQUAL (0, rmdir, path01);
0274   EXPECT_EQUAL (0, rmdir, dir02);
0275 
0276   /*
0277    * The new argument points to an empty directory and
0278    * the old argument points to other empty directory.
0279    */
0280 
0281   puts ("\nRename empty directory with another empty directory\n");
0282 
0283   status = mkdir (dir01, mode);
0284   rtems_test_assert (status == 0);
0285 
0286   status = mkdir (dir02, mode);
0287   rtems_test_assert (status == 0);
0288 
0289   EXPECT_EQUAL (0, rename, dir01, dir02);
0290 
0291   /*
0292    * Clear directory
0293    */
0294 
0295   EXPECT_EQUAL (-1, rmdir, dir01);
0296   EXPECT_EQUAL (0, rmdir, dir02);
0297 
0298   /*
0299    * Go back to parent directory
0300    */
0301 
0302   status = chdir ("..");
0303   rtems_test_assert (status == 0);
0304 
0305   /*
0306    * Remove test directory
0307    */
0308 
0309   status = rmdir (wd);
0310   rtems_test_assert (status == 0);
0311 }
0312 
0313 static void arg_format_test (void)
0314 {
0315   int fd;
0316   int status;
0317   const char *name01 = "name01";
0318 
0319   const char *dir01 = "dir01";
0320 
0321   mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
0322   const char *wd = __func__;
0323 
0324   /*
0325    * Create a new directory and change the current directory to this
0326    */
0327 
0328   status = mkdir (wd, mode);
0329   rtems_test_assert (status == 0);
0330   status = chdir (wd);
0331   rtems_test_assert (status == 0);
0332 
0333   /*
0334    * The new argument points to a directory and
0335    * the old argument points to current directory.
0336    */
0337 
0338   puts ("\nRename directory with current directory\n");
0339 
0340   status = mkdir (dir01, mode);
0341   rtems_test_assert (status == 0);
0342 
0343   EXPECT_EQUAL (-1, rename, "." , dir01);
0344 
0345   puts("Testing errno for EINVAL or EBUSY");
0346 
0347   if (errno == EINVAL || errno == EBUSY) {
0348     FS_PASS ();
0349   } else {
0350     FS_FAIL ();
0351   }
0352 
0353   /*
0354    * The new argument points to current directory and
0355    * the old argument points to a directory.
0356    */
0357 
0358   EXPECT_EQUAL (-1, rename, dir01, ".");
0359 
0360   puts("Testing errno for EINVAL or EBUSY");
0361 
0362   if (errno == EINVAL || errno == EBUSY) {
0363     FS_PASS ();
0364   } else {
0365     FS_FAIL ();
0366   }
0367 
0368   /*
0369    * The new argument points to a directory and
0370    * the old argument points to previous directory.
0371    */
0372 
0373   puts ("\nRename directory with previous directory\n");
0374 
0375   EXPECT_EQUAL (-1, rename, ".." , dir01);
0376 
0377   puts("Testing errno for EINVAL or EBUSY");
0378 
0379   if (errno == EINVAL || errno == EBUSY) {
0380     FS_PASS ();
0381   } else {
0382     FS_FAIL ();
0383   }
0384 
0385   /*
0386    * The new argument points to previous directory and
0387    * the old argument points to a directory.
0388    */
0389 
0390   EXPECT_EQUAL (-1, rename, dir01, "..");
0391 
0392   puts("Testing errno for EINVAL or EBUSY");
0393 
0394   if (errno == EINVAL || errno == EBUSY) {
0395     FS_PASS ();
0396   } else {
0397     FS_FAIL ();
0398   }
0399 
0400   /*
0401    * Clear directory
0402    */
0403 
0404   EXPECT_EQUAL (0, rmdir, dir01);
0405 
0406   /*
0407    * The new argument points to a file and
0408    * the old argument is an empty string.
0409    */
0410 
0411   puts("\nTesting empty filepaths\n");
0412 
0413   fd = creat (name01, mode);
0414   rtems_test_assert (fd >= 0);
0415   status = close (fd);
0416   rtems_test_assert (status == 0);
0417 
0418   EXPECT_ERROR (ENOENT, rename, name01, "");
0419 
0420   /*
0421    * Clear directory
0422    */
0423 
0424   EXPECT_EQUAL (0, unlink, name01);
0425 
0426   /*
0427    * The new argument is an empty string and
0428    * the old argument points to a file.
0429    */
0430 
0431   fd = creat (name01, mode);
0432   rtems_test_assert (fd >= 0);
0433   status = close (fd);
0434   rtems_test_assert (status == 0);
0435 
0436   EXPECT_ERROR (ENOENT, rename, "", name01);
0437 
0438   /*
0439    * Clear directory
0440    */
0441 
0442   EXPECT_EQUAL (0, unlink, name01);
0443 
0444   /*
0445    * Go back to parent directory
0446    */
0447 
0448   status = chdir ("..");
0449   rtems_test_assert (status == 0);
0450 
0451   /*
0452    * Remove test directory
0453    */
0454 
0455   status = rmdir (wd);
0456   rtems_test_assert (status == 0);
0457 }
0458 
0459 void test (void)
0460 {
0461   rename_error ();
0462   same_file_test ();
0463   directory_test ();
0464   arg_format_test ();
0465 }