summaryrefslogtreecommitdiff
path: root/src/serv/tempmap.cc
blob: 43b8732317bb0fb7f89388de5e7ba24d4d92a3d0 (plain)
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
/*************************************************************************
 *
 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <serv/htcd.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>

const mstring TEMPDIR="/tmp/";

inline dbmap* GetDB(Env& env)
{
  dbref_t* p=(dbref_t*)env.toks->get(TEMP_tag);
  if(!env.is_clear(p)||!p->is_dbref()) THROW("htc: could not find tempfile db");
  p->db.remap();
  return &p->db;
}

TOK_IMPL(tempfile)
{
  mstring path=Reqd(env)->htcd()->docroot+TEMPDIR;
  mstring file=Random(8),ext=env[1];
  int fd;
  while((fd=open((path+file+ext).c_str(),O_CREAT|O_EXCL|O_WRONLY|O_TRUNC,0600))==-1)
    file=Random(8);
  close(fd);
  tempmap_t temp;
  temp.name()=file+ext;
  temp.start()=itoa(time(0));
  temp.stop()=itoa(time(0)+atoi(env[0]));
  temp.refer()=Reqd(env)->user.uid();
  dbmap* db=GetDB(env);
  if(db->empty()) temp.id()=itoa(1);
  else temp.id()=itoa(atoi(db->at(db->size()-1,temp_id))+1);
  int row=db->Insert(temp.list,1);
  if(row<0) THROW("htc: could not add tempfile - record not unique");
  out<<temp.name();
}

TOK_IMPL(tempfile_update)
{
  mstring path=Reqd(env)->htcd()->docroot+TEMPDIR;
  time_t now=time(0);
  dbmap* db=GetDB(env);
  int n=0;
  while(n<db->size()) {
    if(now>atoi(db->at(n,temp_stop))) {
      remove((path+db->at(n,temp_name)).c_str());
      db->Rem(n);
    }
    else n++;
  }
}

void envAddTemp(tokmap& T)
{
  TOK_ADD(tempfile,"xx",tok_t::SYSONLY,
          "%Create a temporary file"
          "%Syntax: \\tempfile{duration}{.ext}\n"
          );
  TOK_ADD(tempfile_update,"",tok_t::SYSONLY,
          "%Update tempfile database\n"
          "%Syntax: \\tempfile_update\n"
          );
}