}
}
-off_t filesize(char *filename) {
- struct stat s;
-
- if (stat(filename, &s) != 0) return -1;
-
- return s.st_size;
-}
-
dev_t getdevice(char *filename) {
struct stat s;
return s.st_ino;
}
-time_t getmtime(char *filename) {
- struct stat s;
-
- if (stat(filename, &s) != 0) return 0;
-
- return s.st_mtime;
-}
-
-time_t getctime(char *filename) {
- struct stat s;
-
- if (stat(filename, &s) != 0) return 0;
-
- return s.st_ctime;
-}
-
-char *fmtmtime(char *filename) {
+char *fmttime(time_t t) {
static char buf[64];
- time_t t = getmtime(filename);
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M", localtime(&t));
+
return buf;
}
return x;
}
-void getfilestats(file_t *file)
+void getfilestats(file_t *file, struct stat *info, struct stat *linfo)
{
- file->size = filesize(file->d_name);
- file->inode = getinode(file->d_name);
- file->device = getdevice(file->d_name);
-
- switch (ordertype)
- {
- case ORDER_CTIME:
- file->sorttime = getctime(file->d_name);
- break;
- case ORDER_MTIME:
- default:
- file->sorttime = getmtime(file->d_name);
- break;
- }
+ file->size = info->st_size;;
+ file->inode = info->st_ino;
+ file->device = info->st_dev;
+ file->ctime = info->st_ctime;
+ file->mtime = info->st_mtime;
}
int grokdir(char *dir, file_t **filelistp, struct stat *logfile_status)
static int progress = 0;
static char indicator[] = "-\\|/";
char *fullname, *name;
- off_t size;
cd = opendir(dir);
continue;
}
- size = filesize(newfile->d_name);
- if (!S_ISDIR(info.st_mode) && (((size == 0 && ISFLAG(flags, F_EXCLUDEEMPTY)) || size < minsize || (size > maxsize && maxsize != -1)))) {
+ if (!S_ISDIR(info.st_mode) && (((info.st_size == 0 && ISFLAG(flags, F_EXCLUDEEMPTY)) || info.st_size < minsize || (info.st_size > maxsize && maxsize != -1)))) {
free(newfile->d_name);
free(newfile);
continue;
free(newfile);
} else {
if (S_ISREG(linfo.st_mode) || (S_ISLNK(linfo.st_mode) && ISFLAG(flags, F_FOLLOWLINKS))) {
- getfilestats(newfile);
+ getfilestats(newfile, &info, &linfo);
*filelistp = newfile;
filecount++;
} else {
return filecount;
}
-md5_byte_t *getcrcsignatureuntil(char *filename, off_t max_read)
+md5_byte_t *getcrcsignatureuntil(char *filename, off_t fsize, off_t max_read)
{
- off_t fsize;
off_t toread;
md5_state_t state;
static md5_byte_t digest[MD5_DIGEST_LENGTH];
FILE *file;
md5_init(&state);
-
-
- fsize = filesize(filename);
if (max_read != 0 && fsize > max_read)
fsize = max_read;
return digest;
}
-md5_byte_t *getcrcsignature(char *filename)
+md5_byte_t *getcrcsignature(char *filename, off_t fsize)
{
- return getcrcsignatureuntil(filename, 0);
+ return getcrcsignatureuntil(filename, fsize, 0);
}
-md5_byte_t *getcrcpartialsignature(char *filename)
+md5_byte_t *getcrcpartialsignature(char *filename, off_t fsize)
{
- return getcrcsignatureuntil(filename, PARTIAL_MD5_SIZE);
+ return getcrcsignatureuntil(filename, fsize, PARTIAL_MD5_SIZE);
}
int md5cmp(const md5_byte_t *a, const md5_byte_t *b)
{
int cmpresult;
md5_byte_t *crcsignature;
- off_t fsize;
if (ISFLAG(flags, F_CONSIDERHARDLINKS))
{
if (is_hardlink(checktree, file))
return NULL;
}
-
- fsize = filesize(file->d_name);
- if (fsize < checktree->file->size)
+ if (file->size < checktree->file->size)
cmpresult = -1;
else
- if (fsize > checktree->file->size) cmpresult = 1;
+ if (file->size > checktree->file->size) cmpresult = 1;
else
if (ISFLAG(flags, F_PERMISSIONS) &&
!same_permissions(file->d_name, checktree->file->d_name))
cmpresult = -1;
else {
if (checktree->file->crcpartial == NULL) {
- crcsignature = getcrcpartialsignature(checktree->file->d_name);
+ crcsignature = getcrcpartialsignature(checktree->file->d_name, checktree->file->size);
if (crcsignature == NULL) {
errormsg ("cannot read file %s\n", checktree->file->d_name);
return NULL;
}
if (file->crcpartial == NULL) {
- crcsignature = getcrcpartialsignature(file->d_name);
+ crcsignature = getcrcpartialsignature(file->d_name, file->size);
if (crcsignature == NULL) {
errormsg ("cannot read file %s\n", file->d_name);
return NULL;
if (cmpresult == 0) {
if (checktree->file->crcsignature == NULL) {
- crcsignature = getcrcsignature(checktree->file->d_name);
+ crcsignature = getcrcsignature(checktree->file->d_name, checktree->file->size);
if (crcsignature == NULL) return NULL;
checktree->file->crcsignature = (md5_byte_t*) malloc(MD5_DIGEST_LENGTH * sizeof(md5_byte_t));
}
if (file->crcsignature == NULL) {
- crcsignature = getcrcsignature(file->d_name);
+ crcsignature = getcrcsignature(file->d_name, file->size);
if (crcsignature == NULL) return NULL;
file->crcsignature = (md5_byte_t*) malloc(MD5_DIGEST_LENGTH * sizeof(md5_byte_t));
if (ISFLAG(flags, F_SHOWSIZE)) printf("%lld byte%seach:\n", (long long int)files->size,
(files->size != 1) ? "s " : " ");
if (ISFLAG(flags, F_SHOWTIME))
- printf("%s ", fmtmtime(files->d_name));
+ printf("%s ", fmttime(files->mtime));
if (ISFLAG(flags, F_DSAMELINE)) escapefilename("\\ ", &files->d_name);
printf("%s%c", files->d_name, ISFLAG(flags, F_DSAMELINE)?' ':'\n');
}
tmpfile = files->duplicates;
while (tmpfile != NULL) {
if (ISFLAG(flags, F_SHOWTIME))
- printf("%s ", fmtmtime(tmpfile->d_name));
+ printf("%s ", fmttime(tmpfile->mtime));
if (ISFLAG(flags, F_DSAMELINE)) escapefilename("\\ ", &tmpfile->d_name);
printf("%s%c", tmpfile->d_name, ISFLAG(flags, F_DSAMELINE)?' ':'\n');
tmpfile = tmpfile->duplicates;
if (prompt)
{
if (ISFLAG(flags, F_SHOWTIME))
- printf("[%d] [%s] %s\n", counter, fmtmtime(files->d_name), files->d_name);
+ printf("[%d] [%s] %s\n", counter, fmttime(files->mtime), files->d_name);
else
printf("[%d] %s\n", counter, files->d_name);
}
if (prompt)
{
if (ISFLAG(flags, F_SHOWTIME))
- printf("[%d] [%s] %s\n", counter, fmtmtime(tmpfile->d_name), tmpfile->d_name);
+ printf("[%d] [%s] %s\n", counter, fmttime(tmpfile->mtime), tmpfile->d_name);
else
printf("[%d] %s\n", counter, tmpfile->d_name);
}
return !ISFLAG(flags, F_REVERSE) ? -1 : 1;
}
-int sort_pairs_by_time(file_t *f1, file_t *f2)
+int sort_pairs_by_ctime(file_t *f1, file_t *f2)
{
- if (f1->sorttime < f2->sorttime)
+ if (f1->ctime < f2->ctime)
return !ISFLAG(flags, F_REVERSE) ? -1 : 1;
- else if (f1->sorttime > f2->sorttime)
+ else if (f1->ctime > f2->ctime)
return !ISFLAG(flags, F_REVERSE) ? 1 : -1;
return 0;
}
+int sort_pairs_by_mtime(file_t *f1, file_t *f2)
+{
+ if (f1->mtime < f2->mtime)
+ return !ISFLAG(flags, F_REVERSE) ? -1 : 1;
+ else if (f1->mtime > f2->mtime)
+ return !ISFLAG(flags, F_REVERSE) ? 1 : -1;
+ else
+ return sort_pairs_by_ctime(f1, f2);
+}
+
int sort_pairs_by_filename(file_t *f1, file_t *f2)
{
int strvalue = strcmp(f1->d_name, f2->d_name);
if (confirmmatch(file1, file2)) {
if (ISFLAG(flags, F_DELETEFILES) && ISFLAG(flags, F_IMMEDIATE))
deletesuccessor(match, curfile,
- (ordertype == ORDER_MTIME ||
- ordertype == ORDER_CTIME) ? sort_pairs_by_time : sort_pairs_by_filename, loginfo );
+ ordertype == ORDER_MTIME ? sort_pairs_by_mtime :
+ ordertype == ORDER_CTIME ? sort_pairs_by_ctime :
+ sort_pairs_by_filename, loginfo );
else
registerpair(match, curfile,
- (ordertype == ORDER_MTIME ||
- ordertype == ORDER_CTIME) ? sort_pairs_by_time : sort_pairs_by_filename );
+ ordertype == ORDER_MTIME ? sort_pairs_by_mtime :
+ ordertype == ORDER_CTIME ? sort_pairs_by_ctime :
+ sort_pairs_by_filename );
}
fclose(file1);