"Fossies" - the Fresh Open Source Software Archive

Member "mirrordir-0.10.49/src/hardlink.c" (23 Nov 1999, 4269 Bytes) of package /linux/misc/old/mirrordir-0.10.49.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "hardlink.c" see the Fossies "Dox" file reference documentation.

    1 /* hardlink.c - handles hardlinked files
    2    This has nothing to do with cryptography.
    3    Copyright (C) 1998 Paul Sheer
    4 
    5    This program is free software; you can redistribute it and/or modify
    6    it under the terms of the GNU General Public License as published by
    7    the Free Software Foundation; either version 2 of the License, or
    8    (at your option) any later version.
    9 
   10    This program is distributed in the hope that it will be useful,
   11    but WITHOUT ANY WARRANTY; without even the implied warranty of
   12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13    GNU General Public License for more details.
   14 
   15    You should have received a copy of the GNU General Public License
   16    along with this program; if not, write to the Free Software
   17    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   18    02111-1307, USA.  
   19  */
   20 
   21 #include "mostincludes.h"
   22 #include "mirrordir.h"
   23 #include "vfs/vfs.h"
   24 #include "diffie/compat.h"
   25 #include "mad.h"
   26 
   27 
   28 /*
   29    Procedure is as follows (done in mirrordir.c):
   30 
   31    If we come accross a hardlink we search for it in the list of previously
   32    found hardlinks.
   33 
   34    If it does exists, then we reduce the count of the previously found
   35    hardlink, and make a hardlink instead of copying the file.
   36 
   37    If it doesn't exist, then we add it to the list, set the link count of
   38    the list entry to the link count of the found hardlink, and copy the
   39    file.
   40  */
   41 
   42 static struct hardlink_list {
   43     struct hardlink_list *prev;
   44     struct hardlink_list *next;
   45     char *name;
   46     dev_t mirror_st_dev;
   47     ino_t mirror_st_ino;
   48     dev_t st_dev;
   49     ino_t st_ino;
   50     nlink_t st_nlink;
   51 } *hl = 0;
   52 
   53 void free_all_hardlinks (void)
   54 {
   55     struct hardlink_list *l;
   56     if (!hl)
   57     return;
   58     rewind (hl);
   59     do {
   60     l = hl->next;
   61     if (hl->name)
   62         free (hl->name);
   63     free (hl);
   64     hl = l;
   65     } while (l);
   66 }
   67 
   68 void add_hardlink (char *path, struct stat *s, struct stat *m)
   69 {
   70     struct hardlink_list *l;
   71 
   72     if (hl)
   73     forward (hl);
   74     l = malloc (sizeof (struct hardlink_list));
   75 
   76     memset (l, 0, sizeof (struct hardlink_list));
   77     l->name = (char *) strdup (path);
   78     if (m) {
   79     l->mirror_st_dev = m->st_dev;
   80     l->mirror_st_ino = m->st_ino;
   81     }
   82     l->st_dev = s->st_dev;
   83     l->st_ino = s->st_ino;
   84     l->st_nlink = s->st_nlink;
   85     l->prev = hl;
   86     if (hl)
   87     hl->next = l;
   88     hl = l;
   89 }
   90 
   91 int correct_link (struct stat *s)
   92 {
   93     return memcmp (&s->st_dev, &hl->mirror_st_dev, sizeof (hl->mirror_st_dev)) == 0
   94     && s->st_ino == hl->mirror_st_ino;
   95 }
   96 
   97 int is_hardlink (struct stat *s)
   98 {
   99     mode_t m = s->st_mode;
  100     if (handle_hardlinks)
  101     if (S_ISREG (m) || S_ISLNK (m) || S_IS_SPECIAL (m))
  102         if (s->st_nlink >= 2)
  103         return 1;
  104     return 0;
  105 }
  106 
  107 int find_hardlink (struct stat *s)
  108 {
  109     if (!hl)
  110     return 0;
  111 
  112 /* sometimes we search for things twice in a row,
  113    so lets check if its not already found */
  114     if (memcmp (&hl->st_dev, &s->st_dev, sizeof (hl->mirror_st_dev)) == 0
  115     && hl->st_ino == s->st_ino)
  116     return 1;
  117     rewind (hl);
  118     for (;;) {
  119     if (memcmp (&hl->st_dev, &s->st_dev, sizeof (hl->st_dev)) == 0
  120         && hl->st_ino == s->st_ino)
  121         return 1;
  122     if (!hl->next)
  123         break;
  124     hl = hl->next;
  125     }
  126     return 0;
  127 }
  128 
  129 /* reduces a hardlinks count and deletes the list entry if it reached zero */
  130 void reduce_hardlink (void)
  131 {
  132     if (!hl)
  133     return;
  134     if (hl->st_nlink <= 2) {
  135     struct hardlink_list *l;
  136     l = hl->prev;
  137     if (!l)
  138         l = hl->next;
  139     if (!l) {
  140         ;
  141     } else if (!hl->prev) {
  142         hl->next->prev = 0; /* begining of list */
  143     } else if (!hl->next)
  144         hl->prev->next = 0; /* end of list */
  145     else {
  146         hl->prev->next = hl->next;
  147         hl->next->prev = hl->prev;
  148     }
  149     if (hl->name)
  150         free (hl->name);
  151     free (hl);
  152     hl = l;
  153     return;
  154     }
  155     hl->st_nlink--;
  156 }
  157 
  158 void print_hardlinks (void)
  159 {
  160     if (!hl) {
  161     verbmess ("all hardlinks located", 0);
  162     return;
  163     }
  164     rewind (hl);
  165     for (;;) {
  166     verbmess ("file should have a hardlink to outside of mirror directory", hl->name);
  167     if (!hl->next)
  168         break;
  169     hl = hl->next;
  170     }
  171 }
  172 
  173 void create_link (char *q)
  174 {
  175     if (only_erase)
  176     return;
  177     if (verbose > 1)
  178     verbmess ("creating hardlink", q);
  179     if (!only_test)
  180     if (mc_link (hl->name, q))
  181         progmess_strerror ("error trying to create hardlinks", q);
  182 }
  183 
  184 char *retrieve_link (void)
  185 {
  186     return hl->name;
  187 }
  188 
  189 
  190 
  191 
  192 
  193