summaryrefslogtreecommitdiff
path: root/src/serv/tempmap.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/serv/tempmap.cc')
-rw-r--r--src/serv/tempmap.cc82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/serv/tempmap.cc b/src/serv/tempmap.cc
new file mode 100644
index 0000000..43b8732
--- /dev/null
+++ b/src/serv/tempmap.cc
@@ -0,0 +1,82 @@
1/*************************************************************************
2 *
3 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <serv/htcd.h>
21#include <unistd.h>
22#include <fcntl.h>
23#include <sys/stat.h>
24
25const mstring TEMPDIR="/tmp/";
26
27inline dbmap* GetDB(Env& env)
28{
29 dbref_t* p=(dbref_t*)env.toks->get(TEMP_tag);
30 if(!env.is_clear(p)||!p->is_dbref()) THROW("htc: could not find tempfile db");
31 p->db.remap();
32 return &p->db;
33}
34
35TOK_IMPL(tempfile)
36{
37 mstring path=Reqd(env)->htcd()->docroot+TEMPDIR;
38 mstring file=Random(8),ext=env[1];
39 int fd;
40 while((fd=open((path+file+ext).c_str(),O_CREAT|O_EXCL|O_WRONLY|O_TRUNC,0600))==-1)
41 file=Random(8);
42 close(fd);
43 tempmap_t temp;
44 temp.name()=file+ext;
45 temp.start()=itoa(time(0));
46 temp.stop()=itoa(time(0)+atoi(env[0]));
47 temp.refer()=Reqd(env)->user.uid();
48 dbmap* db=GetDB(env);
49 if(db->empty()) temp.id()=itoa(1);
50 else temp.id()=itoa(atoi(db->at(db->size()-1,temp_id))+1);
51 int row=db->Insert(temp.list,1);
52 if(row<0) THROW("htc: could not add tempfile - record not unique");
53 out<<temp.name();
54}
55
56TOK_IMPL(tempfile_update)
57{
58 mstring path=Reqd(env)->htcd()->docroot+TEMPDIR;
59 time_t now=time(0);
60 dbmap* db=GetDB(env);
61 int n=0;
62 while(n<db->size()) {
63 if(now>atoi(db->at(n,temp_stop))) {
64 remove((path+db->at(n,temp_name)).c_str());
65 db->Rem(n);
66 }
67 else n++;
68 }
69}
70
71void envAddTemp(tokmap& T)
72{
73 TOK_ADD(tempfile,"xx",tok_t::SYSONLY,
74 "%Create a temporary file"
75 "%Syntax: \\tempfile{duration}{.ext}\n"
76 );
77 TOK_ADD(tempfile_update,"",tok_t::SYSONLY,
78 "%Update tempfile database\n"
79 "%Syntax: \\tempfile_update\n"
80 );
81}
82