netapi: add NetFileGetInfo example code.
[Samba/gebeck_regimport.git] / source3 / lib / netapi / examples / file / file_getinfo.c
blob9ad8305bc5748e9950b25f11d1735c85a1428d93
1 /*
2 * Unix SMB/CIFS implementation.
3 * NetFileGetInfo query
4 * Copyright (C) Guenther Deschner 2008
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <sys/types.h>
21 #include <inttypes.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include <netapi.h>
28 #include "common.h"
30 int main(int argc, const char **argv)
32 NET_API_STATUS status;
33 struct libnetapi_ctx *ctx = NULL;
34 const char *hostname = NULL;
35 uint32_t fileid = 0;
36 uint32_t level = 3;
37 uint8_t *buffer = NULL;
39 struct FILE_INFO_2 *i2 = NULL;
40 struct FILE_INFO_3 *i3 = NULL;
42 poptContext pc;
43 int opt;
45 struct poptOption long_options[] = {
46 POPT_AUTOHELP
47 POPT_COMMON_LIBNETAPI_EXAMPLES
48 POPT_TABLEEND
51 status = libnetapi_init(&ctx);
52 if (status != 0) {
53 return status;
56 pc = poptGetContext("file_getinfo", argc, argv, long_options, 0);
58 poptSetOtherOptionHelp(pc, "hostname fileid");
59 while((opt = poptGetNextOpt(pc)) != -1) {
62 if (!poptPeekArg(pc)) {
63 poptPrintHelp(pc, stderr, 0);
64 goto out;
66 hostname = poptGetArg(pc);
68 if (!poptPeekArg(pc)) {
69 poptPrintHelp(pc, stderr, 0);
70 goto out;
72 fileid = atoi(poptGetArg(pc));
74 if (poptPeekArg(pc)) {
75 level = atoi(poptGetArg(pc));
78 /* NetFileGetInfo */
80 status = NetFileGetInfo(hostname,
81 fileid,
82 level,
83 &buffer);
84 if (status != 0) {
85 printf("NetFileGetInfo failed with: %s\n",
86 libnetapi_get_error_string(ctx, status));
87 goto out;
90 switch (level) {
91 case 2:
92 i2 = (struct FILE_INFO_2 *)buffer;
93 printf("file_id: %d\n", i2->fi2_id);
94 break;
95 case 3:
96 i3 = (struct FILE_INFO_3 *)buffer;
97 printf("file_id: %d\n", i3->fi3_id);
98 printf("permissions: %d\n", i3->fi3_permissions);
99 printf("num_locks: %d\n", i3->fi3_num_locks);
100 printf("pathname: %s\n", i3->fi3_pathname);
101 printf("username: %s\n", i3->fi3_username);
102 break;
103 default:
104 break;
107 out:
108 libnetapi_free(ctx);
109 poptFreeContext(pc);
111 return status;