File: cache.c

package info (click to toggle)
xdir 2.1-3
  • links: PTS
  • area: non-free
  • in suites: slink
  • size: 2,376 kB
  • ctags: 2,449
  • sloc: ansic: 30,140; makefile: 481; sh: 64
file content (707 lines) | stat: -rw-r--r-- 19,490 bytes parent folder | download
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/***************************************************************************/
/***************************************************************************/
/*                                                                         */
/*   (c) 1995-1998.  The Regents of the University of California.  All     */
/*   rights reserved.                                                      */
/*                                                                         */
/*   This work was produced at the University of California, Lawrence      */
/*   Livermore National Laboratory (UC LLNL) under contract no.            */
/*   W-7405-ENG-48 (Contract 48) between the U.S. Department of Energy     */
/*   (DOE) and The Regents of the University of California (University)    */
/*   for the operation of UC LLNL.  Copyright is reserved to the           */
/*   University for purposes of controlled dissemination,                  */
/*   commercialization through formal licensing, or other disposition      */
/*   under terms of Contract 48; DOE policies, regulations and orders;     */
/*   and U.S. statutes.  The rights of the Federal Government are          */
/*   reserved under Contract 48 subject to the restrictions agreed upon    */
/*   by the DOE and University.                                            */
/*                                                                         */
/*                                                                         */
/*                              DISCLAIMER                                 */
/*                                                                         */
/*   This software was prepared as an account of work sponsored by an      */
/*   agency of the United States Government.  Neither the United States    */
/*   Government nor the University of California nor any of their          */
/*   employees, makes any warranty, express or implied, or assumes any     */
/*   liability or responsibility for the accuracy, completeness, or        */
/*   usefulness of any information, apparatus, product, or process         */
/*   disclosed, or represents that its specific commercial products,       */
/*   process, or service by trade name, trademark, manufacturer, or        */
/*   otherwise, does not necessarily constitute or imply its               */
/*   endorsement, recommendation, or favoring by the United States         */
/*   Government or the University of California. The views and opinions    */
/*   of the authors expressed herein do not necessarily state or reflect   */
/*   those of the United States Government or the University of            */
/*   California, and shall not be used for advertising or product          */
/*   endorsement purposes.                                                 */
/*                                                                         */
/*   Permission to use, copy, modify and distribute this software and its  */
/*   documentation for any non-commercial purpose, without fee, is         */
/*   hereby granted, provided that the above copyright notice and this     */
/*   permission notice appear in all copies of the software and            */
/*   supporting documentation, and that all UC LLNL identification in      */
/*   the user interface remain unchanged.  The title to copyright LLNL     */
/*   XDIR shall at all times remain with The Regents of the University     */
/*   of California and users agree to preserve same. Users seeking the     */
/*   right to make derivative works with LLNL XDIR for commercial          */
/*   purposes may obtain a license from the Lawrence Livermore National    */
/*   Laboratory's Technology Transfer Office, P.O. Box 808, L-795,         */
/*   Livermore, CA 94550.                                                  */
/*                                                                         */
/***************************************************************************/
/***************************************************************************/

#include <Xm/Xm.h>
#include "xdir.h"
#include "list.h"

#define BYTES_PER_KB  1024

#define DIRLIST   0
#define TRUEPATH  1

struct hostuser_st {
	struct hostuser_st *next;
	char *hostname;
	char *username;
};

struct dirlist_part {
	int layout;
	int dotfiles;
	int count;
	char *list;
};

struct mapping_part {
	char *true_path;
};

struct cache_link {
	struct cache_link *prev;
	struct cache_link *next;
	struct hostuser_st *hostuser;
	char *path;
	long len;
	int type;
	union {
		struct dirlist_part d;
		struct mapping_part m;
	} part; 
};

static struct hostuser_st *head_hostuser = NULL;
static struct cache_link *head = NULL;
static struct cache_link *tail = NULL;
static long cache_len = 0;

extern int max_dir_cache_len;
extern struct st_host_info hinfo[];
extern int diagnostics;


/*
 * insert_at_cache_head - Insert the cache entry pointed by "ptr" at the
 *                        head of the cache.
 */
insert_at_cache_head(ptr)
struct cache_link *ptr;
{
	ptr->next = head;
	ptr->prev = NULL;
	if (head)
		head->prev = ptr;
	head = ptr;
	if (!tail)
		tail = ptr;
}


/*
 * make_room_in_cache - Free up the least recently used cache entries
 *                      to create enough space for a directory list
 *                      "len" bytes in length.  Returns True if successful,
 *                      else False.
 */
make_room_in_cache(len)
long len;
{
	long bytes_available = max_dir_cache_len*BYTES_PER_KB-cache_len;

	/* No hope if entry is bigger than entire cache */
	if (len > max_dir_cache_len*BYTES_PER_KB)
		return False;

	/* Delete least recently used entries until there is enough room */
	while (head && (bytes_available<len)) {
		bytes_available += tail->len;
		free_cache_entry(tail);
	}

	/* Sanity check */
	if (len > bytes_available)
		fatal_error("Bug in make_room_in_cache()");

	/* Looks good */
	return True;
}


/*
 * free_cache_entry - Free the cache entry pointed to by "ptr".  This
 *                    function is a no-op if "ptr" is NULL.
 */
free_cache_entry(ptr)
struct cache_link *ptr;
{
	/* No-op if pointer is NULL */
	if (!ptr)
		return;

	/* Debug edit */
	report_cache_deletion(ptr);

	/* Adjust cache len */
	cache_len -= ptr->len;

	/* Free the entry's fields */
	XtFree(ptr->path);
	switch (ptr->type) {
	case DIRLIST:
		XtFree(ptr->part.d.list);
		break;
	case TRUEPATH:
		XtFree(ptr->part.m.true_path);
	}

	/* Now remove the entry from the doubly-linked list */
	if (ptr->prev)
		ptr->prev->next = ptr->next;
	else
		head = ptr->next;
	if (ptr->next)
		ptr->next->prev = ptr->prev;
	else
		tail = ptr->prev;
	XtFree((char *)ptr);
}


/*
 * find_dirlist_in_cache - Return a pointer to the link in the cache
 *                         that contains the directory list that is
 *                         uniquely specified by the arguments.  If
 *                         there is no hit, return NULL.
 */
struct cache_link *
find_dirlist_in_cache(host, path, layout, dotfiles)
int host;
char *path;
int layout;
int dotfiles;
{
	struct hostuser_st *hostuser = head_hostuser;
	struct cache_link *ptr = head;

	/* Find hostname/username pair */
	while (hostuser) {
		if (!strcmp(hinfo[host].hostname, hostuser->hostname) &&
				(!strcmp(hinfo[host].username, hostuser->username)))
			break;
		hostuser = hostuser->next;
	}
	if (hostuser == NULL)
		return NULL;

	/* TREE layout is the same as ICONIC */
	if (layout == TREE)
		layout = ICONIC;

	/* Search the cache for a match */
	while (ptr) {
		if ((ptr->type == DIRLIST) && (hostuser == ptr->hostuser) &&
				!strcmp(path, ptr->path) && (layout == ptr->part.d.layout) &&
				(dotfiles == ptr->part.d.dotfiles))
			return ptr;
		ptr = ptr->next;
	}
	return NULL;
}


/*
 * find_path_in_cache - Return a pointer to the link in the cache
 *                      that contains the directory mapping that is
 *                      uniquely specified by the arguments.  If
 *                      there is no hit, return NULL.
 */
struct cache_link *
find_path_in_cache(host, path, type)
int host;
char *path;
int type;
{
	struct hostuser_st *hostuser = head_hostuser;
	struct cache_link *ptr = head;

	/* Find hostname/username pair */
	while (hostuser) {
		if (!strcmp(hinfo[host].hostname, hostuser->hostname) &&
				(!strcmp(hinfo[host].username, hostuser->username)))
			break;
		hostuser = hostuser->next;
	}
	if (hostuser == NULL)
		return NULL;

	/* Search the cache for an exact match */
	while (ptr) {
		if ((ptr->type == type) && (ptr->hostuser == hostuser) &&
				!strcmp(ptr->path, path))
			return ptr;
		ptr = ptr->next;
	}
	return NULL;
}


/*
 * find_truepath_in_cache - Return a pointer to the link in the cache
 *                          that contains the specified path in the
 *                          "true_path" field.  If there is no hit,
 *                          return NULL.
 */
struct cache_link *
find_truepath_in_cache(host, path)
int host;
char *path;
{
	struct hostuser_st *hostuser = head_hostuser;
	struct cache_link *ptr = head;

	/* Find hostname/username pair */
	while (hostuser) {
		if (!strcmp(hinfo[host].hostname, hostuser->hostname) &&
				(!strcmp(hinfo[host].username, hostuser->username)))
			break;
		hostuser = hostuser->next;
	}
	if (hostuser == NULL)
		return NULL;

	/* Search the cache for an exact match */
	while (ptr) {
		if ((ptr->type == TRUEPATH) && (ptr->hostuser == hostuser) &&
				!strcmp(ptr->part.m.true_path, path))
			return ptr;
		ptr = ptr->next;
	}
	return NULL;
}


/*
 * add_dirlist_to_cache - Add to the cache the directory list "dlist",
 *                        which is uniquely specified by the other
 *                        arguments.
 */
add_dirlist_to_cache(host, path, layout, dotfiles, dlist)
int host;
char *path;
int layout;
int dotfiles;
struct sl_struct *dlist;
{
	struct hostuser_st *hostuser = head_hostuser;
	long len;
	long list_len;
	int i;
	int slen;
	struct cache_link *ptr;
	char *p;

	/* Make sure that cache len is consistent with current max len */
	check_max_cache_len();

	/* Find hostname/username pair */
	while (hostuser) {
		if (!strcmp(hinfo[host].hostname, hostuser->hostname) &&
				(!strcmp(hinfo[host].username, hostuser->username)))
			break;
		hostuser = hostuser->next;
	}
	if (hostuser == NULL) {
		hostuser = XtNew(struct hostuser_st);
		hostuser->hostname = XtNewString(hinfo[host].hostname);
		hostuser->username = XtNewString(hinfo[host].username);
		hostuser->next = head_hostuser;
		head_hostuser = hostuser;
	}

	/* If directory is already in the cache, delete it */
	free_cache_entry(find_dirlist_in_cache(host, path, layout, dotfiles));

	/* Determine the length of the directory list (plus overhead) */
	list_len = 0;
	for (i=0; i<dlist->nentries; i++)
		list_len += strlen(dlist->entries[i]);
	list_len += dlist->nentries;
	len = list_len+strlen(path)+sizeof(struct cache_link);

	/* Make room for cache entry */
	if (!make_room_in_cache(len))
		return;

	/* Create and initialize the cache entry */
	ptr = XtNew(struct cache_link);
	ptr->type = DIRLIST;
	ptr->hostuser = hostuser;
	ptr->path = XtNewString(path);
	if (layout == TREE)
		ptr->part.d.layout = ICONIC;
	else
		ptr->part.d.layout = layout;
	ptr->part.d.dotfiles = dotfiles;
	ptr->len = len;
	ptr->part.d.count = dlist->nentries;
	ptr->part.d.list = XtMalloc(len);

	/* Now copy the actual directory entries */
	p = ptr->part.d.list;
	for (i=0; i<dlist->nentries; i++) {
		slen = strlen(dlist->entries[i]);
		bcopy(dlist->entries[i], p, slen+1);
		p += slen+1;
	}

	/* Insert the entry at the head of the the cache */
	insert_at_cache_head(ptr);
	cache_len += ptr->len;
	report_cache_addition();
}


/*
 * add_truepath_to_cache - Add to the cache the directory mapping specified
 *                         by the arguments.
 */
add_truepath_to_cache(host, path, true_path)
int host;
char *path;
char *true_path;
{
	struct hostuser_st *hostuser = head_hostuser;
	long len;
	struct cache_link *ptr;

	/* Make sure that cache len is consistent with current max len */
	check_max_cache_len();

	/* Find hostname/username pair */
	while (hostuser) {
		if (!strcmp(hinfo[host].hostname, hostuser->hostname) &&
				(!strcmp(hinfo[host].username, hostuser->username)))
			break;
		hostuser = hostuser->next;
	}
	if (hostuser == NULL) {
		hostuser = XtNew(struct hostuser_st);
		hostuser->hostname = XtNewString(hinfo[host].hostname);
		hostuser->username = XtNewString(hinfo[host].username);
		hostuser->next = head_hostuser;
		head_hostuser = hostuser;
	}

	/* If a mapping for "path" is already in the cache, delete it */
	ptr = find_path_in_cache(host, path, TRUEPATH);
	free_cache_entry(ptr);

	/* Determine the length of the mapping (plus overhead) */
	len = strlen(path)+strlen(true_path)+sizeof(struct cache_link);

	/* Make room for cache entry */
	if (!make_room_in_cache(len))
		return;

	/* Create and initialize the cache entry */
	ptr = XtNew(struct cache_link);
	ptr->type = TRUEPATH;
	ptr->hostuser = hostuser;
	ptr->path = XtNewString(path);
	ptr->part.m.true_path = XtNewString(true_path);
	ptr->len = len;

	/* Insert the entry at the head of the the cache */
	insert_at_cache_head(ptr);
	cache_len += ptr->len;
	report_cache_addition();
}


/*
 * fetch_dirlist_from_cache - If the directory list uniquely specified
 *                            by the input arguments exists in the cache,
 *                            then this function sets "dlist" to a pointer
 *                            to a structure that contains that directory
 *                            list.  Returns True if successful, else False.
 */
fetch_dirlist_from_cache(host, path, layout, dotfiles, dlist)
int host;
char *path;
int layout;
int dotfiles;
struct sl_struct **dlist;
{
	struct cache_link *ptr;
	int i;
	int slen;
	char *p;

	/* Make sure that cache len is consistent with current max len */
	check_max_cache_len();

	/* Look for directory in cache */
	if ((ptr = find_dirlist_in_cache(host, path, layout, dotfiles)) == NULL)
		return False;

	/* Move found directory to head of cache */
	if (ptr->prev)
		ptr->prev->next = ptr->next;
	else
		head = ptr->next;
	if (ptr->next)
		ptr->next->prev = ptr->prev;
	else
		tail = ptr->prev;
	insert_at_cache_head(ptr);

	/* Allocate list structure and fill it in */
	*dlist = XtNew(struct sl_struct);
	(*dlist)->nentries = ptr->part.d.count;
	(*dlist)->entries = (char **)XtMalloc(sizeof(char *)*ptr->part.d.count);
	p = ptr->part.d.list;
	for (i=0; i<ptr->part.d.count; i++) {
		slen = strlen(p)+1;
		(*dlist)->entries[i] = XtMalloc(slen);
		bcopy(p, (*dlist)->entries[i], slen);
		p += slen;
	}
	report_cache_dirlist_hit();
	return True;
}


/*
 * fetch_truepath_from_cache - If the true path information specified by the
 *                             input arguments exists in the cache, then
 *                             this function sets "true_path" to a pointer
 *                             to a string that contains the true directory
 *                             path for "path".  Returns True if successful,
 *                             else False.
 */
fetch_truepath_from_cache(host, path, true_path)
int host;
char *path;
char **true_path;
{
	struct cache_link *ptr;

	/* Make sure that cache len is consistent with current max len */
	check_max_cache_len();

	/* First look for mapping in cache */
	if ((ptr = find_path_in_cache(host, path, TRUEPATH)) == NULL)
		if ((ptr = find_truepath_in_cache(host, path)) == NULL)
			return False;

	/* Move found mapping to head of cache */
	if (ptr->prev)
		ptr->prev->next = ptr->next;
	else
		head = ptr->next;
	if (ptr->next)
		ptr->next->prev = ptr->prev;
	else
		tail = ptr->prev;
	insert_at_cache_head(ptr);

	/* Return true path */
	*true_path = XtNewString(ptr->part.m.true_path);
	report_cache_truepath_hit();
	return True;
}


/*
 * report_cache_use - Let's user know if cache information was used to
 *                    update "dirwin".  Set "used_cache" to True for
 *                    message to be displayed.  Set "used_cache" to
 *                    False for no message.
 */
report_cache_use(dirwin, used_cache)
struct dirwin_st *dirwin;
int used_cache;
{
	if (used_cache)
		XtManageChild(dirwin->w_cacheMsg);
	else
		XtUnmanageChild(dirwin->w_cacheMsg);
}


/*
 * report_cache_addition - Debug edit
 */
report_cache_addition()
{
	char msg[100];

	if (diagnostics == DEBUG) {
		sprintf(msg, "*** Entry added to cache.  Cache len = %ld\n", cache_len);
		write_log(msg);
	}
}


/*
 * report_cache_deletion - Debug edit
 */
report_cache_deletion(ptr)
struct cache_link *ptr;
{
	char msg[200];

	if (diagnostics == DEBUG) {
		if (ptr->type == DIRLIST) {
			sprintf(msg,
				"*** Removed directory list from cache.  Cache len = %ld\n",
				cache_len);
			write_log(msg);
			sprintf(msg, "          %s", ptr->path);
			write_log(msg);
			switch (ptr->part.d.layout) {
			case TABULAR:
				write_log(", Tabular");
				break;
			case ICONIC:
			case TREE:
				write_log(", Iconic/Tree");
				break;
			case FULL_INFO:
				write_log(", Full Info");
			}
			if (ptr->part.d.dotfiles)
				write_log(", Dotfiles");
			write_log("\n");
		} else {
			sprintf(msg,
				"*** Removed true path from cache.  Cache len = %ld\n",
				cache_len);
			write_log(msg);
			sprintf(msg, "          %s\n", ptr->path);
			write_log(msg);
		}
	}
}


/*
 * report_cache_truepath_hit - Debug edit
 */
report_cache_truepath_hit()
{

	if (diagnostics == DEBUG)
		write_log("*** True path cache hit\n");
}


/*
 * report_cache_dirlist_hit - Debug edit
 */
report_cache_dirlist_hit()
{

	if (diagnostics == DEBUG)
		write_log("*** Directory list cache hit\n");
}


/*
 * check_max_cache_len - If cache length exceeds the maximum allowable (this
 *                       could happen if the user changes this limit), then
 *                       free up as many of the least recently used cache
 *                       entries as necessary.
 */
check_max_cache_len()
{
	long bytes_available = max_dir_cache_len*BYTES_PER_KB-cache_len;

	/* Delete least recently used entries until cache len is small enough */
	while (head && (bytes_available<0)) {
		bytes_available += tail->len;
		free_cache_entry(tail);
	}

	/* Sanity check */
	if (bytes_available < 0)
		fatal_error("Bug in check_max_cache_len()");
}


/*
 * remove_dirlist_from_cache - Remove the cache entry that contains the
 *                             directory list that is  uniquely specified
 *                             by the arguments.
 */
remove_dirlist_from_cache(host, path, layout, dotfiles)
int host;
char *path;
int layout;
int dotfiles;
{
	free_cache_entry(find_dirlist_in_cache(host, path, layout, dotfiles));
}


/*
 * flush_cache_all - Removes all entries from the cache.
 */
flush_cache_all()
{
	while (head)
		free_cache_entry(head);
}


/*
 * flush_cache_directory - Remove all cache entries for the directory
 *                         specified by "host" and "path".
 */
flush_cache_directory(host, path)
int host;
char *path;
{
	struct hostuser_st *hostuser = head_hostuser;
	struct cache_link *ptr = head;
	struct cache_link *tptr;

	/* Find hostname/username pair */
	while (hostuser) {
		if (!strcmp(hinfo[host].hostname, hostuser->hostname) &&
				(!strcmp(hinfo[host].username, hostuser->username)))
			break;
		hostuser = hostuser->next;
	}
	if (hostuser == NULL)
		return;

	/* Now remove cache entries */
	while (ptr) {
		tptr = ptr;
		ptr = ptr->next;
		if ((hostuser == tptr->hostuser) && !strcmp(path, tptr->path))
			free_cache_entry(tptr);
	}
}