From 403ae7b98805d29da82152f8874d2d0936ba0fe0 Mon Sep 17 00:00:00 2001 From: cristy Date: Thu, 18 Apr 2013 12:49:11 +0000 Subject: [PATCH] --- MagickCore/utility.c | 94 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 90 insertions(+), 4 deletions(-) diff --git a/MagickCore/utility.c b/MagickCore/utility.c index 50155e471..6dedc6013 100644 --- a/MagickCore/utility.c +++ b/MagickCore/utility.c @@ -53,6 +53,8 @@ #include "MagickCore/nt-base-private.h" #include "MagickCore/option.h" #include "MagickCore/policy.h" +#include "MagickCore/random_.h" +#include "MagickCore/registry.h" #include "MagickCore/resource_.h" #include "MagickCore/semaphore.h" #include "MagickCore/signature-private.h" @@ -1761,9 +1763,10 @@ MagickExport size_t MultilineCensus(const char *label) % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % -% ShredFile() first overwrites the specified file with random data and then -% removed it. The overwrite is optional and is only required to help keep -% the contents of the file private. +% ShredFile() overwrites the specified file with zeros or random data and then +% removes it. The overwrite is optional and is only required to help keep +% the contents of the file private. The first, the file is zeroed. For +% other passes, random data is written. % % The format of the ShredFile method is: % @@ -1776,15 +1779,98 @@ MagickExport size_t MultilineCensus(const char *label) */ MagickPrivate MagickBooleanType ShredFile(const char *path) { + char + *iterations; + + ExceptionInfo + *exception; + int + file, status; + MagickSizeType + length; + + register ssize_t + i; + + size_t + quantum; + + struct stat + file_stats; + if ((path == (const char *) NULL) || (*path == '\0')) return(MagickFalse); + exception=AcquireExceptionInfo(); + iterations=(char *) GetImageRegistry(StringRegistryType,"temporary-path", + exception); + exception=DestroyExceptionInfo(exception); + if (iterations == (char *) NULL) + iterations=GetEnvironmentValue("MAGICK_SHRED_FILE"); + if (iterations == (char *) NULL) + { + /* + Don't shred the file, just remove it. + */ + status=remove_utf8(path); + if (status == -1) + return(MagickFalse); + return(MagickTrue); + } + file=open_utf8(path,O_WRONLY | O_EXCL | O_BINARY,S_MODE); + if (file == -1) + return(MagickFalse); + /* + Shred the file. + */ + quantum=(size_t) MagickMaxBufferExtent; + if ((fstat(file,&file_stats) == 0) && (file_stats.st_size != 0)) + quantum=(size_t) MagickMin((MagickSizeType) file_stats.st_size, + MagickMaxBufferExtent); + length=(MagickSizeType) file_stats.st_size; + for (i=0; i < StringToInteger(iterations); i++) + { + RandomInfo + *random_info; + + register ssize_t + j; + + ssize_t + count; + + if (lseek(file,0,SEEK_SET) < 0) + break; + random_info=AcquireRandomInfo(); + for (j=0; j < length; j+=count) + { + StringInfo + *key; + + key=GetRandomKey(random_info,quantum); + if (j == 0) + ResetStringInfo(key); /* zero on first pass */ + count=write(file,GetStringInfoDatum(key),(size_t) + MagickMin(quantum,length-j)); + key=DestroyStringInfo(key); + if (count <= 0) + { + count=0; + if (errno != EINTR) + break; + } + } + random_info=DestroyRandomInfo(random_info); + if (j < length) + break; + } + status=close(file); status=remove_utf8(path); if (status == -1) return(MagickFalse); - return(MagickTrue); + return(i < StringToInteger(iterations) ? MagickFalse : MagickTrue); } /* -- 2.40.0