File: SmbClient.xs

package info (click to toggle)
libfilesys-smbclient-perl 3.2-3
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 392 kB
  • sloc: perl: 757; ansic: 84; makefile: 8
file content (525 lines) | stat: -rwxr-xr-x 10,981 bytes parent folder | download | duplicates (2)
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#include "config.h"
/* AIX requires this to be the first thing in the file.  */
#ifndef __GNUC__
# if HAVE_ALLOCA_H
#  include <alloca.h>
# else
#  ifdef _AIX
 #pragma alloca
#  else
#   ifndef alloca /* predefined by HP cc +Olibcalls */
char *alloca ();
#   endif
#  endif
# endif
#endif

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "libsmbclient.h"
#include "libauthSamba.h"
#include "config.h"

/* 
 * Ce fichier definit les fonctions d'interface avec libsmbclient.so 
 */

MODULE = Filesys::SmbClient    PACKAGE = Filesys::SmbClient
PROTOTYPES: ENABLE

SMBCCTX *
_init(user, password, workgroup, debug)
  char *user
  char *password  
  char* workgroup
  int debug
CODE:
/* 
 * Initialize things ... 
 */	
SMBCCTX *context;
context = smbc_new_context();
if (!context) {
  XSRETURN_UNDEF;
}
smbc_setDebug(context, 4); //4 gives a good level of trace.
set_fn(workgroup, user, password);
smbc_setFunctionAuthData(context, auth_fn);
smbc_setDebug(context, debug);
if (smbc_init_context(context) == 0) {
  smbc_free_context(context, 1); 
  XSRETURN_UNDEF;
} 
RETVAL = context; 
#ifdef VERBOSE
  fprintf(stderr, "! Filesys::SmbClient : "
	          "init %p context\n", context); 
#endif
OUTPUT:
  RETVAL


int
_shutdown( context, flag)
    SMBCCTX * context
    int flag
CODE:
    smbc_free_context(context, flag);
    RETVAL = 1;
OUTPUT:
    RETVAL

int
_set_flags(context, flag)
  SMBCCTX *context
  int flag
CODE:
/* 
 * Create directory fname
 *
 */
#ifdef HAVE_SMBCTXX_FLAG
    context->flags = flag;
#endif
#ifdef VERBOSE
  fprintf(stderr, "! Filesys::SmbClient : "
                  "_set_flags value %d\n", flag); 
#endif
  RETVAL = 1;
OUTPUT:
  RETVAL


int
_mkdir(context, fname, mode)
  SMBCCTX *context
  char *fname
  int mode
CODE:
/* 
 * Create directory fname
 *
 */
RETVAL = context->mkdir(context, fname, mode);
if (RETVAL < 0) {
  RETVAL=0;
#ifdef VERBOSE
  fprintf(stderr, "*** Error Filesys::SmbClient : "
	          "mkdir %s directory : %s\n", fname,strerror(errno)); 
#endif
}
else RETVAL = 1;
OUTPUT:
  RETVAL




int
_rmdir(context, fname)
  SMBCCTX *context
  char *fname
CODE:
/* 
 * Remove directory fname
 *
 */
RETVAL = context->rmdir(context, fname);
if (RETVAL < 0) {
  RETVAL = 0;
#ifdef VERBOSE
  fprintf(stderr, "*** Error Filesys::SmbClient : "
      	          "rmdir %s directory : %s\n", fname,strerror(errno));
#endif
} else RETVAL = 1;
OUTPUT:
  RETVAL



SMBCFILE *
_opendir(context, fname)
  SMBCCTX *context
  char *fname
CODE:
/* 
 * Open directory fname
 *
 */
  RETVAL = context->opendir(context, fname);
#ifdef VERBOSE
  fprintf(stderr, "! Filesys::SmbClient : _opendir: %d\n", RETVAL); 
#endif

  if (RETVAL < 0) { 
    RETVAL = 0;
#ifdef VERBOSE
    fprintf(stderr, "*** Error Filesys::SmbClient : "
                      "Error opendir %s : %s\n", fname, strerror(errno));
#endif
  }
OUTPUT:
  RETVAL




int
_closedir(context, fd)
  SMBCCTX *context
  SMBCFILE *fd
CODE:
/* 
 * Close file descriptor for directory fd
 *
 */
RETVAL = context->closedir(context, fd);
#ifdef VERBOSE
  if (RETVAL < 0) { 
    fprintf(stderr, "*** Error Filesys::SmbClient : "
                    "Closedir : %s\n", strerror(errno)); }
#endif
OUTPUT:
  RETVAL




void
_readdir(context, fd)
  SMBCCTX *context
  SMBCFILE *fd
PREINIT:
/* 
 * Read file descriptor for directory fd and return file type, name and comment
 *
 */
  struct smbc_dirent * dirp;
PPCODE:
#ifdef VERBOSE
  fprintf(stderr, "! Filesys::SmbClient : _readdir: %d\n", fd); 
#endif
// Fix for rt#12221 : macro "readdir" passed 2 arguments, but takes just 1
// Seems only work on linux, not solaris
// Already defined in usr/lib/perl/5.8/CORE/reentr.inc:1322:# define readdir(a)
#if !(defined (__SVR4) && defined (__sun)) && !defined(_AIX)
#undef readdir
#endif
  dirp = (struct smbc_dirent *)context->readdir(context, fd);
  if (dirp) {
    XPUSHs(sv_2mortal(newSVnv(dirp->smbc_type)));
/*
 * 	  original code here produces strings which include NULL as last char
 *        with samba 3. Reported by dpavlin at rot13.org
 *
    XPUSHs(sv_2mortal((SV*)newSVpv(dirp->name, dirp->namelen)));
    XPUSHs(sv_2mortal((SV*)newSVpv(dirp->comment, dirp->commentlen)));
*/
    XPUSHs(sv_2mortal((SV*)newSVpv(dirp->name, strlen(dirp->name))));
    XPUSHs(sv_2mortal((SV*)newSVpv(dirp->comment, strlen(dirp->comment))));
}



void
_stat(context, fname)
  SMBCCTX *context
  char *fname
PREINIT:
/* 
 * _stat(fname) : Get information about a file or directory.
 *
 */
  int i;
  struct stat buf;
PPCODE:
  i = context->stat(context, fname, &buf);
  if (i == 0) {
    XPUSHs(sv_2mortal(newSVnv(buf.st_dev)));
    XPUSHs(sv_2mortal(newSVnv(buf.st_ino)));
    XPUSHs(sv_2mortal(newSVnv(buf.st_mode)));
    XPUSHs(sv_2mortal(newSVnv(buf.st_nlink)));
    XPUSHs(sv_2mortal(newSVnv(buf.st_uid)));
    XPUSHs(sv_2mortal(newSVnv(buf.st_gid)));
    XPUSHs(sv_2mortal(newSVnv(buf.st_rdev)));
    XPUSHs(sv_2mortal(newSVnv(buf.st_size)));
    XPUSHs(sv_2mortal(newSVnv(buf.st_blksize)));
    XPUSHs(sv_2mortal(newSVnv(buf.st_blocks)));
    XPUSHs(sv_2mortal(newSVnv(buf.st_atime)));
    XPUSHs(sv_2mortal(newSVnv(buf.st_mtime)));
    XPUSHs(sv_2mortal(newSVnv(buf.st_ctime)));
} else {
#ifdef VERBOSE
  fprintf(stderr, "! Filesys::SmbClient : Stat: %s\n", strerror(errno)); 
#endif
    XPUSHs(sv_2mortal(newSVnv(0)));
}



void
_fstat(context, fd)
  SMBCCTX *context
  SMBCFILE *fd
PREINIT:
/* 
 * Get information about a file or directory via a file descriptor.
 *
 */
  int i;
  struct stat buf;
PPCODE:
i = context->fstat(context, fd, &buf);
if (i == 0) {
  XPUSHs(sv_2mortal(newSVnv(buf.st_dev)));
  XPUSHs(sv_2mortal(newSVnv(buf.st_ino)));
  XPUSHs(sv_2mortal(newSVnv(buf.st_mode)));
  XPUSHs(sv_2mortal(newSVnv(buf.st_nlink)));
  XPUSHs(sv_2mortal(newSVnv(buf.st_uid)));
  XPUSHs(sv_2mortal(newSVnv(buf.st_gid)));
  XPUSHs(sv_2mortal(newSVnv(buf.st_rdev)));
  XPUSHs(sv_2mortal(newSVnv(buf.st_size)));
  XPUSHs(sv_2mortal(newSVnv(buf.st_blksize)));
  XPUSHs(sv_2mortal(newSVnv(buf.st_blocks)));
  XPUSHs(sv_2mortal(newSVnv(buf.st_atime)));
  XPUSHs(sv_2mortal(newSVnv(buf.st_mtime)));
  XPUSHs(sv_2mortal(newSVnv(buf.st_ctime)));
} else {
  XPUSHs(sv_2mortal(newSVnv(errno)));
}




int
_rename(context, oname, nname)
  SMBCCTX *context
  char *oname
  char *nname
CODE:
/* 
 * Rename old file oname in nname
 *
 */
RETVAL = context->rename(context, oname, context, nname);
if (RETVAL < 0) { 
  RETVAL = 0;
#ifdef VERBOSE	
  fprintf(stderr, "*** Error Filesys::SmbClient : "
 		  "Rename %s in %s : %s\n", oname, nname, strerror(errno)); 
#endif
} else {
  RETVAL = 1;
}
OUTPUT:
  RETVAL


SMBCFILE*
_open(context, fname, mode)
  SMBCCTX *context
  char *fname
  int mode
PREINIT:
/* 
 * Open file fname with perm mode
 *
 */	
  int flags; 
  int seek_end = 0;
CODE:
  /* Mode >> */
  if ( (*fname != '\0') && (*(fname+1) != '\0') &&
     (*fname == '>') && (*(fname+1) == '>')) { 
    flags = O_WRONLY | O_CREAT | O_APPEND; 
    fname+=2; 
    seek_end = 1;
#ifdef VERBOSE
fprintf(stderr, "! Filesys::SmbClient :"
	        "Open append %s : %s\n", fname); 
#endif
  /* Mode > */
  } else if ( (*fname != '\0') && (*fname == '>')) {
    flags = O_WRONLY | O_CREAT | O_TRUNC; fname++; 
  /* Mode < */
  } else if ( (*fname != '\0') && (*fname == '<')) {
    flags = O_RDONLY; fname++; 
  /* Mod < */
  } else flags =  O_RDONLY;
RETVAL = context->open(context, fname, flags, mode);	
#ifdef VERBOSE
  fprintf(stderr, "! Filesys::SmbClient :"
	          "Open %s return %d\n", fname, RETVAL); 
#endif
if (RETVAL < 0) { 
  RETVAL = 0;
#ifdef VERBOSE
 fprintf(stderr, "*** Error Filesys::SmbClient :"
                 "Open %s : %s\n", fname, strerror(errno)); 
#endif
} else if (seek_end) { context->lseek(context, RETVAL, 0, SEEK_END); }
OUTPUT:
  RETVAL


SV*
_read(context,fd, count)
  SMBCCTX *context
  SMBCFILE *fd
  int count
PREINIT:
/* 
 * Read count bytes on file descriptor fd
 *
 */
  char *buf;
  int returnValue;
CODE:
  buf = (char*)alloca(sizeof(char)*(count+1));
  returnValue = context->read(context, fd, buf, count);
  buf[returnValue]='\0';
#ifdef VERBOSE
  if (returnValue <= 0){ 
    fprintf(stderr, "*** Error Filesys::SmbClient: "
                    "Read %s : %s\n", buf, strerror(errno)); 
}
#endif
  if (returnValue<0) {RETVAL=&PL_sv_undef;}
  else {RETVAL=newSVpvn(buf,returnValue);}
OUTPUT:
  RETVAL

int
_write(context, fd, buf, count)
  SMBCCTX *context
  SMBCFILE *fd
  char *buf
  int count
CODE:
/* 
 * Write buf on file descriptor fd
 *
 */
  RETVAL=context->write(context, fd, buf, count);
#ifdef VERBOSE
  fprintf(stderr, "! Filesys::SmbClient :"
	          "write %d bytes: %s\n",count, buf);	
  if (RETVAL < 0) { 
    if (RETVAL == EBADF) 
      fprintf(stderr, "*** Error Filesys::SmbClient: "
		      "write fd non valide\n");
    else if (RETVAL == EINVAL) 
      fprintf(stderr, "*** Error Filesys::SmbClient: "
	              "write param non valide\n");
    else 
      fprintf(stderr, "*** Error Filesys::SmbClient: "
	               "write %d : %s\n", fd, strerror(errno)); 
  }
#endif
OUTPUT:
  RETVAL

int 
_lseek(context, fd,offset,whence)
  SMBCCTX *context
  SMBCFILE *fd
  int offset
  int whence
CODE:
  RETVAL=context->lseek(context, fd, offset, whence);
#ifdef VERBOSE
if (RETVAL < 0) { 
  if (RETVAL == EBADF) 
     fprintf(stderr, "*** Error Filesys::SmbClient: "
                     "lseek fd not open\n");
  else if (RETVAL == EINVAL) 
     fprintf(stderr, "*** Error Filesys::SmbClient: "
	   	    "smbc_init not called or fd not a filehandle\n");
  else 
     fprintf(stderr, "*** Error Filesys::SmbClient: "
	             "write %d : %s\n", fd, strerror(errno)); 
}
#endif
OUTPUT:
  RETVAL


int
_close(context, fd)
  SMBCCTX *context
  SMBCFILE *fd
CODE:
/* 
 * Close file desriptor fd
 *
 */
  RETVAL=context->close_fn(context, fd);
OUTPUT:
  RETVAL



int
_unlink(context, fname)
  SMBCCTX *context
  char *fname
CODE:
/* 
 * Remove file fname
 *
 */
  RETVAL = context->unlink(context, fname);
  if (RETVAL < 0) { 
    RETVAL = 0;
#ifdef VERBOSE	
  fprintf(stderr, "*** Error Filesys::SmbClient: Failed to unlink %s : %s\n", 
          fname, strerror(errno)); 
#endif
  } else RETVAL = 1;
OUTPUT:
  RETVAL


int
_unlink_print_job(context, purl, id)
  SMBCCTX *context
  char *purl
  int id
CODE:
/* 
 * Remove job print no id on printer purl
 *
 */
  RETVAL = smbc_unlink_print_job(purl, id);
#ifdef VERBOSE
  if (RETVAL<0)
    fprintf(stderr, "*** Error Filesys::SmbClient: "
	            "Failed to unlink job id %u on %s, %s, %u\n", 
                    id, purl, strerror(errno), errno);
#endif
OUTPUT:
  RETVAL


int
_print_file(context, purl, printer)
  SMBCCTX *context
  char *purl
  char *printer
CODE:
/* 
 * Print url purl on printer purl
 *
 */
  RETVAL = smbc_print_file(purl, printer);
#ifdef VERBOSE
  if (RETVAL<0)
    fprintf(stderr, "*** Error Filesys::SmbClient *** "
		    "Failed to print file %s on %s, %s, %u\n", 
	            purl, printer, strerror(errno), errno);
#endif
OUTPUT:
  RETVAL