mirror of
https://github.com/fumiama/CMoe-Counter.git
synced 2024-11-23 22:00:56 +01:00
fix overlay
This commit is contained in:
parent
5ad5065a44
commit
da014b2485
4
.gitignore
vendored
4
.gitignore
vendored
@ -5,3 +5,7 @@ build
|
||||
|
||||
# misc
|
||||
.DS_Store
|
||||
|
||||
#for test.c
|
||||
/test
|
||||
/testlog.txt
|
||||
|
@ -2,8 +2,8 @@ cmake_minimum_required(VERSION 2.6)
|
||||
project(CMoe-Counter C)
|
||||
SET(CMAKE_BUILD_TYPE "Release")
|
||||
|
||||
# include_directories("/usr/local/include")
|
||||
# link_directories("/usr/local/lib")
|
||||
include_directories("/usr/local/include")
|
||||
link_directories("/usr/local/lib")
|
||||
|
||||
#在编译选项中加入c99支持
|
||||
add_compile_options(-std=c99)
|
||||
|
28
cmoe.c
28
cmoe.c
@ -9,6 +9,7 @@
|
||||
|
||||
static uint32_t* items_len;
|
||||
static counter_t counter;
|
||||
static FILE* fp;
|
||||
|
||||
static char* datfile = "dat.sp";
|
||||
static char* token = "fumiama";
|
||||
@ -44,20 +45,20 @@ static char* get_arg(const char* query) {
|
||||
return name;
|
||||
}
|
||||
|
||||
static int del_user(FILE* fp, SIMPLE_PB* spb) {
|
||||
static int del_user(FILE* fp, simple_pb_t* spb) {
|
||||
counter_t *d = (counter_t *)spb->target;
|
||||
uint32_t next = ftell(fp);
|
||||
uint32_t this = next - spb->real_len;
|
||||
fseek(fp, 0, SEEK_END);
|
||||
if (fseek(fp, 0, SEEK_END) < 0) return -2;
|
||||
uint32_t end = ftell(fp);
|
||||
if (next == end) return ftruncate(fileno(fp), end - spb->real_len);
|
||||
uint32_t cap = end - next;
|
||||
char *data = malloc(cap);
|
||||
if (data) {
|
||||
fseek(fp, next, SEEK_SET);
|
||||
if (fseek(fp, next, SEEK_SET)) return -3;
|
||||
if (fread(data, cap, 1, fp) == 1) {
|
||||
if (!ftruncate(fileno(fp), end - spb->real_len)){
|
||||
fseek(fp, this, SEEK_SET);
|
||||
if (fseek(fp, this, SEEK_SET)) return -4;
|
||||
if (fwrite(data, cap, 1, fp) == 1) {
|
||||
free(data);
|
||||
fflush(fp);
|
||||
@ -73,7 +74,7 @@ static int del_user(FILE* fp, SIMPLE_PB* spb) {
|
||||
static inline int add_user(char* name, uint32_t count, FILE* fp) {
|
||||
counter.count = count;
|
||||
strncpy(counter.name, name, COUNTER_NAME_LEN-1);
|
||||
fseek(fp, 0, SEEK_END);
|
||||
if (fseek(fp, 0, SEEK_END) < 0) return -2;
|
||||
return !set_pb(fp, items_len, sizeof(counter_t), &counter);
|
||||
}
|
||||
|
||||
@ -95,9 +96,9 @@ static inline uint32_t get_content_len(int isbig, uint16_t* len_type, char* cnts
|
||||
}
|
||||
static void return_count(FILE* fp, char* name, char* theme) {
|
||||
int ch, exist = 0, user_exist = 0;
|
||||
char buf[sizeof(SIMPLE_PB)+sizeof(counter_t)];
|
||||
char buf[sizeof(simple_pb_t)+sizeof(counter_t)];
|
||||
while (has_next(fp, ch)) {
|
||||
SIMPLE_PB *spb = read_pb_into(fp, (SIMPLE_PB*)buf);
|
||||
simple_pb_t *spb = read_pb_into(fp, (simple_pb_t*)buf);
|
||||
counter_t *d = (counter_t *)spb->target;
|
||||
if (strcmp(name, d->name)) continue;
|
||||
if (del_user(fp, spb)) {
|
||||
@ -156,9 +157,9 @@ static void return_count(FILE* fp, char* name, char* theme) {
|
||||
|
||||
static int name_exist(FILE* fp, char* name) {
|
||||
int ch, exist = 0;
|
||||
char buf[sizeof(SIMPLE_PB)+sizeof(counter_t)];
|
||||
char buf[sizeof(simple_pb_t)+sizeof(counter_t)];
|
||||
while (has_next(fp, ch)) {
|
||||
SIMPLE_PB *spb = read_pb_into(fp, (SIMPLE_PB*)buf);
|
||||
simple_pb_t *spb = read_pb_into(fp, (simple_pb_t*)buf);
|
||||
counter_t *d = (counter_t *)spb->target;
|
||||
if (!strcmp(name, d->name)) return 1;
|
||||
}
|
||||
@ -198,7 +199,7 @@ int main(int argc, char **argv) {
|
||||
theme = get_arg(theme + 6);
|
||||
}
|
||||
char* reg = strstr(QS, "reg=");
|
||||
int fd; FILE* fp;
|
||||
int fd;
|
||||
if (!reg) {
|
||||
if ((fd=create_or_open(datfile)) < 0) {
|
||||
http_error(HTTP500, "Open File Error.");
|
||||
@ -210,7 +211,6 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
fp = fdopen(fd, "rb+");
|
||||
return_count(fp, name, theme);
|
||||
fflush(fp); flock(fd, LOCK_UN); close(fd);
|
||||
return 0;
|
||||
}
|
||||
reg = get_arg(reg + 4);
|
||||
@ -232,13 +232,11 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
fp = fdopen(fd, "rb+");
|
||||
if (name_exist(fp, name)) {
|
||||
fflush(fp); flock(fd, LOCK_UN); close(fd);
|
||||
http_error(HTTP400, "Name Exist.");
|
||||
return 7;
|
||||
}
|
||||
fseek(fp, 0, SEEK_END);
|
||||
add_user(name, 0, fp);
|
||||
fflush(fp); flock(fd, LOCK_UN); close(fd);
|
||||
char* msg = "<P>Success.\r\n";
|
||||
if (headers(strlen(msg), "text/html")) {
|
||||
write(1, "\0\0\0\0", 4);
|
||||
@ -246,3 +244,7 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
return write(1, msg, strlen(msg)) <= 0;
|
||||
}
|
||||
|
||||
static void __attribute__((destructor)) defer_close_fp() {
|
||||
if (fp) fclose(fp);
|
||||
}
|
||||
|
16
editor.c
16
editor.c
@ -10,9 +10,9 @@ static counter_t counter;
|
||||
|
||||
static FILE* fp;
|
||||
|
||||
static char buf[sizeof(SIMPLE_PB)+sizeof(counter_t)];
|
||||
static char buf[sizeof(simple_pb_t)+sizeof(counter_t)];
|
||||
|
||||
static int del_user(FILE* fp, SIMPLE_PB* spb) {
|
||||
static int del_user(FILE* fp, simple_pb_t* spb) {
|
||||
counter_t *d = (counter_t *)spb->target;
|
||||
uint32_t next = ftell(fp);
|
||||
uint32_t this = next - spb->real_len;
|
||||
@ -99,7 +99,7 @@ int main(int argc, char** argv) {
|
||||
return 5;
|
||||
}
|
||||
while(has_next(fp, c)) {
|
||||
SIMPLE_PB *spb = read_pb_into(fp, (SIMPLE_PB*)buf);
|
||||
simple_pb_t *spb = read_pb_into(fp, (simple_pb_t*)buf);
|
||||
counter_t *d = (counter_t *)spb->target;
|
||||
if (strcmp(counter.name, d->name)) continue;
|
||||
if(del_user(fp, spb)) {
|
||||
@ -127,7 +127,7 @@ int main(int argc, char** argv) {
|
||||
return 7;
|
||||
}
|
||||
while(has_next(fp, c)) {
|
||||
SIMPLE_PB *spb = read_pb_into(fp, (SIMPLE_PB*)buf);
|
||||
simple_pb_t *spb = read_pb_into(fp, (simple_pb_t*)buf);
|
||||
counter_t *d = (counter_t *)spb->target;
|
||||
if (strstr(d->name, counter.name)) {
|
||||
if(del_user(fp, spb)) {
|
||||
@ -151,7 +151,7 @@ int main(int argc, char** argv) {
|
||||
return 5;
|
||||
}
|
||||
while(has_next(fp, c)) {
|
||||
SIMPLE_PB *spb = read_pb_into(fp, (SIMPLE_PB*)buf);
|
||||
simple_pb_t *spb = read_pb_into(fp, (simple_pb_t*)buf);
|
||||
counter_t *d = (counter_t *)spb->target;
|
||||
if (strcmp(counter.name, d->name)) continue;
|
||||
printf("user '%s' = %u.\n", d->name, d->count);
|
||||
@ -170,7 +170,7 @@ int main(int argc, char** argv) {
|
||||
return 7;
|
||||
}
|
||||
while(has_next(fp, c)) {
|
||||
SIMPLE_PB *spb = read_pb_into(fp, (SIMPLE_PB*)buf);
|
||||
simple_pb_t *spb = read_pb_into(fp, (simple_pb_t*)buf);
|
||||
counter_t *d = (counter_t *)spb->target;
|
||||
if (d->count < counter.count) {
|
||||
printf("'%s' = %u\n", d->name, d->count);
|
||||
@ -195,7 +195,7 @@ int main(int argc, char** argv) {
|
||||
return 7;
|
||||
}
|
||||
while(has_next(fp, c)) {
|
||||
SIMPLE_PB *spb = read_pb_into(fp, (SIMPLE_PB*)buf);
|
||||
simple_pb_t *spb = read_pb_into(fp, (simple_pb_t*)buf);
|
||||
counter_t *d = (counter_t *)spb->target;
|
||||
if (d->count < counter.count) {
|
||||
if(del_user(fp, spb)) {
|
||||
@ -222,7 +222,7 @@ int main(int argc, char** argv) {
|
||||
return 7;
|
||||
}
|
||||
while(has_next(fp, c)) {
|
||||
SIMPLE_PB *spb = read_pb_into(fp, (SIMPLE_PB*)buf);
|
||||
simple_pb_t *spb = read_pb_into(fp, (simple_pb_t*)buf);
|
||||
counter_t *d = (counter_t *)spb->target;
|
||||
if (!counter.name[0] || strstr(d->name, counter.name)) {
|
||||
printf("'%s' = %u\n", d->name, d->count);
|
||||
|
89
test.c
Normal file
89
test.c
Normal file
@ -0,0 +1,89 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <sys/file.h>
|
||||
#include <sys/uio.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#define THREADCNT 1024
|
||||
|
||||
int pipefds[2*THREADCNT];
|
||||
|
||||
#define create_or_open(filename) (open(filename, O_CREAT|O_RDWR, 0600))
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
printf("run: ");
|
||||
for (int i = 0; i < argc; i++) {
|
||||
printf("%s ", argv[i]);
|
||||
}
|
||||
puts(".");
|
||||
if (argc == 1) {
|
||||
for (int i = 0; i < THREADCNT; i++) {
|
||||
if (pipe(&pipefds[i*2]) < 0) {
|
||||
perror("pipe");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
pid_t pid = fork();
|
||||
if (pid < 0) {
|
||||
perror("fork");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
char buf[5];
|
||||
sprintf(buf, "%04d", i);
|
||||
if (!pid) {
|
||||
printf("[%s] start@[%d,%d]=(%d,%d).\n", buf, i*2, i*2+1, pipefds[i*2], pipefds[i*2+1]);
|
||||
dup2(pipefds[i*2+1], STDOUT_FILENO);
|
||||
close(pipefds[i*2]);
|
||||
//printf("[%s] sub close@%d=%d, execl.\n", buf, i*2, pipefds[i*2]);
|
||||
execl("./test", "./test", buf, NULL);
|
||||
perror("execl");
|
||||
exit(EXIT_FAILURE); // a success execl will never return
|
||||
} else {
|
||||
close(pipefds[i*2+1]);
|
||||
//printf("[%s] main close@%d=%d, read.\n", buf, i*2+1, pipefds[i*2+1]);
|
||||
}
|
||||
}
|
||||
if (!fork()) {
|
||||
char buf[128]; ssize_t n;
|
||||
for (int i = 0; i < THREADCNT; i++) {
|
||||
do {
|
||||
n = read(pipefds[i*2], buf, sizeof(buf));
|
||||
if (n < 0) {
|
||||
perror("read");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (n > 0) {
|
||||
buf[n] = 0;
|
||||
printf("%zu> %s", n, buf);
|
||||
}
|
||||
} while (n > 0);
|
||||
}
|
||||
} else waitpid(-1, NULL, 0);
|
||||
return 0;
|
||||
}
|
||||
usleep(rand()%1000);
|
||||
int fd = create_or_open("testlog.txt");
|
||||
if (fd < 0) {
|
||||
perror("open");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if(flock(fd, LOCK_EX)) {
|
||||
perror("flock");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
FILE* fp = fdopen(fd, "rb+");
|
||||
fseek(fp, 0, SEEK_END);
|
||||
fputs(argv[1], fp);
|
||||
putc('\n', fp);
|
||||
srand(*(uint32_t*)argv[1]);
|
||||
int rndslp = rand()%1000;
|
||||
printf("[%s] get lock. sleep %d ms\n", argv[1], rndslp);
|
||||
usleep(rndslp);
|
||||
printf("[%s] release lock.\n", argv[1]);
|
||||
fclose(fp);
|
||||
//close(fd);
|
||||
return 0;
|
||||
}
|
21
test.sh
Executable file
21
test.sh
Executable file
@ -0,0 +1,21 @@
|
||||
#!/bin/bash
|
||||
|
||||
cd build
|
||||
./cmoe GET "name=fumiama®=fumiama"
|
||||
./cmoe GET "name=fumiam®=fumiama"
|
||||
./cmoe GET "name=fumia®=fumiama"
|
||||
./cmoe GET "name=fumi®=fumiama"
|
||||
./cmoe GET "name=fum®=fumiama"
|
||||
./cmoe GET "name=fu®=fumiama"
|
||||
./cmoe GET "name=f®=fumiama"
|
||||
|
||||
for ((i=0; i<$1; i++))
|
||||
do
|
||||
./cmoe GET "name=fumiama&theme=r34" &
|
||||
./cmoe GET "name=fumiam&theme=r34" &
|
||||
./cmoe GET "name=fumia&theme=r34" &
|
||||
./cmoe GET "name=fumi&theme=r34" &
|
||||
./cmoe GET "name=fum&theme=r34" &
|
||||
./cmoe GET "name=fu&theme=r34" &
|
||||
./cmoe GET "name=f&theme=r34" &
|
||||
done
|
Loading…
Reference in New Issue
Block a user