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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
/*
* 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 subdirs
*/
#include <ac/errno.h>
#include <ac/dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ac/unistd.h>
#include <error_intl.h>
#include <fingerprint.h>
#include <fingerprint/filename.h>
#include <fingerprint/find.h>
#include <fingerprint/gram.h>
#include <fingerprint/record.h>
#include <fingerprint/subdir.h>
#include <mem.h>
#include <os.h>
#include <star.h>
#include <str.h>
#include <symtab.h>
#include <trace.h>
/*
* NAME
* reap
*
* SYNOPSIS
* void reap(void *);
*
* DESCRIPTION
* The reap function is used to delete fp_record_ty symbol table
* entries. Called exclusively by symtab_free().
*/
static void reap _((void *));
static void
reap(p)
void *p;
{
fp_record_ty *frp;
trace(("reap\n"));
frp = p;
fp_record_delete(frp);
}
/*
* NAME
* fp_subdir_new
*
* SYNOPSIS
* void fp_subdir_new(string_ty *path);
*
* DESCRIPTION
* The fp_subdir_new function is used to allocate a fp_subdir_ty
* structure in dynamic memory and initialize it to empty. The path
* is remembered for later reading and writing of the cache file.
*/
fp_subdir_ty *
fp_subdir_new(path)
string_ty *path;
{
fp_subdir_ty *this;
trace(("fp_subdir_new(path = \"%s\")\n{\n", path->str_text));
this = mem_alloc(sizeof(fp_subdir_ty));
this->stp = symtab_alloc(5);
this->stp->reap = reap;
this->path = str_copy(path);
this->dirty = 0;
this->cache_in_dot = 0;
this->need_to_read = 1;
trace(("return %08lX;\n", (long)this));
trace(("}\n"));
return this;
}
/*
* NAME
* fp_subdir_delete
*
* SYNOPSIS
* void fp_subdir_delete(void);
*
* DESCRIPTION
* The fp_subdir_delete function is used to release the resources
* held by a fp_subdir_ty structure.
*/
void
fp_subdir_delete(this)
fp_subdir_ty *this;
{
trace(("fp_subdir_delete(this = %08lX)\n{\n", (long)this));
str_free(this->path);
this->path = 0;
symtab_free(this->stp);
this->stp = 0;
mem_free(this);
trace(("}\n"));
}
/*
* NAME
* fp_subdir_read
*
* SYNOPSIS
* void fp_subdir_read(fp_subdir_ty *);
*
* DESCRIPTION
* The fp_subdir_read function is used to read the cache file
* assoictaed with a sub-directory, if it exists.
*/
void
fp_subdir_read(this)
fp_subdir_ty *this;
{
string_ty *fn;
if (!this->need_to_read)
return;
this->need_to_read = 0;
trace(("fp_subdir_read(this = %08lX)\n{\n", (long)this));
fn = os_path_cat(this->path, fp_filename());
fp_gram(this, fn);
str_free(fn);
trace(("}\n"));
}
/*
* NAME
* walk
*
* SYNOPSIS
* void walk _((symtab_ty *stp, string_ty *key, fp_record_ty *value,
* FILE *ofile));
*
* DESCRIPTION
* The walk function is used to emit entries in a fp_record_ty
* symbol table to a file. Called exclusively via symtab_walk
* by fp_subdir_write.
*/
static void walk _((symtab_ty *, string_ty *, void *, void *));
static void
walk(sp, key, p, arg)
symtab_ty *sp;
string_ty *key;
void *p;
void *arg;
{
fp_record_ty *data;
FILE *fp;
trace(("walk(sp = %08lX, key = \"%s\", p = %08lX, arg = %08lX)\n{\n", \
(long)sp, key->str_text, (long)p, (long)arg));
data = p;
fp = arg;
fp_record_write(data, key, fp);
trace(("}\n"));
}
/*
* NAME
* fp_subdir_write
*
* SYNOPSIS
* void fp_subdir_write(void);
*
* DESCRIPTION
* The fp_subdir_write function is used to write the contents of
* a fp_subdir_ty structure out to the corresponding disk cache file.
*
* It will not be written out if it isn't dirty (hasn't changed)
* or if cahce updating has been displabed.
*/
void
fp_subdir_write(this, need_to_write_dot)
fp_subdir_ty *this;
int *need_to_write_dot;
{
static string_ty *dot;
string_ty *fn;
FILE *fp;
/*
* If we aren't dirty, don't write us out
*/
trace(("fp_subdir_write(this = %08lX)\n{\n", (long)this));
if (!this->dirty)
{
trace(("not dirty\n"));
trace(("}\n"));
return;
}
/*
* build the filename to write
*/
fn = os_path_cat(this->path, fp_filename());
/*
* Open the file for writing.
* If there is a permissions problem, quietly slink away.
*/
if (unlink(fn->str_text))
{
switch (errno)
{
case ENOENT:
break;
case EACCES:
str_free(fn);
trace(("mark not writable any more\n"));
this->cache_in_dot = 1;
*need_to_write_dot = 1;
trace(("}\n"));
return;
default:
fatal_intl_unlink(fn->str_text);
/* NOTREACHED */
}
}
fp = fopen(fn->str_text, "w");
if (!fp)
{
if (errno == EACCES || errno == ENOENT)
{
str_free(fn);
trace(("mark not writable any more\n"));
this->cache_in_dot = 1;
*need_to_write_dot = 1;
trace(("}\n"));
return;
}
fatal_intl_open(fn->str_text);
}
/*
* walk the symbol table, bumping as we go.
*/
star_as_specified('@');
symtab_walk(this->stp, walk, fp);
/*
* NOW we're clean.
*/
this->dirty = 0;
if (this->cache_in_dot)
{
*need_to_write_dot = 1;
this->cache_in_dot = 0;
}
/*
* The top-level cache also gets all of the files from deeper,
* unwritable directories.
*/
if (!dot)
dot = str_from_c(".");
if (str_equal(this->path, dot))
fp_find_main_write(fp);
/*
* close up and go home
*/
fflush_and_check(fp, fn->str_text);
fclose_and_check(fp, fn->str_text);
str_free(fn);
trace(("}\n"));
}
void
fp_subdir_dirty_notify(this, filename)
fp_subdir_ty *this;
string_ty *filename;
{
/*
* ignore the fingerprint cache file
*/
if (str_equal(filename, fp_filename()))
return;
/*
* Update this flag, even if cache_in_dot,
* because the "." cache will get it.
*/
this->dirty = 1;
}
void
fp_subdir_tweak(this)
fp_subdir_ty *this;
{
DIR *dp;
struct dirent *dep;
string_ty *filename;
static string_ty *avoid;
static string_ty *dot;
static string_ty *dotdot;
string_list_ty sl;
size_t j;
/*
* read the directory
*/
trace(("fp_subdir_update(this = %08lX)\n{\n", (long)this));
star_as_specified('*');
dp = opendir(this->path->str_text);
if (!dp)
fatal_intl_opendir(this->path->str_text);
if (!avoid)
avoid = fp_filename();
if (!dot)
dot = str_from_c(".");
if (!dotdot)
dotdot = str_from_c("..");
string_list_constructor(&sl);
for (;;)
{
dep = readdir(dp);
if (!dep)
break;
filename = str_from_c(dep->d_name);
if
(
!str_equal(filename, avoid)
&&
!str_equal(filename, dot)
&&
!str_equal(filename, dotdot)
)
string_list_append(&sl, filename);
str_free(filename);
}
closedir(dp);
/*
* visit each entry in the directory
* - for each file/symlink, update the stat cache
* - for each directory, recurse
* - ignore evrything else
*/
for (j = 0; j < sl.nstrings; ++j)
{
struct stat st;
string_ty *s;
int err;
s = os_path_cat(this->path, sl.string[j]);
trace(("stat(\"%s\")\n", s->str_text));
#ifdef S_IFLNK
err = lstat(s->str_text, &st);
#else
err = stat(s->str_text, &st);
#endif
if (err)
fatal_intl_stat(s->str_text);
switch (st.st_mode & S_IFMT)
{
case S_IFDIR:
fp_subdir_tweak(fp_find_subdir(s, 1));
break;
case S_IFREG:
#ifdef S_IFLNK
case S_IFLNK:
#endif
fp_record_tweak
(
fp_find_record(s),
st.st_mtime,
fp_fingerprint(s)
);
break;
}
str_free(s);
}
string_list_destructor(&sl);
trace(("}\n"));
}
|