123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- /*
- * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
- * The President and Fellows of Harvard College.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
- /*
- * rmdirtest.c
- *
- * Tests file system synchronization and directory implementation by
- * removing the current directory under itself and then trying to do
- * things. It's ok for most of those things to fail, but the system
- * shouldn't crash.
- */
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <limits.h>
- #include <err.h>
- static const char testdir[] = "testdir";
- static char startpoint[PATH_MAX - sizeof(testdir)];
- /*
- * Create the test directory, and change into it, remembering
- * where we came from.
- */
- static
- void
- startup(void)
- {
- if (getcwd(startpoint, sizeof(startpoint))==NULL) {
- err(1, "getcwd (not in test dir)");
- }
- if (mkdir(testdir, 0775) < 0) {
- err(1, "%s: mkdir", testdir);
- }
- if (chdir(testdir) < 0) {
- err(1, "%s: chdir", testdir);
- }
- }
- /*
- * Remove the test directory.
- *
- * Note that even though it's the current directory, we can't do it
- * with rmdir(".") - what that would try to do is remove the "." entry
- * from the current directory, which is justifiably prohibited.
- */
- static
- void
- killdir(void)
- {
- char tmp[PATH_MAX];
- snprintf(tmp, sizeof(tmp), "%s/%s", startpoint, testdir);
- if (rmdir(tmp)<0) {
- err(1, "%s: rmdir", tmp);
- }
- }
- /*
- * Leave the test directory and go back to where we came from, so we
- * can try again.
- */
- static
- void
- finish(void)
- {
- if (chdir(startpoint)<0) {
- err(1, "%s: chdir", startpoint);
- }
- }
- /*************************************************************/
- /*
- * Basic test - just try removing the directory without doing anything
- * evil.
- */
- static
- void
- test1(void)
- {
- printf("Making %s\n", testdir);
- startup();
- printf("Removing %s while in it\n", testdir);
- killdir();
- printf("Leaving the test directory\n");
- finish();
- }
- /*
- * Now do it while we also have the directory open.
- */
- static
- void
- test2(void)
- {
- int fd;
- printf("Now trying with the directory open...\n");
- startup();
- fd = open(".", O_RDONLY);
- if (fd<0) {
- err(1, ".: open");
- }
- killdir();
- finish();
- /* close *after* leaving, just for excitement */
- if (close(fd)<0) {
- err(1, "removed %s: close", testdir);
- }
- }
- /*
- * Now see if . and .. work after rmdir.
- */
- static
- void
- test3(void)
- {
- char buf[PATH_MAX];
- int fd;
- printf("Checking if . exists after rmdir\n");
- startup();
- killdir();
- fd = open(".", O_RDONLY);
- if (fd<0) {
- switch (errno) {
- case EINVAL:
- case EIO:
- case ENOENT:
- break;
- default:
- err(1, ".");
- break;
- }
- }
- else {
- close(fd);
- }
- fd = open("..", O_RDONLY);
- if (fd<0) {
- switch (errno) {
- case EINVAL:
- case EIO:
- case ENOENT:
- break;
- default:
- err(1, "..");
- break;
- }
- }
- else {
- warnx("..: openable after rmdir - might be bad");
- close(fd);
- }
- snprintf(buf, sizeof(buf), "../%s", testdir);
- fd = open(buf, O_RDONLY);
- if (fd<0) {
- switch (errno) {
- case EINVAL:
- case EIO:
- case ENOENT:
- break;
- default:
- err(1, "%s", buf);
- break;
- }
- }
- else {
- errx(1, "%s: works after rmdir", buf);
- }
- finish();
- }
- /*
- * Now try to create files.
- */
- static
- void
- test4(void)
- {
- char buf[4096];
- int fd;
- printf("Checking if creating files works after rmdir...\n");
- startup();
- killdir();
- fd = open("newfile", O_WRONLY|O_CREAT|O_TRUNC, 0664);
- if (fd<0) {
- switch (errno) {
- case EINVAL:
- case EIO:
- case ENOENT:
- break;
- default:
- err(1, "%s", buf);
- break;
- }
- }
- else {
- warnx("newfile: creating files after rmdir works");
- warnx("(this is only ok if the space gets reclaimed)");
- /*
- * Waste a bunch of space so we'll be able to tell
- */
- memset(buf, 'J', sizeof(buf));
- write(fd, buf, sizeof(buf));
- write(fd, buf, sizeof(buf));
- write(fd, buf, sizeof(buf));
- write(fd, buf, sizeof(buf));
- close(fd);
- }
- finish();
- }
- /*
- * Now try to create directories.
- */
- static
- void
- test5(void)
- {
- printf("Checking if creating subdirs works after rmdir...\n");
- startup();
- killdir();
- if (mkdir("newdir", 0775)<0) {
- switch (errno) {
- case EINVAL:
- case EIO:
- case ENOENT:
- break;
- default:
- err(1, "mkdir in removed dir");
- break;
- }
- }
- else {
- warnx("newfile: creating directories after rmdir works");
- warnx("(this is only ok if the space gets reclaimed)");
- /*
- * Waste a bunch of space so we'll be able to tell
- */
- mkdir("newdir/t0", 0775);
- mkdir("newdir/t1", 0775);
- mkdir("newdir/t2", 0775);
- mkdir("newdir/t3", 0775);
- mkdir("newdir/t4", 0775);
- mkdir("newdir/t5", 0775);
- }
- finish();
- }
- /*
- * Now try listing the directory.
- */
- static
- void
- test6(void)
- {
- char buf[PATH_MAX];
- int fd, len;
- printf("Now trying to list the directory...\n");
- startup();
- fd = open(".", O_RDONLY);
- if (fd<0) {
- err(1, ".: open");
- }
- killdir();
- while ((len = getdirentry(fd, buf, sizeof(buf)-1))>0) {
- if ((unsigned)len >= sizeof(buf)-1) {
- errx(1, ".: getdirentry: returned invalid length");
- }
- buf[len] = 0;
- if (!strcmp(buf, ".") || !strcmp(buf, "..")) {
- /* these are allowed to appear */
- continue;
- }
- errx(1, ".: getdirentry: returned unexpected name %s", buf);
- }
- if (len==0) {
- /* EOF - ok */
- }
- else { /* len < 0 */
- switch (errno) {
- case EINVAL:
- case EIO:
- break;
- default:
- err(1, ".: getdirentry");
- break;
- }
- }
- finish();
- /* close *after* leaving, just for excitement */
- if (close(fd)<0) {
- err(1, "removed %s: close", testdir);
- }
- }
- /*
- * Try getcwd.
- */
- static
- void
- test7(void)
- {
- char buf[PATH_MAX];
- startup();
- killdir();
- if (getcwd(buf, sizeof(buf))==NULL) {
- switch (errno) {
- case EINVAL:
- case EIO:
- case ENOENT:
- break;
- default:
- err(1, "getcwd after removing %s", testdir);
- break;
- }
- }
- else {
- errx(1, "getcwd after removing %s: succeeded (got %s)",
- testdir, buf);
- }
- finish();
- }
- /**************************************************************/
- int
- main(void)
- {
- test1();
- test2();
- test3();
- test4();
- test5();
- test6();
- test7();
- printf("Whew... survived.\n");
- return 0;
- }
|