home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / a / bin / fileutil.12 / fileutil / fileutils-3.12 / src / mvdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-01  |  6.3 KB  |  237 lines

  1. /* mvdir -- rename directory on System V r<4
  2.    Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    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., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Helper program for GNU mv on systems that lack the rename system call.
  19.  
  20.    Usage: mvdir from to
  21.  
  22.    FROM must be an existing directory.
  23.    TO must not exist, but its parent must exist.
  24.    This program performs the necessary sanity checking on its arguments.
  25.  
  26.    Must be installed setuid root.
  27.  
  28.    Ian Dall (ian@sibyl.eleceng.ua.oz.au)
  29.    and David MacKenzie (djm@gnu.ai.mit.edu) */
  30.  
  31. #include <config.h>
  32. #include <stdio.h>
  33. #include <getopt.h>
  34. #include <sys/types.h>
  35. #include <signal.h>
  36. #include "system.h"
  37. #include "version.h"
  38. #include "safe-stat.h"
  39.  
  40. #ifndef HIPRI
  41. #define HIPRI -10
  42. #endif
  43.  
  44. #ifdef DEBUG
  45. #define link(FROM, TO) (printf("Linking %s to %s\n", FROM, TO), 0)
  46. #define unlink(FILE) (printf("Unlinking %s\n", FILE), 0)
  47. #endif
  48.  
  49. /* The name this program was run with. */
  50. char *program_name;
  51.  
  52. char *getcwd ();
  53.  
  54. char *basename ();
  55. char *dirname ();
  56. char *xmalloc ();
  57. void error ();
  58. void strip_trailing_slashes ();
  59.  
  60. static char *fullpath ();
  61.  
  62. /* If non-zero, display usage information and exit.  */
  63. static int show_help;
  64.  
  65. /* If non-zero, print the version on standard output and exit.  */
  66. static int show_version;
  67.  
  68. static struct option const long_options[] =
  69. {
  70.   {"help", no_argument, &show_help, 1},
  71.   {"version", no_argument, &show_version, 1},
  72.   {0, 0, 0, 0}
  73. };
  74.  
  75. static void
  76. usage (status)
  77.      int status;
  78. {
  79.   if (status != 0)
  80.     fprintf (stderr, "Try `%s --help' for more information.\n",
  81.          program_name);
  82.   else
  83.     {
  84.       printf ("Usage: %s [OPTION]... EXISTING_DIR NEW_DIR\n", program_name);
  85.       printf ("\
  86. \n\
  87.    --help      display this help and exit\n\
  88.    --version   output version information and exit\n");
  89.     }
  90.   exit (status);
  91. }
  92.  
  93. void
  94. main (argc, argv)
  95.      int argc;
  96.      char **argv;
  97. {
  98.   char *from, *from_parent, *from_base;
  99.   char *to, *to_parent, *to_parent_path, *to_base, *to_dotdot;
  100.   struct stat from_stats, to_stats;
  101.   char *slash;
  102.   int c, i;
  103.  
  104.   program_name = argv[0];
  105.  
  106.   while ((c = getopt_long (argc, argv, "", long_options, (int *) 0)) != EOF)
  107.     {
  108.       switch (c)
  109.     {
  110.     case 0:
  111.       break;
  112.  
  113.     default:
  114.       usage (2);
  115.     }
  116.     }
  117.  
  118.   if (show_version)
  119.     {
  120.       printf ("%s\n", version_string);
  121.       exit (0);
  122.     }
  123.  
  124.   if (show_help)
  125.     usage (0);
  126.  
  127.   if (argc - optind != 2)
  128.     usage (2);
  129.  
  130.   from = argv[optind];
  131.   to = argv[optind + 1];
  132.   strip_trailing_slashes (from);
  133.   strip_trailing_slashes (to);
  134.   from_parent = dirname (from);
  135.   to_parent = dirname (to);
  136.   if (!from_parent || !to_parent)
  137.     error (2, 0, "virtual memory exhausted");
  138.  
  139.   /* Make sure `from' is not "." or "..". */
  140.   from_base = basename (from);
  141.   if (!strcmp (from_base, ".") || !strcmp (from_base, ".."))
  142.     error (1, 0, "cannot rename `.' or `..'");
  143.  
  144.   /* Even with an effective uid of root, link fails if the target exists.
  145.      That is what we want, so don't unlink `to' first.
  146.      However, we do need to check that the directories that link and unlink
  147.      will modify exist and are writable by the user. */
  148.  
  149.   if (SAFE_STAT (from, &from_stats))
  150.     error (1, errno, "%s", from);
  151.   if (!S_ISDIR (from_stats.st_mode))
  152.     error (1, 0, "`%s' is not a directory", from);
  153.   if (access (from_parent, W_OK))
  154.     error (1, errno, "cannot write to `%s'", from_parent);
  155.   if (access (to_parent, W_OK))
  156.     error (1, errno, "cannot write to `%s'", to_parent);
  157.  
  158.   /* To prevent disconnecting the tree rooted at `from' from its parent,
  159.      quit if any of the directories in `to' are the same (dev and ino)
  160.      as the directory `from'. */
  161.  
  162.   slash = to_parent_path = fullpath (to_parent);
  163.   while (1)
  164.     {
  165.       while (*slash == '/')
  166.     ++slash;
  167.       slash = index (slash, '/');
  168.       if (slash != NULL)
  169.     *slash = '\0';
  170.  
  171.       if (SAFE_STAT (to_parent_path, &to_stats))
  172.     error (1, errno, "%s", to_parent_path);
  173.       if (to_stats.st_dev == from_stats.st_dev
  174.       && to_stats.st_ino == from_stats.st_ino)
  175.     error (1, 0, "`%s' is an ancestor of `%s'", from, to);
  176.       if (slash != NULL)
  177.     *slash++ = '/';
  178.       else
  179.     break;
  180.     }
  181.  
  182.   /* We can't make the renaming atomic, but we do our best. */
  183.   for (i = NSIG; i > 0; i--)
  184.     if (i != SIGKILL)
  185.       signal (i, SIG_IGN);
  186.   setuid (0);            /* Make real uid 0 so it is harder to kill. */
  187.   nice (HIPRI - nice (0));    /* Raise priority. */
  188.  
  189.   if (link (from, to))
  190.     error (1, errno, "cannot link `%s' to `%s'", from, to);
  191.   if (unlink (from))
  192.     error (1, errno, "cannot unlink `%s'", from);
  193.  
  194.   /* Replace the directory's `..' entry.  It used to be a link to
  195.      the parent of `from'; make it a link to the parent of `to' instead.
  196.      To handle the case where `from' is the current directory
  197.      and `to' starts with `../', we go to the full path of `to's parent,
  198.      lest we try to reference the new directory's `..' while creating it.  */
  199.  
  200.   to_base = basename (to);
  201.   i = strlen (to_base);
  202.   to_dotdot = xmalloc (i + 4);
  203.   strcpy (to_dotdot, to_base);
  204.   strcpy (to_dotdot + i, "/..");
  205.  
  206.   if (chdir (to_parent_path))
  207.     error (1, errno, "%s", to_parent_path);
  208.   if (unlink (to_dotdot) && errno != ENOENT)
  209.     error (1, errno, "cannot unlink `%s'", to_dotdot);
  210.   if (link (".", to_dotdot))
  211.     error (1, errno, "cannot link `%s' to `%s'", ".", to_dotdot);
  212.  
  213.   exit (0);
  214. }
  215.  
  216. /* Return the full pathname (from /) of the directory DIR,
  217.    as static data. */
  218.  
  219. static char *
  220. fullpath (dir)
  221.      char *dir;
  222. {
  223.   char wd[PATH_MAX + 2];
  224.   static char path[PATH_MAX + 2];
  225.  
  226.   if (getcwd (wd, PATH_MAX + 2) == NULL)
  227.     error (1, errno, "cannot get current directory");
  228.   if (chdir (dir))
  229.     error (1, errno, "%s", dir);
  230.   if (getcwd (path, PATH_MAX + 2) == NULL)
  231.     error (1, errno, "cannot get current directory");
  232.   if (chdir (wd))
  233.     error (1, errno, "%s", wd);
  234.  
  235.   return path;
  236. }
  237.