summaryrefslogtreecommitdiff
path: root/examples/libsmbclient/smbwrapper/smbw_dir.c
blob: f3ec03e5a8583ceae53dc97baad0f3fd795f9343 (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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/* 
   Unix SMB/Netbios implementation.
   Version 2.0
   SMB wrapper directory functions
   Copyright (C) Andrew Tridgell 1998
   Copyright (C) Derrell Lipman 2003-2005
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "smbw.h"

/***************************************************** 
determine if a directory handle is a smb one
*******************************************************/
int smbw_dirp(DIR * dirp)
{
        return ((char *) dirp >= (char *) smbw_fd_map &&
                (char *) dirp < (char *) &smbw_fd_map[__FD_SETSIZE] &&
                *(int *) dirp != -1);
}


/***************************************************** 
a wrapper for getdents()
*******************************************************/
int smbw_getdents(unsigned int fd_smbw,
                  struct SMBW_dirent *dirent_external,
                  int count)
{
        int remaining;
        int fd_client = smbw_fd_map[fd_smbw];
        struct smbc_dirent *dirent_internal;


        for (remaining = count;
             remaining > sizeof(struct SMBW_dirent);
             dirent_external++) {

                /*
                 * We do these one at a time because there's otherwise no way
                 * to limit how many smbc_getdents() will return for us, and
                 * if it returns too many, it also doesn't give us offsets to
                 * be able to seek back to where we need to be.  In practice,
                 * this one-at-a-time retrieval isn't a problem because the
                 * time-consuming network transaction is all done at
                 * smbc_opendir() time.
                 */
                dirent_internal = smbc_readdir(fd_client);
                if (dirent_internal == NULL) {
                        break;
                }

                remaining -= sizeof(struct SMBW_dirent);

                dirent_external->d_ino = -1; /* not supported */
		dirent_external->d_off = smbc_telldir(fd_client);
		dirent_external->d_reclen = sizeof(struct SMBW_dirent);
                dirent_external->d_type = dirent_internal->smbc_type;

                strncpy(dirent_external->d_name,
                        dirent_internal->name,
                        sizeof(dirent_external->d_name) - 1);
                strncpy(dirent_external->d_comment,
                        dirent_internal->comment,
                        sizeof(dirent_external->d_comment) - 1);
	}

	return(count - remaining);
}


/***************************************************** 
a wrapper for chdir()
*******************************************************/
int smbw_chdir(const char *name)
{
        int simulate;
        struct stat statbuf;
        char path[PATH_MAX];
        char *p;

	SMBW_INIT();

	if (!name) {
		errno = EINVAL;
                return -1;
	}

	if (! smbw_path((char *) name)) {
		if ((* smbw_libc.chdir)(name) == 0) {
                        *smbw_cwd = '\0';
                        return 0;
		}

                return -1;
	}

        smbw_fix_path(name, path);

        /* ensure it exists */
        p = path + 6;           /* look just past smb:// */
        simulate = (strchr(p, '/') == NULL);

        /* special case for full-network scan, workgroups, and servers */
        if (! simulate) {
            
            if (smbc_stat(path, &statbuf) < 0) {
                return -1;
            }
            
            /* ensure it's a directory */
            if (! S_ISDIR(statbuf.st_mode)) {
                errno = ENOTDIR;
                return -1;
            }
        }

        strncpy(smbw_cwd, path, PATH_MAX);

	/* we don't want the old directory to be busy */
	(* smbw_libc.chdir)("/");

	return 0;
}


/***************************************************** 
a wrapper for mkdir()
*******************************************************/
int smbw_mkdir(const char *fname, mode_t mode)
{
        char path[PATH_MAX];

	if (!fname) {
		errno = EINVAL;
		return -1;
	}

	SMBW_INIT();

        smbw_fix_path(fname, path);
        return smbc_mkdir(path, mode);
}

/***************************************************** 
a wrapper for rmdir()
*******************************************************/
int smbw_rmdir(const char *fname)
{
        char path[PATH_MAX];

	if (!fname) {
		errno = EINVAL;
		return -1;
	}

	SMBW_INIT();

        smbw_fix_path(fname, path);
        return smbc_rmdir(path);
}


/***************************************************** 
a wrapper for getcwd()
*******************************************************/
char *smbw_getcwd(char *buf, size_t size)
{
	SMBW_INIT();

        if (*smbw_cwd == '\0') {
                return (* smbw_libc.getcwd)(buf, size);
        }

        if (buf == NULL) {
                if (size == 0) {
                        size = strlen(smbw_cwd) + 1;
                }
                buf = malloc(size);
                if (buf == NULL) {
                        errno = ENOMEM;
                        return NULL;
                }
        }

        strncpy(buf, smbw_cwd, size);
        buf[size-1] = '\0';
	return buf;
}

/***************************************************** 
a wrapper for fchdir()
*******************************************************/
int smbw_fchdir(int fd_smbw)
{
        int ret;

        SMBW_INIT();

        if (! smbw_fd(fd_smbw)) {
                ret = (* smbw_libc.fchdir)(fd_smbw);
                (void) (* smbw_libc.getcwd)(smbw_cwd, PATH_MAX);
                return ret;
        }

        errno = EACCES;
        return -1;
}

/***************************************************** 
open a directory on the server
*******************************************************/
DIR *smbw_opendir(const char *fname)
{
        int fd_client;
        int fd_smbw;
	char path[PATH_MAX];
        DIR * dirp;

	SMBW_INIT();

	if (!fname) {
		errno = EINVAL;
		return NULL;
	}

	fd_smbw = (smbw_libc.open)(SMBW_DUMMY, O_WRONLY, 0200);
	if (fd_smbw == -1) {
		errno = EMFILE;
                return NULL;
	}

        smbw_fix_path(fname, path);
        fd_client =  smbc_opendir(path);

        if (fd_client < 0) {
                (* smbw_libc.close)(fd_smbw);
                return NULL;
        }

        smbw_fd_map[fd_smbw] = fd_client;
        smbw_ref(fd_client, SMBW_RCT_Increment);
        dirp = (DIR *) &smbw_fd_map[fd_smbw];
        return dirp;
}

/***************************************************** 
read one entry from a directory
*******************************************************/
struct SMBW_dirent *smbw_readdir(DIR *dirp)
{
        int fd_smbw;
        int fd_client;
        struct smbc_dirent *dirent_internal;
        static struct SMBW_dirent dirent_external;

        fd_smbw = (int *) dirp - smbw_fd_map;
        fd_client = smbw_fd_map[fd_smbw];

	if ((dirent_internal = smbc_readdir(fd_client)) == NULL) {
		return NULL;
        }
 
        dirent_external.d_ino = -1; /* not supported */
        dirent_external.d_off = smbc_telldir(fd_client);
        dirent_external.d_reclen = sizeof(struct SMBW_dirent);
        dirent_external.d_type = dirent_internal->smbc_type;
        strncpy(dirent_external.d_name,
                dirent_internal->name,
                sizeof(dirent_external.d_name) - 1);
        strncpy(dirent_external.d_comment,
                dirent_internal->comment,
                sizeof(dirent_external.d_comment) - 1);

	return &dirent_external;
}

/***************************************************** 
read one entry from a directory in a reentrant fashion
ha!  samba is not re-entrant, and neither is the
libsmbclient library
*******************************************************/
int smbw_readdir_r(DIR *dirp,
                   struct SMBW_dirent *__restrict entry,
                   struct SMBW_dirent **__restrict result)
{
        SMBW_dirent *dirent;

        dirent = smbw_readdir(dirp);

        if (dirent != NULL) {
                *entry = *dirent;
                if (result != NULL) {
                        *result = entry;
                }
                return 0;
        }

        if (result != NULL) {
                *result = NULL;
        }
	return EBADF;
}


/***************************************************** 
close a DIR*
*******************************************************/
int smbw_closedir(DIR *dirp)
{
        int fd_smbw = (int *) dirp - smbw_fd_map;
        int fd_client = smbw_fd_map[fd_smbw];

        (* smbw_libc.close)(fd_smbw);
        if (smbw_ref(fd_client, SMBW_RCT_Decrement) > 0) {
                return 0;
        }
        smbw_fd_map[fd_smbw] = -1;
        return smbc_closedir(fd_client);
}

/***************************************************** 
seek in a directory
*******************************************************/
void smbw_seekdir(DIR *dirp, long long offset)
{
        int fd_smbw = (int *) dirp - smbw_fd_map;
        int fd_client = smbw_fd_map[fd_smbw];

        smbc_lseekdir(fd_client, offset);
}

/***************************************************** 
current loc in a directory
*******************************************************/
long long smbw_telldir(DIR *dirp)
{
        int fd_smbw = (int *) dirp - smbw_fd_map;
        int fd_client = smbw_fd_map[fd_smbw];

        return (long long) smbc_telldir(fd_client);
}