\doxysubsection{fileutil.\+c}
\label{fileutil_8c_source}\index{fileutil.c@{fileutil.c}}

\begin{DoxyCode}{0}
\DoxyCodeLine{00001 \textcolor{comment}{/*= -\/*-\/ c-\/basic-\/offset: 4; indent-\/tabs-\/mode: nil; -\/*-\/}}
\DoxyCodeLine{00002 \textcolor{comment}{ *}}
\DoxyCodeLine{00003 \textcolor{comment}{ * librsync -\/-\/ library for network deltas}}
\DoxyCodeLine{00004 \textcolor{comment}{ *}}
\DoxyCodeLine{00005 \textcolor{comment}{ * Copyright (C) 1999, 2000, 2001 by Martin Pool <mbp@sourcefrog.net>}}
\DoxyCodeLine{00006 \textcolor{comment}{ * Copyright (C) 1999 by Andrew Tridgell <tridge@samba.org>}}
\DoxyCodeLine{00007 \textcolor{comment}{ *}}
\DoxyCodeLine{00008 \textcolor{comment}{ * This program is free software; you can redistribute it and/or modify}}
\DoxyCodeLine{00009 \textcolor{comment}{ * it under the terms of the GNU Lesser General Public License as published by}}
\DoxyCodeLine{00010 \textcolor{comment}{ * the Free Software Foundation; either version 2.1 of the License, or}}
\DoxyCodeLine{00011 \textcolor{comment}{ * (at your option) any later version.}}
\DoxyCodeLine{00012 \textcolor{comment}{ *}}
\DoxyCodeLine{00013 \textcolor{comment}{ * This program is distributed in the hope that it will be useful,}}
\DoxyCodeLine{00014 \textcolor{comment}{ * but WITHOUT ANY WARRANTY; without even the implied warranty of}}
\DoxyCodeLine{00015 \textcolor{comment}{ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the}}
\DoxyCodeLine{00016 \textcolor{comment}{ * GNU Lesser General Public License for more details.}}
\DoxyCodeLine{00017 \textcolor{comment}{ *}}
\DoxyCodeLine{00018 \textcolor{comment}{ * You should have received a copy of the GNU Lesser General Public License}}
\DoxyCodeLine{00019 \textcolor{comment}{ * along with this program; if not, write to the Free Software}}
\DoxyCodeLine{00020 \textcolor{comment}{ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.}}
\DoxyCodeLine{00021 \textcolor{comment}{ */}}
\DoxyCodeLine{00022 }
\DoxyCodeLine{00023 \textcolor{comment}{/* This provides a compatiblity layer for file operations on different}}
\DoxyCodeLine{00024 \textcolor{comment}{   platforms. We need to tell IWYU to keep some headers because they are}}
\DoxyCodeLine{00025 \textcolor{comment}{   required on some platforms but not others. */}}
\DoxyCodeLine{00026 \textcolor{preprocessor}{\#include "{}config.h"{}}             \textcolor{comment}{/* IWYU pragma: keep */}}
\DoxyCodeLine{00027 \textcolor{preprocessor}{\#include <stdlib.h>}}
\DoxyCodeLine{00028 \textcolor{preprocessor}{\#include <stdio.h>}}
\DoxyCodeLine{00029 \textcolor{preprocessor}{\#include <string.h>}}
\DoxyCodeLine{00030 \textcolor{preprocessor}{\#include <errno.h>}}
\DoxyCodeLine{00031 \textcolor{preprocessor}{\#ifdef HAVE\_UNISTD\_H}}
\DoxyCodeLine{00032 \textcolor{preprocessor}{\#  include <unistd.h>}           \textcolor{comment}{/* IWYU pragma: keep */}}
\DoxyCodeLine{00033 \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00034 \textcolor{preprocessor}{\#ifdef HAVE\_FCNTL\_H}}
\DoxyCodeLine{00035 \textcolor{preprocessor}{\#  include <fcntl.h>}            \textcolor{comment}{/* IWYU pragma: keep */}}
\DoxyCodeLine{00036 \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00037 \textcolor{preprocessor}{\#ifdef HAVE\_SYS\_TYPES\_H}}
\DoxyCodeLine{00038 \textcolor{preprocessor}{\#  include <sys/types.h>}        \textcolor{comment}{/* IWYU pragma: keep */}}
\DoxyCodeLine{00039 \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00040 \textcolor{preprocessor}{\#ifdef HAVE\_SYS\_FILE\_H}}
\DoxyCodeLine{00041 \textcolor{preprocessor}{\#  include <sys/file.h>}         \textcolor{comment}{/* IWYU pragma: keep */}}
\DoxyCodeLine{00042 \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00043 \textcolor{preprocessor}{\#ifdef HAVE\_SYS\_STAT\_H}}
\DoxyCodeLine{00044 \textcolor{preprocessor}{\#  include <sys/stat.h>}         \textcolor{comment}{/* IWYU pragma: keep */}}
\DoxyCodeLine{00045 \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00046 \textcolor{preprocessor}{\#ifdef HAVE\_IO\_H}}
\DoxyCodeLine{00047 \textcolor{preprocessor}{\#  include <io.h>}               \textcolor{comment}{/* IWYU pragma: keep */}}
\DoxyCodeLine{00048 \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00049 \textcolor{preprocessor}{\#include "{}librsync.h"{}}}
\DoxyCodeLine{00050 \textcolor{preprocessor}{\#include "{}trace.h"{}}}
\DoxyCodeLine{00051 }
\DoxyCodeLine{00052 \textcolor{comment}{/* Use fseeko64, \_fseeki64, or fseeko for long files if they exist. */}}
\DoxyCodeLine{00053 \textcolor{preprocessor}{\#if defined(HAVE\_FSEEKO64) \&\& (SIZEOF\_OFF\_T < 8)}}
\DoxyCodeLine{00054 \textcolor{preprocessor}{\#  define fopen(f, m) fopen64((f), (m))}}
\DoxyCodeLine{00055 \textcolor{preprocessor}{\#  define fseek(f, o, w) fseeko64((f), (o), (w))}}
\DoxyCodeLine{00056 \textcolor{preprocessor}{\#elif defined(HAVE\_\_FSEEKI64)}}
\DoxyCodeLine{00057 \textcolor{preprocessor}{\#  define fseek(f, o, w) \_fseeki64((f), (o), (w))}}
\DoxyCodeLine{00058 \textcolor{preprocessor}{\#elif defined(HAVE\_FSEEKO)}}
\DoxyCodeLine{00059 \textcolor{preprocessor}{\#  define fseek(f, o, w) fseeko((f), (o), (w))}}
\DoxyCodeLine{00060 \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00061 }
\DoxyCodeLine{00062 \textcolor{comment}{/* Use fstat64 or \_fstati64 for long file fstat if they exist. */}}
\DoxyCodeLine{00063 \textcolor{preprocessor}{\#if defined(HAVE\_FSTAT64) \&\& (SIZEOF\_OFF\_T < 8)}}
\DoxyCodeLine{00064 \textcolor{preprocessor}{\#  define stat stat64}}
\DoxyCodeLine{00065 \textcolor{preprocessor}{\#  define fstat(f,s) fstat64((f), (s))}}
\DoxyCodeLine{00066 \textcolor{preprocessor}{\#elif defined(HAVE\_\_FSTATI64)}}
\DoxyCodeLine{00067 \textcolor{preprocessor}{\#  define stat \_stati64}}
\DoxyCodeLine{00068 \textcolor{preprocessor}{\#  define fstat(f,s) \_fstati64((f), (s))}}
\DoxyCodeLine{00069 \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00070 }
\DoxyCodeLine{00071 \textcolor{comment}{/* Make sure S\_ISREG is defined. */}}
\DoxyCodeLine{00072 \textcolor{preprocessor}{\#ifndef S\_ISREG}}
\DoxyCodeLine{00073 \textcolor{preprocessor}{\#  define S\_ISREG(x) ((x) \& \_S\_IFREG)}}
\DoxyCodeLine{00074 \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00075 }
\DoxyCodeLine{00076 \textcolor{comment}{/* Use and prefer \_fileno if it exists. */}}
\DoxyCodeLine{00077 \textcolor{preprocessor}{\#ifdef HAVE\_\_FILENO}}
\DoxyCodeLine{00078 \textcolor{preprocessor}{\#  define fileno(f) \_fileno((f))}}
\DoxyCodeLine{00079 \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00080 }
\DoxyCodeLine{00081 FILE *rs\_file\_open(\textcolor{keywordtype}{char} \textcolor{keyword}{const} *filename, \textcolor{keywordtype}{char} \textcolor{keyword}{const} *mode, \textcolor{keywordtype}{int} force)}
\DoxyCodeLine{00082 \{}
\DoxyCodeLine{00083     FILE *f;}
\DoxyCodeLine{00084     \textcolor{keywordtype}{int} is\_write;}
\DoxyCodeLine{00085 }
\DoxyCodeLine{00086     is\_write = mode[0] == \textcolor{charliteral}{'w'};}
\DoxyCodeLine{00087 }
\DoxyCodeLine{00088     \textcolor{keywordflow}{if} (!filename || !strcmp(\textcolor{stringliteral}{"{}-\/"{}}, filename)) \{}
\DoxyCodeLine{00089         \textcolor{keywordflow}{if} (is\_write) \{}
\DoxyCodeLine{00090 \textcolor{preprocessor}{\#if \_WIN32}}
\DoxyCodeLine{00091             \_setmode(\_fileno(stdout), \_O\_BINARY);}
\DoxyCodeLine{00092 \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00093             \textcolor{keywordflow}{return} stdout;}
\DoxyCodeLine{00094         \} \textcolor{keywordflow}{else} \{}
\DoxyCodeLine{00095 \textcolor{preprocessor}{\#if \_WIN32}}
\DoxyCodeLine{00096             \_setmode(\_fileno(stdin), \_O\_BINARY);}
\DoxyCodeLine{00097 \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00098             \textcolor{keywordflow}{return} stdin;}
\DoxyCodeLine{00099         \}}
\DoxyCodeLine{00100     \}}
\DoxyCodeLine{00101 }
\DoxyCodeLine{00102     \textcolor{keywordflow}{if} (!force \&\& is\_write) \{}
\DoxyCodeLine{00103         \textcolor{keywordflow}{if} ((f = fopen(filename, \textcolor{stringliteral}{"{}rb"{}}))) \{}
\DoxyCodeLine{00104             \textcolor{comment}{// File exists}}
\DoxyCodeLine{00105             rs\_error(\textcolor{stringliteral}{"{}File exists \(\backslash\)"{}\%s\(\backslash\)"{}, aborting!"{}}, filename);}
\DoxyCodeLine{00106             fclose(f);}
\DoxyCodeLine{00107             exit(RS\_IO\_ERROR);}
\DoxyCodeLine{00108         \}}
\DoxyCodeLine{00109     \}}
\DoxyCodeLine{00110 }
\DoxyCodeLine{00111     \textcolor{keywordflow}{if} (!(f = fopen(filename, mode))) \{}
\DoxyCodeLine{00112         rs\_error(\textcolor{stringliteral}{"{}Error opening \(\backslash\)"{}\%s\(\backslash\)"{} for \%s: \%s"{}}, filename,}
\DoxyCodeLine{00113                  is\_write ? \textcolor{stringliteral}{"{}write"{}} : \textcolor{stringliteral}{"{}read"{}}, strerror(errno));}
\DoxyCodeLine{00114         exit(RS\_IO\_ERROR);}
\DoxyCodeLine{00115     \}}
\DoxyCodeLine{00116 }
\DoxyCodeLine{00117     \textcolor{keywordflow}{return} f;}
\DoxyCodeLine{00118 \}}
\DoxyCodeLine{00119 }
\DoxyCodeLine{00120 \textcolor{keywordtype}{int} rs\_file\_close(FILE *f)}
\DoxyCodeLine{00121 \{}
\DoxyCodeLine{00122     \textcolor{keywordflow}{if} ((f == stdin) || (f == stdout))}
\DoxyCodeLine{00123         \textcolor{keywordflow}{return} 0;}
\DoxyCodeLine{00124     \textcolor{keywordflow}{return} fclose(f);}
\DoxyCodeLine{00125 \}}
\DoxyCodeLine{00126 }
\DoxyCodeLine{00127 rs\_long\_t rs\_file\_size(FILE *f)}
\DoxyCodeLine{00128 \{}
\DoxyCodeLine{00129     \textcolor{keyword}{struct }stat st;}
\DoxyCodeLine{00130     \textcolor{keywordflow}{if} ((fstat(fileno(f), \&st) == 0) \&\& (S\_ISREG(st.st\_mode)))}
\DoxyCodeLine{00131         \textcolor{keywordflow}{return} st.st\_size;}
\DoxyCodeLine{00132     \textcolor{keywordflow}{return} -\/1;}
\DoxyCodeLine{00133 \}}
\DoxyCodeLine{00134 }
\DoxyCodeLine{00135 rs\_result rs\_file\_copy\_cb(\textcolor{keywordtype}{void} *arg, rs\_long\_t pos, \textcolor{keywordtype}{size\_t} *len, \textcolor{keywordtype}{void} **buf)}
\DoxyCodeLine{00136 \{}
\DoxyCodeLine{00137     FILE *f = (FILE *)arg;}
\DoxyCodeLine{00138 }
\DoxyCodeLine{00139     \textcolor{keywordflow}{if} (fseek(f, pos, SEEK\_SET)) \{}
\DoxyCodeLine{00140         rs\_error(\textcolor{stringliteral}{"{}seek failed: \%s"{}}, strerror(errno));}
\DoxyCodeLine{00141         \textcolor{keywordflow}{return} RS\_IO\_ERROR;}
\DoxyCodeLine{00142     \}}
\DoxyCodeLine{00143     *len = fread(*buf, 1, *len, f);}
\DoxyCodeLine{00144     \textcolor{keywordflow}{if} (*len) \{}
\DoxyCodeLine{00145         \textcolor{keywordflow}{return} RS\_DONE;}
\DoxyCodeLine{00146     \} \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (ferror(f)) \{}
\DoxyCodeLine{00147         rs\_error(\textcolor{stringliteral}{"{}read error: \%s"{}}, strerror(errno));}
\DoxyCodeLine{00148         \textcolor{keywordflow}{return} RS\_IO\_ERROR;}
\DoxyCodeLine{00149     \} \textcolor{keywordflow}{else} \{}
\DoxyCodeLine{00150         rs\_error(\textcolor{stringliteral}{"{}unexpected eof on fd\%d"{}}, fileno(f));}
\DoxyCodeLine{00151         \textcolor{keywordflow}{return} RS\_INPUT\_ENDED;}
\DoxyCodeLine{00152     \}}
\DoxyCodeLine{00153 \}}

\end{DoxyCode}
