Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

syscall.c

Go to the documentation of this file.
00001 /* 
00002    Copyright (C) Andrew Tridgell 1998
00003    Copyright (C) 2002 by Martin Pool
00004    
00005    This program is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published by
00007    the Free Software Foundation; either version 2 of the License, or
00008    (at your option) any later version.
00009    
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.
00014    
00015    You should have received a copy of the GNU General Public License
00016    along with this program; if not, write to the Free Software
00017    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 /**
00021  * @file syscall.c
00022  *
00023  * Syscall wrappers to ensure that nothing gets done in dry_run mode
00024  * and to handle system peculiarities.
00025  **/
00026 
00027 #include "rsync.h"
00028 
00029 extern int dry_run;
00030 extern int read_only;
00031 extern int list_only;
00032 
00033 #define CHECK_RO if (read_only || list_only) {errno = EROFS; return -1;}
00034 
00035 int do_unlink(char *fname)
00036 {
00037         if (dry_run) return 0;
00038         CHECK_RO
00039         return unlink(fname);
00040 }
00041 
00042 int do_symlink(char *fname1, char *fname2)
00043 {
00044         if (dry_run) return 0;
00045         CHECK_RO
00046         return symlink(fname1, fname2);
00047 }
00048 
00049 #if HAVE_LINK
00050 int do_link(char *fname1, char *fname2)
00051 {
00052         if (dry_run) return 0;
00053         CHECK_RO
00054         return link(fname1, fname2);
00055 }
00056 #endif
00057 
00058 int do_lchown(const char *path, uid_t owner, gid_t group)
00059 {
00060         if (dry_run) return 0;
00061         CHECK_RO
00062         return lchown(path, owner, group);
00063 }
00064 
00065 #if HAVE_MKNOD
00066 int do_mknod(char *pathname, mode_t mode, dev_t dev)
00067 {
00068         if (dry_run) return 0;
00069         CHECK_RO
00070         return mknod(pathname, mode, dev);
00071 }
00072 #endif
00073 
00074 int do_rmdir(char *pathname)
00075 {
00076         if (dry_run) return 0;
00077         CHECK_RO
00078         return rmdir(pathname);
00079 }
00080 
00081 int do_open(char *pathname, int flags, mode_t mode)
00082 {
00083         if (flags != O_RDONLY) {
00084             if (dry_run) return -1;
00085             CHECK_RO
00086         }
00087 #ifdef O_BINARY
00088         /* for Windows */
00089         flags |= O_BINARY;
00090 #endif
00091         /* some systems can't handle a double / */
00092         if (pathname[0] == '/' && pathname[1] == '/') pathname++;
00093 
00094         return open(pathname, flags, mode);
00095 }
00096 
00097 #if HAVE_CHMOD
00098 int do_chmod(const char *path, mode_t mode)
00099 {
00100         if (dry_run) return 0;
00101         CHECK_RO
00102         return chmod(path, mode);
00103 }
00104 #endif
00105 
00106 int do_rename(char *fname1, char *fname2)
00107 {
00108         if (dry_run) return 0;
00109         CHECK_RO
00110         return rename(fname1, fname2);
00111 }
00112 
00113 
00114 void trim_trailing_slashes(char *name)
00115 {
00116         int l;
00117         /* Some BSD systems cannot make a directory if the name
00118          * contains a trailing slash.
00119          * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */
00120         
00121         /* Don't change empty string; and also we can't improve on
00122          * "/" */
00123         
00124         l = strlen(name);
00125         while (l > 1) {
00126                 if (name[--l] != '/')
00127                         break;
00128                 name[l] = '\0';
00129         }
00130 }
00131 
00132 
00133 int do_mkdir(char *fname, mode_t mode)
00134 {
00135         if (dry_run)
00136                 return 0;
00137         CHECK_RO;
00138         trim_trailing_slashes(fname);   
00139         return mkdir(fname, mode);
00140 }
00141 
00142 
00143 /* like mkstemp but forces permissions */
00144 int do_mkstemp(char *template, mode_t perms)
00145 {
00146         if (dry_run) return -1;
00147         if (read_only) {errno = EROFS; return -1;}
00148 
00149 #if defined(HAVE_SECURE_MKSTEMP) && defined(HAVE_FCHMOD)
00150         {
00151                 int fd = mkstemp(template);
00152                 if (fd == -1) return -1;
00153                 if (fchmod(fd, perms) != 0) {
00154                         close(fd);
00155                         unlink(template);
00156                         return -1;
00157                 }
00158                 return fd;
00159         }
00160 #else
00161         if (!mktemp(template)) return -1;
00162         return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
00163 #endif
00164 }
00165 
00166 int do_stat(const char *fname, STRUCT_STAT *st)
00167 {
00168 #if HAVE_OFF64_T
00169         return stat64(fname, st);
00170 #else
00171         return stat(fname, st);
00172 #endif
00173 }
00174 
00175 #if SUPPORT_LINKS
00176 int do_lstat(const char *fname, STRUCT_STAT *st)
00177 {
00178 #if HAVE_OFF64_T
00179         return lstat64(fname, st);
00180 #else
00181         return lstat(fname, st);
00182 #endif
00183 }
00184 #endif
00185 
00186 int do_fstat(int fd, STRUCT_STAT *st)
00187 {
00188 #if HAVE_OFF64_T
00189         return fstat64(fd, st);
00190 #else
00191         return fstat(fd, st);
00192 #endif
00193 }
00194 
00195 OFF_T do_lseek(int fd, OFF_T offset, int whence)
00196 {
00197 #if HAVE_OFF64_T
00198         off64_t lseek64();
00199         return lseek64(fd, offset, whence);
00200 #else
00201         return lseek(fd, offset, whence);
00202 #endif
00203 }
00204 
00205 #ifdef USE_MMAP
00206 void *do_mmap(void *start, int len, int prot, int flags, int fd, OFF_T offset)
00207 {
00208 #if HAVE_OFF64_T
00209         return mmap64(start, len, prot, flags, fd, offset);
00210 #else
00211         return mmap(start, len, prot, flags, fd, offset);
00212 #endif
00213 }
00214 #endif
00215 
00216 char *d_name(struct dirent *di)
00217 {
00218 #if HAVE_BROKEN_READDIR
00219         return (di->d_name - 2);
00220 #else
00221         return di->d_name;
00222 #endif
00223 }

Generated on Tue Apr 16 12:37:37 2002 for rsync by doxygen1.2.15