Fix bug #6017, - magic script does not work. based on a patch from monyo@samba.gr.jp.
[Samba/gbeck.git] / source3 / torture / cmd_vfs.c
blobd984dd661c50a71c54a796e04d1f3acaba0086e2
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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "vfstest.h"
25 static const char *null_string = "";
27 static NTSTATUS cmd_load_module(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
29 int i;
31 if (argc < 2) {
32 printf("Usage: load <modules>\n");
33 return NT_STATUS_OK;
36 for (i=argc-1;i>0;i--) {
37 if (!vfs_init_custom(vfs->conn, argv[i])) {
38 DEBUG(0, ("load: (vfs_init_custom failed for %s)\n", argv[i]));
39 return NT_STATUS_UNSUCCESSFUL;
42 printf("load: ok\n");
43 return NT_STATUS_OK;
46 static NTSTATUS cmd_populate(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
48 char c;
49 size_t size;
50 if (argc != 3) {
51 printf("Usage: populate <char> <size>\n");
52 return NT_STATUS_OK;
54 c = argv[1][0];
55 size = atoi(argv[2]);
56 vfs->data = TALLOC_ARRAY(mem_ctx, char, size);
57 if (vfs->data == NULL) {
58 printf("populate: error=-1 (not enough memory)");
59 return NT_STATUS_UNSUCCESSFUL;
61 memset(vfs->data, c, size);
62 vfs->data_size = size;
63 return NT_STATUS_OK;
66 static NTSTATUS cmd_show_data(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
68 size_t offset;
69 size_t len;
70 if (argc != 1 && argc != 3) {
71 printf("Usage: showdata [<offset> <len>]\n");
72 return NT_STATUS_OK;
74 if (vfs->data == NULL || vfs->data_size == 0) {
75 printf("show_data: error=-1 (buffer empty)\n");
76 return NT_STATUS_UNSUCCESSFUL;
79 if (argc == 3) {
80 offset = atoi(argv[1]);
81 len = atoi(argv[2]);
82 } else {
83 offset = 0;
84 len = vfs->data_size;
86 if ((offset + len) > vfs->data_size) {
87 printf("show_data: error=-1 (not enough data in buffer)\n");
88 return NT_STATUS_UNSUCCESSFUL;
90 dump_data(0, (uint8 *)(vfs->data) + offset, len);
91 return NT_STATUS_OK;
94 static NTSTATUS cmd_connect(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
96 SMB_VFS_CONNECT(vfs->conn, lp_servicename(SNUM(vfs->conn)), "vfstest");
97 return NT_STATUS_OK;
100 static NTSTATUS cmd_disconnect(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
102 SMB_VFS_DISCONNECT(vfs->conn);
103 return NT_STATUS_OK;
106 static NTSTATUS cmd_disk_free(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
108 uint64_t diskfree, bsize, dfree, dsize;
109 if (argc != 2) {
110 printf("Usage: disk_free <path>\n");
111 return NT_STATUS_OK;
114 diskfree = SMB_VFS_DISK_FREE(vfs->conn, argv[1], False, &bsize, &dfree, &dsize);
115 printf("disk_free: %lu, bsize = %lu, dfree = %lu, dsize = %lu\n",
116 (unsigned long)diskfree,
117 (unsigned long)bsize,
118 (unsigned long)dfree,
119 (unsigned long)dsize);
120 return NT_STATUS_OK;
124 static NTSTATUS cmd_opendir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
126 if (argc != 2) {
127 printf("Usage: opendir <fname>\n");
128 return NT_STATUS_OK;
131 vfs->currentdir = SMB_VFS_OPENDIR(vfs->conn, argv[1], NULL, 0);
132 if (vfs->currentdir == NULL) {
133 printf("opendir error=%d (%s)\n", errno, strerror(errno));
134 return NT_STATUS_UNSUCCESSFUL;
137 printf("opendir: ok\n");
138 return NT_STATUS_OK;
142 static NTSTATUS cmd_readdir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
144 SMB_STRUCT_DIRENT *dent;
146 if (vfs->currentdir == NULL) {
147 printf("readdir: error=-1 (no open directory)\n");
148 return NT_STATUS_UNSUCCESSFUL;
151 dent = SMB_VFS_READDIR(vfs->conn, vfs->currentdir);
152 if (dent == NULL) {
153 printf("readdir: NULL\n");
154 return NT_STATUS_OK;
157 printf("readdir: %s\n", dent->d_name);
158 return NT_STATUS_OK;
162 static NTSTATUS cmd_mkdir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
164 if (argc != 2) {
165 printf("Usage: mkdir <path>\n");
166 return NT_STATUS_OK;
169 if (SMB_VFS_MKDIR(vfs->conn, argv[1], 00755) == -1) {
170 printf("mkdir error=%d (%s)\n", errno, strerror(errno));
171 return NT_STATUS_UNSUCCESSFUL;
174 printf("mkdir: ok\n");
175 return NT_STATUS_OK;
179 static NTSTATUS cmd_closedir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
181 int ret;
183 if (vfs->currentdir == NULL) {
184 printf("closedir: failure (no directory open)\n");
185 return NT_STATUS_UNSUCCESSFUL;
188 ret = SMB_VFS_CLOSEDIR(vfs->conn, vfs->currentdir);
189 if (ret == -1) {
190 printf("closedir failure: %s\n", strerror(errno));
191 return NT_STATUS_UNSUCCESSFUL;
194 printf("closedir: ok\n");
195 vfs->currentdir = NULL;
196 return NT_STATUS_OK;
200 static NTSTATUS cmd_open(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
202 int flags;
203 mode_t mode;
204 const char *flagstr;
205 files_struct *fsp;
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], "%ho", (unsigned short *)&mode) == 0) {
276 printf("open: error=-1 (invalid mode!)\n");
277 return NT_STATUS_UNSUCCESSFUL;
281 fsp = SMB_MALLOC_P(struct files_struct);
282 if (fsp == NULL) {
283 return NT_STATUS_NO_MEMORY;
285 fsp->fsp_name = SMB_STRDUP(argv[1]);
286 if (fsp->fsp_name == NULL) {
287 SAFE_FREE(fsp);
288 return NT_STATUS_NO_MEMORY;
290 fsp->fh = SMB_MALLOC_P(struct fd_handle);
291 if (fsp->fh == NULL) {
292 SAFE_FREE(fsp->fsp_name);
293 SAFE_FREE(fsp);
294 return NT_STATUS_NO_MEMORY;
296 fsp->conn = vfs->conn;
298 fsp->fh->fd = SMB_VFS_OPEN(vfs->conn, argv[1], fsp, flags, mode);
299 if (fsp->fh->fd == -1) {
300 printf("open: error=%d (%s)\n", errno, strerror(errno));
301 SAFE_FREE(fsp->fh);
302 SAFE_FREE(fsp->fsp_name);
303 SAFE_FREE(fsp);
304 return NT_STATUS_UNSUCCESSFUL;
307 vfs->files[fsp->fh->fd] = fsp;
308 printf("open: fd=%d\n", fsp->fh->fd);
309 return NT_STATUS_OK;
313 static NTSTATUS cmd_pathfunc(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
315 int ret = -1;
317 if (argc != 2) {
318 printf("Usage: %s <path>\n", argv[0]);
319 return NT_STATUS_OK;
322 if (strcmp("rmdir", argv[0]) == 0 ) {
323 ret = SMB_VFS_RMDIR(vfs->conn, argv[1]);
324 } else if (strcmp("unlink", argv[0]) == 0 ) {
325 ret = SMB_VFS_UNLINK(vfs->conn, argv[1]);
326 } else if (strcmp("chdir", argv[0]) == 0 ) {
327 ret = SMB_VFS_CHDIR(vfs->conn, argv[1]);
328 } else {
329 printf("%s: error=%d (invalid function name!)\n", argv[0], errno);
330 return NT_STATUS_UNSUCCESSFUL;
333 if (ret == -1) {
334 printf("%s: error=%d (%s)\n", argv[0], errno, strerror(errno));
335 return NT_STATUS_UNSUCCESSFUL;
338 printf("%s: ok\n", argv[0]);
339 return NT_STATUS_OK;
343 static NTSTATUS cmd_close(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
345 int fd, ret;
347 if (argc != 2) {
348 printf("Usage: close <fd>\n");
349 return NT_STATUS_OK;
352 fd = atoi(argv[1]);
353 if (vfs->files[fd] == NULL) {
354 printf("close: error=-1 (invalid file descriptor)\n");
355 return NT_STATUS_OK;
358 ret = SMB_VFS_CLOSE(vfs->files[fd]);
359 if (ret == -1 )
360 printf("close: error=%d (%s)\n", errno, strerror(errno));
361 else
362 printf("close: ok\n");
364 SAFE_FREE(vfs->files[fd]->fsp_name);
365 SAFE_FREE(vfs->files[fd]->fh);
366 SAFE_FREE(vfs->files[fd]);
367 vfs->files[fd] = NULL;
368 return NT_STATUS_OK;
372 static NTSTATUS cmd_read(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
374 int fd;
375 size_t size, rsize;
377 if (argc != 3) {
378 printf("Usage: read <fd> <size>\n");
379 return NT_STATUS_OK;
382 /* do some error checking on these */
383 fd = atoi(argv[1]);
384 size = atoi(argv[2]);
385 vfs->data = TALLOC_ARRAY(mem_ctx, char, size);
386 if (vfs->data == NULL) {
387 printf("read: error=-1 (not enough memory)");
388 return NT_STATUS_UNSUCCESSFUL;
390 vfs->data_size = size;
392 rsize = SMB_VFS_READ(vfs->files[fd], vfs->data, size);
393 if (rsize == -1) {
394 printf("read: error=%d (%s)\n", errno, strerror(errno));
395 return NT_STATUS_UNSUCCESSFUL;
398 printf("read: ok\n");
399 return NT_STATUS_OK;
403 static NTSTATUS cmd_write(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
405 int fd, size, wsize;
407 if (argc != 3) {
408 printf("Usage: write <fd> <size>\n");
409 return NT_STATUS_OK;
412 /* some error checking should go here */
413 fd = atoi(argv[1]);
414 size = atoi(argv[2]);
415 if (vfs->data == NULL) {
416 printf("write: error=-1 (buffer empty, please populate it before writing)");
417 return NT_STATUS_UNSUCCESSFUL;
420 if (vfs->data_size < size) {
421 printf("write: error=-1 (buffer too small, please put some more data in)");
422 return NT_STATUS_UNSUCCESSFUL;
425 wsize = SMB_VFS_WRITE(vfs->files[fd], vfs->data, size);
427 if (wsize == -1) {
428 printf("write: error=%d (%s)\n", errno, strerror(errno));
429 return NT_STATUS_UNSUCCESSFUL;
432 printf("write: ok\n");
433 return NT_STATUS_OK;
437 static NTSTATUS cmd_lseek(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
439 int fd, offset, whence;
440 SMB_OFF_T pos;
442 if (argc != 4) {
443 printf("Usage: lseek <fd> <offset> <whence>\n...where whence is 1 => SEEK_SET, 2 => SEEK_CUR, 3 => SEEK_END\n");
444 return NT_STATUS_OK;
447 fd = atoi(argv[1]);
448 offset = atoi(argv[2]);
449 whence = atoi(argv[3]);
450 switch (whence) {
451 case 1: whence = SEEK_SET; break;
452 case 2: whence = SEEK_CUR; break;
453 default: whence = SEEK_END;
456 pos = SMB_VFS_LSEEK(vfs->files[fd], offset, whence);
457 if (pos == (SMB_OFF_T)-1) {
458 printf("lseek: error=%d (%s)\n", errno, strerror(errno));
459 return NT_STATUS_UNSUCCESSFUL;
462 printf("lseek: ok\n");
463 return NT_STATUS_OK;
467 static NTSTATUS cmd_rename(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
469 int ret;
470 if (argc != 3) {
471 printf("Usage: rename <old> <new>\n");
472 return NT_STATUS_OK;
475 ret = SMB_VFS_RENAME(vfs->conn, argv[1], argv[2]);
476 if (ret == -1) {
477 printf("rename: error=%d (%s)\n", errno, strerror(errno));
478 return NT_STATUS_UNSUCCESSFUL;
481 printf("rename: ok\n");
482 return NT_STATUS_OK;
486 static NTSTATUS cmd_fsync(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
488 int ret, fd;
489 if (argc != 2) {
490 printf("Usage: fsync <fd>\n");
491 return NT_STATUS_OK;
494 fd = atoi(argv[1]);
495 ret = SMB_VFS_FSYNC(vfs->files[fd]);
496 if (ret == -1) {
497 printf("fsync: error=%d (%s)\n", errno, strerror(errno));
498 return NT_STATUS_UNSUCCESSFUL;
501 printf("fsync: ok\n");
502 return NT_STATUS_OK;
506 static NTSTATUS cmd_stat(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
508 int ret;
509 const char *user;
510 const char *group;
511 struct passwd *pwd = NULL;
512 struct group *grp = NULL;
513 SMB_STRUCT_STAT st;
515 if (argc != 2) {
516 printf("Usage: stat <fname>\n");
517 return NT_STATUS_OK;
520 ret = SMB_VFS_STAT(vfs->conn, argv[1], &st);
521 if (ret == -1) {
522 printf("stat: error=%d (%s)\n", errno, strerror(errno));
523 return NT_STATUS_UNSUCCESSFUL;
526 pwd = sys_getpwuid(st.st_uid);
527 if (pwd != NULL) user = pwd->pw_name;
528 else user = null_string;
529 grp = sys_getgrgid(st.st_gid);
530 if (grp != NULL) group = grp->gr_name;
531 else group = null_string;
533 printf("stat: ok\n");
534 printf(" File: %s", argv[1]);
535 if (S_ISREG(st.st_mode)) printf(" Regular File\n");
536 else if (S_ISDIR(st.st_mode)) printf(" Directory\n");
537 else if (S_ISCHR(st.st_mode)) printf(" Character Device\n");
538 else if (S_ISBLK(st.st_mode)) printf(" Block Device\n");
539 else if (S_ISFIFO(st.st_mode)) printf(" Fifo\n");
540 else if (S_ISLNK(st.st_mode)) printf(" Symbolic Link\n");
541 else if (S_ISSOCK(st.st_mode)) printf(" Socket\n");
542 printf(" Size: %10u", (unsigned int)st.st_size);
543 #ifdef HAVE_STAT_ST_BLOCKS
544 printf(" Blocks: %9u", (unsigned int)st.st_blocks);
545 #endif
546 #ifdef HAVE_STAT_ST_BLKSIZE
547 printf(" IO Block: %u\n", (unsigned int)st.st_blksize);
548 #endif
549 printf(" Device: 0x%10x", (unsigned int)st.st_dev);
550 printf(" Inode: %10u", (unsigned int)st.st_ino);
551 printf(" Links: %10u\n", (unsigned int)st.st_nlink);
552 printf(" Access: %05o", (st.st_mode) & 007777);
553 printf(" Uid: %5lu/%.16s Gid: %5lu/%.16s\n", (unsigned long)st.st_uid, user,
554 (unsigned long)st.st_gid, group);
555 printf(" Access: %s", ctime(&(st.st_atime)));
556 printf(" Modify: %s", ctime(&(st.st_mtime)));
557 printf(" Change: %s", ctime(&(st.st_ctime)));
559 return NT_STATUS_OK;
563 static NTSTATUS cmd_fstat(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
565 int fd;
566 const char *user;
567 const char *group;
568 struct passwd *pwd = NULL;
569 struct group *grp = NULL;
570 SMB_STRUCT_STAT st;
572 if (argc != 2) {
573 printf("Usage: fstat <fd>\n");
574 return NT_STATUS_OK;
577 fd = atoi(argv[1]);
578 if (fd < 0 || fd >= 1024) {
579 printf("fstat: error=%d (file descriptor out of range)\n", EBADF);
580 return NT_STATUS_OK;
583 if (vfs->files[fd] == NULL) {
584 printf("fstat: error=%d (invalid file descriptor)\n", EBADF);
585 return NT_STATUS_OK;
588 if (SMB_VFS_FSTAT(vfs->files[fd], &st) == -1) {
589 printf("fstat: error=%d (%s)\n", errno, strerror(errno));
590 return NT_STATUS_UNSUCCESSFUL;
593 pwd = sys_getpwuid(st.st_uid);
594 if (pwd != NULL) user = pwd->pw_name;
595 else user = null_string;
596 grp = sys_getgrgid(st.st_gid);
597 if (grp != NULL) group = grp->gr_name;
598 else group = null_string;
600 printf("fstat: ok\n");
601 if (S_ISREG(st.st_mode)) printf(" Regular File\n");
602 else if (S_ISDIR(st.st_mode)) printf(" Directory\n");
603 else if (S_ISCHR(st.st_mode)) printf(" Character Device\n");
604 else if (S_ISBLK(st.st_mode)) printf(" Block Device\n");
605 else if (S_ISFIFO(st.st_mode)) printf(" Fifo\n");
606 else if (S_ISLNK(st.st_mode)) printf(" Symbolic Link\n");
607 else if (S_ISSOCK(st.st_mode)) printf(" Socket\n");
608 printf(" Size: %10u", (unsigned int)st.st_size);
609 #ifdef HAVE_STAT_ST_BLOCKS
610 printf(" Blocks: %9u", (unsigned int)st.st_blocks);
611 #endif
612 #ifdef HAVE_STAT_ST_BLKSIZE
613 printf(" IO Block: %u\n", (unsigned int)st.st_blksize);
614 #endif
615 printf(" Device: 0x%10x", (unsigned int)st.st_dev);
616 printf(" Inode: %10u", (unsigned int)st.st_ino);
617 printf(" Links: %10u\n", (unsigned int)st.st_nlink);
618 printf(" Access: %05o", (st.st_mode) & 007777);
619 printf(" Uid: %5lu/%.16s Gid: %5lu/%.16s\n", (unsigned long)st.st_uid, user,
620 (unsigned long)st.st_gid, group);
621 printf(" Access: %s", ctime(&(st.st_atime)));
622 printf(" Modify: %s", ctime(&(st.st_mtime)));
623 printf(" Change: %s", ctime(&(st.st_ctime)));
625 return NT_STATUS_OK;
629 static NTSTATUS cmd_lstat(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
631 const char *user;
632 const char *group;
633 struct passwd *pwd = NULL;
634 struct group *grp = NULL;
635 SMB_STRUCT_STAT st;
637 if (argc != 2) {
638 printf("Usage: lstat <path>\n");
639 return NT_STATUS_OK;
642 if (SMB_VFS_LSTAT(vfs->conn, argv[1], &st) == -1) {
643 printf("lstat: error=%d (%s)\n", errno, strerror(errno));
644 return NT_STATUS_UNSUCCESSFUL;
647 pwd = sys_getpwuid(st.st_uid);
648 if (pwd != NULL) user = pwd->pw_name;
649 else user = null_string;
650 grp = sys_getgrgid(st.st_gid);
651 if (grp != NULL) group = grp->gr_name;
652 else group = null_string;
654 printf("lstat: ok\n");
655 if (S_ISREG(st.st_mode)) printf(" Regular File\n");
656 else if (S_ISDIR(st.st_mode)) printf(" Directory\n");
657 else if (S_ISCHR(st.st_mode)) printf(" Character Device\n");
658 else if (S_ISBLK(st.st_mode)) printf(" Block Device\n");
659 else if (S_ISFIFO(st.st_mode)) printf(" Fifo\n");
660 else if (S_ISLNK(st.st_mode)) printf(" Symbolic Link\n");
661 else if (S_ISSOCK(st.st_mode)) printf(" Socket\n");
662 printf(" Size: %10u", (unsigned int)st.st_size);
663 #ifdef HAVE_STAT_ST_BLOCKS
664 printf(" Blocks: %9u", (unsigned int)st.st_blocks);
665 #endif
666 #ifdef HAVE_STAT_ST_BLKSIZE
667 printf(" IO Block: %u\n", (unsigned int)st.st_blksize);
668 #endif
669 printf(" Device: 0x%10x", (unsigned int)st.st_dev);
670 printf(" Inode: %10u", (unsigned int)st.st_ino);
671 printf(" Links: %10u\n", (unsigned int)st.st_nlink);
672 printf(" Access: %05o", (st.st_mode) & 007777);
673 printf(" Uid: %5lu/%.16s Gid: %5lu/%.16s\n", (unsigned long)st.st_uid, user,
674 (unsigned long)st.st_gid, group);
675 printf(" Access: %s", ctime(&(st.st_atime)));
676 printf(" Modify: %s", ctime(&(st.st_mtime)));
677 printf(" Change: %s", ctime(&(st.st_ctime)));
679 return NT_STATUS_OK;
683 static NTSTATUS cmd_chmod(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
685 mode_t mode;
686 if (argc != 3) {
687 printf("Usage: chmod <path> <mode>\n");
688 return NT_STATUS_OK;
691 mode = atoi(argv[2]);
692 if (SMB_VFS_CHMOD(vfs->conn, argv[1], mode) == -1) {
693 printf("chmod: error=%d (%s)\n", errno, strerror(errno));
694 return NT_STATUS_UNSUCCESSFUL;
697 printf("chmod: ok\n");
698 return NT_STATUS_OK;
702 static NTSTATUS cmd_fchmod(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
704 int fd;
705 mode_t mode;
706 if (argc != 3) {
707 printf("Usage: fchmod <fd> <mode>\n");
708 return NT_STATUS_OK;
711 fd = atoi(argv[1]);
712 mode = atoi(argv[2]);
713 if (fd < 0 || fd >= 1024) {
714 printf("fchmod: error=%d (file descriptor out of range)\n", EBADF);
715 return NT_STATUS_OK;
717 if (vfs->files[fd] == NULL) {
718 printf("fchmod: error=%d (invalid file descriptor)\n", EBADF);
719 return NT_STATUS_OK;
722 if (SMB_VFS_FCHMOD(vfs->files[fd], mode) == -1) {
723 printf("fchmod: error=%d (%s)\n", errno, strerror(errno));
724 return NT_STATUS_UNSUCCESSFUL;
727 printf("fchmod: ok\n");
728 return NT_STATUS_OK;
732 static NTSTATUS cmd_chown(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
734 uid_t uid;
735 gid_t gid;
736 if (argc != 4) {
737 printf("Usage: chown <path> <uid> <gid>\n");
738 return NT_STATUS_OK;
741 uid = atoi(argv[2]);
742 gid = atoi(argv[3]);
743 if (SMB_VFS_CHOWN(vfs->conn, argv[1], uid, gid) == -1) {
744 printf("chown: error=%d (%s)\n", errno, strerror(errno));
745 return NT_STATUS_UNSUCCESSFUL;
748 printf("chown: ok\n");
749 return NT_STATUS_OK;
753 static NTSTATUS cmd_fchown(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
755 uid_t uid;
756 gid_t gid;
757 int fd;
758 if (argc != 4) {
759 printf("Usage: fchown <fd> <uid> <gid>\n");
760 return NT_STATUS_OK;
763 uid = atoi(argv[2]);
764 gid = atoi(argv[3]);
765 fd = atoi(argv[1]);
766 if (fd < 0 || fd >= 1024) {
767 printf("fchown: faliure=%d (file descriptor out of range)\n", EBADF);
768 return NT_STATUS_OK;
770 if (vfs->files[fd] == NULL) {
771 printf("fchown: error=%d (invalid file descriptor)\n", EBADF);
772 return NT_STATUS_OK;
774 if (SMB_VFS_FCHOWN(vfs->files[fd], uid, gid) == -1) {
775 printf("fchown error=%d (%s)\n", errno, strerror(errno));
776 return NT_STATUS_UNSUCCESSFUL;
779 printf("fchown: ok\n");
780 return NT_STATUS_OK;
784 static NTSTATUS cmd_getwd(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
786 char buf[PATH_MAX];
787 if (SMB_VFS_GETWD(vfs->conn, buf) == NULL) {
788 printf("getwd: error=%d (%s)\n", errno, strerror(errno));
789 return NT_STATUS_UNSUCCESSFUL;
792 printf("getwd: %s\n", buf);
793 return NT_STATUS_OK;
796 static NTSTATUS cmd_utime(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
798 struct timespec ts[2];
799 if (argc != 4) {
800 printf("Usage: utime <path> <access> <modify>\n");
801 return NT_STATUS_OK;
803 ts[0] = convert_time_t_to_timespec(atoi(argv[2]));
804 ts[1] = convert_time_t_to_timespec(atoi(argv[3]));
805 if (SMB_VFS_NTIMES(vfs->conn, argv[1], ts) != 0) {
806 printf("utime: error=%d (%s)\n", errno, strerror(errno));
807 return NT_STATUS_UNSUCCESSFUL;
810 printf("utime: ok\n");
811 return NT_STATUS_OK;
814 static NTSTATUS cmd_ftruncate(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
816 int fd;
817 SMB_OFF_T off;
818 if (argc != 3) {
819 printf("Usage: ftruncate <fd> <length>\n");
820 return NT_STATUS_OK;
823 fd = atoi(argv[1]);
824 off = atoi(argv[2]);
825 if (fd < 0 || fd >= 1024) {
826 printf("ftruncate: error=%d (file descriptor out of range)\n", EBADF);
827 return NT_STATUS_OK;
829 if (vfs->files[fd] == NULL) {
830 printf("ftruncate: error=%d (invalid file descriptor)\n", EBADF);
831 return NT_STATUS_OK;
834 if (SMB_VFS_FTRUNCATE(vfs->files[fd], off) == -1) {
835 printf("ftruncate: error=%d (%s)\n", errno, strerror(errno));
836 return NT_STATUS_UNSUCCESSFUL;
839 printf("ftruncate: ok\n");
840 return NT_STATUS_OK;
843 static NTSTATUS cmd_lock(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
845 int fd;
846 int op;
847 long offset;
848 long count;
849 int type;
850 const char *typestr;
852 if (argc != 6) {
853 printf("Usage: lock <fd> <op> <offset> <count> <type>\n");
854 printf(" ops: G = F_GETLK\n");
855 printf(" S = F_SETLK\n");
856 printf(" W = F_SETLKW\n");
857 printf(" type: R = F_RDLCK\n");
858 printf(" W = F_WRLCK\n");
859 printf(" U = F_UNLCK\n");
860 return NT_STATUS_OK;
863 if (sscanf(argv[1], "%d", &fd) == 0) {
864 printf("lock: error=-1 (error parsing fd)\n");
865 return NT_STATUS_UNSUCCESSFUL;
868 op = 0;
869 switch (*argv[2]) {
870 case 'G':
871 op = F_GETLK;
872 break;
873 case 'S':
874 op = F_SETLK;
875 break;
876 case 'W':
877 op = F_SETLKW;
878 break;
879 default:
880 printf("lock: error=-1 (invalid op flag!)\n");
881 return NT_STATUS_UNSUCCESSFUL;
884 if (sscanf(argv[3], "%ld", &offset) == 0) {
885 printf("lock: error=-1 (error parsing fd)\n");
886 return NT_STATUS_UNSUCCESSFUL;
889 if (sscanf(argv[4], "%ld", &count) == 0) {
890 printf("lock: error=-1 (error parsing fd)\n");
891 return NT_STATUS_UNSUCCESSFUL;
894 type = 0;
895 typestr = argv[5];
896 while(*typestr) {
897 switch (*typestr) {
898 case 'R':
899 type |= F_RDLCK;
900 break;
901 case 'W':
902 type |= F_WRLCK;
903 break;
904 case 'U':
905 type |= F_UNLCK;
906 break;
907 default:
908 printf("lock: error=-1 (invalid type flag!)\n");
909 return NT_STATUS_UNSUCCESSFUL;
911 typestr++;
914 printf("lock: debug lock(fd=%d, op=%d, offset=%ld, count=%ld, type=%d))\n", fd, op, offset, count, type);
916 if (SMB_VFS_LOCK(vfs->files[fd], op, offset, count, type) == False) {
917 printf("lock: error=%d (%s)\n", errno, strerror(errno));
918 return NT_STATUS_UNSUCCESSFUL;
921 printf("lock: ok\n");
922 return NT_STATUS_OK;
925 static NTSTATUS cmd_symlink(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
927 if (argc != 3) {
928 printf("Usage: symlink <path> <link>\n");
929 return NT_STATUS_OK;
932 if (SMB_VFS_SYMLINK(vfs->conn, argv[1], argv[2]) == -1) {
933 printf("symlink: error=%d (%s)\n", errno, strerror(errno));
934 return NT_STATUS_UNSUCCESSFUL;
937 printf("symlink: ok\n");
938 return NT_STATUS_OK;
942 static NTSTATUS cmd_readlink(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
944 char buffer[PATH_MAX];
945 int size;
947 if (argc != 2) {
948 printf("Usage: readlink <path>\n");
949 return NT_STATUS_OK;
952 if ((size = SMB_VFS_READLINK(vfs->conn, argv[1], buffer, PATH_MAX)) == -1) {
953 printf("readlink: error=%d (%s)\n", errno, strerror(errno));
954 return NT_STATUS_UNSUCCESSFUL;
957 buffer[size] = '\0';
958 printf("readlink: %s\n", buffer);
959 return NT_STATUS_OK;
963 static NTSTATUS cmd_link(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
965 if (argc != 3) {
966 printf("Usage: link <path> <link>\n");
967 return NT_STATUS_OK;
970 if (SMB_VFS_LINK(vfs->conn, argv[1], argv[2]) == -1) {
971 printf("link: error=%d (%s)\n", errno, strerror(errno));
972 return NT_STATUS_UNSUCCESSFUL;
975 printf("link: ok\n");
976 return NT_STATUS_OK;
979 static NTSTATUS cmd_mknod(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
981 mode_t mode;
982 unsigned int dev_val;
983 SMB_DEV_T dev;
985 if (argc != 4) {
986 printf("Usage: mknod <path> <mode> <dev>\n");
987 printf(" mode is octal\n");
988 printf(" dev is hex\n");
989 return NT_STATUS_OK;
992 if (sscanf(argv[2], "%ho", (unsigned short *)&mode) == 0) {
993 printf("open: error=-1 (invalid mode!)\n");
994 return NT_STATUS_UNSUCCESSFUL;
997 if (sscanf(argv[3], "%x", &dev_val) == 0) {
998 printf("open: error=-1 (invalid dev!)\n");
999 return NT_STATUS_UNSUCCESSFUL;
1001 dev = (SMB_DEV_T)dev_val;
1003 if (SMB_VFS_MKNOD(vfs->conn, argv[1], mode, dev) == -1) {
1004 printf("mknod: error=%d (%s)\n", errno, strerror(errno));
1005 return NT_STATUS_UNSUCCESSFUL;
1008 printf("mknod: ok\n");
1009 return NT_STATUS_OK;
1012 static NTSTATUS cmd_realpath(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1014 char respath[PATH_MAX];
1016 if (argc != 2) {
1017 printf("Usage: realpath <path>\n");
1018 return NT_STATUS_OK;
1021 if (SMB_VFS_REALPATH(vfs->conn, argv[1], respath) == NULL) {
1022 printf("realpath: error=%d (%s)\n", errno, strerror(errno));
1023 return NT_STATUS_UNSUCCESSFUL;
1026 printf("realpath: ok\n");
1027 return NT_STATUS_OK;
1030 struct cmd_set vfs_commands[] = {
1032 { "VFS Commands" },
1034 { "load", cmd_load_module, "Load a module", "load <module.so>" },
1035 { "populate", cmd_populate, "Populate a data buffer", "populate <char> <size>" },
1036 { "showdata", cmd_show_data, "Show data currently in data buffer", "show_data [<offset> <len>]"},
1037 { "connect", cmd_connect, "VFS connect()", "connect" },
1038 { "disconnect", cmd_disconnect, "VFS disconnect()", "disconnect" },
1039 { "disk_free", cmd_disk_free, "VFS disk_free()", "disk_free <path>" },
1040 { "opendir", cmd_opendir, "VFS opendir()", "opendir <fname>" },
1041 { "readdir", cmd_readdir, "VFS readdir()", "readdir" },
1042 { "mkdir", cmd_mkdir, "VFS mkdir()", "mkdir <path>" },
1043 { "rmdir", cmd_pathfunc, "VFS rmdir()", "rmdir <path>" },
1044 { "closedir", cmd_closedir, "VFS closedir()", "closedir" },
1045 { "open", cmd_open, "VFS open()", "open <fname>" },
1046 { "close", cmd_close, "VFS close()", "close <fd>" },
1047 { "read", cmd_read, "VFS read()", "read <fd> <size>" },
1048 { "write", cmd_write, "VFS write()", "write <fd> <size>" },
1049 { "lseek", cmd_lseek, "VFS lseek()", "lseek <fd> <offset> <whence>" },
1050 { "rename", cmd_rename, "VFS rename()", "rename <old> <new>" },
1051 { "fsync", cmd_fsync, "VFS fsync()", "fsync <fd>" },
1052 { "stat", cmd_stat, "VFS stat()", "stat <fname>" },
1053 { "fstat", cmd_fstat, "VFS fstat()", "fstat <fd>" },
1054 { "lstat", cmd_lstat, "VFS lstat()", "lstat <fname>" },
1055 { "unlink", cmd_pathfunc, "VFS unlink()", "unlink <fname>" },
1056 { "chmod", cmd_chmod, "VFS chmod()", "chmod <path> <mode>" },
1057 { "fchmod", cmd_fchmod, "VFS fchmod()", "fchmod <fd> <mode>" },
1058 { "chown", cmd_chown, "VFS chown()", "chown <path> <uid> <gid>" },
1059 { "fchown", cmd_fchown, "VFS fchown()", "fchown <fd> <uid> <gid>" },
1060 { "chdir", cmd_pathfunc, "VFS chdir()", "chdir <path>" },
1061 { "getwd", cmd_getwd, "VFS getwd()", "getwd" },
1062 { "utime", cmd_utime, "VFS utime()", "utime <path> <access> <modify>" },
1063 { "ftruncate", cmd_ftruncate, "VFS ftruncate()", "ftruncate <fd> <length>" },
1064 { "lock", cmd_lock, "VFS lock()", "lock <f> <op> <offset> <count> <type>" },
1065 { "symlink", cmd_symlink, "VFS symlink()", "symlink <old> <new>" },
1066 { "readlink", cmd_readlink, "VFS readlink()", "readlink <path>" },
1067 { "link", cmd_link, "VFS link()", "link <oldpath> <newpath>" },
1068 { "mknod", cmd_mknod, "VFS mknod()", "mknod <path> <mode> <dev>" },
1069 { "realpath", cmd_realpath, "VFS realpath()", "realpath <path>" },
1070 { NULL }