-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy.c
More file actions
111 lines (97 loc) · 2.82 KB
/
copy.c
File metadata and controls
111 lines (97 loc) · 2.82 KB
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
#pragma once
#include "os.c"
static const char *NAMED_PIPE = "/tmp/clf-pipe";
typedef struct {
char source_file_path[PATH_MAX];
bool delete_source_on_paste;
} message_t;
void copy_yank(const char *source_file_path, const bool delete_source_on_paste) {
int ret = mkfifo(NAMED_PIPE, 0666);
if (ret == -1) {
switch (errno) {
case EEXIST:
// TODO: drain any other pending copies
break;
default:
snprintf(g_msg, sizeof g_msg, "(%s mkfifo) %s", __func__, strerror(errno));
g_msg_type = MSG_TYPE_ERROR;
return;
}
}
const pid_t p = fork();
if (p == -1) {
snprintf(g_msg, sizeof g_msg, "(%s fork) %s", __func__, strerror(errno));
g_msg_type = MSG_TYPE_ERROR;
return;
}
if (p == 0) {
const int fd = open(NAMED_PIPE, O_WRONLY);
if (fd == -1) {
snprintf(g_msg, sizeof g_msg, "(%s open) %s", __func__, strerror(errno));
g_msg_type = MSG_TYPE_ERROR;
return;
}
message_t msg;
strlcpy(msg.source_file_path, source_file_path, PATH_MAX);
msg.delete_source_on_paste = delete_source_on_paste;
ret = write(fd, &msg, sizeof(msg));
if (ret == -1) {
snprintf(g_msg, sizeof g_msg, "(%s write) %s", __func__, strerror(errno));
g_msg_type = MSG_TYPE_ERROR;
}
ret = close(fd);
if (ret == -1) {
snprintf(g_msg, sizeof g_msg, "(%s close) %s", __func__, strerror(errno));
g_msg_type = MSG_TYPE_ERROR;
}
_exit(0);
}
}
void copy_paste(const char *target_dir_path) {
int ret = mkfifo(NAMED_PIPE, 0666);
if (ret == -1) {
switch (errno) {
case EEXIST:
break;
default:
snprintf(g_msg, sizeof g_msg, "(%s mkfifo) %s", __func__, strerror(errno));
g_msg_type = MSG_TYPE_ERROR;
return;
}
}
const int fd = open(NAMED_PIPE, O_RDONLY);
if (fd == -1) {
snprintf(g_msg, sizeof g_msg, "(%s open) %s", __func__, strerror(errno));
g_msg_type = MSG_TYPE_ERROR;
return;
}
message_t msg;
ret = read(fd, &msg, sizeof(msg));
if (ret == -1) {
snprintf(g_msg, sizeof g_msg, "(%s read) %s", __func__, strerror(errno));
g_msg_type = MSG_TYPE_ERROR;
return;
}
ret = close(fd);
if (ret == -1) {
snprintf(g_msg, sizeof g_msg, "(%s close) %s", __func__, strerror(errno));
g_msg_type = MSG_TYPE_ERROR;
return;
}
if (msg.delete_source_on_paste) {
if (os_move(msg.source_file_path, target_dir_path) == 0) {
snprintf(g_msg, sizeof g_msg, "Moved successfully");
g_msg_type = MSG_TYPE_SUCCESS;
}
} else {
if (os_copy(msg.source_file_path, target_dir_path) == 0) {
snprintf(g_msg, sizeof g_msg, "Copied successfully");
g_msg_type = MSG_TYPE_SUCCESS;
}
}
ret = unlink(NAMED_PIPE);
if (ret == -1) {
snprintf(g_msg, sizeof g_msg, "(%s unlink) %s", __func__, strerror(errno));
g_msg_type = MSG_TYPE_ERROR;
}
}