]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/removefile.c
4 * "Secure" file removal function for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * "LICENSE" which should have been included with this file. If this
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * cupsdRemoveFile() - Remove a file using the 7-pass US DoD method.
18 * overwrite_data() - Overwrite the data in a file.
22 * Include necessary headers...
26 #ifdef HAVE_REMOVEFILE
27 # include <removefile.h>
29 static int overwrite_data(int fd
, const char *buffer
, int bufsize
,
31 #endif /* HAVE_REMOVEFILE */
35 * 'cupsdRemoveFile()' - Remove a file using the 7-pass US DoD method.
38 int /* O - 0 on success, -1 on error */
39 cupsdRemoveFile(const char *filename
) /* I - File to remove */
41 #ifdef HAVE_REMOVEFILE
42 int ret
; /* Return value */
43 removefile_state_t s
; /* Remove state variable */
46 s
= removefile_state_alloc();
47 ret
= removefile(filename
, s
, REMOVEFILE_SECURE_7_PASS
);
49 removefile_state_free(s
);
54 int fd
; /* File descriptor */
55 struct stat info
; /* File information */
56 char buffer
[512]; /* Data buffer */
57 int i
; /* Looping var */
61 * First open the file for writing in exclusive mode.
64 if ((fd
= open(filename
, O_WRONLY
| O_EXCL
)) < 0)
68 * Delete the file now - it will still be around as long as the file is
75 * Then get the file size...
85 * Overwrite the file 7 times with 0xF6, 0x00, 0xFF, random, 0x00, 0xFF,
86 * and more random data.
89 memset(buffer
, 0xF6, sizeof(buffer
));
90 if (overwrite_data(fd
, buffer
, sizeof(buffer
), (int)info
.st_size
))
96 memset(buffer
, 0x00, sizeof(buffer
));
97 if (overwrite_data(fd
, buffer
, sizeof(buffer
), (int)info
.st_size
))
103 memset(buffer
, 0xFF, sizeof(buffer
));
104 if (overwrite_data(fd
, buffer
, sizeof(buffer
), (int)info
.st_size
))
110 for (i
= 0; i
< sizeof(buffer
); i
++)
112 if (overwrite_data(fd
, buffer
, sizeof(buffer
), (int)info
.st_size
))
118 memset(buffer
, 0x00, sizeof(buffer
));
119 if (overwrite_data(fd
, buffer
, sizeof(buffer
), (int)info
.st_size
))
125 memset(buffer
, 0xFF, sizeof(buffer
));
126 if (overwrite_data(fd
, buffer
, sizeof(buffer
), (int)info
.st_size
))
132 for (i
= 0; i
< sizeof(buffer
); i
++)
134 if (overwrite_data(fd
, buffer
, sizeof(buffer
), (int)info
.st_size
))
141 * Whew! Close the file (which will lead to the actual deletion) and
147 #endif /* HAVE_REMOVEFILE */
151 #ifndef HAVE_REMOVEFILE
153 * 'overwrite_data()' - Overwrite the data in a file.
156 static int /* O - 0 on success, -1 on error */
157 overwrite_data(int fd
, /* I - File descriptor */
158 const char *buffer
, /* I - Buffer to write */
159 int bufsize
, /* I - Size of buffer */
160 int filesize
) /* I - Size of file */
162 int bytes
; /* Bytes to write/written */
166 * Start at the beginning of the file...
169 if (lseek(fd
, 0, SEEK_SET
) < 0)
173 * Fill the file with the provided data...
178 if (filesize
> bufsize
)
183 if ((bytes
= write(fd
, buffer
, bytes
)) < 0)
190 * Force the changes to disk...
195 #endif /* HAVE_REMOVEFILE */
199 # define testmain main
206 fp
= fopen("testfile.secure", "w");
207 fputs("Hello, World!\n", fp
);
208 fputs("Now is the time for all good men to come to the aid of their "
212 if (cupsdRemoveFile("testfile.secure"))
214 printf("cupsdRemoveFile: FAIL (%s)\n", strerror(errno
));
219 puts("cupsdRemoveFile: PASS");