+
+/****************************************************************************
+create any necessary directories in fname. Unfortunately we don't know
+what perms to give the directory when this is called so we need to rely
+on the umask
+****************************************************************************/
+int create_directory_path(char *fname)
+{
+ extern int orig_umask;
+ char *p;
+
+ while (*fname == '/') fname++;
+ while (strncmp(fname,"./",2)==0) fname += 2;
+
+ p = fname;
+ while ((p=strchr(p,'/'))) {
+ *p = 0;
+ do_mkdir(fname,0777 & ~orig_umask);
+ *p = '/';
+ p++;
+ }
+ return 0;
+}
+
+
+/* Write LEN bytes at PTR to descriptor DESC, retrying if interrupted.
+ Return LEN upon success, write's (negative) error code otherwise.
+
+ derived from GNU C's cccp.c.
+*/
+int full_write(int desc, char *ptr, int len)
+{
+ int total_written;
+
+ total_written = 0;
+ while (len > 0) {
+ int written = write (desc, ptr, len);
+ if (written < 0) {
+#ifdef EINTR
+ if (errno == EINTR)
+ continue;
+#endif
+ return written;
+ }
+ total_written += written;
+ ptr += written;
+ len -= written;
+ }
+ return total_written;
+}
+
+/* Read LEN bytes at PTR from descriptor DESC, retrying if interrupted.
+ Return the actual number of bytes read, zero for EOF, or negative
+ for an error.
+
+ derived from GNU C's cccp.c. */
+int safe_read(int desc, char *ptr, int len)
+{
+ int n_chars;
+
+ if (len <= 0)
+ return len;
+
+#ifdef EINTR
+ do {
+ n_chars = read(desc, ptr, len);
+ } while (n_chars < 0 && errno == EINTR);
+#else
+ n_chars = read(desc, ptr, len);
+#endif
+
+ return n_chars;
+}
+
+
+/* copy a file - this is used in conjunction with the --temp-dir option */
+int copy_file(char *source, char *dest, mode_t mode)
+{
+ int ifd;
+ int ofd;
+ char buf[1024 * 8];
+ int len; /* Number of bytes read into `buf'. */
+
+ ifd = open(source, O_RDONLY);
+ if (ifd == -1) {
+ rprintf(FERROR,"open %s: %s\n",
+ source,strerror(errno));
+ return -1;
+ }
+
+ if (do_unlink(dest) && errno != ENOENT) {
+ rprintf(FERROR,"unlink %s: %s\n",
+ dest,strerror(errno));
+ return -1;
+ }
+
+ ofd = do_open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode);
+ if (ofd < 0) {
+ rprintf(FERROR,"open %s: %s\n",
+ dest,strerror(errno));
+ close(ifd);
+ return -1;
+ }
+
+ while ((len = safe_read(ifd, buf, sizeof(buf))) > 0) {
+ if (full_write(ofd, buf, len) < 0) {
+ rprintf(FERROR,"write %s: %s\n",
+ dest,strerror(errno));
+ close(ifd);
+ close(ofd);
+ return -1;
+ }
+ }
+
+ close(ifd);
+ close(ofd);
+
+ if (len < 0) {
+ rprintf(FERROR,"read %s: %s\n",
+ source,strerror(errno));
+ return -1;
+ }
+
+ return 0;
+}
+
+/* sleep for a while via select */
+void u_sleep(int usec)
+{
+ struct timeval tv;
+
+ tv.tv_sec = 0;
+ tv.tv_usec = usec;
+ select(0, NULL, NULL, NULL, &tv);
+}
+
+
+static pid_t all_pids[10];
+static int num_pids;
+
+/* fork and record the pid of the child */
+pid_t do_fork(void)
+{
+ pid_t newpid = fork();
+
+ if (newpid) {
+ all_pids[num_pids++] = newpid;
+ }
+ return newpid;
+}
+
+/* kill all children */
+void kill_all(int sig)
+{
+ int i;
+ for (i=0;i<num_pids;i++) {
+ if (all_pids[i] != getpid())
+ kill(all_pids[i], sig);
+ }
+}
+
+/* like strncpy but does not 0 fill the buffer and always null
+ terminates (thus it can use maxlen+1 space in d) */
+void strlcpy(char *d, char *s, int maxlen)
+{
+ int len = strlen(s);
+ if (len > maxlen) len = maxlen;
+ memcpy(d, s, len);
+ d[len] = 0;
+}
+
+/* turn a user name into a uid */
+int name_to_uid(char *name, uid_t *uid)
+{
+ struct passwd *pass;
+ if (!name || !*name) return 0;
+ pass = getpwnam(name);
+ if (pass) {
+ *uid = pass->pw_uid;
+ return 1;
+ }
+ return 0;
+}
+
+/* turn a group name into a gid */
+int name_to_gid(char *name, gid_t *gid)
+{
+ struct group *grp;
+ if (!name || !*name) return 0;
+ grp = getgrnam(name);
+ if (grp) {
+ *gid = grp->gr_gid;
+ return 1;
+ }
+ return 0;
+}
+
+
+/****************************************************************************
+check if a process exists.
+****************************************************************************/
+int process_exists(int pid)
+{
+ return(kill(pid,0) == 0 || errno != ESRCH);
+}
+
+/* lock a byte range in a open file */
+int lock_range(int fd, int offset, int len)
+{
+ struct flock lock;
+
+ lock.l_type = F_WRLCK;
+ lock.l_whence = SEEK_SET;
+ lock.l_start = offset;
+ lock.l_len = len;
+ lock.l_pid = 0;
+
+ return fcntl(fd,F_SETLK,&lock) == 0;
+}