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 <unistd.h>
0034 #include <fcntl.h>
0035 #include <limits.h>
0036 #include <stdio.h>
0037 #include <unistd.h>
0038 #include <errno.h>
0039 #include <sys/stat.h>
0040 #include <time.h>
0041 #include <stdint.h>
0042 #include <math.h>
0043 
0044 #include "fstest.h"
0045 #include "fs_config.h"
0046 #include <tmacros.h>
0047 
0048 const char rtems_test_name[] = "FSLINK " FILESYSTEM;
0049 const RTEMS_TEST_STATE rtems_test_state = TEST_STATE;
0050 
0051 /*
0052  * Test if the successful call works as expect
0053  */
0054 static void link_test01 (void)
0055 {
0056   char *name0 = "t0";
0057   char *name1 = "t1";
0058   char *name2 = "t2";
0059 
0060 
0061   int status;
0062   int fd;
0063 
0064   mode_t mode = 0644;
0065   struct stat statbuf;
0066 
0067 
0068   puts ("link creates hardlinks");
0069   fd = creat (name0, mode);
0070   status = close (fd);
0071   rtems_test_assert (status == 0);
0072 
0073   status = stat (name0, &statbuf);
0074   rtems_test_assert (status == 0);
0075   rtems_test_assert (statbuf.st_nlink == 1);
0076 
0077   puts ("test if the stat is the same");
0078   status = link (name0, name1);
0079   rtems_test_assert (status == 0);
0080 
0081   status = stat (name0, &statbuf);
0082   rtems_test_assert (status == 0);
0083 
0084   rtems_test_assert (S_ISREG (statbuf.st_mode));
0085   rtems_test_assert (statbuf.st_nlink == 2);
0086 
0087   status = stat (name1, &statbuf);
0088   rtems_test_assert (status == 0);
0089 
0090   rtems_test_assert (S_ISREG (statbuf.st_mode));
0091   rtems_test_assert (statbuf.st_nlink == 2);
0092 
0093   /*
0094    * link the file and check the nlink
0095    */
0096   status = link (name1, name2);
0097   rtems_test_assert (status == 0);
0098 
0099   status = stat (name0, &statbuf);
0100   rtems_test_assert (status == 0);
0101 
0102   rtems_test_assert (S_ISREG (statbuf.st_mode));
0103   rtems_test_assert (statbuf.st_nlink == 3);
0104 
0105   status = stat (name1, &statbuf);
0106   rtems_test_assert (status == 0);
0107 
0108   rtems_test_assert (S_ISREG (statbuf.st_mode));
0109   rtems_test_assert (statbuf.st_nlink == 3);
0110 
0111   status = stat (name2, &statbuf);
0112   rtems_test_assert (status == 0);
0113 
0114   rtems_test_assert (S_ISREG (statbuf.st_mode));
0115   rtems_test_assert (statbuf.st_nlink == 3);
0116 
0117   /*
0118    *  call chmod and chown and test.
0119    */
0120   puts ("chmod and chown");
0121 
0122   chown (name1, 65534, 65533);
0123 
0124   status = stat (name0, &statbuf);
0125   rtems_test_assert (status == 0);
0126 
0127   rtems_test_assert (S_ISREG (statbuf.st_mode));
0128   rtems_test_assert (statbuf.st_nlink == 3);
0129   rtems_test_assert (statbuf.st_uid = 65534);
0130   rtems_test_assert (statbuf.st_gid = 65533);
0131 
0132   status = stat (name1, &statbuf);
0133   rtems_test_assert (status == 0);
0134 
0135   rtems_test_assert (S_ISREG (statbuf.st_mode));
0136   rtems_test_assert (statbuf.st_nlink == 3);
0137   rtems_test_assert (statbuf.st_uid = 65534);
0138   rtems_test_assert (statbuf.st_gid = 65533);
0139 
0140   status = stat (name2, &statbuf);
0141   rtems_test_assert (status == 0);
0142 
0143   rtems_test_assert (S_ISREG (statbuf.st_mode));
0144   rtems_test_assert (statbuf.st_nlink == 3);
0145   rtems_test_assert (statbuf.st_uid = 65534);
0146   rtems_test_assert (statbuf.st_gid = 65533);
0147 
0148   /*
0149    *
0150    *  unlink then test if the nlink changes
0151    */
0152   puts ("unlink then stat the file ");
0153 
0154   status = unlink (name0);
0155   rtems_test_assert (status == 0);
0156 
0157   status = stat (name0, &statbuf);
0158   rtems_test_assert (status == -1);
0159   rtems_test_assert (errno = ENOENT);
0160 
0161   status = stat (name1, &statbuf);
0162   rtems_test_assert (status == 0);
0163   rtems_test_assert (S_ISREG (statbuf.st_mode));
0164   rtems_test_assert (statbuf.st_nlink == 2);
0165 
0166   status = stat (name2, &statbuf);
0167   rtems_test_assert (status == 0);
0168   rtems_test_assert (S_ISREG (statbuf.st_mode));
0169   rtems_test_assert (statbuf.st_nlink == 2);
0170 
0171   status = unlink (name1);
0172   rtems_test_assert (status == 0);
0173 
0174   status = unlink (name2);
0175   rtems_test_assert (status == 0);
0176 
0177 }
0178 
0179 void test (void)
0180 {
0181   link_test01 ();
0182 }