File: Cns_readdir.c

package info (click to toggle)
lcgdm 1.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 17,532 kB
  • sloc: ansic: 226,887; sh: 13,562; perl: 11,575; python: 11,572; cpp: 5,716; sql: 1,824; makefile: 1,301; fortran: 113
file content (190 lines) | stat: -rw-r--r-- 4,392 bytes parent folder | download | duplicates (8)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Copyright (C) 1999-2008 by CERN/IT/PDP/DM
 * All rights reserved
 */

#ifndef lint
static char sccsid[] = "@(#)$RCSfile: Cns_readdir.c,v $ $Revision: 1.5 $ $Date: 2008/02/04 12:08:52 $ CERN IT-PDP/DM Jean-Philippe Baud";
#endif /* not lint */

/*	Cns_readdir - read a directory entry */

#include <errno.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#if defined(_WIN32)
#include <winsock2.h>
#else
#include <unistd.h>
#include <netinet/in.h>
#endif
#include "marshall.h"
#include "Cns_api.h"
#include "Cns.h"
#include "serrno.h"

#if (defined(sun) || defined(linux))
struct dirent64 DLL_DECL *
#else
struct dirent DLL_DECL *
#endif
Cns_readdir64(Cns_DIR *dirp)
{
	int c;
	int direntsz;
#if (defined(sun) || defined(linux))
	struct dirent64 *dp;
#else
	struct dirent *dp;
#endif
	char func[16];
	int getattr = 0;
	gid_t gid;
	int msglen;
	int n;
	int nbentries;
	char *q;
	char *rbp;
	char repbuf[DIRBUFSZ 4];
	char *sbp;
	char sendbuf[REQBUFSZ];
	uid_t uid;
 
	strcpy (func, "Cns_readdir");
	uid = geteuid();
	gid = getegid();
#if defined(_WIN32)
	if (uid < 0 || gid < 0) {
		Cns_errmsg (func, NS053);
		serrno = SENOMAPFND;
		return (NULL);
	}
#endif

	if (! dirp) {
		serrno = EFAULT;
		return (NULL);
	}
	/* compute size of client machine dirent structure excluding d_name */

#if (defined(sun) || defined(linux))
	dp = (struct dirent64 *) dirp->dd_buf;
#else
	dp = (struct dirent *) dirp->dd_buf;
#endif
	direntsz = &dp->d_name[0] - (char *) dp;

	if (dirp->dd_size == 0) {	/* no data in the cache */
		if (dirp->eod)
			return (NULL);

		/* Build request header */

		sbp = sendbuf;
		marshall_LONG (sbp, CNS_MAGIC);
		marshall_LONG (sbp, CNS_READDIR);
		q = sbp;        /* save pointer. The next field will be updated */
		msglen = 3 * LONGSIZE;
		marshall_LONG (sbp, msglen);
	 
		/* Build request body */

		marshall_LONG (sbp, uid);
		marshall_LONG (sbp, gid);
		marshall_WORD (sbp, getattr);
		marshall_WORD (sbp, direntsz);
		marshall_HYPER (sbp, dirp->fileid);
		marshall_WORD (sbp, dirp->bod);

		msglen = sbp - sendbuf;
		marshall_LONG (q, msglen);	/* update length field */

		c = send2nsd (&dirp->dd_fd, NULL, sendbuf, msglen,
		    repbuf, sizeof(repbuf));

		if (c < 0)
			return (NULL);
		rbp = repbuf;
		unmarshall_WORD (rbp, nbentries);
		if (nbentries == 0)
			return (NULL);		/* end of directory */

		/* unmarshall reply into the dirent structures */

#if (defined(sun) || defined(linux))
		dp = (struct dirent64 *) dirp->dd_buf;
#else
		dp = (struct dirent *) dirp->dd_buf;
#endif
		while (nbentries--) {
			dp->d_ino = 0;
#if defined(__linux__) || defined(sgi) || defined(SOLARIS)
			dp->d_off = 0;
#endif
#if defined(_AIX)
			dp->d_offset = 0;
#endif
#if defined(linux) || defined(__APPLE__)
			dp->d_type = 0;
#endif
			unmarshall_STRING (rbp, dp->d_name);
			n = strlen (dp->d_name);
#if defined(_AIX) || (defined(__alpha) && defined(__osf__)) || defined(hpux) || defined(__APPLE__) || defined(__GNU__) || defined(__FreeBSD_kernel__)
			dp->d_namlen = n;
#endif
			dp->d_reclen = ((direntsz   n   8) / 8) * 8;
#if (defined(sun) || defined(linux))
			dp = (struct dirent64 *) ((char *) dp   dp->d_reclen);
#else
			dp = (struct dirent *) ((char *) dp   dp->d_reclen);
#endif
		}
		dirp->bod = 0;
		unmarshall_WORD (rbp, dirp->eod);
		dirp->dd_size = (char *) dp - dirp->dd_buf;
	}
#if (defined(sun) || defined(linux))
	dp = (struct dirent64 *) (dirp->dd_buf   dirp->dd_loc);
#else
	dp = (struct dirent *) (dirp->dd_buf   dirp->dd_loc);
#endif
	dirp->dd_loc  = dp->d_reclen;
	if (dirp->dd_loc >= dirp->dd_size) {	/* must refill next time */
		dirp->dd_loc = 0;
		dirp->dd_size = 0;
	}
	return (dp);
}

struct dirent DLL_DECL *
Cns_readdir(Cns_DIR *dirp)
{
#if !defined(SOLARIS) && !defined(linux)
	return (Cns_readdir64 (dirp));
#else
	struct dirent64 *dp64;
	struct dirent *dp32;
	short namlen;

	if ((dp64 = Cns_readdir64 (dirp)) == NULL)
		return (NULL);

	namlen = strlen (dp64->d_name);
	dp32 = (struct dirent *) dp64;
	dp32->d_ino = dp64->d_ino;
#if defined(SOLARIS) || defined(__linux__)
	dp32->d_off = dp64->d_off;
#endif
#if defined(linux)
	dp32->d_type = dp64->d_type;
#endif
#if defined(__GNU__) || defined(__FreeBSD_kernel__)
	dp32->d_namlen = dp64->d_namlen;
#endif
	strcpy (dp32->d_name, dp64->d_name);
	dp32->d_reclen = ((&dp32->d_name[0] - (char *) dp32   namlen   8) / 8) * 8;
	return (dp32);
#endif
}