syscall.c

説明を見る。
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 #if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
00030 #include <sys/un.h>
00031 #endif
00032 
00033 extern int dry_run;
00034 extern int read_only;
00035 extern int list_only;
00036 extern int preserve_perms;
00037 
00038 #define RETURN_ERROR_IF(x,e) \
00039         do { \
00040                 if (x) { \
00041                         errno = (e); \
00042                         return -1; \
00043                 } \
00044         } while (0)
00045 
00046 #define RETURN_ERROR_IF_RO_OR_LO RETURN_ERROR_IF(read_only || list_only, EROFS)
00047 
00048 int do_unlink(const char *fname)
00049 {
00050         if (dry_run) return 0;
00051         RETURN_ERROR_IF_RO_OR_LO;
00052         return unlink(fname);
00053 }
00054 
00055 int do_symlink(const char *fname1, const char *fname2)
00056 {
00057         if (dry_run) return 0;
00058         RETURN_ERROR_IF_RO_OR_LO;
00059         return symlink(fname1, fname2);
00060 }
00061 
00062 #ifdef HAVE_LINK
00063 int do_link(const char *fname1, const char *fname2)
00064 {
00065         if (dry_run) return 0;
00066         RETURN_ERROR_IF_RO_OR_LO;
00067         return link(fname1, fname2);
00068 }
00069 #endif
00070 
00071 int do_lchown(const char *path, uid_t owner, gid_t group)
00072 {
00073         if (dry_run) return 0;
00074         RETURN_ERROR_IF_RO_OR_LO;
00075 #ifndef HAVE_LCHOWN
00076 #define lchown chown
00077 #endif
00078         return lchown(path, owner, group);
00079 }
00080 
00081 int do_mknod(char *pathname, mode_t mode, dev_t dev)
00082 {
00083         if (dry_run) return 0;
00084         RETURN_ERROR_IF_RO_OR_LO;
00085 #if !defined MKNOD_CREATES_FIFOS && defined HAVE_MKFIFO
00086         if (S_ISFIFO(mode))
00087                 return mkfifo(pathname, mode);
00088 #endif
00089 #if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
00090         if (S_ISSOCK(mode)) {
00091                 int sock;
00092                 struct sockaddr_un saddr;
00093                 unsigned int len;
00094 
00095                 saddr.sun_family = AF_UNIX;
00096                 len = strlcpy(saddr.sun_path, pathname, sizeof saddr.sun_path);
00097 #ifdef HAVE_SOCKADDR_UN_LEN
00098                 saddr.sun_len = len >= sizeof saddr.sun_path
00099                               ? sizeof saddr.sun_path : len + 1;
00100 #endif
00101 
00102                 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0
00103                     || (unlink(pathname) < 0 && errno != ENOENT)
00104                     || (bind(sock, (struct sockaddr*)&saddr, sizeof saddr)) < 0)
00105                         return -1;
00106                 close(sock);
00107 #ifdef HAVE_CHMOD
00108                 return do_chmod(pathname, mode);
00109 #else
00110                 return 0;
00111 #endif
00112         }
00113 #endif
00114 #ifdef HAVE_MKNOD
00115         return mknod(pathname, mode, dev);
00116 #else
00117         return -1;
00118 #endif
00119 }
00120 
00121 int do_rmdir(const char *pathname)
00122 {
00123         if (dry_run) return 0;
00124         RETURN_ERROR_IF_RO_OR_LO;
00125         return rmdir(pathname);
00126 }
00127 
00128 int do_open(const char *pathname, int flags, mode_t mode)
00129 {
00130         if (flags != O_RDONLY) {
00131                 RETURN_ERROR_IF(dry_run, 0);
00132                 RETURN_ERROR_IF_RO_OR_LO;
00133         }
00134 
00135         return open(pathname, flags | O_BINARY, mode);
00136 }
00137 
00138 #ifdef HAVE_CHMOD
00139 int do_chmod(const char *path, mode_t mode)
00140 {
00141         int code;
00142         if (dry_run) return 0;
00143         RETURN_ERROR_IF_RO_OR_LO;
00144         if (S_ISLNK(mode)) {
00145 #ifdef HAVE_LCHMOD
00146                 code = lchmod(path, mode & CHMOD_BITS);
00147 #else
00148                 code = 1;
00149 #endif
00150         } else
00151                 code = chmod(path, mode & CHMOD_BITS);
00152         if (code != 0 && preserve_perms)
00153             return code;
00154         return 0;
00155 }
00156 #endif
00157 
00158 int do_rename(const char *fname1, const char *fname2)
00159 {
00160         if (dry_run) return 0;
00161         RETURN_ERROR_IF_RO_OR_LO;
00162         return rename(fname1, fname2);
00163 }
00164 
00165 void trim_trailing_slashes(char *name)
00166 {
00167         int l;
00168         /* Some BSD systems cannot make a directory if the name
00169          * contains a trailing slash.
00170          * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */
00171         
00172         /* Don't change empty string; and also we can't improve on
00173          * "/" */
00174         
00175         l = strlen(name);
00176         while (l > 1) {
00177                 if (name[--l] != '/')
00178                         break;
00179                 name[l] = '\0';
00180         }
00181 }
00182 
00183 int do_mkdir(char *fname, mode_t mode)
00184 {
00185         if (dry_run) return 0;
00186         RETURN_ERROR_IF_RO_OR_LO;
00187         trim_trailing_slashes(fname);   
00188         return mkdir(fname, mode);
00189 }
00190 
00191 /* like mkstemp but forces permissions */
00192 int do_mkstemp(char *template, mode_t perms)
00193 {
00194         RETURN_ERROR_IF(dry_run, 0);
00195         RETURN_ERROR_IF(read_only, EROFS);
00196 
00197 #if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64)
00198         {
00199                 int fd = mkstemp(template);
00200                 if (fd == -1)
00201                         return -1;
00202                 if (fchmod(fd, perms) != 0 && preserve_perms) {
00203                         int errno_save = errno;
00204                         close(fd);
00205                         unlink(template);
00206                         errno = errno_save;
00207                         return -1;
00208                 }
00209 #if defined HAVE_SETMODE && O_BINARY
00210                 setmode(fd, O_BINARY);
00211 #endif
00212                 return fd;
00213         }
00214 #else
00215         if (!mktemp(template))
00216                 return -1;
00217         return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
00218 #endif
00219 }
00220 
00221 int do_stat(const char *fname, STRUCT_STAT *st)
00222 {
00223 #ifdef USE_STAT64_FUNCS
00224         return stat64(fname, st);
00225 #else
00226         return stat(fname, st);
00227 #endif
00228 }
00229 
00230 int do_lstat(const char *fname, STRUCT_STAT *st)
00231 {
00232 #ifdef SUPPORT_LINKS
00233 # ifdef USE_STAT64_FUNCS
00234         return lstat64(fname, st);
00235 # else
00236         return lstat(fname, st);
00237 # endif
00238 #else
00239         return do_stat(fname, st);
00240 #endif
00241 }
00242 
00243 int do_fstat(int fd, STRUCT_STAT *st)
00244 {
00245 #ifdef USE_STAT64_FUNCS
00246         return fstat64(fd, st);
00247 #else
00248         return fstat(fd, st);
00249 #endif
00250 }
00251 
00252 OFF_T do_lseek(int fd, OFF_T offset, int whence)
00253 {
00254 #ifdef HAVE_LSEEK64
00255 #if !SIZEOF_OFF64_T
00256         OFF_T lseek64();
00257 #else
00258         off64_t lseek64();
00259 #endif
00260         return lseek64(fd, offset, whence);
00261 #else
00262         return lseek(fd, offset, whence);
00263 #endif
00264 }
00265 
00266 char *d_name(struct dirent *di)
00267 {
00268 #ifdef HAVE_BROKEN_READDIR
00269         return (di->d_name - 2);
00270 #else
00271         return di->d_name;
00272 #endif
00273 }

rsyncに対してSat Dec 5 19:45:42 2009に生成されました。  doxygen 1.4.7