aboutsummaryrefslogtreecommitdiffhomepage
path: root/contrib/notmuch-deliver/src/maildiropen.c
blob: 5071df7681ea6c052700337e3ad2ded7acb3ddc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
** Copyright 2000 Double Precision, Inc.
** See COPYING for distribution information.
*/

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include	<sys/types.h>
#include	<sys/stat.h>
#include	<string.h>
#include	<stdlib.h>
#include	<time.h>
#if	HAVE_UNISTD_H
#include	<unistd.h>
#endif
#include	<stdio.h>
#include	<ctype.h>
#include	<errno.h>
#include	<fcntl.h>

#include	"maildirmisc.h"

static const char rcsid[]="$Id: maildiropen.c,v 1.8 2003/01/19 16:39:52 mrsam Exp $";

char *maildir_getlink(const char *filename)
{
#if     HAVE_READLINK
size_t	bufsiz;
char	*buf;

	bufsiz=0;
	buf=0;

	for (;;)
	{
	int	n;

		if (buf)	free(buf);
		bufsiz += 256;
		if ((buf=malloc(bufsiz)) == 0)
		{
			perror("malloc");
			return (0);
		}
		if ((n=readlink(filename, buf, bufsiz)) < 0)
		{
			free(buf);
			return (0);
		}
		if (n < bufsiz)
		{
			buf[n]=0;
			break;
		}
	}
	return (buf);
#else
	return (0);
#endif
}

int maildir_semisafeopen(const char *path, int mode, int perm)
{

#if	HAVE_READLINK

char	*l=maildir_getlink(path);

	if (l)
	{
	int	f;

		if (*l != '/')
		{
		char	*q=malloc(strlen(path)+strlen(l)+2);
		char	*s;

			if (!q)
			{
				free(l);
				return (-1);
			}

			strcpy(q, path);
			if ((s=strchr(q, '/')) != 0)
				s[1]=0;
			else	*q=0;
			strcat(q, l);
			free(l);
			l=q;
		}

		f=maildir_safeopen(l, mode, perm);

		free(l);
		return (f);
	}
#endif

	return (maildir_safeopen(path, mode, perm));
}
		
int maildir_safeopen(const char *path, int mode, int perm)
{
	struct	stat	stat1;

	return maildir_safeopen_stat(path, mode, perm, &stat1);
}

int maildir_safeopen_stat(const char *path, int mode, int perm,
			  struct stat *stat1)
{
	struct	stat	stat2;

	int	fd=open(path, mode
#ifdef	O_NONBLOCK
			| O_NONBLOCK
#else
			| O_NDELAY
#endif
			, perm);

	if (fd < 0)	return (fd);
	if (fcntl(fd, F_SETFL, (mode & O_APPEND)) || fstat(fd, stat1)
	    || lstat(path, &stat2))
	{
		close(fd);
		return (-1);
	}

	if (stat1->st_dev != stat2.st_dev || stat1->st_ino != stat2.st_ino)
	{
		close(fd);
		errno=ENOENT;
		return (-1);
	}

	return (fd);
}