/************************************************************************* * * 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 #include #include #include 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<htcd()->docroot+TEMPDIR; time_t now=time(0); dbmap* db=GetDB(env); int n=0; while(nsize()) { 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" ); }