Remove {Dputs, Dprintf, Dperror} and convert callers to ntfs_log_*. Make ntfsinfo use ntfs_logging_parse_option().

edge.strict_endians
uvman 2005-10-25 01:28:10 +00:00
parent f9cdaed3db
commit 0dd0cdc6be
9 changed files with 101 additions and 227 deletions

View File

@ -26,52 +26,10 @@
#include "config.h"
#endif
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#include "logging.h"
struct _runlist_element;
extern void __Sprintf(const int silent, const char *fmt, ...)
__attribute__((format(printf, 2, 3)));
#define Sprintf(silent, f, a...) __Sprintf(silent, f, ##a)
#ifdef DEBUG
/* Debug output to stderr. To get it run ./configure --enable-debug. */
extern void __ntfs_debug(const char *file, int line, const char *function,
const char *format, ...) __attribute__((format(printf, 4, 5)));
#define ntfs_debug(f, a...) \
__ntfs_debug(__FILE__, __LINE__, __FUNCTION__, f, ##a)
extern void __ntfs_error(const char *function,
const char *fmt, ...) __attribute__((format(printf, 2, 3)));
#define ntfs_error(sb, f, a...) __ntfs_error(__FUNCTION__, f, ##a)
extern void __Dprintf(const char *fmt, ...)
__attribute__((format(printf, 1, 2)));
#define Dprintf(f, a...) __Dprintf(f, ##a)
extern void __Dputs(const char *s);
#define Dputs(s) __Dputs(s)
extern void __Dperror(const char *s);
#define Dperror(s) __Dperror(s)
#else /* if !DEBUG */
#define ntfs_debug(f, a...) do {} while (0)
#define ntfs_error(f, a...) do {} while (0)
#define Dprintf(f, a...) do {} while (0)
#define Dputs(s) do {} while (0)
#define Dperror(s) do {} while (0)
#endif /* !DEBUG */
#ifdef NTFS_DISABLE_DEBUG_LOGGING
static __inline__ void ntfs_debug_runlist_dump(const struct _runlist_element *rl __attribute__((unused))) {}
#else

View File

@ -32,119 +32,6 @@
#include "debug.h"
#include "logging.h"
/**
* Sprintf - silencable output to stderr
* @silent: if 0 string is output to stderr
* @fmt: printf style format string
* @...: optional arguments for the printf style format string
*
* If @silent is 0, output the string @fmt to stderr.
*
* This is basically a replacement for:
*
* if (!silent)
* fprintf(stderr, fmt, ...);
*
* It is more convenient to use Sprintf instead of the above code and perhaps
* more importantly, Sprintf makes it much easier to turn it into a "do
* nothing" function, by defining it to "do {} while (0)" in debug.h instead of
* to * __Sprintf, thus removing the whole output completely.
*/
void __Sprintf(const int silent, const char *fmt, ...)
{
int eo;
va_list ap;
if (silent)
return;
eo = errno;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
errno = eo;
}
#ifdef DEBUG
/* Debug output to stderr. To get it run ./configure --enable-debug. */
/**
* __ntfs_error
*/
void __ntfs_error(const char *function, const char *fmt, ...)
{
int eo = errno;
int flen = 0;
va_list args;
char err_buf[1024];
if (function)
flen = strlen(function);
va_start(args, fmt);
vsnprintf(err_buf, sizeof(err_buf), fmt, args);
va_end(args);
fprintf(stderr, "NTFS error: %s(): %s\n", flen ? function : "",
err_buf);
errno = eo;
}
/**
* __ntfs_debug
*/
void __ntfs_debug(const char *file, int line, const char *function,
const char *fmt, ...)
{
int eo = errno;
int flen = 0;
va_list args;
char err_buf[1024];
if (function)
flen = strlen(function);
va_start(args, fmt);
vsnprintf(err_buf, sizeof(err_buf), fmt, args);
va_end(args);
fprintf(stderr, "NTFS DEBUG (%s, %d): %s(): %s\n", file, line,
flen ? function : "", err_buf);
errno = eo;
}
/**
* __Dprintf
*/
void __Dprintf(const char *fmt, ...)
{
int eo = errno;
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
errno = eo;
}
/**
* __Dputs
*/
void __Dputs(const char *s)
{
int eo = errno;
fprintf(stderr, "%s\n", s);
errno = eo;
}
/**
* __Dperror
*/
void __Dperror(const char *s)
{
int eo = errno;
perror(s);
errno = eo;
}
#endif
#ifndef NTFS_DISABLE_DEBUG_LOGGING
/**
* ntfs_debug_runlist_dump - Dump a runlist.

View File

@ -485,7 +485,7 @@ BOOL ntfs_logging_parse_option(const char *option)
return TRUE;
}
ntfs_log_warning("Unknown logging option '%s'\n", option);
ntfs_log_debug("Unknown logging option '%s'\n", option);
return FALSE;
}

View File

@ -1412,7 +1412,7 @@ static int mkntfs_attr_find(const ATTR_TYPES type, const ntfschar *name,
}
}
}
Dputs("mkntfs_attr_find(): File is corrupt. Run chkdsk.");
ntfs_log_trace("File is corrupt. Run chkdsk.\n");
errno = EIO;
return -1;
}

View File

@ -1187,8 +1187,8 @@ static s64 device_size_get(int fd)
{ u64 size;
if (ioctl(fd, BLKGETSIZE64, &size) >= 0) {
Dprintf("BLKGETSIZE64 nr bytes = %llu (0x%llx)\n",
(unsigned long long)size,
ntfs_log_debug("BLKGETSIZE64 nr bytes = %llu "
"(0x%llx).\n", (unsigned long long)size,
(unsigned long long)size);
return (s64)size;
}
@ -1198,8 +1198,8 @@ static s64 device_size_get(int fd)
{ unsigned long size;
if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
Dprintf("BLKGETSIZE nr 512 byte blocks = %lu "
"(0x%lx)\n", size, size);
ntfs_log_debug("BLKGETSIZE nr 512 byte blocks = %lu "
"(0x%lx).\n", size, size);
return (s64)size * 512;
}
}
@ -1208,8 +1208,9 @@ static s64 device_size_get(int fd)
{ struct floppy_struct this_floppy;
if (ioctl(fd, FDGETPRM, &this_floppy) >= 0) {
Dprintf("FDGETPRM nr 512 byte blocks = %lu (0x%lx)\n",
this_floppy.size, this_floppy.size);
ntfs_log_debug("FDGETPRM nr 512 byte blocks = %lu "
"(0x%lx).\n", this_floppy.size,
this_floppy.size);
return (s64)this_floppy.size * 512;
}
}

View File

@ -176,33 +176,34 @@ static int OLD_ntfs_volume_set_flags(ntfs_volume *vol, const u16 flags)
return -1;
}
if (ntfs_file_record_read(vol, FILE_Volume, &m, NULL)) {
Dperror("Failed to read $Volume");
ntfs_log_perror("Failed to read $Volume");
return -1;
}
/* Sanity check */
if (!(m->flags & MFT_RECORD_IN_USE)) {
Dprintf("Error: $Volume has been deleted. Cannot "
"handle this yet. Run chkdsk to fix this.\n");
ntfs_log_error("$Volume has been deleted. Cannot handle this "
"yet. Run chkdsk to fix this.\n");
errno = EIO;
goto err_exit;
}
/* Get a pointer to the volume information attribute. */
ctx = ntfs_attr_get_search_ctx(NULL, m);
if (!ctx) {
Dperror("Failed to allocate attribute search context");
ntfs_log_debug("Failed to allocate attribute search "
"context.\n");
goto err_exit;
}
if (ntfs_attr_lookup(AT_VOLUME_INFORMATION, AT_UNNAMED, 0, 0, 0, NULL,
0, ctx)) {
Dputs("Error: Attribute $VOLUME_INFORMATION was not found in "
"$Volume!");
ntfs_log_error("Attribute $VOLUME_INFORMATION was not found in "
"$Volume!\n");
goto err_out;
}
a = ctx->attr;
/* Sanity check. */
if (a->non_resident) {
Dputs("Error: Attribute $VOLUME_INFORMATION must be resident "
"(and it isn't)!");
ntfs_log_error("Attribute $VOLUME_INFORMATION must be resident "
"(and it isn't)!\n");
errno = EIO;
goto err_out;
}
@ -213,15 +214,15 @@ static int OLD_ntfs_volume_set_flags(ntfs_volume *vol, const u16 flags)
(char*)m + le32_to_cpu(m->bytes_in_use) ||
le16_to_cpu(a->value_offset) +
le32_to_cpu(a->value_length) > le32_to_cpu(a->length)) {
Dputs("Error: Attribute $VOLUME_INFORMATION in $Volume is "
"corrupt!");
ntfs_log_error("Attribute $VOLUME_INFORMATION in $Volume is "
"corrupt!\n");
errno = EIO;
goto err_out;
}
/* Set the volume flags. */
vol->flags = c->flags = cpu_to_le16(flags);
if (ntfs_mft_record_write(vol, FILE_Volume, m)) {
Dperror("Error writing $Volume");
ntfs_log_perror("Error writing $Volume");
goto err_out;
}
ret = 0; /* success */

View File

@ -153,7 +153,7 @@ static void usage (void)
*/
static int parse_options (int argc, char *argv[])
{
static const char *sopt = "-fh?i:F:mqtTvVd:";
static const char *sopt = "-:fhi:F:mqtTvVd:";
static const struct option lopt[] = {
{ "device", required_argument, NULL, 'd' },
{ "force", no_argument, NULL, 'f' },
@ -179,6 +179,8 @@ static int parse_options (int argc, char *argv[])
opts.filename = NULL;
while ((c = getopt_long (argc, argv, sopt, lopt, NULL)) != (char)-1) {
ntfs_log_trace("optind=%d; c='%c' optarg=\"%s\".\n", optind, c,
optarg);
switch (c) {
case 'd':
if (!opts.device)
@ -205,7 +207,6 @@ static int parse_options (int argc, char *argv[])
opts.force++;
break;
case 'h':
case '?':
help++;
break;
case 'q':
@ -228,12 +229,24 @@ static int parse_options (int argc, char *argv[])
case 'm':
opts.mft++;
break;
default:
if ((optopt == 'i') && (!optarg)) {
Eprintf ("Option '%s' requires an argument.\n", argv[optind-1]);
} else {
Eprintf ("Unknown option '%s'.\n", argv[optind-1]);
case '?':
if (optopt=='?') {
help++;
continue;
}
if (ntfs_logging_parse_option(argv[optind-1]))
continue;
ntfs_log_error("Unknown option '%s'.\n",
argv[optind-1]);
err++;
break;
case ':':
ntfs_log_error("Option '%s' requires an "
"argument.\n", argv[optind-1]);
err++;
break;
default:
ntfs_log_error("Unhandled option case: %d.\n", c);
err++;
break;
}
@ -1362,9 +1375,10 @@ static void ntfs_dump_index_allocation(ATTR_RECORD *attr, ntfs_inode *ni)
Vprintf("\tDumping index block "
"(VCN %lld, used %u/%u):", le64_to_cpu(
tmp_alloc->index_block_vcn),
(unsigned int)le32_to_cpu(tmp_alloc->
index.index_length), (unsigned int)
le32_to_cpu(tmp_alloc->index.
index_length), le32_to_cpu(tmp_alloc->
index.allocated_size));
allocated_size));
total_entries += ntfs_dump_index_entries(entry, type);
total_indx_blocks++;
}

View File

@ -26,7 +26,7 @@
#include "config.h"
#ifdef HAVE_FEATURES_H
# include <features.h>
#include <features.h>
#endif
#ifdef HAVE_STDIO_H
#include <stdio.h>
@ -83,9 +83,7 @@
static const char *EXEC_NAME = "ntfsundelete";
static const char *MFTFILE = "mft";
#ifdef DEBUG
static const char *UNNAMED = "<unnamed>";
#endif
static const char *NONE = "<none>";
static const char *UNKNOWN = "unknown";
static struct options opts;
@ -333,7 +331,8 @@ static int transform (const char *pattern, char **regex)
result[j] = '$';
result[j+1] = 0;
Dprintf ("Pattern '%s' replaced with regex '%s'\n", pattern, result);
ntfs_log_debug("Pattern '%s' replaced with regex '%s'.\n", pattern,
result);
*regex = result;
return 1;
@ -372,7 +371,7 @@ static int parse_time (const char *value, time_t *since)
if (!value || !since)
return -1;
Dprintf ("parsing time '%s' ago\n", value);
ntfs_log_trace("Parsing time '%s' ago.\n", value);
result = strtoll (value, &suffix, 10);
if (result < 0 || errno == ERANGE) {
@ -406,7 +405,8 @@ static int parse_time (const char *value, time_t *since)
now = time (NULL);
Dprintf ("Time now = %lld, Time then = %lld.\n", (long long) now, (long long) result);
ntfs_log_debug("Time now = %lld, Time then = %lld.\n", (long long) now,
(long long) result);
*since = now - result;
return 1;
}
@ -709,19 +709,23 @@ static void free_file (struct ufile *file)
list_for_each_safe (item, tmp, &file->name) { /* List of filenames */
struct filename *f = list_entry (item, struct filename, list);
Dprintf ("freeing filename '%s'\n", f->name ? f->name : NONE);
ntfs_log_debug("freeing filename '%s'", f->name ? f->name :
NONE);
if (f->name)
free (f->name);
if (f->parent_name) {
Dprintf ("\tand parent filename '%s'\n", f->parent_name);
ntfs_log_debug(" and parent filename '%s'",
f->parent_name);
free (f->parent_name);
}
ntfs_log_debug(".\n");
free (f);
}
list_for_each_safe (item, tmp, &file->data) { /* List of data streams */
struct data *d = list_entry (item, struct data, list);
Dprintf ("freeing data stream '%s'\n", d->name ? d->name : UNNAMED);
ntfs_log_debug("Freeing data stream '%s'.\n", d->name ?
d->name : UNNAMED);
if (d->name)
free (d->name);
if (d->runlist)
@ -826,12 +830,14 @@ static void get_parent_name (struct filename* name, ntfs_volume* vol)
if (ntfs_attr_pread(mft_data, vol->mft_record_size * inode_num, vol->mft_record_size, rec) < 1) {
Eprintf ("ERROR: Couldn't read MFT Record %lld.\n", inode_num);
} else {
if ((filename_attr = verify_parent(name, rec))) {
if (ntfs_ucstombs(filename_attr->file_name, filename_attr->file_name_length, &name->parent_name, 0) < 0) {
Dprintf ("Couldn't translate filename to current locale.\n");
name->parent_name = NULL;
}
} else if ((filename_attr = verify_parent(name, rec))) {
if (ntfs_ucstombs(filename_attr->file_name,
filename_attr->file_name_length,
&name->parent_name, 0) < 0) {
ntfs_log_debug("ERROR: Couldn't translate "
"filename to current "
"locale.\n");
name->parent_name = NULL;
}
}
}
@ -907,7 +913,8 @@ static int get_filenames (struct ufile *file, ntfs_volume* vol)
if (ntfs_ucstombs (name->uname, name->uname_len, &name->name,
0) < 0) {
Dprintf ("ERROR: Couldn't translate filename to current locale.\n");
ntfs_log_debug("ERROR: Couldn't translate filename to "
"current locale.\n");
}
name->parent_name = NULL;
@ -931,7 +938,7 @@ static int get_filenames (struct ufile *file, ntfs_volume* vol)
}
ntfs_attr_put_search_ctx(ctx);
Dprintf ("File has %d names.\n", count);
ntfs_log_debug("File has %d names.\n", count);
return count;
}
@ -998,7 +1005,7 @@ static int get_data (struct ufile *file, ntfs_volume *vol)
data->runlist = ntfs_mapping_pairs_decompress(vol, rec, NULL);
if (!data->runlist) {
Dprintf ("Couldn't decompress the data runs\n");
ntfs_log_debug("Couldn't decompress the data runs.\n");
}
file->max_size = max (file->max_size, data->size_data);
@ -1009,7 +1016,7 @@ static int get_data (struct ufile *file, ntfs_volume *vol)
}
ntfs_attr_put_search_ctx(ctx);
Dprintf ("File has %d data streams.\n", count);
ntfs_log_debug("File has %d data streams.\n", count);
return count;
}
@ -1071,7 +1078,8 @@ static struct ufile * read_record (ntfs_volume *vol, long long record)
attr20 = find_first_attribute (AT_ATTRIBUTE_LIST, file->mft);
attr90 = find_first_attribute (AT_INDEX_ROOT, file->mft);
Dprintf ("Attributes present: %s %s %s\n", attr10?"0x10":"", attr20?"0x20":"", attr90?"0x90":"");
ntfs_log_debug("Attributes present: %s %s %s.\n", attr10?"0x10":"",
attr20?"0x20":"", attr90?"0x90":"");
if (attr10) {
STANDARD_INFORMATION *si;
@ -1131,7 +1139,7 @@ static int calc_percentage (struct ufile *file, ntfs_volume *vol)
return -1;
if (file->directory) {
Dprintf ("Found a directory: not recoverable.\n");
ntfs_log_debug("Found a directory: not recoverable.\n");
return 0;
}
@ -1297,7 +1305,7 @@ static void dump_record (struct ufile *file)
Qprintf ("Data Streams:\n");
list_for_each (item, &file->data) {
struct data *d = list_entry (item, struct data, list);
Qprintf ("Name: %s\n", (d->name) ? d->name : "<unnamed>");
Qprintf ("Name: %s\n", (d->name) ? d->name : UNNAMED);
Qprintf ("Flags: ");
if (d->resident) Qprintf ("Resident\n");
if (d->compressed) Qprintf ("Compressed\n");
@ -1428,12 +1436,12 @@ static int name_match (regex_t *re, struct ufile *file)
Eprintf ("Couldn't compare filename with regex: %s\n", strerror (errno));
return 0;
} else if (result == REG_NOERROR) {
Dprintf ("Found a matching filename.\n");
ntfs_log_debug("Found a matching filename.\n");
return 1;
}
}
Dprintf ("Filename '%s' doesn't match regex.\n", file->pref_name);
ntfs_log_debug("Filename '%s' doesn't match regex.\n", file->pref_name);
return 0;
}
@ -1981,7 +1989,8 @@ static int copy_mft (ntfs_volume *vol, long long mft_begin, long long mft_end)
name = opts.output;
if (!name) {
name = MFTFILE;
Dprintf ("No output filename, defaulting to '%s'.\n", name);
ntfs_log_debug("No output filename, defaulting to '%s'.\n",
name);
}
create_pathname (opts.dest, name, NULL, pathname, sizeof (pathname));
@ -1996,10 +2005,10 @@ static int copy_mft (ntfs_volume *vol, long long mft_begin, long long mft_end)
mft_end = min (mft_end, nr_mft_records - 1);
Dprintf ("MFT records\n");
Dprintf (" Total: %8lld\n", nr_mft_records);
Dprintf (" Begin: %8lld\n", mft_begin);
Dprintf (" End: %8lld\n", mft_end);
ntfs_log_debug("MFT records:\n");
ntfs_log_debug("\tTotal: %8lld\n", nr_mft_records);
ntfs_log_debug("\tBegin: %8lld\n", mft_begin);
ntfs_log_debug("\tEnd: %8lld\n", mft_end);
for (i = mft_begin; i <= mft_end; i++) {
if (ntfs_attr_pread (mft, vol->mft_record_size * i, vol->mft_record_size, buffer) < vol->mft_record_size) {

View File

@ -170,10 +170,10 @@ static int parse_list (char *list, int **result)
ptr = end + 1;
}
Dprintf ("Parsing list '%s' - ", list);
ntfs_log_debug("Parsing list '%s' - ", list);
for (i = 0; i <= count; i++)
Dprintf ("0x%02x ", mem[i]);
Dprintf ("\n");
ntfs_log_debug("0x%02x ", mem[i]);
ntfs_log_debug("\n");
*result = mem;
return count;
@ -1094,18 +1094,18 @@ static s64 wipe_logfile (ntfs_volume *vol, int byte, enum action act
//Qprintf ("wipe_logfile (not implemented) 0x%02x\n", byte);
if ((ni = ntfs_inode_open(vol, FILE_LogFile)) == NULL) {
Dprintf("Failed to open inode FILE_LogFile.\n");
ntfs_log_debug("Failed to open inode FILE_LogFile.\n");
return -1;
}
if ((na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0)) == NULL) {
Dprintf("Failed to open $FILE_LogFile/$DATA\n");
ntfs_log_debug("Failed to open $FILE_LogFile/$DATA.\n");
goto error_exit;
}
/* The $DATA attribute of the $LogFile has to be non-resident. */
if (!NAttrNonResident(na)) {
Dprintf("$LogFile $DATA attribute is resident!?!\n");
ntfs_log_debug("$LogFile $DATA attribute is resident!?!\n");
errno = EIO;
goto io_error_exit;
}
@ -1113,7 +1113,8 @@ static s64 wipe_logfile (ntfs_volume *vol, int byte, enum action act
/* Get length of $LogFile contents. */
len = na->data_size;
if (!len) {
Dprintf("$LogFile has zero length, no disk write needed.\n");
ntfs_log_debug("$LogFile has zero length, no disk write "
"needed.\n");
return 0;
}
@ -1125,8 +1126,8 @@ static s64 wipe_logfile (ntfs_volume *vol, int byte, enum action act
pos += count;
if (count == -1 || pos != len) {
Dprintf("Amount of $LogFile data read does not "
"correspond to expected length!");
ntfs_log_debug("Amount of $LogFile data read does not "
"correspond to expected length!\n");
if (count != -1)
errno = EIO;
goto io_error_exit;
@ -1142,7 +1143,8 @@ static s64 wipe_logfile (ntfs_volume *vol, int byte, enum action act
count = NTFS_BUF_SIZE2;
if ((count = ntfs_attr_pwrite(na, pos, count, buf)) <= 0) {
Dprintf("Failed to set the $LogFile attribute value.");
ntfs_log_debug("Failed to set the $LogFile attribute "
"value.\n");
if (count != -1)
errno = EIO;
goto io_error_exit;
@ -1198,18 +1200,18 @@ static s64 wipe_pagefile (ntfs_volume *vol, int byte, enum action act
ni = ntfs_pathname_to_inode(vol, NULL, "pagefile.sys");
if (!ni) {
Dprintf("Failed to open inode of pagefile.sys.\n");
ntfs_log_debug("Failed to open inode of pagefile.sys.\n");
return 0;
}
if ((na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0)) == NULL) {
Dprintf("Failed to open pagefile.sys/$DATA\n");
ntfs_log_debug("Failed to open pagefile.sys/$DATA.\n");
goto error_exit;
}
/* The $DATA attribute of the pagefile.sys has to be non-resident. */
if (!NAttrNonResident(na)) {
Dprintf("pagefile.sys $DATA attribute is resident!?!\n");
ntfs_log_debug("pagefile.sys $DATA attribute is resident!?!\n");
errno = EIO;
goto io_error_exit;
}
@ -1217,7 +1219,8 @@ static s64 wipe_pagefile (ntfs_volume *vol, int byte, enum action act
/* Get length of pagefile.sys contents. */
len = na->data_size;
if (!len) {
Dprintf("pagefile.sys has zero length, no disk write needed.\n");
ntfs_log_debug("pagefile.sys has zero length, no disk write "
"needed.\n");
return 0;
}
@ -1230,7 +1233,8 @@ static s64 wipe_pagefile (ntfs_volume *vol, int byte, enum action act
count = NTFS_BUF_SIZE2;
if ((count = ntfs_attr_pwrite(na, pos, count, buf)) <= 0) {
Dprintf("Failed to set the pagefile.sys attribute value.");
ntfs_log_debug("Failed to set the pagefile.sys "
"attribute value.\n");
if (count != -1)
errno = EIO;
goto io_error_exit;