Line data Source code
1 : #include "../../burp.h"
2 : #include "../../alloc.h"
3 : #include "../../conf.h"
4 : #include "../../fsops.h"
5 : #include "../../prepend.h"
6 : #include "../sdirs.h"
7 :
8 0 : int deleteme_move(struct sdirs *sdirs, const char *fullpath, const char *path,
9 : struct conf **cconfs)
10 : {
11 0 : int ret=-1;
12 0 : char *tmp=NULL;
13 0 : char *dest=NULL;
14 0 : int attempts=0;
15 : struct stat statp;
16 0 : char suffix[16]="";
17 0 : char *timestamp=NULL;
18 :
19 0 : if(lstat(fullpath, &statp) && errno==ENOENT)
20 : {
21 : // The path to move aside does not exist.
22 : // Treat this as OK.
23 0 : ret=0;
24 0 : goto end;
25 : }
26 :
27 0 : if(!(tmp=prepend_s(sdirs->deleteme, path))
28 0 : || mkpath(&tmp, sdirs->deleteme)
29 0 : || !(dest=prepend("", tmp)))
30 0 : goto end;
31 :
32 : // Try to generate destination paths if the desired one is already
33 : // taken.
34 : while(1)
35 : {
36 0 : if(lstat(dest, &statp)) break;
37 0 : snprintf(suffix, sizeof(suffix), ".%d", ++attempts);
38 0 : free_w(&dest);
39 0 : if(!(dest=prepend(tmp, suffix)))
40 0 : goto end;
41 0 : if(attempts>=100) break; // Give up.
42 : }
43 :
44 : // Paranoia - really do not want the deleteme directory to be loaded
45 : // as if it were a normal storage directory, so remove the timestamp.
46 0 : if(!(timestamp=prepend_s(fullpath, "timestamp")))
47 0 : goto end;
48 0 : unlink(timestamp);
49 :
50 : // Possible race condition is of no consequence, as the destination
51 : // will need to be deleted at some point anyway.
52 0 : ret=do_rename(fullpath, dest);
53 :
54 : end:
55 0 : free_w(&dest);
56 0 : free_w(&tmp);
57 0 : free_w(×tamp);
58 0 : return ret;
59 : }
60 :
61 0 : int deleteme_maybe_delete(struct conf **cconfs, struct sdirs *sdirs)
62 : {
63 : // If manual_delete is on, they will have to delete the files
64 : // manually, via a cron job or something.
65 0 : if(get_string(cconfs[OPT_MANUAL_DELETE])) return 0;
66 0 : return recursive_delete(sdirs->deleteme);
67 : }
|