00001 /* 00002 * Copyright (c) 2002 - 2006, Netherlands Forensic Institute 00003 * 00004 * All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 1. Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * 2. Redistributions in binary form must reproduce the above copyright 00012 * notice, this list of conditions and the following disclaimer in the 00013 * documentation and/or other materials provided with the distribution. 00014 * 3. Neither the name of the Institute nor the names of its contributors 00015 * may be used to endorse or promote products derived from this software 00016 * without specific prior written permission. 00017 00018 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 00019 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00021 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 00022 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00023 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00024 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00025 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00026 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00027 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00028 * SUCH DAMAGE. 00029 */ 00030 00031 00032 /* 00033 * $Author: raoul $ 00034 * $LastChangedBy: kojak $ 00035 * $LastChangedDate: 2003-01-02 19:32:23 +0100 (Thu, 02 Jan 2003) $ 00036 */ 00037 00038 #ifndef lint 00039 static char copyright[] = 00040 "@(#) Copyright (c) 2002\n\ 00041 Netherlands Forensic Institute. All rights reserved.\n"; 00042 #endif /* not lint */ 00043 00044 /* 00045 * Periodic progress reporting. 00046 */ 00047 00048 #ifdef HAVE_CONFIG_H 00049 #include <config.h> 00050 #endif 00051 00052 #include <string.h> 00053 #include <sys/time.h> 00054 00055 #include "rdd.h" 00056 #include "rdd_internals.h" 00057 #include "error.h" 00058 #include "progress.h" 00059 00060 #define INIT_POLL_BYTES 1048576 00061 00062 int 00063 rdd_progress_init(RDD_PROGRESS *p, rdd_count_t size, unsigned secs) 00064 { 00065 memset(p, 0, sizeof(*p)); 00066 00067 p->input_size = size; 00068 p->period = (double) secs; 00069 p->start_time = rdd_gettime(); 00070 p->last_time = p->start_time; 00071 p->poll_delta = INIT_POLL_BYTES; 00072 p->curpos = 0; 00073 p->last_pos = 0; 00074 p->nsubst = 0; 00075 00076 return RDD_OK; 00077 } 00078 00079 int 00080 rdd_progress_update(RDD_PROGRESS *p, rdd_count_t pos, rdd_count_t nsubst) 00081 { 00082 p->curpos = pos; 00083 p->nsubst = nsubst; 00084 return RDD_OK; 00085 } 00086 00087 int 00088 rdd_progress_poll(RDD_PROGRESS *p, RDD_PROGRESS_INFO *info) 00089 { 00090 double now; 00091 double speed; 00092 00093 if (p->curpos - p->last_pos < p->poll_delta) { 00094 return RDD_EAGAIN; /* too early to report progress */ 00095 } 00096 00097 now = rdd_gettime(); 00098 speed = p->curpos / (now - p->start_time); /* bytes/sec */ 00099 p->poll_delta = p->period * speed; 00100 if ((now - p->last_time) < p->period) { 00101 return RDD_EAGAIN; 00102 } 00103 p->last_time = now; 00104 p->last_pos = p->curpos; 00105 00106 info->pos = p->curpos; 00107 info->speed = speed; 00108 if (p->input_size == RDD_WHOLE_FILE) { 00109 info->fraction = -1.0; 00110 } else { 00111 info->fraction = ((double) p->curpos) / ((double) p->input_size); 00112 } 00113 info->nsubst = p->nsubst; 00114 00115 return RDD_OK; 00116 }