1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
/*
* cook - file construction tool
* Copyright (C) 1999 Peter Miller;
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
*
* MANIFEST: functions to manipulate records
*/
#include <fingerprint/record.h>
#include <fingerprint/subdir.h>
#include <fingerprint/value.h>
#include <mem.h>
#include <trace.h>
/*
* NAME
* fp_record_new
*
* SYNOPSIS
* fp_record_ty *fp_record_new(fp_subdir_ty *parent);
*
* DESCRIPTION
* The fp_record_new function is used to allocate and initialize
* a fp_record_ty structure in dynamic memory. Use fp_record-delete
* when you are done with it.
*
* Note: the parent's dirty flag is not altered.
*/
fp_record_ty *
fp_record_new(filename, parent)
string_ty *filename;
fp_subdir_ty *parent;
{
fp_record_ty *this;
trace(("fp_record_new(parent = %08lX)\n{\n", (long)parent));
this = mem_alloc(sizeof(fp_record_ty));
this->filename = str_copy(filename);
this->parent = parent;
this->exists = 0;
fp_value_constructor(&this->value);
trace(("return %08lX;\n", (long)this));
trace(("}\n"));
return this;
}
/*
* NAME
* fp_record_new2
*
* SYNOPSIS
* fp_record_ty *fp_record_new2(fp_subdir_ty *parent, fp_value_ty *value);
*
* DESCRIPTION
* The fp_record_new2 function is used to allocate and initialize a
* fp_record_ty structure in dynamic memory. The parent and value
* fields are set as specified. Use fp_record_delete when you are
* done with it.
*
* Note: the parent's dirty flag is not altered.
*/
fp_record_ty *
fp_record_new2(filename, parent, fp)
string_ty *filename;
fp_subdir_ty *parent;
fp_value_ty *fp;
{
fp_record_ty *this;
trace(("fp_record_new2(parent = %08lX, fp = %08lX)\n{\n",
(long)parent, (long)fp));
this = mem_alloc(sizeof(fp_record_ty));
this->filename = str_copy(filename);
this->parent = parent;
this->exists = 1;
fp_value_constructor_copy(&this->value, fp);
trace(("return %08lX;\n", (long)this));
trace(("}\n"));
return this;
}
/*
* NAME
* fp_record_delete
*
* SYNOPSIS
* void fp_record_delete(fp_record_ty *);
*
* DESCRIPTION
* The fp_record_delete function is used to release the resources
* held by a fp_record_ty structure in dynamic memory.
*
* Note: the parent's dirty flag is not altered.
*/
void
fp_record_delete(this)
fp_record_ty *this;
{
trace(("fp_record_delete(this = %08lX)\n{\n", (long)this));
str_free(this->filename);
fp_value_destructor(&this->value);
mem_free(this);
trace(("}\n"));
}
/*
* NAME
* fp_record_write
*
* SYNOPSIS
* void fp_record_write(fp_record_ty *, string_ty *, FILE *);
*
* DESCRIPTION
* The fp_record_write function is used to write a fp_record_ty
* structure to the fingerprint cache file on disk. the value is
* only written if it exists.
*/
void
fp_record_write(this, key, fp)
fp_record_ty *this;
string_ty *key;
FILE *fp;
{
trace(("fp_record_write(this = %08lX, key = \"%s\", fp = %08lX)\n{\n",
(long)this, key->str_text, (long)fp));
if (this->exists)
fp_value_write(&this->value, (key ? key : this->filename), fp);
trace(("}\n"));
}
/*
* NAME
* fp_record_update
*
* SYNOPSIS
* void fp_record_update(fp_record _ty *this, fp_value_ty *value);
*
* DESCRIPTION
* The fp_record_update function is used to update the value of
* a fingerprint held in a fp_record_ty structure. The existence
* attributes is updated if necessary. The parent's dirty flag is
* set if necessary.
*/
void
fp_record_update(this, fp)
fp_record_ty *this;
fp_value_ty *fp;
{
trace(("fp_record_update(this = %08lX, fp = %08lX)\n{\n",
(long)this, (long)fp));
if (!this->exists || !fp_value_equal(&this->value, fp))
{
this->exists = 1;
fp_subdir_dirty_notify(this->parent, this->filename);
fp_value_copy(&this->value, fp);
}
trace(("}\n"));
}
/*
* NAME
* fp_record_clear
*
* SYNOPSIS
* void fp_record_clear(fp_record_ty *this);
*
* DESCRIPTION
* The fp_record_clear function is used to clear the value of
* a fingerprint held in a fp_record_ty structure. The existence
* attribute is updated if necessary. The parent's dirty flag is
* set if necessary.
*/
void
fp_record_clear(this)
fp_record_ty *this;
{
trace(("fp_record_clear(this = %08lX)\n{\n", (long)this));
if (this->exists)
{
this->exists = 0;
fp_subdir_dirty_notify(this->parent, this->filename);
}
trace(("}\n"));
}
/*
* NAME
* fp_record_tweak
*
* SYNOPSIS
* void fp_record_tweak(fp_record_ty *this, time_t when,
* string_ty *crypto);
*
* DESCRIPTION
* The fp_record_tweak function is used to tweak the value
* of a fingerprint held in a fp_record_ty structure IF it is
* out-of-date with respect to the filesystem. The existence
* attribute is updated if necessary. The parent's dirty flag is
* set if necessary.
*/
void
fp_record_tweak(this, when, crypto)
fp_record_ty *this;
time_t when;
string_ty *crypto;
{
/*
* If the file is not known to exist, or if the fingerprint is
* different, replace the value with the information given.
*/
if (!this->exists || !str_equal(this->value.fingerprint, crypto))
{
fp_value_ty value;
fp_subdir_dirty_notify(this->parent, this->filename);
fp_value_constructor3(&value, when, when, crypto);
fp_value_copy(&this->value, &value);
fp_value_destructor(&value);
this->exists = 1;
}
else if (this->value.newest != when)
{
/*
* If the newest time is the same (it is supposted to
* be the same as the stat::st_mtime value) then leave
* everything alone. (The upper bound on oldest is in
* case the file heads into the past.)
*/
fp_subdir_dirty_notify(this->parent, this->filename);
this->value.newest = when;
if (this->value.oldest >= when)
this->value.oldest = when;
}
}
|