forked from aceld/distributed_memory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fdfs_api.c
147 lines (123 loc) · 3.18 KB
/
fdfs_api.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "fdfs_api.h"
#define FDFS_TEST_MODULE "api"
#define FDFS_TEST_PROC "fdfs"
/* -------------------------------------------*/
/**
* @brief 上传到fdfs一个文件
*
* @param filename
* @param fileid OUT
*
* @returns
* 0 succ -1 fail
*/
/* -------------------------------------------*/
int fdfs_upload_by_filename(const char* filename, char *fileid)
{
int retn = 0;
pid_t pid;
int pfd[2];
//创建管道
retn = pipe(pfd);
if (retn < 0) {
perror("pipe error");
return -1;
}
//创建进程
pid = fork();
if (pid < 0) {
perror("fork error");
return -1;
}
else if (pid == 0) {
//子进程
//执行指令
//关闭 读管道
close(pfd[0]);
//将标准输出重定向到 管道
dup2(pfd[1], STDOUT_FILENO);
//执行指令
execlp("fdfs_upload_file", "fdfs_upload_file", "./conf/client.conf", filename, NULL);
LOG(FDFS_TEST_MODULE, FDFS_TEST_PROC, "execl upload cmd error");
return -1;
}
else if (pid > 0) {
//父进程 读管道
//关闭写管道
close(pfd[1]);
//wait
wait(NULL);
//从管道中去读数据
read(pfd[0], fileid, FILEID_LEN);
//将fileid 记入日志
// LOG(FDFS_TEST_MODULE, FDFS_TEST_PROC, "fileid=[%s]", fileid);
close(pfd[0]);
}
return 0;
}
/* -------------------------------------------*/
/**
* @brief fdfs_upload_by_filename1
*
* @param local_filename 要上传的文件路径
* @param fileid OUT 得到的fileid
*
* @returns
* 0 succ !0 fail
*/
/* -------------------------------------------*/
int fdfs_upload_by_filename1(const char *local_filename, char *file_id)
{
char *conf_filename;
char group_name[FDFS_GROUP_NAME_MAX_LEN + 1];
ConnectionInfo *pTrackerServer;
int result;
int store_path_index;
ConnectionInfo storageServer;
log_init();
g_log_context.log_level = LOG_ERR;
ignore_signal_pipe();
conf_filename = "./conf/client.conf";
if ((result=fdfs_client_init(conf_filename)) != 0)
{
return result;
}
//获取tracker的链接句柄
pTrackerServer = tracker_get_connection();
if (pTrackerServer == NULL)
{
fdfs_client_destroy();
return errno != 0 ? errno : ECONNREFUSED;
}
*group_name = '\0';
//根据tracker 句柄 得到一个可以上传的storage句柄
if ((result=tracker_query_storage_store(pTrackerServer, \
&storageServer, group_name, &store_path_index)) != 0)
{
fdfs_client_destroy();
fprintf(stderr, "tracker_query_storage fail, " \
"error no: %d, error info: %s\n", \
result, STRERROR(result));
return result;
}
//将文件传给storage
result = storage_upload_by_filename1(pTrackerServer, \
&storageServer, store_path_index, \
local_filename, NULL, \
NULL, 0, group_name, file_id);
if (result == 0)
{
//成功将fileid打印到屏幕
//printf("%s\n", file_id);
}
else
{
fprintf(stderr, "upload file fail, " \
"error no: %d, error info: %s\n", \
result, STRERROR(result));
}
//回收资源
tracker_disconnect_server_ex(pTrackerServer, true);
fdfs_client_destroy();
return result;
}