r14156: Fix coverity #114: free storage alloc'ed by sstring_sub()
[Samba/nascimento.git] / source3 / torture / cmd_vfs.c
blobcc683977e8cdee215f4868601cfa7fc1c93fe1e5
1 /*
2 Unix SMB/CIFS implementation.
3 VFS module functions
5 Copyright (C) Simo Sorce 2002
6 Copyright (C) Eric Lorimer 2002
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
24 #include "vfstest.h"
26 static const char *null_string = "";
28 static NTSTATUS cmd_load_module(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
30 int i;
32 if (argc < 2) {
33 printf("Usage: load <modules>\n");
34 return NT_STATUS_OK;
37 for (i=argc-1;i>0;i--) {
38 if (!vfs_init_custom(vfs->conn, argv[i])) {
39 DEBUG(0, ("load: (vfs_init_custom failed for %s)\n", argv[i]));
40 return NT_STATUS_UNSUCCESSFUL;
43 printf("load: ok\n");
44 return NT_STATUS_OK;
47 static NTSTATUS cmd_populate(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
49 char c;
50 size_t size;
51 if (argc != 3) {
52 printf("Usage: populate <char> <size>\n");
53 return NT_STATUS_OK;
55 c = argv[1][0];
56 size = atoi(argv[2]);
57 vfs->data = TALLOC_ARRAY(mem_ctx, char, size);
58 if (vfs->data == NULL) {
59 printf("populate: error=-1 (not enough memory)");
60 return NT_STATUS_UNSUCCESSFUL;
62 memset(vfs->data, c, size);
63 vfs->data_size = size;
64 return NT_STATUS_OK;
67 static NTSTATUS cmd_show_data(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
69 size_t offset;
70 size_t len;
71 if (argc != 1 && argc != 3) {
72 printf("Usage: showdata [<offset> <len>]\n");
73 return NT_STATUS_OK;
75 if (vfs->data == NULL || vfs->data_size == 0) {
76 printf("show_data: error=-1 (buffer empty)\n");
77 return NT_STATUS_UNSUCCESSFUL;
80 if (argc == 3) {
81 offset = atoi(argv[1]);
82 len = atoi(argv[2]);
83 } else {
84 offset = 0;
85 len = vfs->data_size;
87 if ((offset + len) > vfs->data_size) {
88 printf("show_data: error=-1 (not enough data in buffer)\n");
89 return NT_STATUS_UNSUCCESSFUL;
91 dump_data(0, (char *)(vfs->data) + offset, len);
92 return NT_STATUS_OK;
95 static NTSTATUS cmd_connect(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
97 SMB_VFS_CONNECT(vfs->conn, lp_servicename(vfs->conn->service), "vfstest");
98 return NT_STATUS_OK;
101 static NTSTATUS cmd_disconnect(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
103 SMB_VFS_DISCONNECT(vfs->conn);
104 return NT_STATUS_OK;
107 static NTSTATUS cmd_disk_free(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
109 SMB_BIG_UINT diskfree, bsize, dfree, dsize;
110 if (argc != 2) {
111 printf("Usage: disk_free <path>\n");
112 return NT_STATUS_OK;
115 diskfree = SMB_VFS_DISK_FREE(vfs->conn, argv[1], False, &bsize, &dfree, &dsize);
116 printf("disk_free: %lu, bsize = %lu, dfree = %lu, dsize = %lu\n",
117 (unsigned long)diskfree,
118 (unsigned long)bsize,
119 (unsigned long)dfree,
120 (unsigned long)dsize);
121 return NT_STATUS_OK;
125 static NTSTATUS cmd_opendir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
127 if (argc != 2) {
128 printf("Usage: opendir <fname>\n");
129 return NT_STATUS_OK;
132 vfs->currentdir = SMB_VFS_OPENDIR(vfs->conn, argv[1], NULL, 0);
133 if (vfs->currentdir == NULL) {
134 printf("opendir error=%d (%s)\n", errno, strerror(errno));
135 return NT_STATUS_UNSUCCESSFUL;
138 printf("opendir: ok\n");
139 return NT_STATUS_OK;
143 static NTSTATUS cmd_readdir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
145 SMB_STRUCT_DIRENT *dent;
147 if (vfs->currentdir == NULL) {
148 printf("readdir: error=-1 (no open directory)\n");
149 return NT_STATUS_UNSUCCESSFUL;
152 dent = SMB_VFS_READDIR(vfs->conn, vfs->currentdir);
153 if (dent == NULL) {
154 printf("readdir: NULL\n");
155 return NT_STATUS_OK;
158 printf("readdir: %s\n", dent->d_name);
159 return NT_STATUS_OK;
163 static NTSTATUS cmd_mkdir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
165 if (argc != 2) {
166 printf("Usage: mkdir <path>\n");
167 return NT_STATUS_OK;
170 if (SMB_VFS_MKDIR(vfs->conn, argv[1], 00755) == -1) {
171 printf("mkdir error=%d (%s)\n", errno, strerror(errno));
172 return NT_STATUS_UNSUCCESSFUL;
175 printf("mkdir: ok\n");
176 return NT_STATUS_OK;
180 static NTSTATUS cmd_closedir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
182 int ret;
184 if (vfs->currentdir == NULL) {
185 printf("closedir: failure (no directory open)\n");
186 return NT_STATUS_UNSUCCESSFUL;
189 ret = SMB_VFS_CLOSEDIR(vfs->conn, vfs->currentdir);
190 if (ret == -1) {
191 printf("closedir failure: %s\n", strerror(errno));
192 return NT_STATUS_UNSUCCESSFUL;
195 printf("closedir: ok\n");
196 vfs->currentdir = NULL;
197 return NT_STATUS_OK;
201 static NTSTATUS cmd_open(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
203 int flags, fd;
204 mode_t mode;
205 const char *flagstr;
207 mode = 00400;
209 if (argc < 3 || argc > 5) {
210 printf("Usage: open <filename> <flags> <mode>\n");
211 printf(" flags: O = O_RDONLY\n");
212 printf(" R = O_RDWR\n");
213 printf(" W = O_WRONLY\n");
214 printf(" C = O_CREAT\n");
215 printf(" E = O_EXCL\n");
216 printf(" T = O_TRUNC\n");
217 printf(" A = O_APPEND\n");
218 printf(" N = O_NONBLOCK/O_NDELAY\n");
219 #ifdef O_SYNC
220 printf(" S = O_SYNC\n");
221 #endif
222 #ifdef O_NOFOLLOW
223 printf(" F = O_NOFOLLOW\n");
224 #endif
225 printf(" mode: see open.2\n");
226 printf(" mode is ignored if C flag not present\n");
227 printf(" mode defaults to 00400\n");
228 return NT_STATUS_OK;
230 flags = 0;
231 flagstr = argv[2];
232 while (*flagstr) {
233 switch (*flagstr) {
234 case 'O':
235 flags |= O_RDONLY;
236 break;
237 case 'R':
238 flags |= O_RDWR;
239 break;
240 case 'W':
241 flags |= O_WRONLY;
242 break;
243 case 'C':
244 flags |= O_CREAT;
245 break;
246 case 'E':
247 flags |= O_EXCL;
248 break;
249 case 'T':
250 flags |= O_TRUNC;
251 break;
252 case 'A':
253 flags |= O_APPEND;
254 break;
255 case 'N':
256 flags |= O_NONBLOCK;
257 break;
258 #ifdef O_SYNC
259 case 'S':
260 flags |= O_SYNC;
261 break;
262 #endif
263 #ifdef O_NOFOLLOW
264 case 'F':
265 flags |= O_NOFOLLOW;
266 break;
267 #endif
268 default:
269 printf("open: error=-1 (invalid flag!)\n");
270 return NT_STATUS_UNSUCCESSFUL;
272 flagstr++;
274 if ((flags & O_CREAT) && argc == 4) {
275 if (sscanf(argv[3], "%o", &mode) == 0) {
276 printf("open: error=-1 (invalid mode!)\n");
277 return NT_STATUS_UNSUCCESSFUL;
281 fd = SMB_VFS_OPEN(vfs->conn, argv[1], flags, mode);
282 if (fd == -1) {
283 printf("open: error=%d (%s)\n", errno, strerror(errno));
284 return NT_STATUS_UNSUCCESSFUL;
287 vfs->files[fd] = SMB_MALLOC_P(struct files_struct);
288 vfs->files[fd]->fsp_name = SMB_STRDUP(argv[1]);
289 vfs->files[fd]->fh = SMB_MALLOC_P(struct fd_handle);
290 vfs->files[fd]->fh->fd = fd;
291 vfs->files[fd]->conn = vfs->conn;
292 printf("open: fd=%d\n", fd);
293 return NT_STATUS_OK;
297 static NTSTATUS cmd_pathfunc(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
299 int ret = -1;
301 if (argc != 2) {
302 printf("Usage: %s <path>\n", argv[0]);
303 return NT_STATUS_OK;
306 if (strcmp("rmdir", argv[0]) == 0 ) {
307 ret = SMB_VFS_RMDIR(vfs->conn, argv[1]);
308 } else if (strcmp("unlink", argv[0]) == 0 ) {
309 ret = SMB_VFS_UNLINK(vfs->conn, argv[1]);
310 } else if (strcmp("chdir", argv[0]) == 0 ) {
311 ret = SMB_VFS_CHDIR(vfs->conn, argv[1]);
312 } else {
313 printf("%s: error=%d (invalid function name!)\n", argv[0], errno);
314 return NT_STATUS_UNSUCCESSFUL;
317 if (ret == -1) {
318 printf("%s: error=%d (%s)\n", argv[0], errno, strerror(errno));
319 return NT_STATUS_UNSUCCESSFUL;
322 printf("%s: ok\n", argv[0]);
323 return NT_STATUS_OK;
327 static NTSTATUS cmd_close(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
329 int fd, ret;
331 if (argc != 2) {
332 printf("Usage: close <fd>\n");
333 return NT_STATUS_OK;
336 fd = atoi(argv[1]);
337 if (vfs->files[fd] == NULL) {
338 printf("close: error=-1 (invalid file descriptor)\n");
339 return NT_STATUS_OK;
342 ret = SMB_VFS_CLOSE(vfs->files[fd], fd);
343 if (ret == -1 )
344 printf("close: error=%d (%s)\n", errno, strerror(errno));
345 else
346 printf("close: ok\n");
348 SAFE_FREE(vfs->files[fd]->fsp_name);
349 SAFE_FREE(vfs->files[fd]->fh);
350 SAFE_FREE(vfs->files[fd]);
351 vfs->files[fd] = NULL;
352 return NT_STATUS_OK;
356 static NTSTATUS cmd_read(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
358 int fd;
359 size_t size, rsize;
361 if (argc != 3) {
362 printf("Usage: read <fd> <size>\n");
363 return NT_STATUS_OK;
366 /* do some error checking on these */
367 fd = atoi(argv[1]);
368 size = atoi(argv[2]);
369 vfs->data = TALLOC_ARRAY(mem_ctx, char, size);
370 if (vfs->data == NULL) {
371 printf("read: error=-1 (not enough memory)");
372 return NT_STATUS_UNSUCCESSFUL;
374 vfs->data_size = size;
376 rsize = SMB_VFS_READ(vfs->files[fd], fd, vfs->data, size);
377 if (rsize == -1) {
378 printf("read: error=%d (%s)\n", errno, strerror(errno));
379 return NT_STATUS_UNSUCCESSFUL;
382 printf("read: ok\n");
383 return NT_STATUS_OK;
387 static NTSTATUS cmd_write(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
389 int fd, size, wsize;
391 if (argc != 3) {
392 printf("Usage: write <fd> <size>\n");
393 return NT_STATUS_OK;
396 /* some error checking should go here */
397 fd = atoi(argv[1]);
398 size = atoi(argv[2]);
399 if (vfs->data == NULL) {
400 printf("write: error=-1 (buffer empty, please populate it before writing)");
401 return NT_STATUS_UNSUCCESSFUL;
404 if (vfs->data_size < size) {
405 printf("write: error=-1 (buffer too small, please put some more data in)");
406 return NT_STATUS_UNSUCCESSFUL;
409 wsize = SMB_VFS_WRITE(vfs->files[fd], fd, vfs->data, size);
411 if (wsize == -1) {
412 printf("write: error=%d (%s)\n", errno, strerror(errno));
413 return NT_STATUS_UNSUCCESSFUL;
416 printf("write: ok\n");
417 return NT_STATUS_OK;
421 static NTSTATUS cmd_lseek(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
423 int fd, offset, whence;
424 SMB_OFF_T pos;
426 if (argc != 4) {
427 printf("Usage: lseek <fd> <offset> <whence>\n...where whence is 1 => SEEK_SET, 2 => SEEK_CUR, 3 => SEEK_END\n");
428 return NT_STATUS_OK;
431 fd = atoi(argv[1]);
432 offset = atoi(argv[2]);
433 whence = atoi(argv[3]);
434 switch (whence) {
435 case 1: whence = SEEK_SET; break;
436 case 2: whence = SEEK_CUR; break;
437 default: whence = SEEK_END;
440 pos = SMB_VFS_LSEEK(vfs->files[fd], fd, offset, whence);
441 if (pos == (SMB_OFF_T)-1) {
442 printf("lseek: error=%d (%s)\n", errno, strerror(errno));
443 return NT_STATUS_UNSUCCESSFUL;
446 printf("lseek: ok\n");
447 return NT_STATUS_OK;
451 static NTSTATUS cmd_rename(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
453 int ret;
454 if (argc != 3) {
455 printf("Usage: rename <old> <new>\n");
456 return NT_STATUS_OK;
459 ret = SMB_VFS_RENAME(vfs->conn, argv[1], argv[2]);
460 if (ret == -1) {
461 printf("rename: error=%d (%s)\n", errno, strerror(errno));
462 return NT_STATUS_UNSUCCESSFUL;
465 printf("rename: ok\n");
466 return NT_STATUS_OK;
470 static NTSTATUS cmd_fsync(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
472 int ret, fd;
473 if (argc != 2) {
474 printf("Usage: fsync <fd>\n");
475 return NT_STATUS_OK;
478 fd = atoi(argv[1]);
479 ret = SMB_VFS_FSYNC(vfs->files[fd], fd);
480 if (ret == -1) {
481 printf("fsync: error=%d (%s)\n", errno, strerror(errno));
482 return NT_STATUS_UNSUCCESSFUL;
485 printf("fsync: ok\n");
486 return NT_STATUS_OK;
490 static NTSTATUS cmd_stat(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
492 int ret;
493 const char *user;
494 const char *group;
495 struct passwd *pwd = NULL;
496 struct group *grp = NULL;
497 SMB_STRUCT_STAT st;
499 if (argc != 2) {
500 printf("Usage: stat <fname>\n");
501 return NT_STATUS_OK;
504 ret = SMB_VFS_STAT(vfs->conn, argv[1], &st);
505 if (ret == -1) {
506 printf("stat: error=%d (%s)\n", errno, strerror(errno));
507 return NT_STATUS_UNSUCCESSFUL;
510 pwd = sys_getpwuid(st.st_uid);
511 if (pwd != NULL) user = pwd->pw_name;
512 else user = null_string;
513 grp = sys_getgrgid(st.st_gid);
514 if (grp != NULL) group = grp->gr_name;
515 else group = null_string;
517 printf("stat: ok\n");
518 printf(" File: %s", argv[1]);
519 if (S_ISREG(st.st_mode)) printf(" Regular File\n");
520 else if (S_ISDIR(st.st_mode)) printf(" Directory\n");
521 else if (S_ISCHR(st.st_mode)) printf(" Character Device\n");
522 else if (S_ISBLK(st.st_mode)) printf(" Block Device\n");
523 else if (S_ISFIFO(st.st_mode)) printf(" Fifo\n");
524 else if (S_ISLNK(st.st_mode)) printf(" Symbolic Link\n");
525 else if (S_ISSOCK(st.st_mode)) printf(" Socket\n");
526 printf(" Size: %10u", (unsigned int)st.st_size);
527 #ifdef HAVE_STAT_ST_BLOCKS
528 printf(" Blocks: %9u", (unsigned int)st.st_blocks);
529 #endif
530 #ifdef HAVE_STAT_ST_BLKSIZE
531 printf(" IO Block: %u\n", (unsigned int)st.st_blksize);
532 #endif
533 printf(" Device: 0x%10x", (unsigned int)st.st_dev);
534 printf(" Inode: %10u", (unsigned int)st.st_ino);
535 printf(" Links: %10u\n", (unsigned int)st.st_nlink);
536 printf(" Access: %05o", (st.st_mode) & 007777);
537 printf(" Uid: %5lu/%.16s Gid: %5lu/%.16s\n", (unsigned long)st.st_uid, user,
538 (unsigned long)st.st_gid, group);
539 printf(" Access: %s", ctime(&(st.st_atime)));
540 printf(" Modify: %s", ctime(&(st.st_mtime)));
541 printf(" Change: %s", ctime(&(st.st_ctime)));
543 return NT_STATUS_OK;
547 static NTSTATUS cmd_fstat(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
549 int fd;
550 const char *user;
551 const char *group;
552 struct passwd *pwd = NULL;
553 struct group *grp = NULL;
554 SMB_STRUCT_STAT st;
556 if (argc != 2) {
557 printf("Usage: fstat <fd>\n");
558 return NT_STATUS_OK;
561 fd = atoi(argv[1]);
562 if (fd < 0 || fd > 1024) {
563 printf("fstat: error=%d (file descriptor out of range)\n", EBADF);
564 return NT_STATUS_OK;
567 if (vfs->files[fd] == NULL) {
568 printf("fstat: error=%d (invalid file descriptor)\n", EBADF);
569 return NT_STATUS_OK;
572 if (SMB_VFS_FSTAT(vfs->files[fd], fd, &st) == -1) {
573 printf("fstat: error=%d (%s)\n", errno, strerror(errno));
574 return NT_STATUS_UNSUCCESSFUL;
577 pwd = sys_getpwuid(st.st_uid);
578 if (pwd != NULL) user = pwd->pw_name;
579 else user = null_string;
580 grp = sys_getgrgid(st.st_gid);
581 if (grp != NULL) group = grp->gr_name;
582 else group = null_string;
584 printf("fstat: ok\n");
585 if (S_ISREG(st.st_mode)) printf(" Regular File\n");
586 else if (S_ISDIR(st.st_mode)) printf(" Directory\n");
587 else if (S_ISCHR(st.st_mode)) printf(" Character Device\n");
588 else if (S_ISBLK(st.st_mode)) printf(" Block Device\n");
589 else if (S_ISFIFO(st.st_mode)) printf(" Fifo\n");
590 else if (S_ISLNK(st.st_mode)) printf(" Symbolic Link\n");
591 else if (S_ISSOCK(st.st_mode)) printf(" Socket\n");
592 printf(" Size: %10u", (unsigned int)st.st_size);
593 #ifdef HAVE_STAT_ST_BLOCKS
594 printf(" Blocks: %9u", (unsigned int)st.st_blocks);
595 #endif
596 #ifdef HAVE_STAT_ST_BLKSIZE
597 printf(" IO Block: %u\n", (unsigned int)st.st_blksize);
598 #endif
599 printf(" Device: 0x%10x", (unsigned int)st.st_dev);
600 printf(" Inode: %10u", (unsigned int)st.st_ino);
601 printf(" Links: %10u\n", (unsigned int)st.st_nlink);
602 printf(" Access: %05o", (st.st_mode) & 007777);
603 printf(" Uid: %5lu/%.16s Gid: %5lu/%.16s\n", (unsigned long)st.st_uid, user,
604 (unsigned long)st.st_gid, group);
605 printf(" Access: %s", ctime(&(st.st_atime)));
606 printf(" Modify: %s", ctime(&(st.st_mtime)));
607 printf(" Change: %s", ctime(&(st.st_ctime)));
609 return NT_STATUS_OK;
613 static NTSTATUS cmd_lstat(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
615 const char *user;
616 const char *group;
617 struct passwd *pwd = NULL;
618 struct group *grp = NULL;
619 SMB_STRUCT_STAT st;
621 if (argc != 2) {
622 printf("Usage: lstat <path>\n");
623 return NT_STATUS_OK;
626 if (SMB_VFS_LSTAT(vfs->conn, argv[1], &st) == -1) {
627 printf("lstat: error=%d (%s)\n", errno, strerror(errno));
628 return NT_STATUS_UNSUCCESSFUL;
631 pwd = sys_getpwuid(st.st_uid);
632 if (pwd != NULL) user = pwd->pw_name;
633 else user = null_string;
634 grp = sys_getgrgid(st.st_gid);
635 if (grp != NULL) group = grp->gr_name;
636 else group = null_string;
638 printf("lstat: ok\n");
639 if (S_ISREG(st.st_mode)) printf(" Regular File\n");
640 else if (S_ISDIR(st.st_mode)) printf(" Directory\n");
641 else if (S_ISCHR(st.st_mode)) printf(" Character Device\n");
642 else if (S_ISBLK(st.st_mode)) printf(" Block Device\n");
643 else if (S_ISFIFO(st.st_mode)) printf(" Fifo\n");
644 else if (S_ISLNK(st.st_mode)) printf(" Symbolic Link\n");
645 else if (S_ISSOCK(st.st_mode)) printf(" Socket\n");
646 printf(" Size: %10u", (unsigned int)st.st_size);
647 #ifdef HAVE_STAT_ST_BLOCKS
648 printf(" Blocks: %9u", (unsigned int)st.st_blocks);
649 #endif
650 #ifdef HAVE_STAT_ST_BLKSIZE
651 printf(" IO Block: %u\n", (unsigned int)st.st_blksize);
652 #endif
653 printf(" Device: 0x%10x", (unsigned int)st.st_dev);
654 printf(" Inode: %10u", (unsigned int)st.st_ino);
655 printf(" Links: %10u\n", (unsigned int)st.st_nlink);
656 printf(" Access: %05o", (st.st_mode) & 007777);
657 printf(" Uid: %5lu/%.16s Gid: %5lu/%.16s\n", (unsigned long)st.st_uid, user,
658 (unsigned long)st.st_gid, group);
659 printf(" Access: %s", ctime(&(st.st_atime)));
660 printf(" Modify: %s", ctime(&(st.st_mtime)));
661 printf(" Change: %s", ctime(&(st.st_ctime)));
663 return NT_STATUS_OK;
667 static NTSTATUS cmd_chmod(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
669 mode_t mode;
670 if (argc != 3) {
671 printf("Usage: chmod <path> <mode>\n");
672 return NT_STATUS_OK;
675 mode = atoi(argv[2]);
676 if (SMB_VFS_CHMOD(vfs->conn, argv[1], mode) == -1) {
677 printf("chmod: error=%d (%s)\n", errno, strerror(errno));
678 return NT_STATUS_UNSUCCESSFUL;
681 printf("chmod: ok\n");
682 return NT_STATUS_OK;
686 static NTSTATUS cmd_fchmod(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
688 int fd;
689 mode_t mode;
690 if (argc != 3) {
691 printf("Usage: fchmod <fd> <mode>\n");
692 return NT_STATUS_OK;
695 fd = atoi(argv[1]);
696 mode = atoi(argv[2]);
697 if (fd < 0 || fd > 1024) {
698 printf("fchmod: error=%d (file descriptor out of range)\n", EBADF);
699 return NT_STATUS_OK;
701 if (vfs->files[fd] == NULL) {
702 printf("fchmod: error=%d (invalid file descriptor)\n", EBADF);
703 return NT_STATUS_OK;
706 if (SMB_VFS_FCHMOD(vfs->files[fd], fd, mode) == -1) {
707 printf("fchmod: error=%d (%s)\n", errno, strerror(errno));
708 return NT_STATUS_UNSUCCESSFUL;
711 printf("fchmod: ok\n");
712 return NT_STATUS_OK;
716 static NTSTATUS cmd_chown(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
718 uid_t uid;
719 gid_t gid;
720 if (argc != 4) {
721 printf("Usage: chown <path> <uid> <gid>\n");
722 return NT_STATUS_OK;
725 uid = atoi(argv[2]);
726 gid = atoi(argv[3]);
727 if (SMB_VFS_CHOWN(vfs->conn, argv[1], uid, gid) == -1) {
728 printf("chown: error=%d (%s)\n", errno, strerror(errno));
729 return NT_STATUS_UNSUCCESSFUL;
732 printf("chown: ok\n");
733 return NT_STATUS_OK;
737 static NTSTATUS cmd_fchown(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
739 uid_t uid;
740 gid_t gid;
741 int fd;
742 if (argc != 4) {
743 printf("Usage: fchown <fd> <uid> <gid>\n");
744 return NT_STATUS_OK;
747 uid = atoi(argv[2]);
748 gid = atoi(argv[3]);
749 fd = atoi(argv[1]);
750 if (fd < 0 || fd > 1024) {
751 printf("fchown: faliure=%d (file descriptor out of range)\n", EBADF);
752 return NT_STATUS_OK;
754 if (vfs->files[fd] == NULL) {
755 printf("fchown: error=%d (invalid file descriptor)\n", EBADF);
756 return NT_STATUS_OK;
758 if (SMB_VFS_FCHOWN(vfs->files[fd], fd, uid, gid) == -1) {
759 printf("fchown error=%d (%s)\n", errno, strerror(errno));
760 return NT_STATUS_UNSUCCESSFUL;
763 printf("fchown: ok\n");
764 return NT_STATUS_OK;
768 static NTSTATUS cmd_getwd(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
770 char buf[PATH_MAX];
771 if (SMB_VFS_GETWD(vfs->conn, buf) == NULL) {
772 printf("getwd: error=%d (%s)\n", errno, strerror(errno));
773 return NT_STATUS_UNSUCCESSFUL;
776 printf("getwd: %s\n", buf);
777 return NT_STATUS_OK;
780 static NTSTATUS cmd_utime(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
782 struct utimbuf times;
783 if (argc != 4) {
784 printf("Usage: utime <path> <access> <modify>\n");
785 return NT_STATUS_OK;
787 times.actime = atoi(argv[2]);
788 times.modtime = atoi(argv[3]);
789 if (SMB_VFS_UTIME(vfs->conn, argv[1], &times) != 0) {
790 printf("utime: error=%d (%s)\n", errno, strerror(errno));
791 return NT_STATUS_UNSUCCESSFUL;
794 printf("utime: ok\n");
795 return NT_STATUS_OK;
798 static NTSTATUS cmd_ftruncate(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
800 int fd;
801 SMB_OFF_T off;
802 if (argc != 3) {
803 printf("Usage: ftruncate <fd> <length>\n");
804 return NT_STATUS_OK;
807 fd = atoi(argv[1]);
808 off = atoi(argv[2]);
809 if (fd < 0 || fd > 1024) {
810 printf("ftruncate: error=%d (file descriptor out of range)\n", EBADF);
811 return NT_STATUS_OK;
813 if (vfs->files[fd] == NULL) {
814 printf("ftruncate: error=%d (invalid file descriptor)\n", EBADF);
815 return NT_STATUS_OK;
818 if (SMB_VFS_FTRUNCATE(vfs->files[fd], fd, off) == -1) {
819 printf("ftruncate: error=%d (%s)\n", errno, strerror(errno));
820 return NT_STATUS_UNSUCCESSFUL;
823 printf("ftruncate: ok\n");
824 return NT_STATUS_OK;
827 static NTSTATUS cmd_lock(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
829 BOOL ret;
830 int fd;
831 int op;
832 long offset;
833 long count;
834 int type;
835 const char *typestr;
837 if (argc != 6) {
838 printf("Usage: lock <fd> <op> <offset> <count> <type>\n");
839 printf(" ops: G = F_GETLK\n");
840 printf(" S = F_SETLK\n");
841 printf(" W = F_SETLKW\n");
842 printf(" type: R = F_RDLCK\n");
843 printf(" W = F_WRLCK\n");
844 printf(" U = F_UNLCK\n");
845 return NT_STATUS_OK;
848 if (sscanf(argv[1], "%d", &fd) == 0) {
849 printf("lock: error=-1 (error parsing fd)\n");
850 return NT_STATUS_UNSUCCESSFUL;
853 op = 0;
854 switch (*argv[2]) {
855 case 'G':
856 op = F_GETLK;
857 break;
858 case 'S':
859 op = F_SETLK;
860 break;
861 case 'W':
862 op = F_SETLKW;
863 break;
864 default:
865 printf("lock: error=-1 (invalid op flag!)\n");
866 return NT_STATUS_UNSUCCESSFUL;
869 if (sscanf(argv[3], "%ld", &offset) == 0) {
870 printf("lock: error=-1 (error parsing fd)\n");
871 return NT_STATUS_UNSUCCESSFUL;
874 if (sscanf(argv[4], "%ld", &count) == 0) {
875 printf("lock: error=-1 (error parsing fd)\n");
876 return NT_STATUS_UNSUCCESSFUL;
879 type = 0;
880 typestr = argv[5];
881 while(*typestr) {
882 switch (*typestr) {
883 case 'R':
884 type |= F_RDLCK;
885 break;
886 case 'W':
887 type |= F_WRLCK;
888 break;
889 case 'U':
890 type |= F_UNLCK;
891 break;
892 default:
893 printf("lock: error=-1 (invalid type flag!)\n");
894 return NT_STATUS_UNSUCCESSFUL;
896 typestr++;
899 printf("lock: debug lock(fd=%d, op=%d, offset=%ld, count=%ld, type=%d))\n", fd, op, offset, count, type);
901 if ((ret = SMB_VFS_LOCK(vfs->files[fd], fd, op, offset, count, type)) == False) {
902 printf("lock: error=%d (%s)\n", errno, strerror(errno));
903 return NT_STATUS_UNSUCCESSFUL;
906 printf("lock: ok\n");
907 return NT_STATUS_OK;
910 static NTSTATUS cmd_symlink(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
912 if (argc != 3) {
913 printf("Usage: symlink <path> <link>\n");
914 return NT_STATUS_OK;
917 if (SMB_VFS_SYMLINK(vfs->conn, argv[1], argv[2]) == -1) {
918 printf("symlink: error=%d (%s)\n", errno, strerror(errno));
919 return NT_STATUS_UNSUCCESSFUL;
922 printf("symlink: ok\n");
923 return NT_STATUS_OK;
927 static NTSTATUS cmd_readlink(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
929 char buffer[PATH_MAX];
930 int size;
932 if (argc != 2) {
933 printf("Usage: readlink <path>\n");
934 return NT_STATUS_OK;
937 if ((size = SMB_VFS_READLINK(vfs->conn, argv[1], buffer, PATH_MAX)) == -1) {
938 printf("readlink: error=%d (%s)\n", errno, strerror(errno));
939 return NT_STATUS_UNSUCCESSFUL;
942 buffer[size] = '\0';
943 printf("readlink: %s\n", buffer);
944 return NT_STATUS_OK;
948 static NTSTATUS cmd_link(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
950 if (argc != 3) {
951 printf("Usage: link <path> <link>\n");
952 return NT_STATUS_OK;
955 if (SMB_VFS_LINK(vfs->conn, argv[1], argv[2]) == -1) {
956 printf("link: error=%d (%s)\n", errno, strerror(errno));
957 return NT_STATUS_UNSUCCESSFUL;
960 printf("link: ok\n");
961 return NT_STATUS_OK;
964 static NTSTATUS cmd_mknod(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
966 mode_t mode;
967 unsigned int dev_val;
968 SMB_DEV_T dev;
970 if (argc != 4) {
971 printf("Usage: mknod <path> <mode> <dev>\n");
972 printf(" mode is octal\n");
973 printf(" dev is hex\n");
974 return NT_STATUS_OK;
977 if (sscanf(argv[2], "%o", &mode) == 0) {
978 printf("open: error=-1 (invalid mode!)\n");
979 return NT_STATUS_UNSUCCESSFUL;
982 if (sscanf(argv[3], "%x", &dev_val) == 0) {
983 printf("open: error=-1 (invalid dev!)\n");
984 return NT_STATUS_UNSUCCESSFUL;
986 dev = (SMB_DEV_T)dev_val;
988 if (SMB_VFS_MKNOD(vfs->conn, argv[1], mode, dev) == -1) {
989 printf("mknod: error=%d (%s)\n", errno, strerror(errno));
990 return NT_STATUS_UNSUCCESSFUL;
993 printf("mknod: ok\n");
994 return NT_STATUS_OK;
997 static NTSTATUS cmd_realpath(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
999 char respath[PATH_MAX];
1001 if (argc != 2) {
1002 printf("Usage: realpath <path>\n");
1003 return NT_STATUS_OK;
1006 if (SMB_VFS_REALPATH(vfs->conn, argv[1], respath) == NULL) {
1007 printf("realpath: error=%d (%s)\n", errno, strerror(errno));
1008 return NT_STATUS_UNSUCCESSFUL;
1011 printf("realpath: ok\n");
1012 return NT_STATUS_OK;
1015 struct cmd_set vfs_commands[] = {
1017 { "VFS Commands" },
1019 { "load", cmd_load_module, "Load a module", "load <module.so>" },
1020 { "populate", cmd_populate, "Populate a data buffer", "populate <char> <size>" },
1021 { "showdata", cmd_show_data, "Show data currently in data buffer", "show_data [<offset> <len>]"},
1022 { "connect", cmd_connect, "VFS connect()", "connect" },
1023 { "disconnect", cmd_disconnect, "VFS disconnect()", "disconnect" },
1024 { "disk_free", cmd_disk_free, "VFS disk_free()", "disk_free <path>" },
1025 { "opendir", cmd_opendir, "VFS opendir()", "opendir <fname>" },
1026 { "readdir", cmd_readdir, "VFS readdir()", "readdir" },
1027 { "mkdir", cmd_mkdir, "VFS mkdir()", "mkdir <path>" },
1028 { "rmdir", cmd_pathfunc, "VFS rmdir()", "rmdir <path>" },
1029 { "closedir", cmd_closedir, "VFS closedir()", "closedir" },
1030 { "open", cmd_open, "VFS open()", "open <fname>" },
1031 { "close", cmd_close, "VFS close()", "close <fd>" },
1032 { "read", cmd_read, "VFS read()", "read <fd> <size>" },
1033 { "write", cmd_write, "VFS write()", "write <fd> <size>" },
1034 { "lseek", cmd_lseek, "VFS lseek()", "lseek <fd> <offset> <whence>" },
1035 { "rename", cmd_rename, "VFS rename()", "rename <old> <new>" },
1036 { "fsync", cmd_fsync, "VFS fsync()", "fsync <fd>" },
1037 { "stat", cmd_stat, "VFS stat()", "stat <fname>" },
1038 { "fstat", cmd_fstat, "VFS fstat()", "fstat <fd>" },
1039 { "lstat", cmd_lstat, "VFS lstat()", "lstat <fname>" },
1040 { "unlink", cmd_pathfunc, "VFS unlink()", "unlink <fname>" },
1041 { "chmod", cmd_chmod, "VFS chmod()", "chmod <path> <mode>" },
1042 { "fchmod", cmd_fchmod, "VFS fchmod()", "fchmod <fd> <mode>" },
1043 { "chown", cmd_chown, "VFS chown()", "chown <path> <uid> <gid>" },
1044 { "fchown", cmd_fchown, "VFS fchown()", "fchown <fd> <uid> <gid>" },
1045 { "chdir", cmd_pathfunc, "VFS chdir()", "chdir <path>" },
1046 { "getwd", cmd_getwd, "VFS getwd()", "getwd" },
1047 { "utime", cmd_utime, "VFS utime()", "utime <path> <access> <modify>" },
1048 { "ftruncate", cmd_ftruncate, "VFS ftruncate()", "ftruncate <fd> <length>" },
1049 { "lock", cmd_lock, "VFS lock()", "lock <f> <op> <offset> <count> <type>" },
1050 { "symlink", cmd_symlink, "VFS symlink()", "symlink <old> <new>" },
1051 { "readlink", cmd_readlink, "VFS readlink()", "readlink <path>" },
1052 { "link", cmd_link, "VFS link()", "link <oldpath> <newpath>" },
1053 { "mknod", cmd_mknod, "VFS mknod()", "mknod <path> <mode> <dev>" },
1054 { "realpath", cmd_realpath, "VFS realpath()", "realpath <path>" },
1055 { NULL }