summaryrefslogtreecommitdiffstats
path: root/examples/libsmbclient/teststat.c
blob: 079ac8974ad87e092264da559a9c781e361f3d88 (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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <libsmbclient.h>
#include <stdbool.h>
#include "get_auth_data_fn.h"

static const char *filetypestr(mode_t mode)
{
	if (S_ISREG(mode)) {
		return "regular file";
	}
	if (S_ISDIR(mode)) {
		return "directory";
	}
	if (S_ISFIFO(mode)) {
		return "fifo";
	}
	if (S_ISLNK(mode)) {
		return "symbolic link";
	}
	if (S_ISSOCK(mode)) {
		return "socket";
	}
	if (S_ISCHR(mode)) {
		return "character special file";
	}
	if (S_ISBLK(mode)) {
		return "block special file";
	}
	return "unknown file type";
}

int main(int argc, char * argv[])
{
	SMBCCTX *ctx = NULL;
	int             debug = 0;
	char            m_time[32];
	char            c_time[32];
	char            a_time[32];
	const char *          pSmbPath = NULL;
	const char *          pLocalPath = NULL;
	struct stat     st;
	int ret;

	if (argc == 1) {
		pSmbPath = "smb://RANDOM/Public/small";
		pLocalPath = "/random/home/samba/small";
	}
	else if (argc == 2) {
		pSmbPath = argv[1];
		pLocalPath = NULL;
	}
	else if (argc == 3) {
		pSmbPath = argv[1];
		pLocalPath = argv[2];
	} else {
		printf("usage: %s [ smb://path/to/file "
		       "[ /nfs/or/local/path/to/file ] ]\n",
		       argv[0]);
		return 1;
	}

	ctx = smbc_new_context();
	if (ctx == NULL) {
		perror("smbc_new_context failed");
		return 1;
	}

	smbc_setOptionDebugToStderr(ctx, 1);
	smbc_setDebug(ctx, debug);
	smbc_init_context(ctx);
	smbc_setFunctionAuthData(ctx, get_auth_data_fn);
	smbc_setOptionPosixExtensions(ctx, true);

	ret = smbc_getFunctionStat(ctx)(ctx, pSmbPath, &st);
	if (ret < 0) {
		perror("smbc_stat");
		return 1;
	}

	printf("\nSAMBA\n mtime:%jd/%s ctime:%jd/%s atime:%jd/%s %s\n",
	       (intmax_t)st.st_mtime,
	       ctime_r(&st.st_mtime, m_time),
	       (intmax_t)st.st_ctime,
	       ctime_r(&st.st_ctime, c_time),
	       (intmax_t)st.st_atime,
	       ctime_r(&st.st_atime, a_time),
	       filetypestr(st.st_mode));

	if (pLocalPath != NULL) {
		ret = stat(pLocalPath, &st);
		if (ret < 0) {
			perror("stat");
			return 1;
		}

		printf("LOCAL\n mtime:%jd/%s ctime:%jd/%s atime:%jd/%s %s\n",
		       (intmax_t)st.st_mtime,
		       ctime_r(&st.st_mtime, m_time),
		       (intmax_t)st.st_ctime,
		       ctime_r(&st.st_ctime, c_time),
		       (intmax_t)st.st_atime,
		       ctime_r(&st.st_atime, a_time),
		       filetypestr(st.st_mode));
	}

	return 0;
}