"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/sunlink.c" between
srm-1.2.14.tar.gz and srm-1.2.15.tar.gz

About: srm is a secure replacement for standard "rm" (to prevent command-line recovery it overwrites the data in the target files before unlinking them).

sunlink.c  (srm-1.2.14):sunlink.c  (srm-1.2.15)
skipping to change at line 78 skipping to change at line 78
#include <sys/extattr.h> #include <sys/extattr.h>
#include <libutil.h> #include <libutil.h>
#endif #endif
#include "srm.h" #include "srm.h"
#include "impl.h" #include "impl.h"
#ifndef O_SYNC #ifndef O_SYNC
#define O_SYNC 0 #define O_SYNC 0
#endif #endif
#ifndef _O_BINARY
#define _O_BINARY 0
#endif
#define KiB 1024 #define KiB 1024
#define MiB (KiB*KiB) #define MiB (KiB*KiB)
#define GiB (KiB*KiB*KiB) #define GiB (KiB*KiB*KiB)
#ifdef _MSC_VER
typedef long long my_off_t;
#else
typedef off_t my_off_t;
#endif
struct srm_target struct srm_target
{ {
int fd; int fd;
const char* file_name; const char* file_name;
#ifdef _MSC_VER my_off_t file_size;
long long file_size;
#else
off_t file_size;
#endif
unsigned char *buffer; unsigned char *buffer;
unsigned buffer_size; unsigned buffer_size;
int options; int options;
}; };
static volatile int SIGINT_received = 0; static volatile int SIGINT_received = 0;
#if defined(__unix__) #if defined(__unix__)
#include <signal.h> #include <signal.h>
#if defined(__linux__) && !defined(__USE_GNU) #if defined(__linux__) && !defined(__USE_GNU)
typedef __sighandler_t sighandler_t; typedef __sighandler_t sighandler_t;
skipping to change at line 190 skipping to change at line 195
/* we're not on a fs that supports this; fall back to plain fsync */ /* we're not on a fs that supports this; fall back to plain fsync */
fsync(fd); fsync(fd);
} }
#elif HAVE_FDATASYNC #elif HAVE_FDATASYNC
fdatasync(fd); fdatasync(fd);
#else #else
fsync(fd); fsync(fd);
#endif #endif
} }
#if defined(HAVE_ATTR_XATTR_H) || defined(HAVE_SYS_XATTR_H) || defined(HAVE_SYS_ EXTATTR_H)
static int extattr_overwrite(struct srm_target *srm, const int pass, const int a ttrnamespace) static int extattr_overwrite(struct srm_target *srm, const int pass, const int a ttrnamespace)
{ {
char *list = NULL; char *list = NULL;
unsigned char *value = NULL; unsigned char *value = NULL;
size_t list_size = 256, value_size = 0, key_len = 0; size_t list_size = 256, value_size = 0, key_len = 0;
ssize_t len = 0, i = 0; ssize_t len = 0, i = 0;
/* get list of atrributes */ /* get list of atrributes */
for(;;) { for(;;) {
list = alloca(list_size); list = alloca(list_size);
if (! list) { if (! list) {
skipping to change at line 234 skipping to change at line 240
len = extattr_list_fd(srm->fd, attrnamespace, list, list_size); len = extattr_list_fd(srm->fd, attrnamespace, list, list_size);
#endif #endif
if (len < 0) { if (len < 0) {
break; break;
} }
break; break;
} }
/* iterate list of attributes */ /* iterate list of attributes */
for(i = 0; i < len; i += key_len + 1) { for(i = 0; i < len; i += key_len + 1) {
int ret; int ret = 0;
ssize_t val_len; ssize_t val_len = 0;
char *key; char *key = NULL;
#if defined(HAVE_ATTR_XATTR_H) || defined(HAVE_SYS_XATTR_H) #if defined(HAVE_ATTR_XATTR_H) || defined(HAVE_SYS_XATTR_H)
key = list + i; key = list + i;
key_len = strlen(key); key_len = strlen(key);
#if defined(HAVE_ATTR_XATTR_H) #if defined(HAVE_ATTR_XATTR_H)
val_len = fgetxattr(srm->fd, key, NULL, 0); val_len = fgetxattr(srm->fd, key, NULL, 0);
#elif defined(HAVE_SYS_XATTR_H) && defined(__APPLE__) #elif defined(HAVE_SYS_XATTR_H) && defined(__APPLE__)
val_len = fgetxattr(srm->fd, key, NULL, 0, 0, 0); val_len = fgetxattr(srm->fd, key, NULL, 0, 0, 0);
#endif #endif
#elif defined(HAVE_SYS_EXTATTR_H) #elif defined(HAVE_SYS_EXTATTR_H)
char keybuf[257]; char keybuf[257];
key_len = *((unsigned char*)(list + i)); key_len = *((unsigned char*)(list + i));
memcpy(keybuf, list+i+1, key_len); memcpy(keybuf, list+i+1, key_len);
keybuf[key_len] = 0; keybuf[key_len] = 0;
key = keybuf; key = keybuf;
val_len = extattr_get_fd(srm->fd, attrnamespace, key, NULL, 0); val_len = extattr_get_fd(srm->fd, attrnamespace, key, NULL, 0);
#endif #endif
if ( ((srm->options & SRM_OPT_V) > 1) && pass == 1) { if ( ((srm->options & SRM_OPT_V) > 1) && pass == 1) {
char *namespace=""; char *name_space="";
#if defined(HAVE_SYS_EXTATTR_H) #if defined(HAVE_SYS_EXTATTR_H)
extattr_namespace_to_string(attrnamespace, &namespace); extattr_namespace_to_string(attrnamespace, &namespace);
#endif #endif
error("found extended attribute %s %s of %i bytes", namespace, key, (int)v al_len); error("found extended attribute %s %s of %i bytes", name_space, key, (int) val_len);
} }
if (val_len > (ssize_t)value_size) { if (val_len > (ssize_t)value_size) {
value_size = val_len; value_size = val_len;
value = alloca(value_size); value = alloca(value_size);
if (! value) { if (! value) {
errno = ENOMEM; errno = ENOMEM;
return -1; return -1;
} }
fill(value, value_size, srm->buffer, srm->buffer_size); fill(value, value_size, srm->buffer, srm->buffer_size);
} }
skipping to change at line 281 skipping to change at line 287
#elif defined(HAVE_SYS_XATTR_H) && defined(__APPLE__) #elif defined(HAVE_SYS_XATTR_H) && defined(__APPLE__)
ret = fsetxattr(srm->fd, key, value, val_len, 0, XATTR_REPLACE); ret = fsetxattr(srm->fd, key, value, val_len, 0, XATTR_REPLACE);
#elif defined(HAVE_SYS_EXTATTR_H) #elif defined(HAVE_SYS_EXTATTR_H)
ret = extattr_set_fd(srm->fd, attrnamespace, key, value, val_len); ret = extattr_set_fd(srm->fd, attrnamespace, key, value, val_len);
#endif #endif
if (ret < 0) { if (ret < 0) {
errorp("could not overwrite extended attribute %s", key); errorp("could not overwrite extended attribute %s", key);
} }
} }
#ifdef _MSC_VER
(void)attrnamespace; (void)attrnamespace;
#endif
return 0; return 0;
} }
#endif
static int overwrite(struct srm_target *srm, const int pass) static int overwrite(struct srm_target *srm, const int pass)
{ {
off_t i = 0; unsigned last_val = ~0u;
my_off_t i = 0;
ssize_t w; ssize_t w;
if(!srm) return -1; if(!srm) return -1;
if(!srm->buffer) return -1; if(!srm->buffer) return -1;
if(srm->buffer_size < 1) return -1; if(srm->buffer_size < 1) return -1;
/* check for extended attributes */ /* check for extended attributes */
#if defined(HAVE_ATTR_XATTR_H) || defined(HAVE_SYS_XATTR_H) #if defined(HAVE_ATTR_XATTR_H) || defined(HAVE_SYS_XATTR_H)
if (extattr_overwrite(srm, pass, 0) < 0) { if (extattr_overwrite(srm, pass, 0) < 0) {
return -1; return -1;
skipping to change at line 316 skipping to change at line 322
return -1; return -1;
} }
#endif #endif
if(lseek(srm->fd, 0, SEEK_SET) != 0) if(lseek(srm->fd, 0, SEEK_SET) != 0)
{ {
perror("could not seek"); perror("could not seek");
return -1; return -1;
} }
if(srm->file_size < (off_t)(srm->buffer_size)) if(srm->file_size < (my_off_t)(srm->buffer_size))
{ {
w=writen(srm->fd, srm->buffer, srm->file_size); w=writen(srm->fd, srm->buffer, srm->file_size);
if(w != srm->file_size) if(w != srm->file_size)
return -1; return -1;
} }
else else
{ {
while (i < srm->file_size - (off_t)srm->buffer_size) while (i < srm->file_size - (my_off_t)srm->buffer_size)
{ {
w=writen(srm->fd, srm->buffer, srm->buffer_size); w=writen(srm->fd, srm->buffer, srm->buffer_size);
if(w != (ssize_t)(srm->buffer_size)) if(w != (ssize_t)(srm->buffer_size))
return -1; return -1;
i += w; i += w;
if((srm->options & SRM_OPT_V) > 1 || SIGINT_received) if ((srm->options & SRM_OPT_V) > 1 || SIGINT_received) {
{ unsigned val = 0, file_size = 0;
if(srm->file_size < MiB) char c = '.';
printf("\rpass %i %uKiB/%uKiB ", pass, (unsigned)(i/KiB), (unsi if (srm->file_size < MiB) {
gned)(srm->file_size/KiB)); val = i / KiB;
else if(srm->file_size < GiB) file_size = (unsigned)(srm->file_size/KiB);
printf("\rpass %i %uMiB/%uMiB ", pass, (unsigned)(i/MiB), (unsi c = 'K';
gned)(srm->file_size/MiB)); } else if(srm->file_size < GiB) {
else val = i / MiB;
printf("\rpass %i %uGiB/%uGiB ", pass, (unsigned)(i/GiB), (unsi file_size = (unsigned)(srm->file_size/MiB);
gned)(srm->file_size/GiB)); c = 'M';
} else {
val = i / GiB;
file_size = (unsigned)(srm->file_size/GiB);
c = 'G';
}
if (val != last_val) {
printf("\rpass %i %u%ciB/%u%ciB ", pass, val, c, file_size,
c);
fflush(stdout);
last_val = val;
}
if(SIGINT_received) if(SIGINT_received)
{ {
if(srm->file_name) if(srm->file_name)
printf("%s\n", srm->file_name); printf("%s\n", srm->file_name);
else else
putchar('\n'); putchar('\n');
SIGINT_received=0; SIGINT_received=0;
fflush(stdout);
} }
fflush(stdout);
} }
} }
w=writen(srm->fd, srm->buffer, srm->file_size - i); w=writen(srm->fd, srm->buffer, srm->file_size - i);
if(w != srm->file_size-i) if(w != srm->file_size-i)
return -1; return -1;
} }
if((srm->options & SRM_OPT_V) > 1) if((srm->options & SRM_OPT_V) > 1)
{ {
printf("\rpass %i sync ", pass); printf("\rpass %i sync ", pass);
skipping to change at line 528 skipping to change at line 547
if(overwrite_byte(srm, 36, 0x00) < 0) return -1; if(overwrite_byte(srm, 36, 0x00) < 0) return -1;
} }
#if 0 #if 0
if((srm->options & SRM_OPT_V) > 1) if((srm->options & SRM_OPT_V) > 1)
printf("\n"); printf("\n");
#endif #endif
return 0; return 0;
} }
#ifdef _MSC_VER #ifdef _MSC_VER
static long long getFileSize(WCHAR *fn) static my_off_t getFileSize(WCHAR *fn)
{ {
/* it's a pain, but it seems that the only way to get the size of an alterna te data stream is to read it once. */ /* it's a pain, but it seems that the only way to get the size of an alterna te data stream is to read it once. */
#if 1 #if 1
long long size = 0, r; my_off_t size = 0, r;
int fd = _wopen(fn, O_RDONLY); int fd = _wopen(fn, O_RDONLY);
if (fd < 0) return 0; if (fd < 0) return 0;
do { do {
char buf[4096]; char buf[4096];
r = read(fd, buf, sizeof(buf)); r = read(fd, buf, sizeof(buf));
if (r > 0) size += r; if (r > 0) size += r;
} while(r > 0); } while(r > 0);
close(fd); close(fd);
return size; return size;
#else #else
skipping to change at line 576 skipping to change at line 595
ret = hfi.nNumberOfLinks; ret = hfi.nNumberOfLinks;
} }
CloseHandle(hFile); CloseHandle(hFile);
return ret; return ret;
} }
#endif #endif
int sunlink_impl(const char *path, const int options) int sunlink_impl(const char *path, const int options)
{ {
const int oflags = O_WRONLY|O_SYNC; const int oflags = O_WRONLY|O_SYNC|_O_BINARY;
struct srm_target srm; struct srm_target srm;
#if defined(_MSC_VER)
struct __stat64 statbuf;
#else
struct stat statbuf; struct stat statbuf;
#endif
#if defined(__unix__) || defined(__APPLE__) #if defined(__unix__) || defined(__APPLE__)
struct flock flock; struct flock flock;
#endif #endif
/* check function arguments */ /* check function arguments */
if(!path) return -1; if(!path) return -1;
memset(&srm, 0, sizeof(srm)); memset(&srm, 0, sizeof(srm));
srm.file_name = path; srm.file_name = path;
srm.options = options; srm.options = options;
/* check if path exists */ /* check if path exists */
if (lstat(path, &statbuf) < 0) #if defined(_MSC_VER)
if (_stat64(path, &statbuf) < 0) {
return -1;
}
#else
if (lstat(path, &statbuf) < 0) {
return -1; return -1;
}
#endif
srm.file_size = statbuf.st_size; srm.file_size = statbuf.st_size;
if (srm.file_size < 0) {
error("%s : file size: %lli, can not work with negative values", path, (long
long)srm.file_size);
return -1;
}
#ifdef _MSC_VER #ifdef _MSC_VER
srm.buffer_size = 4096; srm.buffer_size = 4096;
#else #else
srm.buffer_size = statbuf.st_blksize; srm.buffer_size = statbuf.st_blksize;
#endif #endif
if(srm.buffer_size < 16) if(srm.buffer_size < 16)
srm.buffer_size = 512; srm.buffer_size = 512;
if((srm.options & SRM_OPT_V) > 2) if((srm.options & SRM_OPT_V) > 2)
error("file size: %lli, buffer_size=%u", (long long)srm.file_size, srm.buffe r_size); error("file size: %lli, buffer_size=%u", (long long)srm.file_size, srm.buffe r_size);
skipping to change at line 618 skipping to change at line 652
int secsize=512; int secsize=512;
long blocks=0; long blocks=0;
uint64_t u=0, u_; uint64_t u=0, u_;
if( (srm.fd = open(srm.file_name, O_WRONLY)) < 0) if( (srm.fd = open(srm.file_name, O_WRONLY)) < 0)
return -1; return -1;
if(ioctl(srm.fd, BLKSSZGET, &secsize) < 0) if(ioctl(srm.fd, BLKSSZGET, &secsize) < 0)
{ {
perror("could not ioctl(BLKSSZGET)"); perror("could not ioctl(BLKSSZGET)");
return 1; return -1;
} }
if((options&SRM_OPT_V) > 2) if((options&SRM_OPT_V) > 2)
error("sector size %i bytes", secsize); error("sector size %i bytes", secsize);
if(ioctl(srm.fd, BLKGETSIZE, &blocks) < 0) if(ioctl(srm.fd, BLKGETSIZE, &blocks) < 0)
{ {
perror("could not ioctl(BLKGETSIZE)"); perror("could not ioctl(BLKGETSIZE)");
return 1; return -1;
} }
if((options&SRM_OPT_V) > 2) if((options&SRM_OPT_V) > 2)
error("BLKGETSIZE %i blocks", (int)blocks); error("BLKGETSIZE %i blocks", (int)blocks);
if(ioctl(srm.fd, BLKGETSIZE64, &u) < 0) if(ioctl(srm.fd, BLKGETSIZE64, &u) < 0)
{ {
perror("could not ioctl(BLKGETSIZE64)"); perror("could not ioctl(BLKGETSIZE64)");
return 1; return -1;
} }
if((options&SRM_OPT_V) > 2) if((options&SRM_OPT_V) > 2)
error("BLKGETSIZE64 %llu bytes", (unsigned long long)u); error("BLKGETSIZE64 %llu bytes", (unsigned long long)u);
u_=((uint64_t)blocks)*secsize; u_=((uint64_t)blocks)*secsize;
if(u_ != u) if(u_ != u)
error("!Warning! sectorsize*blocks:%llu != bytes:%llu", (long long unsi gned) u_, (long long unsigned) u); error("!Warning! sectorsize*blocks:%llu != bytes:%llu", (long long unsi gned) u_, (long long unsigned) u);
srm.file_size = u; srm.file_size = u;
srm.buffer_size = secsize; srm.buffer_size = secsize;
 End of changes. 27 change blocks. 
34 lines changed or deleted 67 lines changed or added

Home  |  About  |  All  |  Newest  |  Fossies Dox  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTPS