~ubuntu-branches/ubuntu/vivid/postfix/vivid-proposed

« back to all changes in this revision

Viewing changes to src/util/sane_link.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2005-02-27 09:33:07 UTC
  • Revision ID: james.westby@ubuntu.com-20050227093307-cn789t27ibnlh6tf
Tags: upstream-2.1.5
ImportĀ upstreamĀ versionĀ 2.1.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*++
 
2
/* NAME
 
3
/*      sane_link 3
 
4
/* SUMMARY
 
5
/*      sanitize link() error returns
 
6
/* SYNOPSIS
 
7
/*      #include <sane_fsops.h>
 
8
/*
 
9
/*      int     sane_link(from, to)
 
10
/*      const char *from;
 
11
/*      const char *to;
 
12
/* DESCRIPTION
 
13
/*      sane_link() implements the link(2) system call, and works
 
14
/*      around some errors that are possible with NFS file systems.
 
15
/* LICENSE
 
16
/* .ad
 
17
/* .fi
 
18
/*      The Secure Mailer license must be distributed with this software.
 
19
/* AUTHOR(S)
 
20
/*      Wietse Venema
 
21
/*      IBM T.J. Watson Research
 
22
/*      P.O. Box 704
 
23
/*      Yorktown Heights, NY 10598, USA
 
24
/*--*/
 
25
 
 
26
/* System library. */
 
27
 
 
28
#include "sys_defs.h"
 
29
#include <sys/stat.h>
 
30
#include <errno.h>
 
31
#include <unistd.h>
 
32
 
 
33
/* Utility library. */
 
34
 
 
35
#include "msg.h"
 
36
#include "sane_fsops.h"
 
37
 
 
38
/* sane_link - sanitize link() error returns */
 
39
 
 
40
int     sane_link(const char *from, const char *to)
 
41
{
 
42
    char   *myname = "sane_link";
 
43
    int     saved_errno;
 
44
    struct stat from_st;
 
45
    struct stat to_st;
 
46
 
 
47
    /*
 
48
     * Normal case: link() succeeds.
 
49
     */
 
50
    if (link(from, to) >= 0)
 
51
        return (0);
 
52
 
 
53
    /*
 
54
     * Woops. Save errno, and see if the error is an NFS artefact. If it is,
 
55
     * pretend the error never happened.
 
56
     */
 
57
    saved_errno = errno;
 
58
    if (stat(from, &from_st) >= 0 && stat(to, &to_st) >= 0
 
59
        && from_st.st_dev == to_st.st_dev
 
60
        && from_st.st_ino == to_st.st_ino) {
 
61
        msg_info("%s(%s,%s): worked around spurious NFS error",
 
62
                 myname, from, to);
 
63
        return (0);
 
64
    }
 
65
 
 
66
    /*
 
67
     * Nope, it didn't. Restore errno and report the error.
 
68
     */
 
69
    errno = saved_errno;
 
70
    return (-1);
 
71
}