LCOV - code coverage report
Current view: top level - src/client - find.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 198 224 88.4 %
Date: 2018-08-31 08:21:15 Functions: 20 21 95.2 %

          Line data    Source code
       1             : /*
       2             :    Bacula® - The Network Backup Solution
       3             : 
       4             :    Copyright (C) 2000-2010 Free Software Foundation Europe e.V.
       5             : 
       6             :    The main author of Bacula is Kern Sibbald, with contributions from
       7             :    many others, a complete list can be found in the file AUTHORS.
       8             :    This program is Free Software; you can redistribute it and/or
       9             :    modify it under the terms of version three of the GNU Affero General Public
      10             :    License as published by the Free Software Foundation and included
      11             :    in the file LICENSE.
      12             : 
      13             :    This program is distributed in the hope that it will be useful, but
      14             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
      16             :    General Public License for more details.
      17             : 
      18             :    You should have received a copy of the GNU Affero General Public License
      19             :    along with this program; if not, write to the Free Software
      20             :    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
      21             :    02110-1301, USA.
      22             : 
      23             :    Bacula® is a registered trademark of Kern Sibbald.
      24             :    The licensor of Bacula is the Free Software Foundation Europe
      25             :    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
      26             :    Switzerland, email:ftf@fsfeurope.org.
      27             : */
      28             : /*
      29             :    This file was derived from GNU TAR source code. Except for a few key
      30             :    ideas, it has been entirely rewritten for Bacula.
      31             : 
      32             :       Kern Sibbald, MM
      33             : 
      34             :    Thanks to the TAR programmers.
      35             : */
      36             : /*
      37             :    This file was derived from the findlib code from bacula-5.0.3, and
      38             :    heavily modified. Therefore, I have retained the bacula copyright notice.
      39             :    The specific bacula files were:
      40             :    src/findlib/find.c
      41             :    src/findlib/find_one.c.
      42             :    The comment by Kern above, about TAR, was from find_one.c.
      43             : 
      44             :       Graham Keeling, 2014.
      45             : */
      46             : 
      47             : #include "../burp.h"
      48             : #include "../alloc.h"
      49             : #include "../conf.h"
      50             : #include "../fsops.h"
      51             : #include "../linkhash.h"
      52             : #include "../log.h"
      53             : #include "../pathcmp.h"
      54             : #include "../prepend.h"
      55             : #include "../regexp.h"
      56             : #include "../strlist.h"
      57             : #include "find.h"
      58             : 
      59             : #ifdef HAVE_LINUX_OS
      60             : #include <sys/statfs.h>
      61             : #endif
      62             : #ifdef HAVE_SUN_OS
      63             : #include <sys/statvfs.h>
      64             : #endif
      65             : 
      66             : static int (*my_send_file)(struct asfd *, struct FF_PKT *, struct conf **);
      67             : 
      68             : // Initialize the find files "global" variables
      69          14 : struct FF_PKT *find_files_init(
      70             :         int callback(struct asfd *asfd, struct FF_PKT *ff, struct conf **confs))
      71             : {
      72             :         struct FF_PKT *ff;
      73             : 
      74          14 :         if(!(ff=(struct FF_PKT *)calloc_w(1, sizeof(struct FF_PKT), __func__))
      75          14 :           || linkhash_init())
      76             :                 return NULL;
      77          14 :         my_send_file=callback;
      78             : 
      79          14 :         return ff;
      80             : }
      81             : 
      82          14 : void find_files_free(struct FF_PKT **ff)
      83             : {
      84          14 :         linkhash_free();
      85          14 :         free_v((void **)ff);
      86          14 : }
      87             : 
      88             : // Return 1 to include the file, 0 to exclude it.
      89          55 : static int in_include_ext(struct strlist *incext, const char *fname)
      90             : {
      91          55 :         int i=0;
      92             :         struct strlist *l;
      93          55 :         const char *cp=NULL;
      94             :         // If not doing include_ext, let the file get backed up.
      95          55 :         if(!incext) return 1;
      96             : 
      97             :         // The flag of the first item contains the maximum number of characters
      98             :         // that need to be checked.
      99             :         // FIX THIS: The next two functions do things very similar to this.
     100          32 :         for(cp=fname+strlen(fname)-1; i<incext->flag && cp>=fname; cp--, i++)
     101             :         {
     102          14 :                 if(*cp!='.') continue;
     103           3 :                 for(l=incext; l; l=l->next)
     104           7 :                         if(!strcasecmp(l->path, cp+1))
     105             :                                 return 1;
     106             :                 // If file has no extension, it cannot be included.
     107             :                 return 0;
     108             :         }
     109             :         return 0;
     110             : }
     111             : 
     112         137 : static int in_exclude_ext(struct strlist *excext, const char *fname)
     113             : {
     114         137 :         int i=0;
     115             :         struct strlist *l;
     116         137 :         const char *cp=NULL;
     117             :         // If not doing exclude_ext, let the file get backed up.
     118         137 :         if(!excext) return 0;
     119             : 
     120             :         // The flag of the first item contains the maximum number of characters
     121             :         // that need to be checked.
     122          50 :         for(cp=fname+strlen(fname)-1; i<excext->flag && cp>=fname; cp--, i++)
     123             :         {
     124          20 :                 if(*cp!='.') continue;
     125           5 :                 for(l=excext; l; l=l->next)
     126           8 :                         if(!strcasecmp(l->path, cp+1))
     127             :                                 return 1;
     128             :                 // If file has no extension, it is included.
     129             :                 return 0;
     130             :         }
     131             :         return 0;
     132             : }
     133             : 
     134             : // Returns the level of compression.
     135           8 : int in_exclude_comp(struct strlist *excom, const char *fname, int compression)
     136             : {
     137           8 :         int i=0;
     138             :         struct strlist *l;
     139           8 :         const char *cp=NULL;
     140             :         // If not doing compression, or there are no excludes, return
     141             :         // straight away.
     142           8 :         if(!compression || !excom) return compression;
     143             : 
     144             :         // The flag of the first item contains the maximum number of characters
     145             :         // that need to be checked.
     146           0 :         for(cp=fname+strlen(fname)-1; i<excom->flag && cp>=fname; cp--, i++)
     147             :         {
     148           0 :                 if(*cp!='.') continue;
     149           0 :                 for(l=excom; l; l=l->next)
     150           0 :                         if(!strcasecmp(l->path, cp+1))
     151             :                                 return 0;
     152             :                 return compression;
     153             :         }
     154             :         return compression;
     155             : }
     156             : 
     157             : /* Return 1 to include the file, 0 to exclude it. */
     158             : /* Currently not used - it needs more thinking about. */
     159         131 : int in_include_regex(struct strlist *increg, const char *fname)
     160             : {
     161             :         // If not doing include_regex, let the file get backed up.
     162         131 :         if(!increg) return 1;
     163           8 :         for(; increg; increg=increg->next)
     164          14 :                 if(regex_check(increg->re, fname))
     165             :                         return 1;
     166             :         return 0;
     167             : }
     168             : 
     169             : static int in_exclude_regex(struct strlist *excreg, const char *fname)
     170             : {
     171             :         // If not doing exclude_regex, let the file get backed up.
     172          20 :         for(; excreg; excreg=excreg->next)
     173          23 :                 if(regex_check(excreg->re, fname))
     174             :                         return 1;
     175             :         return 0;
     176             : }
     177             : 
     178             : // When recursing into directories, do not want to check the include_ext list.
     179             : #ifndef UTEST
     180             : static
     181             : #endif
     182         137 : int file_is_included_no_incext(struct conf **confs, const char *fname)
     183             : {
     184         137 :         int ret=0;
     185         137 :         int longest=0;
     186         137 :         int matching=0;
     187         137 :         struct strlist *l=NULL;
     188         137 :         struct strlist *best=NULL;
     189             : 
     190         137 :         if(in_exclude_ext(get_strlist(confs[OPT_EXCEXT]), fname)
     191         268 :           || in_exclude_regex(get_strlist(confs[OPT_EXCREG]), fname)
     192         131 :                 || !in_include_regex(get_strlist(confs[OPT_INCREG]), fname))
     193             :                 return 0;
     194             : 
     195             :         // Check include/exclude directories.
     196         338 :         for(l=get_strlist(confs[OPT_INCEXCDIR]); l; l=l->next)
     197             :         {
     198         210 :                 matching=is_subdir(l->path, fname);
     199         210 :                 if(matching>=longest)
     200             :                 {
     201         158 :                         longest=matching;
     202         158 :                         best=l;
     203             :                 }
     204             :         }
     205         128 :         if(!best) ret=0;
     206         128 :         else ret=best->flag;
     207             : 
     208             :         return ret;
     209             : }
     210             : 
     211          70 : static int file_is_included(struct conf **confs,
     212             :         const char *fname, bool top_level)
     213             : {
     214             :         // Always save the top level directory.
     215             :         // This will help in the simulation of browsing backups because it
     216             :         // will mean that there is always a directory before any files:
     217             :         // d /home/graham
     218             :         // f /home/graham/somefile.txt
     219             :         // This means that we can use the stats of the directory (/home/graham
     220             :         // in this example) as the stats of the parent directories (/home,
     221             :         // for example). Trust me on this.
     222          70 :         if(!top_level
     223          55 :           && !in_include_ext(get_strlist(confs[OPT_INCEXT]), fname)) return 0;
     224             : 
     225          67 :         return file_is_included_no_incext(confs, fname);
     226             : }
     227             : 
     228           0 : static int fs_change_is_allowed(struct conf **confs, const char *fname)
     229             : {
     230             :         struct strlist *l;
     231           0 :         if(get_int(confs[OPT_CROSS_ALL_FILESYSTEMS])) return 1;
     232           0 :         for(l=get_strlist(confs[OPT_FSCHGDIR]); l; l=l->next)
     233           0 :                 if(!strcmp(l->path, fname)) return 1;
     234             :         return 0;
     235             : }
     236             : 
     237           6 : static int need_to_read_fifo(struct conf **confs, const char *fname)
     238             : {
     239             :         struct strlist *l;
     240           6 :         if(get_int(confs[OPT_READ_ALL_FIFOS])) return 1;
     241           7 :         for(l=get_strlist(confs[OPT_FIFOS]); l; l=l->next)
     242           5 :                 if(!strcmp(l->path, fname)) return 1;
     243             :         return 0;
     244             : }
     245             : 
     246           2 : static int need_to_read_blockdev(struct conf **confs, const char *fname)
     247             : {
     248             :         struct strlist *l;
     249           2 :         if(get_int(confs[OPT_READ_ALL_BLOCKDEVS])) return 1;
     250           3 :         for(l=get_strlist(confs[OPT_BLOCKDEVS]); l; l=l->next)
     251           3 :                 if(!strcmp(l->path, fname)) return 1;
     252             :         return 0;
     253             : }
     254             : 
     255          26 : static int nobackup_directory(struct strlist *nobackup, const char *path)
     256             : {
     257             :         struct stat statp;
     258          62 :         for(; nobackup; nobackup=nobackup->next)
     259             :         {
     260           7 :                 char *fullpath=NULL;
     261           7 :                 if(!(fullpath=prepend_s(path, nobackup->path)))
     262           2 :                         return -1;
     263          14 :                 if(!lstat(fullpath, &statp))
     264             :                 {
     265           2 :                         free_w(&fullpath);
     266           2 :                         return 1;
     267             :                 }
     268           5 :                 free_w(&fullpath);
     269             :         }
     270             :         return 0;
     271             : }
     272             : 
     273          32 : static int file_size_match(struct FF_PKT *ff_pkt, struct conf **confs)
     274             : {
     275             :         uint64_t sizeleft;
     276          32 :         uint64_t min_file_size=get_uint64_t(confs[OPT_MIN_FILE_SIZE]);
     277          32 :         uint64_t max_file_size=get_uint64_t(confs[OPT_MAX_FILE_SIZE]);
     278          32 :         sizeleft=(uint64_t)ff_pkt->statp.st_size;
     279             : 
     280          32 :         if(min_file_size && sizeleft<min_file_size)
     281             :                 return 0;
     282          31 :         if(max_file_size && sizeleft>max_file_size)
     283             :                 return 0;
     284             :         return 1;
     285             : }
     286             : 
     287             : // Last checks before actually processing the file system entry.
     288          70 : static int my_send_file_w(struct asfd *asfd, struct FF_PKT *ff, bool top_level, struct conf **confs)
     289             : {
     290          70 :         if(!file_is_included(confs, ff->fname, top_level)) return 0;
     291             : 
     292             :         // Doing the file size match here also catches hard links.
     293          66 :         if(S_ISREG(ff->statp.st_mode)
     294          32 :           && !file_size_match(ff, confs))
     295             :                 return 0;
     296             : 
     297             :         /*
     298             :          * Handle hard linked files
     299             :          * Maintain a list of hard linked files already backed up. This
     300             :          *  allows us to ensure that the data of each file gets backed
     301             :          *  up only once.
     302             :          */
     303          64 :         if(ff->statp.st_nlink > 1
     304          56 :           && (S_ISREG(ff->statp.st_mode)
     305          28 :                 || S_ISCHR(ff->statp.st_mode)
     306          24 :                 || S_ISBLK(ff->statp.st_mode)
     307          24 :                 || S_ISFIFO(ff->statp.st_mode)
     308          24 :                 || S_ISSOCK(ff->statp.st_mode)))
     309             :         {
     310             :                 struct f_link *lp;
     311           4 :                 struct f_link **bucket=NULL;
     312             : 
     313           4 :                 if((lp=linkhash_search(&ff->statp, &bucket)))
     314             :                 {
     315           2 :                         if(!strcmp(lp->name, ff->fname)) return 0;
     316           2 :                         ff->link=lp->name;
     317             :                         /* Handle link, file already saved */
     318           2 :                         ff->type=FT_LNK_H;
     319             :                 }
     320             :                 else
     321             :                 {
     322           2 :                         if(linkhash_add(ff->fname,
     323             :                                 &ff->statp, bucket)) return -1;
     324             :                 }
     325             :         }
     326             : 
     327          64 :         return my_send_file(asfd, ff, confs);
     328             : }
     329             : 
     330             : static int found_regular_file(struct asfd *asfd,
     331             :         struct FF_PKT *ff_pkt, struct conf **confs,
     332             :         bool top_level)
     333             : {
     334          34 :         ff_pkt->type=FT_REG;
     335          34 :         return my_send_file_w(asfd, ff_pkt, top_level, confs);
     336             : }
     337             : 
     338           1 : static int found_soft_link(struct asfd *asfd, struct FF_PKT *ff_pkt, struct conf **confs,
     339             :         char *fname, bool top_level)
     340             : {
     341             :         ssize_t size;
     342           1 :         char *buffer=(char *)alloca(fs_full_path_max+102);
     343             : 
     344           1 :         if((size=readlink(fname, buffer, fs_full_path_max+101))<0)
     345             :         {
     346             :                 /* Could not follow link */
     347           0 :                 ff_pkt->type=FT_NOFOLLOW;
     348             :         }
     349             :         else
     350             :         {
     351           1 :                 buffer[size]=0;
     352           1 :                 ff_pkt->link=buffer; /* point to link */
     353           1 :                 ff_pkt->type=FT_LNK_S;       /* got a soft link */
     354             :         }
     355           1 :         return my_send_file_w(asfd, ff_pkt, top_level, confs);
     356             : }
     357             : 
     358          14 : static int fstype_matches(struct asfd *asfd,
     359             :         struct conf **confs, const char *fname, int inex)
     360             : {
     361             : #if defined(HAVE_LINUX_OS) \
     362             :  || defined(HAVE_SUN_OS)
     363             :         struct strlist *l;
     364             : #if defined(HAVE_LINUX_OS)
     365             :         struct statfs buf;
     366          14 :         if(statfs(fname, &buf))
     367             : #elif defined(HAVE_SUN_OS)
     368             :         struct statvfs buf;
     369             :         if(statvfs(fname, &buf))
     370             : #endif
     371             :         {
     372           0 :                 logw(asfd, get_cntr(confs), "Could not statfs %s: %s\n",
     373           0 :                         fname, strerror(errno));
     374           0 :                 return -1;
     375             :         }
     376          14 :         for(l=get_strlist(confs[inex]); l; l=l->next)
     377             : #if defined(HAVE_LINUX_OS)
     378           0 :                 if(l->flag==buf.f_type)
     379             : #elif defined(HAVE_SUN_OS)
     380             :                 if(strcmp(l->path,buf.f_basetype)==0)
     381             : #endif
     382             :                         return -1;
     383             : #elif defined(HAVE_WIN32)
     384             :         char filesystem_name[MAX_PATH_UTF8 + 1];
     385             :         if (win32_getfsname(fname, filesystem_name, sizeof(filesystem_name)))
     386             :                 return -1;
     387             :         for(strlist *l=get_strlist(confs[inex]); l; l=l->next)
     388             :                 if(strcmp(l->path,filesystem_name)==0)
     389             :                         return -1;
     390             : #endif
     391             :         return 0;
     392             : }
     393             : 
     394             : #if defined(HAVE_WIN32)
     395             : static void windows_reparse_point_fiddling(struct FF_PKT *ff_pkt)
     396             : {
     397             :         /*
     398             :         * We have set st_rdev to 1 if it is a reparse point, otherwise 0,
     399             :         *  if st_rdev is 2, it is a mount point.
     400             :         */
     401             :         /*
     402             :          * A reparse point (WIN32_REPARSE_POINT)
     403             :          *  is something special like one of the following:
     404             :          *  IO_REPARSE_TAG_DFS              0x8000000A
     405             :          *  IO_REPARSE_TAG_DFSR             0x80000012
     406             :          *  IO_REPARSE_TAG_HSM              0xC0000004
     407             :          *  IO_REPARSE_TAG_HSM2             0x80000006
     408             :          *  IO_REPARSE_TAG_SIS              0x80000007
     409             :          *  IO_REPARSE_TAG_SYMLINK          0xA000000C
     410             :          *
     411             :          * A junction point is a:
     412             :          *  IO_REPARSE_TAG_MOUNT_POINT      0xA0000003
     413             :          * which can be either a link to a Volume (WIN32_MOUNT_POINT)
     414             :          * or a link to a directory (WIN32_JUNCTION_POINT)
     415             :          *
     416             :          * Ignore WIN32_REPARSE_POINT and WIN32_JUNCTION_POINT
     417             :          */
     418             :         if (ff_pkt->statp.st_rdev == WIN32_REPARSE_POINT) {
     419             :                 ff_pkt->type = FT_REPARSE;
     420             :         } else if (ff_pkt->statp.st_rdev == WIN32_JUNCTION_POINT) {
     421             :                 ff_pkt->type = FT_JUNCTION;
     422             :         }
     423             : }
     424             : #endif
     425             : 
     426             : // Prototype because process_entries_in_directory() recurses using find_files().
     427             : static int find_files(struct asfd *asfd,
     428             :         struct FF_PKT *ff_pkt, struct conf **confs,
     429             :         char *fname, dev_t parent_device, bool top_level);
     430             : 
     431          18 : static int process_entries_in_directory(struct asfd *asfd, char **nl,
     432             :         int count, char **link, size_t len, size_t *link_len,
     433             :         struct conf **confs, struct FF_PKT *ff_pkt, dev_t our_device)
     434             : {
     435          18 :         int m=0;
     436          18 :         int ret=0;
     437          82 :         for(m=0; m<count; m++)
     438             :         {
     439             :                 size_t i;
     440          64 :                 char *p=NULL;
     441          64 :                 char *q=NULL;
     442             :                 size_t plen;
     443             : 
     444          64 :                 p=nl[m];
     445             : 
     446          64 :                 if(strlen(p)+len>=*link_len)
     447             :                 {
     448           0 :                         *link_len=len+strlen(p)+1;
     449           0 :                         if(!(*link=(char *)
     450           0 :                           realloc_w(*link, (*link_len)+1, __func__)))
     451             :                                 return -1;
     452             :                 }
     453          64 :                 q=(*link)+len;
     454          64 :                 plen=strlen(p);
     455         179 :                 for(i=0; i<plen; i++)
     456         115 :                         *q++=*p++;
     457          64 :                 *q=0;
     458          64 :                 ff_pkt->flen=i;
     459             : 
     460          64 :                 if(file_is_included_no_incext(confs, *link))
     461             :                 {
     462          53 :                         ret=find_files(asfd, ff_pkt,
     463             :                                 confs, *link, our_device, false /*top_level*/);
     464             :                 }
     465             :                 else
     466             :                 {
     467             :                         struct strlist *x;
     468             :                         // Excluded, but there might be a subdirectory that is
     469             :                         // included.
     470          30 :                         for(x=get_strlist(confs[OPT_INCEXCDIR]); x; x=x->next)
     471             :                         {
     472          19 :                                 if(x->flag
     473          16 :                                   && is_subdir(*link, x->path))
     474             :                                 {
     475             :                                         struct strlist *y;
     476           4 :                                         if((ret=find_files(asfd, ff_pkt,
     477             :                                                 confs, x->path,
     478             :                                                 our_device, false)))
     479             :                                                         break;
     480             :                                         // Now need to skip subdirectories of
     481             :                                         // the thing that we just stuck in
     482             :                                         // find_one_file(), or we might get
     483             :                                         // some things backed up twice.
     484           6 :                                         for(y=x->next; y; y=y->next)
     485           2 :                                                 if(y->next
     486           1 :                                                  && is_subdir(x->path, y->path))
     487           1 :                                                         y=y->next;
     488             :                                 }
     489             :                         }
     490             :                 }
     491          64 :                 free_w(&(nl[m]));
     492          64 :                 if(ret) break;
     493             :         }
     494             :         return ret;
     495             : }
     496             : 
     497          26 : static int found_directory(struct asfd *asfd,
     498             :         struct FF_PKT *ff_pkt, struct conf **confs,
     499             :         char *fname, dev_t parent_device, bool top_level)
     500             : {
     501          26 :         int ret=-1;
     502          26 :         char *link=NULL;
     503             :         size_t link_len;
     504             :         size_t len;
     505          26 :         int nbret=0;
     506          26 :         int count=0;
     507             :         dev_t our_device;
     508          26 :         char **nl=NULL;
     509             : 
     510          26 :         our_device=ff_pkt->statp.st_dev;
     511             : 
     512          26 :         if((nbret=nobackup_directory(get_strlist(confs[OPT_NOBACKUP]),
     513          26 :                 ff_pkt->fname)))
     514             :         {
     515           2 :                 if(nbret<0) goto end; // Error.
     516           2 :                 ret=0; // Do not back it up.
     517           2 :                 goto end;
     518             :         }
     519             : 
     520             :         /* Build a canonical directory name with a trailing slash in link var */
     521          24 :         len=strlen(fname);
     522          24 :         link_len=len+200;
     523          24 :         if(!(link=(char *)malloc_w(link_len+2, __func__)))
     524             :                 goto end;
     525          24 :         snprintf(link, link_len, "%s", fname);
     526             : 
     527             :         /* Strip all trailing slashes */
     528          24 :         while(len >= 1 && IsPathSeparator(link[len - 1])) len--;
     529             :         /* add back one */
     530          24 :         link[len++]='/';
     531          24 :         link[len]=0;
     532             : 
     533          24 :         ff_pkt->link=link;
     534          24 :         ff_pkt->type=FT_DIR;
     535             : 
     536             : #if defined(HAVE_WIN32)
     537             :         windows_reparse_point_fiddling(ff_pkt);
     538             : #endif
     539             : 
     540          24 :         if(my_send_file_w(asfd, ff_pkt, top_level, confs))
     541             :                 goto end;
     542          24 :         if(ff_pkt->type==FT_REPARSE || ff_pkt->type==FT_JUNCTION)
     543             :         {
     544             :                 // Ignore.
     545             :                 ret=0;
     546             :                 goto end;
     547             :         }
     548             : 
     549          24 :         if(top_level
     550          10 :           || (parent_device!=ff_pkt->statp.st_dev
     551             : #if defined(HAVE_WIN32)
     552             :                 || ff_pkt->statp.st_rdev==WIN32_MOUNT_POINT
     553             : #endif
     554             :                 ))
     555             :         {
     556          14 :                 if(fstype_matches(asfd, confs, ff_pkt->fname, OPT_EXCFS)
     557          14 :                   || (get_strlist(confs[OPT_INCFS])
     558           0 :                      && !fstype_matches(asfd, confs, ff_pkt->fname, OPT_INCFS)))
     559             :                 {
     560           0 :                         if(top_level)
     561           0 :                                 logw(asfd, get_cntr(confs),
     562             :                                         "Skipping '%s' because of file system include or exclude.\n", fname);
     563           0 :                         ret=my_send_file_w(asfd, ff_pkt, top_level, confs);
     564           0 :                         goto end;
     565             :                 }
     566          14 :                 if(!top_level && !fs_change_is_allowed(confs, ff_pkt->fname))
     567             :                 {
     568           0 :                         ff_pkt->type=FT_NOFSCHG;
     569             :                         // Just backup the directory and return.
     570           0 :                         ret=my_send_file_w(asfd, ff_pkt, top_level, confs);
     571           0 :                         goto end;
     572             :                 }
     573             :         }
     574             : 
     575          24 :         ff_pkt->link=ff_pkt->fname;
     576             : 
     577          24 :         errno=0;
     578          24 :         switch(entries_in_directory_alphasort(fname,
     579             :                 &nl, &count, get_int(confs[OPT_ATIME])))
     580             :         {
     581             :                 case 0: break;
     582             :                 case 1:
     583           0 :                         ff_pkt->type=FT_NOOPEN;
     584           0 :                         ret=my_send_file_w(asfd, ff_pkt, top_level, confs);
     585             :                 default:
     586             :                         goto end;
     587             :         }
     588             : 
     589          24 :         if(nl)
     590             :         {
     591          18 :                 if(process_entries_in_directory(asfd, nl, count,
     592             :                         &link, len, &link_len, confs, ff_pkt, our_device))
     593             :                                 goto end;
     594             :         }
     595             :         ret=0;
     596             : end:
     597          26 :         free_w(&link);
     598          26 :         free_v((void **)&nl);
     599          26 :         return ret;
     600             : }
     601             : 
     602           9 : static int found_other(struct asfd *asfd, struct FF_PKT *ff_pkt,
     603             :         struct conf **confs, bool top_level)
     604             : {
     605             : #ifdef HAVE_FREEBSD_OS
     606             :         /*
     607             :          * On FreeBSD, all block devices are character devices, so
     608             :          *   to be able to read a raw disk, we need the check for
     609             :          *   a character device.
     610             :          * crw-r----- 1 root  operator - 116, 0x00040002 Jun 9 19:32 /dev/ad0s3
     611             :          * crw-r----- 1 root  operator - 116, 0x00040002 Jun 9 19:32 /dev/rad0s3
     612             :          */
     613             :         if((S_ISBLK(ff_pkt->statp.st_mode) || S_ISCHR(ff_pkt->statp.st_mode))
     614             :                 && need_to_read_blockdev(confs, ff_pkt->fname))
     615             :         {
     616             : #else
     617           9 :         if(S_ISBLK(ff_pkt->statp.st_mode)
     618           2 :                 && need_to_read_blockdev(confs, ff_pkt->fname))
     619             :         {
     620             : #endif
     621             :                 /* raw partition */
     622           2 :                 ff_pkt->type=FT_RAW;
     623             :         }
     624           7 :         else if(S_ISFIFO(ff_pkt->statp.st_mode)
     625           6 :                 && need_to_read_fifo(confs, ff_pkt->fname))
     626             :         {
     627           4 :                 ff_pkt->type=FT_FIFO;
     628             :         }
     629             :         else
     630             :         {
     631             :                 /* The only remaining are special (character, ...) files */
     632           3 :                 ff_pkt->type=FT_SPEC;
     633             :         }
     634           9 :         return my_send_file_w(asfd, ff_pkt, top_level, confs);
     635             : }
     636             : 
     637          72 : static int find_files(struct asfd *asfd, struct FF_PKT *ff_pkt, struct conf **confs,
     638             :         char *fname, dev_t parent_device, bool top_level)
     639             : {
     640          72 :         ff_pkt->fname=fname;
     641          72 :         ff_pkt->link=fname;
     642             : 
     643             : #ifdef HAVE_WIN32
     644             :         if(win32_lstat(fname, &ff_pkt->statp, &ff_pkt->winattr))
     645             : #else
     646         144 :         if(lstat(fname, &ff_pkt->statp))
     647             : #endif
     648             :         {
     649           2 :                 ff_pkt->type=FT_NOSTAT;
     650           2 :                 return my_send_file_w(asfd, ff_pkt, top_level, confs);
     651             :         }
     652             : 
     653          70 :         if(S_ISREG(ff_pkt->statp.st_mode))
     654          68 :                 return found_regular_file(asfd, ff_pkt, confs, top_level);
     655          36 :         else if(S_ISLNK(ff_pkt->statp.st_mode))
     656             :         {
     657             : #ifdef S_IFLNK
     658             :                 /* A symlink.
     659             :                    If they have specified the symlink in a read_blockdev
     660             :                    argument, treat it as a block device.
     661             :                 */
     662             :                 struct strlist *l;
     663           4 :                 for(l=get_strlist(confs[OPT_BLOCKDEVS]); l; l=l->next)
     664             :                 {
     665           3 :                         if(!strcmp(l->path, fname))
     666             :                         {
     667           2 :                                 ff_pkt->statp.st_mode ^= S_IFLNK;
     668           2 :                                 ff_pkt->statp.st_mode |= S_IFBLK;
     669           2 :                                 return found_other(asfd, ff_pkt, confs,
     670             :                                         top_level);
     671             :                         }
     672             :                 }
     673             : #endif
     674           1 :                 return found_soft_link(asfd, ff_pkt, confs, fname, top_level);
     675             :         }
     676          33 :         else if(S_ISDIR(ff_pkt->statp.st_mode))
     677          26 :                 return found_directory(asfd, ff_pkt, confs, fname,
     678             :                         parent_device, top_level);
     679             :         else
     680           7 :                 return found_other(asfd, ff_pkt, confs, top_level);
     681             : }
     682             : 
     683          15 : int find_files_begin(struct asfd *asfd,
     684             :         struct FF_PKT *ff_pkt, struct conf **confs, char *fname)
     685             : {
     686          15 :         return find_files(asfd, ff_pkt,
     687             :                 confs, fname, (dev_t)-1, 1 /* top_level */);
     688             : }

Generated by: LCOV version 1.13