1 | /* |
---|
2 | * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc. |
---|
3 | * |
---|
4 | * This file is part of Jam - see jam.c for Copyright information. |
---|
5 | */ |
---|
6 | |
---|
7 | /* This file is ALSO: |
---|
8 | * Copyright 2001-2004 David Abrahams. |
---|
9 | * Distributed under the Boost Software License, Version 1.0. |
---|
10 | * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) |
---|
11 | */ |
---|
12 | |
---|
13 | # include "jam.h" |
---|
14 | # include "hash.h" |
---|
15 | # include "filesys.h" |
---|
16 | # include "pathsys.h" |
---|
17 | # include "timestamp.h" |
---|
18 | # include "newstr.h" |
---|
19 | # include "strings.h" |
---|
20 | |
---|
21 | /* |
---|
22 | * timestamp.c - get the timestamp of a file or archive member |
---|
23 | * |
---|
24 | * 09/22/00 (seiwald) - downshift names on OS2, too |
---|
25 | */ |
---|
26 | |
---|
27 | /* |
---|
28 | * BINDING - all known files |
---|
29 | */ |
---|
30 | |
---|
31 | typedef struct _binding BINDING; |
---|
32 | |
---|
33 | struct _binding { |
---|
34 | char *name; |
---|
35 | short flags; |
---|
36 | |
---|
37 | # define BIND_SCANNED 0x01 /* if directory or arch, has been scanned */ |
---|
38 | |
---|
39 | short progress; |
---|
40 | |
---|
41 | # define BIND_INIT 0 /* never seen */ |
---|
42 | # define BIND_NOENTRY 1 /* timestamp requested but file never found */ |
---|
43 | # define BIND_SPOTTED 2 /* file found but not timed yet */ |
---|
44 | # define BIND_MISSING 3 /* file found but can't get timestamp */ |
---|
45 | # define BIND_FOUND 4 /* file found and time stamped */ |
---|
46 | |
---|
47 | time_t time; /* update time - 0 if not exist */ |
---|
48 | } ; |
---|
49 | |
---|
50 | static struct hash *bindhash = 0; |
---|
51 | static void time_enter( void *, char *, int , time_t ); |
---|
52 | |
---|
53 | static char *time_progress[] = |
---|
54 | { |
---|
55 | "INIT", |
---|
56 | "NOENTRY", |
---|
57 | "SPOTTED", |
---|
58 | "MISSING", |
---|
59 | "FOUND" |
---|
60 | } ; |
---|
61 | |
---|
62 | |
---|
63 | /* |
---|
64 | * timestamp() - return timestamp on a file, if present |
---|
65 | */ |
---|
66 | |
---|
67 | void |
---|
68 | timestamp( |
---|
69 | char *target, |
---|
70 | time_t *time ) |
---|
71 | { |
---|
72 | PATHNAME f1, f2; |
---|
73 | BINDING binding, *b = &binding; |
---|
74 | string buf[1]; |
---|
75 | |
---|
76 | # ifdef DOWNSHIFT_PATHS |
---|
77 | string path; |
---|
78 | char *p; |
---|
79 | |
---|
80 | string_copy( &path, target ); |
---|
81 | p = path.value; |
---|
82 | |
---|
83 | do |
---|
84 | { |
---|
85 | *p = tolower( *p ); |
---|
86 | # ifdef NT |
---|
87 | /* On NT, we must use backslashes or the file won't be found. */ |
---|
88 | if (*p == '/') |
---|
89 | *p = PATH_DELIM; |
---|
90 | # endif |
---|
91 | } |
---|
92 | while( *p++ ); |
---|
93 | |
---|
94 | target = path.value; |
---|
95 | # endif |
---|
96 | string_new( buf ); |
---|
97 | |
---|
98 | if( !bindhash ) |
---|
99 | bindhash = hashinit( sizeof( BINDING ), "bindings" ); |
---|
100 | |
---|
101 | /* Quick path - is it there? */ |
---|
102 | |
---|
103 | b->name = target; |
---|
104 | b->time = b->flags = 0; |
---|
105 | b->progress = BIND_INIT; |
---|
106 | |
---|
107 | if( hashenter( bindhash, (HASHDATA **)&b ) ) |
---|
108 | b->name = newstr( target ); /* never freed */ |
---|
109 | |
---|
110 | if( b->progress != BIND_INIT ) |
---|
111 | goto afterscanning; |
---|
112 | |
---|
113 | b->progress = BIND_NOENTRY; |
---|
114 | |
---|
115 | /* Not found - have to scan for it */ |
---|
116 | |
---|
117 | path_parse( target, &f1 ); |
---|
118 | |
---|
119 | /* Scan directory if not already done so */ |
---|
120 | |
---|
121 | { |
---|
122 | BINDING binding, *b = &binding; |
---|
123 | |
---|
124 | f2 = f1; |
---|
125 | f2.f_grist.len = 0; |
---|
126 | path_parent( &f2 ); |
---|
127 | path_build( &f2, buf, 0 ); |
---|
128 | |
---|
129 | b->name = buf->value; |
---|
130 | b->time = b->flags = 0; |
---|
131 | b->progress = BIND_INIT; |
---|
132 | |
---|
133 | if( hashenter( bindhash, (HASHDATA **)&b ) ) |
---|
134 | b->name = newstr( buf->value ); /* never freed */ |
---|
135 | |
---|
136 | if( !( b->flags & BIND_SCANNED ) ) |
---|
137 | { |
---|
138 | file_dirscan( buf->value, time_enter, bindhash ); |
---|
139 | b->flags |= BIND_SCANNED; |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | /* Scan archive if not already done so */ |
---|
144 | |
---|
145 | if( f1.f_member.len ) |
---|
146 | { |
---|
147 | BINDING binding, *b = &binding; |
---|
148 | |
---|
149 | f2 = f1; |
---|
150 | f2.f_grist.len = 0; |
---|
151 | f2.f_member.len = 0; |
---|
152 | string_truncate( buf, 0 ); |
---|
153 | path_build( &f2, buf, 0 ); |
---|
154 | |
---|
155 | b->name = buf->value; |
---|
156 | b->time = b->flags = 0; |
---|
157 | b->progress = BIND_INIT; |
---|
158 | |
---|
159 | if( hashenter( bindhash, (HASHDATA **)&b ) ) |
---|
160 | b->name = newstr( buf->value ); /* never freed */ |
---|
161 | |
---|
162 | if( !( b->flags & BIND_SCANNED ) ) |
---|
163 | { |
---|
164 | file_archscan( buf->value, time_enter, bindhash ); |
---|
165 | b->flags |= BIND_SCANNED; |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | afterscanning: |
---|
170 | |
---|
171 | if( b->progress == BIND_SPOTTED ) |
---|
172 | { |
---|
173 | if( file_time( b->name, &b->time ) < 0 ) |
---|
174 | b->progress = BIND_MISSING; |
---|
175 | else |
---|
176 | b->progress = BIND_FOUND; |
---|
177 | } |
---|
178 | |
---|
179 | *time = b->progress == BIND_FOUND ? b->time : 0; |
---|
180 | string_free( buf ); |
---|
181 | # ifdef DOWNSHIFT_PATHS |
---|
182 | string_free( &path ); |
---|
183 | #endif |
---|
184 | } |
---|
185 | |
---|
186 | static void |
---|
187 | time_enter( |
---|
188 | void *closure, |
---|
189 | char *target, |
---|
190 | int found, |
---|
191 | time_t time ) |
---|
192 | { |
---|
193 | BINDING binding, *b = &binding; |
---|
194 | struct hash *bindhash = (struct hash *)closure; |
---|
195 | |
---|
196 | # ifdef DOWNSHIFT_PATHS |
---|
197 | char path[ MAXJPATH ]; |
---|
198 | char *p = path; |
---|
199 | |
---|
200 | do *p++ = tolower( *target ); |
---|
201 | while( *target++ ); |
---|
202 | |
---|
203 | target = path; |
---|
204 | # endif |
---|
205 | |
---|
206 | b->name = target; |
---|
207 | b->flags = 0; |
---|
208 | |
---|
209 | if( hashenter( bindhash, (HASHDATA **)&b ) ) |
---|
210 | b->name = newstr( target ); /* never freed */ |
---|
211 | |
---|
212 | b->time = time; |
---|
213 | b->progress = found ? BIND_FOUND : BIND_SPOTTED; |
---|
214 | |
---|
215 | if( DEBUG_BINDSCAN ) |
---|
216 | printf( "time ( %s ) : %s\n", target, time_progress[b->progress] ); |
---|
217 | } |
---|
218 | |
---|
219 | /* |
---|
220 | * donestamps() - free timestamp tables |
---|
221 | */ |
---|
222 | |
---|
223 | void |
---|
224 | donestamps() |
---|
225 | { |
---|
226 | hashdone( bindhash ); |
---|
227 | } |
---|