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