home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / fileutils-3.6 / src / mvdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-14  |  6.0 KB  |  225 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 <stdio.h>
  32. #include <getopt.h>
  33. #include <sys/types.h>
  34. #include <signal.h>
  35. #include "system.h"
  36. #include "version.h"
  37.  
  38. #ifndef HIPRI
  39. #define HIPRI -10
  40. #endif
  41.  
  42. #ifdef DEBUG
  43. #define link(FROM, TO) (printf("Linking %s to %s\n", FROM, TO), 0)
  44. #define unlink(FILE) (printf("Unlinking %s\n", FILE), 0)
  45. #endif
  46.  
  47. /* The name this program was run with. */
  48. char *program_name;
  49.  
  50. char *getcwd ();
  51.  
  52. char *basename ();
  53. char *dirname ();
  54. char *xmalloc ();
  55. void error ();
  56. void strip_trailing_slashes ();
  57.  
  58. static char *fullpath ();
  59.  
  60. /* If non-zero, display usage information and exit.  */
  61. static int flag_help;
  62.  
  63. /* If non-zero, print the version on standard error.  */
  64. static int flag_version;
  65.  
  66. static struct option const long_options[] =
  67. {
  68.   {"help", no_argument, &flag_help, 1},
  69.   {"version", no_argument, &flag_version, 1},
  70.   {0, 0, 0, 0}
  71. };
  72.  
  73. static void
  74. usage ()
  75. {
  76.   fprintf (stderr, "Usage: %s [--help] [--version] existing-dir new-dir\n",
  77.        program_name);
  78.   exit (2);
  79. }
  80.  
  81. void
  82. main (argc, argv)
  83.      int argc;
  84.      char **argv;
  85. {
  86.   char *from, *from_parent, *from_base;
  87.   char *to, *to_parent, *to_parent_path, *to_base, *to_dotdot;
  88.   struct stat from_stats, to_stats;
  89.   char *slash;
  90.   int c, i;
  91.  
  92.   program_name = argv[0];
  93.  
  94.   while ((c = getopt_long (argc, argv, "", long_options, (int *) 0)) != EOF)
  95.     {
  96.       switch (c)
  97.     {
  98.     case 0:
  99.       break;
  100.  
  101.     default:
  102.       usage ();
  103.     }
  104.     }
  105.  
  106.   if (flag_version)
  107.     {
  108.       fprintf (stderr, "%s\n", version_string);
  109.       exit (0);
  110.     }
  111.  
  112.   if (flag_help)
  113.     usage ();
  114.  
  115.   if (argc - optind != 2)
  116.     usage ();
  117.  
  118.   from = argv[optind];
  119.   to = argv[optind + 1];
  120.   strip_trailing_slashes (from);
  121.   strip_trailing_slashes (to);
  122.   from_parent = dirname (from);
  123.   to_parent = dirname (to);
  124.   if (!from_parent || !to_parent)
  125.     error (2, 0, "virtual memory exhausted");
  126.  
  127.   /* Make sure `from' is not "." or "..". */
  128.   from_base = basename (from);
  129.   if (!strcmp (from_base, ".") || !strcmp (from_base, ".."))
  130.     error (1, 0, "cannot rename `.' or `..'");
  131.   
  132.   /* Even with an effective uid of root, link fails if the target exists.
  133.      That is what we want, so don't unlink `to' first.
  134.      However, we do need to check that the directories that link and unlink
  135.      will modify exist and are writable by the user. */
  136.  
  137.   if (stat (from, &from_stats))
  138.     error (1, errno, "%s", from);
  139.   if (!S_ISDIR (from_stats.st_mode))
  140.     error (1, 0, "`%s' is not a directory", from);
  141.   if (access (from_parent, W_OK))
  142.     error (1, errno, "cannot write to `%s'", from_parent);
  143.   if (access (to_parent, W_OK))
  144.     error (1, errno, "cannot write to `%s'", to_parent);
  145.  
  146.   /* To prevent disconnecting the tree rooted at `from' from its parent,
  147.      quit if any of the directories in `to' are the same (dev and ino)
  148.      as the directory `from'. */
  149.   
  150.   slash = to_parent_path = fullpath (to_parent);
  151.   while (1)
  152.     {
  153.       while (*slash == '/')
  154.     ++slash;
  155.       slash = index (slash, '/');
  156.       if (slash != NULL)
  157.     *slash = '\0';
  158.  
  159.       if (stat (to_parent_path, &to_stats))
  160.     error (1, errno, "%s", to_parent_path);
  161.       if (to_stats.st_dev == from_stats.st_dev
  162.       && to_stats.st_ino == from_stats.st_ino)
  163.     error (1, 0, "`%s' is an ancestor of `%s'", from, to);
  164.       if (slash != NULL)
  165.     *slash++ = '/';
  166.       else
  167.     break;
  168.     }
  169.  
  170.   /* We can't make the renaming atomic, but we do our best. */
  171.   for (i = NSIG; i > 0; i--)
  172.     if (i != SIGKILL)
  173.       signal (i, SIG_IGN);
  174.   setuid (0);            /* Make real uid 0 so it is harder to kill. */
  175.   nice (HIPRI - nice (0));    /* Raise priority. */
  176.  
  177.   if (link (from, to))
  178.     error (1, errno, "cannot link `%s' to `%s'", from, to);
  179.   if (unlink (from))
  180.     error (1, errno, "cannot unlink `%s'", from);
  181.  
  182.   /* Replace the directory's `..' entry.  It used to be a link to
  183.      the parent of `from'; make it a link to the parent of `to' instead.
  184.      To handle the case where `from' is the current directory
  185.      and `to' starts with `../', we go to the full path of `to's parent,
  186.      lest we try to reference the new directory's `..' while creating it.  */
  187.  
  188.   to_base = basename (to);
  189.   i = strlen (to_base);
  190.   to_dotdot = xmalloc (i + 4);
  191.   strcpy (to_dotdot, to_base);
  192.   strcpy (to_dotdot + i, "/..");
  193.  
  194.   if (chdir (to_parent_path))
  195.     error (1, errno, "%s", to_parent_path);
  196.   if (unlink (to_dotdot) && errno != ENOENT)
  197.     error (1, errno, "cannot unlink `%s'", to_dotdot);
  198.   if (link (".", to_dotdot))
  199.     error (1, errno, "cannot link `%s' to `%s'", ".", to_dotdot);
  200.  
  201.   exit (0);
  202. }
  203.  
  204. /* Return the full pathname (from /) of the directory DIR,
  205.    as static data. */
  206.  
  207. static char *
  208. fullpath (dir)
  209.      char *dir;
  210. {
  211.   char wd[PATH_MAX + 2];
  212.   static char path[PATH_MAX + 2];
  213.  
  214.   if (getcwd (wd, PATH_MAX + 2) == NULL)
  215.     error (1, errno, "cannot get current directory");
  216.   if (chdir (dir))
  217.     error (1, errno, "%s", dir);
  218.   if (getcwd (path, PATH_MAX + 2) == NULL)
  219.     error (1, errno, "cannot get current directory");
  220.   if (chdir (wd))
  221.     error (1, errno, "%s", wd);
  222.  
  223.   return path;
  224. }
  225.