This repository was archived by the owner on Feb 21, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsgqueue.c
More file actions
77 lines (61 loc) · 1.42 KB
/
Copy pathmsgqueue.c
File metadata and controls
77 lines (61 loc) · 1.42 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <time.h>
#include "msgqueue.h"
#include "usg.h"
#include "xmalloc.h"
int enqueue_message(int recipient, int sender, int seq, int msgclass, char *text, int length)
{
char buf[100];
int fd;
mkdir(path_uin("queue", recipient), 0700);
snprintf(buf, sizeof(buf), "queue/%d/%.10ld-%d-%d-%d", recipient, time(NULL), sender, seq, msgclass);
if ((fd = open(buf, O_WRONLY | O_CREAT, 0600)) == -1)
return -1;
if (write(fd, text, length) < length) {
close(fd);
unlink(buf);
return -1;
}
close(fd);
return 0;
}
int unqueue_message(int uin, msgqueue_t *m)
{
DIR *d;
struct dirent *de;
int fd;
if (!(d = opendir(path_uin("queue", uin))))
return -1;
while ((de = readdir(d))) {
char buf[100];
struct stat st;
snprintf(buf, sizeof(buf), "queue/%d/%s", uin, de->d_name);
if (stat(buf, &st) == -1 || !S_ISREG(st.st_mode))
continue;
m->length = st.st_size;
if (sscanf(de->d_name, "%d-%d-%d-%d", &m->time, &m->sender, &m->seq, &m->msgclass) != 4)
continue;
if ((fd = open(buf, O_RDONLY)) == -1) {
closedir(d);
return -1;
}
m->text = xmalloc(st.st_size);
if (read(fd, m->text, st.st_size) < st.st_size) {
free(m->text);
closedir(d);
return -1;
}
close(fd);
unlink(buf);
return 0;
closedir(d);
}
closedir(d);
return -1;
}