forked from gozfree/gear-lib
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_libfile.c
119 lines (108 loc) · 3.6 KB
/
test_libfile.c
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
/******************************************************************************
* Copyright (C) 2014-2018 Zhifeng Gong <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with libraries; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#include "libfile.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/vfs.h>
#include <errno.h>
static void foo(void)
{
int len = 0;
int i = 0;
file_backend_type type;
char buf[128] = {0};
for (i = 0; i < 2; ++i) {
if (i == 0)
type = FILE_BACKEND_IO;
else if (i == 1)
type = FILE_BACKEND_FIO;
file_backend(type);
printf("backend=%d\n", type);
struct file *fw = file_open("/tmp/lsusb", F_CREATE);
file_write(fw, "hello file\n", 11);
file_sync(fw);
file_seek(fw, 0, SEEK_SET);
memset(buf, 0, sizeof(buf));
len = file_read(fw, buf, sizeof(buf));
printf("read len = %d, buf = %s", len, buf);
file_close(fw);
struct file *f = file_open("/tmp/lsusb", F_RDONLY);
memset(buf, 0, sizeof(buf));
len = file_read(f, buf, sizeof(buf));
printf("read len = %d, buf = %s", len, buf);
printf("len=%zu\n", file_get_size("/tmp/lsusb"));
struct iovec *iobuf = file_dump("/tmp/lsusb");
if (iobuf) {
//printf("len=%zu, buf=%s\n", iobuf->iov_len, (char *)iobuf->iov_base);
}
}
}
static void foo2(void)
{
struct file_systat *stat = file_get_systat("./Makefile");
printf("total = %zuMB\n", stat->size_total/(1024*1024));
printf("avail = %zuMB\n", stat->size_avail/(1024*1024));
printf("free = %zuMB\n", stat->size_free/(1024*1024));
printf("fs type name = %s\n", stat->fs_type_name);
}
static void foo3(void)
{
printf("local path=%s\n", file_path_pwd());
printf("suffix=%s\n", file_path_suffix(file_path_pwd()));
printf("prefix=%s\n", file_path_prefix(file_path_pwd()));
}
static void foo4(void)
{
int len = 0;
char buf[128] = {0};
struct file *fw = file_open("/tmp/lsusb", F_APPEND);
file_write(fw, "hello file\n", 11);
file_sync(fw);
file_seek(fw, 0, SEEK_SET);
memset(buf, 0, sizeof(buf));
len = file_read(fw, buf, sizeof(buf));
printf("read len = %d, buf = %s", len, buf);
file_close(fw);
}
static void foo5(void)
{
struct file_info info;
file_get_info("/tmp/lsusb", &info);
printf("info->size = %zu\n", info.size);
printf("info->type = %d\n", info.type);
printf("info->time_modify = %ld\n", info.modify_sec);
printf("info->time_access = %ld\n", info.access_sec);
}
int main(int argc, char **argv)
{
uint64_t size = 1000;
file_dir_size("./", &size);
printf("folder_size=%zu\n", size);
file_dir_tree("./");
foo5();
foo4();
foo();
foo2();
foo3();
if (0 != file_create("jjj.c")) {
printf("file_create failed!\n");
}
return 0;
}