Skip to content

Commit 4656f6a

Browse files
author
Neo Chen
committed
new file: CMakeLists.txt
new file: outfile.c new file: outfile.h
1 parent 51600b3 commit 4656f6a

File tree

3 files changed

+232
-0
lines changed

3 files changed

+232
-0
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 2.8)
2+
PROJECT(outfile)
3+
ADD_LIBRARY(outfile SHARED outfile.c)
4+
INCLUDE_DIRECTORIES(/usr/include/mysql)
5+
#TARGET_LINK_LIBRARIES(outfile )
6+
INSTALL(PROGRAMS liboutfile.so DESTINATION /usr/lib64/mysql/plugin/)

outfile.c

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
Homepage: http://netkiller.github.io/
3+
Author: netkiller<netkiller@msn.com>
4+
*/
5+
#include <stdlib.h>
6+
#include <stdio.h>
7+
#include <mysql.h>
8+
#include <string.h>
9+
#include <sys/io.h>
10+
#include <sys/stat.h>
11+
#include <sys/types.h>
12+
#include <fcntl.h>
13+
14+
#include "outfile.h"
15+
16+
/* ------------------------ outfile_create ----------------------------- */
17+
18+
my_bool outfile_create_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
19+
{
20+
21+
if (args->arg_count != 1)
22+
{
23+
strncpy(message,
24+
"one arguments must be supplied: outfile_create('<pipename>').",
25+
MYSQL_ERRMSG_SIZE);
26+
return 1;
27+
}
28+
args->arg_type[0]= STRING_RESULT;
29+
30+
return 0;
31+
}
32+
33+
char *outfile_create(UDF_INIT *initid, UDF_ARGS *args,
34+
__attribute__ ((unused)) char *result,
35+
unsigned long *length,
36+
__attribute__ ((unused)) char *is_null,
37+
__attribute__ ((unused)) char *error)
38+
{
39+
40+
char *status;
41+
if (create_outfile(args->args[0]) == 0)
42+
status = "ture";
43+
else
44+
status = "false";
45+
46+
*length = strlen(status);
47+
return ((char *)status);
48+
49+
}
50+
51+
void outfile_create_deinit(UDF_INIT *initid)
52+
{
53+
return;
54+
}
55+
56+
/* ------------------------ outfile_remove ----------------------------- */
57+
58+
my_bool outfile_remove_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
59+
{
60+
61+
if (args->arg_count != 1)
62+
{
63+
strncpy(message,"one arguments must be supplied: outfile_remove('<pipename>').", MYSQL_ERRMSG_SIZE);
64+
return 1;
65+
}
66+
67+
args->arg_type[0]= STRING_RESULT;
68+
69+
return 0;
70+
}
71+
72+
char *outfile_remove(UDF_INIT *initid, UDF_ARGS *args,
73+
__attribute__ ((unused)) char *result,
74+
unsigned long *length,
75+
__attribute__ ((unused)) char *is_null,
76+
__attribute__ ((unused)) char *error)
77+
{
78+
79+
char *data;
80+
if( remove_outfile(args->args[0]) == 0 )
81+
data = "true";
82+
else
83+
//asprintf(&data, "ARG0=%s, ARG1=%d", args->args[0], errno);
84+
data = "false";
85+
86+
*length = strlen(data);
87+
return ((char *)data);
88+
89+
}
90+
91+
void outfile_remove_deinit(UDF_INIT *initid)
92+
{
93+
return;
94+
}
95+
96+
/* ------------------------ outfile_read ----------------------------- */
97+
98+
my_bool outfile_read_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
99+
{
100+
101+
if (args->arg_count != 1)
102+
{
103+
strncpy(message, "one arguments must be supplied: outfile_read('<pipename>').", MYSQL_ERRMSG_SIZE);
104+
return 1;
105+
}
106+
107+
args->arg_type[0]= STRING_RESULT;
108+
109+
return 0;
110+
}
111+
112+
char *outfile_read(UDF_INIT *initid, UDF_ARGS *args,
113+
__attribute__ ((unused)) char *result,
114+
unsigned long *length,
115+
__attribute__ ((unused)) char *is_null,
116+
__attribute__ ((unused)) char *error)
117+
{
118+
119+
char *data;
120+
121+
data = read_outfile(args->args[0]);
122+
123+
//asprintf(&data, "ARG0=%s, ARG1=%d", args->args[0], args->args[1]);
124+
*length = strlen(data);
125+
return ((char *)data);
126+
127+
}
128+
129+
void outfile_read_deinit(UDF_INIT *initid)
130+
{
131+
return;
132+
}
133+
134+
/* ------------------------ outfile_write ----------------------------- */
135+
136+
my_bool outfile_write_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
137+
{
138+
if (args->arg_count != 2)
139+
{
140+
strncpy(message, "two arguments must be supplied: outfile_write('pipename','message').", MYSQL_ERRMSG_SIZE);
141+
return 1;
142+
}
143+
144+
args->arg_type[0]= STRING_RESULT;
145+
return 0;
146+
}
147+
148+
char *outfile_write(UDF_INIT *initid, UDF_ARGS *args,
149+
__attribute__ ((unused)) char *result,
150+
unsigned long *length,
151+
__attribute__ ((unused)) char *is_null,
152+
__attribute__ ((unused)) char *error)
153+
{
154+
155+
char *status;
156+
//asprintf(&status, "SAFENET_URL=%s, SAFENET_KEY=%s", safe_url, safe_key);
157+
if( write_outfile( args->args[0], args->args[1] ) == -1)
158+
status = "false";
159+
else
160+
status = "true";
161+
162+
*length = strlen(status);
163+
return ((char *)status);
164+
}
165+
166+
void outfile_write_deinit(UDF_INIT *initid)
167+
{
168+
return;
169+
}

outfile.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
my_bool outfile_remove_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
2+
char *outfile_remove(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error);
3+
void outfile_remove_deinit(UDF_INIT *initid);
4+
5+
my_bool outfile_read_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
6+
char *outfile_read(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error);
7+
void outfile_read_deinit(UDF_INIT *initid);
8+
9+
my_bool outfile_write_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
10+
char *outfile_write(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error);
11+
void outfile_write_deinit(UDF_INIT *initid);
12+
13+
char* concat(const char* str1, const char* str2)
14+
{
15+
char* res;
16+
asprintf(&res, "%s%s", str1, str2);
17+
return res;
18+
}
19+
20+
char * read_outfile(char * path)
21+
{
22+
int fd;
23+
char buf[8];
24+
char *result="";
25+
/* open, read, and display the message from the FIFO */
26+
fd = open(path, O_RDONLY);
27+
while( read(fd, buf, sizeof(buf)) > 0){
28+
result = concat(result,buf);
29+
memset(buf, 0, sizeof(buf));
30+
}
31+
//printf("Received: %s\n", buf);
32+
close(fd);
33+
34+
return result;
35+
}
36+
37+
int write_outfile(char * path, char * msg)
38+
{
39+
40+
FILE* file = fopen(path, "a+");
41+
if (file == 0) {
42+
return 0;
43+
}
44+
fputs(msg, file);
45+
fclose(file);
46+
return 1;
47+
}
48+
int create_outfile(char * path)
49+
{
50+
/* create the FIFO (named pipe) */
51+
return mkoutfile(path, 0660);
52+
}
53+
int remove_outfile(char *path)
54+
{
55+
/* remove the FIFO */
56+
return unlink(path);
57+
}

0 commit comments

Comments
 (0)