File: dcap_access.c

package info (click to toggle)
dcap 2.47.8-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,388 kB
  • ctags: 1,474
  • sloc: ansic: 14,235; makefile: 395; python: 75; sh: 58
file content (124 lines) | stat: -rw-r--r-- 1,687 bytes parent folder | download | duplicates (7)
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
/*
 *   DCAP - dCache Access Protocol client interface
 *
 *   Copyright (C) 2000,2004 DESY Hamburg DMG-Division.
 *
 *   AUTHOR: Tigran Mkrtchayn (tigran.mkrtchyan@desy.de)
 *
 *   This program can be distributed under the terms of the GNU LGPL.
 *   See the file COPYING.LIB
 *
 */


/*
 * $Id: dcap_access.c,v 1.3 2004-11-01 19:33:29 tigran Exp $
 */

#include "dcap.h"

/**
  * faked access system call, based on stat.
  */


#ifndef WIN32

int
dc_access(const char *path, int mode)
{
	int rc;
	struct stat sbuf;
	uid_t uid;
	gid_t gid;
	int isOwner = 0;
	int isGroup = 0;
	int result = 0;

	rc = dc_stat(path, &sbuf);
	if( rc == 0 ) {

		if( mode == F_OK ) {
			return 0;
		}

		uid = geteuid();
		gid = getegid();


		if( uid == sbuf.st_uid ) {
			isOwner = 1;
		}

		if( gid == sbuf.st_gid ) {
			isGroup = 1;
		}


		rc = 0;
		result = 1;

		if( (mode & R_OK) == R_OK ) {
			if( isOwner ) {
				rc |= sbuf.st_mode & S_IRUSR;
			}

			if( isGroup ) {
				rc |= sbuf.st_mode & S_IRGRP;
			}

			rc |= sbuf.st_mode & S_IROTH;

			result &= rc != 0 ? 1 : 0;
		}

		if( (mode & W_OK) == W_OK ) {
			if( isOwner ) {
				rc |= sbuf.st_mode & S_IWUSR;
			}

			if( isGroup ) {
				rc |= sbuf.st_mode & S_IWGRP;
			}

			rc |= sbuf.st_mode & S_IWOTH;

			result &= rc != 0 ? 1 : 0;
		}

		if( (mode & X_OK) == X_OK ) {
			if( isOwner ) {
				rc |= sbuf.st_mode & S_IXUSR;
			}

			if( isGroup ) {
				rc |= sbuf.st_mode & S_IXGRP;
			}

			rc |= sbuf.st_mode & S_IXOTH;

			result &= rc != 0 ? 1 : 0;

		}

	}


	rc  = result == 0 ? -1 : 0;

	return rc;
}

#else
int
dc_access(const char *path, int mode)
{
	int rc;
	struct stat sbuf;

	rc  = dc_stat(path, &sbuf);

	return rc;
}

#endif