... | ... |
@@ -19,7 +19,7 @@ function request(querytext, func, errorfunc) { |
19 | 19 |
|
20 | 20 |
donewpost.onclick = function(e) { |
21 | 21 |
request("/newpost"+window.location.search+"&t="+encodeURIComponent(document.getElementById("wysihtml5-editor").value),function(r) { |
22 |
- window.location="/posts.html"+window.location.search+"&n=r";},function() {}); |
|
22 |
+ window.location="/posts.html"+window.location.search;},function() {}); |
|
23 | 23 |
}; |
24 | 24 |
|
25 | 25 |
}()); |
... | ... |
@@ -24,11 +24,11 @@ gotonewpost.onclick = function(e) { |
24 | 24 |
document.getElementById("posts").innerHTML='Loading posts...'; |
25 | 25 |
|
26 | 26 |
request("/lastpost"+window.location.search,function(r) { |
27 |
- request("/getpost"+window.location.search+"n="+r, function(r1) { |
|
27 |
+ request("/getpost"+window.location.search+"&n="+r, function(r1) { |
|
28 | 28 |
document.getElementById("posts").innerHTML=r1; |
29 | 29 |
},function() { |
30 | 30 |
document.getElementById("posts").innerHTML='Error getting posts'; |
31 |
- } |
|
31 |
+ }); |
|
32 | 32 |
},function() { |
33 | 33 |
document.getElementById("posts").innerHTML='There are no posts'; |
34 | 34 |
}); |
... | ... |
@@ -334,7 +334,7 @@ http_lastpost(wk *web, int connid, wk_uri *uri, void *userptr,char *user) |
334 | 334 |
char reply[64]; |
335 | 335 |
int n; |
336 | 336 |
kakumei *ka=(kakumei *)userptr; |
337 |
- if((n=post_last(ka))<=0) { |
|
337 |
+ if((n=post_last(ka))<0) { |
|
338 | 338 |
wk_serve_error(web,connid,wkerr_internal); |
339 | 339 |
log_write("LSTP","invalid postnum, send error"); |
340 | 340 |
return(wkact_finished); |
... | ... |
@@ -15,6 +15,7 @@ |
15 | 15 |
#include <sys/types.h> |
16 | 16 |
#include <sys/stat.h> |
17 | 17 |
#include <time.h> |
18 |
+#include "loglib.h" |
|
18 | 19 |
#include "kakumei.h" |
19 | 20 |
#include "kakumei_posts.h" |
20 | 21 |
|
... | ... |
@@ -29,8 +30,10 @@ post_new(kakumei *ka, char *user, char *text) |
29 | 30 |
char dirname[1024]; |
30 | 31 |
FILE *f; |
31 | 32 |
struct stat st; |
32 |
- if(ka==NULL || user==NULL || user[0]=='\0' || text==NULL || text[0]=='\0') |
|
33 |
+ if(ka==NULL || user==NULL || user[0]=='\0' || text==NULL || text[0]=='\0') { |
|
34 |
+ log_write("POST","sanity check error\n"); |
|
33 | 35 |
return(-1); |
36 |
+ } |
|
34 | 37 |
last=post_last(ka); |
35 | 38 |
filename[0]='\0'; |
36 | 39 |
for(num=last+1;num<MAXPOSTS;num++) { |
... | ... |
@@ -39,8 +42,10 @@ post_new(kakumei *ka, char *user, char *text) |
39 | 42 |
if(stat(filename,&st)!=0) |
40 | 43 |
break; /* this one is ok: file doesn't exist */ |
41 | 44 |
} |
42 |
- if(filename[0]=='\0' || num>=MAXPOSTS) |
|
45 |
+ if(filename[0]=='\0' || num>=MAXPOSTS) { |
|
46 |
+ log_write("POST","couldn't generate filename or too many posts\n"); |
|
43 | 47 |
return(-1); |
48 |
+ } |
|
44 | 49 |
/* create dir structure */ |
45 | 50 |
mkdir(DATADIR,0700); |
46 | 51 |
mkdir(POSTSDIR,0700); |
... | ... |
@@ -48,12 +53,13 @@ post_new(kakumei *ka, char *user, char *text) |
48 | 53 |
dirname[sizeof(dirname)-1]='\0'; |
49 | 54 |
mkdir(dirname,0700); |
50 | 55 |
/* save the new post */ |
51 |
- if((f=fopen(filename,"w"))==NULL) |
|
56 |
+ if((f=fopen(filename,"w"))==NULL) { |
|
57 |
+ log_write("POST","couldn't save post\n"); |
|
52 | 58 |
return(-1); |
59 |
+ } |
|
53 | 60 |
fprintf(f,"{\n \"author\":\"%s\",\n",user); |
54 | 61 |
fprintf(f," \"date\":%li,\n",(long)time(NULL)); |
55 | 62 |
fprintf(f," \"text\":\"%s\",\n",text); |
56 |
- fprintf(f," \"text\":\"%s\",\n",text); |
|
57 | 63 |
fprintf(f," \"comments\":[\n]}\n"); |
58 | 64 |
fclose(f),f=NULL; |
59 | 65 |
/* save the "last" mark */ |
... | ... |
@@ -74,8 +80,10 @@ post_last(kakumei *ka) |
74 | 80 |
int fd; |
75 | 81 |
snprintf(filename,sizeof(filename)-1,"%s/%s",POSTSDIR,"last"); |
76 | 82 |
filename[sizeof(filename)-1]='\0'; |
77 |
- if((fd=open(filename,O_RDONLY))==-1) |
|
83 |
+ if((fd=open(filename,O_RDONLY))==-1) { |
|
84 |
+ log_write("POST","couldn't get last post number\n"); |
|
78 | 85 |
return(-1); |
86 |
+ } |
|
79 | 87 |
memset(readbuf,0,sizeof(readbuf)); |
80 | 88 |
read(fd,readbuf,sizeof(readbuf)-1); |
81 | 89 |
close(fd),fd=-1; |
... | ... |
@@ -86,12 +94,16 @@ int |
86 | 94 |
post_get(kakumei *ka, int num, char *filename, int filenamesize) |
87 | 95 |
{ |
88 | 96 |
struct stat st; |
89 |
- if(ka==NULL || num<0 || filename==NULL || filenamesize<=0) |
|
97 |
+ if(ka==NULL || num<0 || filename==NULL || filenamesize<=0) { |
|
98 |
+ log_write("POST","Sanity check error\n"); |
|
90 | 99 |
return(-1); |
100 |
+ } |
|
91 | 101 |
snprintf(filename,filenamesize,"%s/%04i/%04i.json",POSTSDIR,num/10000,num%10000); |
92 | 102 |
filename[filenamesize-1]='\0'; |
93 |
- if(stat(filename,&st)!=0) |
|
103 |
+ if(stat(filename,&st)!=0) { |
|
104 |
+ log_write("POST","Post file doesn't exist\n"); |
|
94 | 105 |
return(-1); /* file doesn't exist */ |
106 |
+ } |
|
95 | 107 |
return(0); |
96 | 108 |
} |
97 | 109 |
|
... | ... |
@@ -101,15 +113,20 @@ post_addcomment(kakumei *ka, int postnum, char *user, char *text) |
101 | 113 |
char filename[1024]; |
102 | 114 |
char buf[16]; |
103 | 115 |
FILE *f; |
104 |
- if(ka==NULL || postnum<0 || user==NULL || text==NULL) |
|
116 |
+ if(ka==NULL || postnum<0 || user==NULL || text==NULL) { |
|
117 |
+ log_write("POST","Sanity check error\n"); |
|
105 | 118 |
return(-1); |
106 |
- if(post_get(ka,postnum,filename,sizeof(filename))!=0) |
|
119 |
+ } |
|
120 |
+ if(post_get(ka,postnum,filename,sizeof(filename))!=0) |
|
107 | 121 |
return(-1); |
108 |
- if((f=fopen(filename,"r+"))==NULL) |
|
122 |
+ if((f=fopen(filename,"r+"))==NULL) { |
|
123 |
+ log_write("POST","Couldn't open post file fot appending\n"); |
|
109 | 124 |
return(-1); |
125 |
+ } |
|
110 | 126 |
fseek(f,-2,SEEK_END); |
111 | 127 |
if(fread(buf,1,2,f)!=2 || buf[0]!=']' || buf[1]!='}') { |
112 | 128 |
fclose(f),f=NULL; |
129 |
+ log_write("POST","Post file contents are corrupted\n"); |
|
113 | 130 |
return(-1); |
114 | 131 |
} |
115 | 132 |
fseek(f,-2,SEEK_END); |