summaryrefslogtreecommitdiff
path: root/timestamp.c
blob: 8a59c8c0e0fbd344578aeb83344a045694fd2fd3 (plain)
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
/*
 * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
 *
 * This file is part of Jam - see jam.c for Copyright information.
 */

/*  This file is ALSO:
 *  Copyright 2001-2004 David Abrahams.
 *  Distributed under the Boost Software License, Version 1.0.
 *  (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
 */

# include "jam.h"

# include "hash.h"
# include "filesys.h"
# include "pathsys.h"
# include "timestamp.h"
# include "newstr.h"
# include "strings.h"

/*
 * timestamp.c - get the timestamp of a file or archive member
 *
 * 09/22/00 (seiwald) - downshift names on OS2, too
 */

/*
 * BINDING - all known files
 */

typedef struct _binding BINDING;

struct _binding {
    char    *name;
    short   flags;

# define BIND_SCANNED   0x01    /* if directory or arch, has been scanned */

    short   progress;

# define BIND_INIT  0   /* never seen */
# define BIND_NOENTRY   1   /* timestamp requested but file never found */
# define BIND_SPOTTED   2   /* file found but not timed yet */
# define BIND_MISSING   3   /* file found but can't get timestamp */
# define BIND_FOUND 4   /* file found and time stamped */

    time_t  time;       /* update time - 0 if not exist */
};

static struct hash * bindhash = 0;
static void time_enter( void *, char *, int, time_t );

static char * time_progress[] =
{
    "INIT",
    "NOENTRY",
    "SPOTTED",
    "MISSING",
    "FOUND"
};


/*
 * timestamp() - return timestamp on a file, if present.
 */

void timestamp( char * target, time_t * time )
{
    PROFILE_ENTER( timestamp );

    PATHNAME   f1;
    PATHNAME   f2;
    BINDING    binding;
    BINDING  * b = &binding;
    string     buf[ 1 ];
#ifdef DOWNSHIFT_PATHS
    string     path;
    char     * p;
#endif

#ifdef DOWNSHIFT_PATHS
    string_copy( &path, target );
    p = path.value;

    do
    {
        *p = tolower( *p );
#ifdef NT
        /* On NT, we must use backslashes or the file will not be found. */
        if ( *p == '/' )
            *p = PATH_DELIM;
#endif
    }
    while ( *p++ );

    target = path.value;
#endif  /* #ifdef DOWNSHIFT_PATHS */
    string_new( buf );

    if ( !bindhash )
        bindhash = hashinit( sizeof( BINDING ), "bindings" );

    /* Quick path - is it there? */
    b->name = target;
    b->time = b->flags = 0;
    b->progress = BIND_INIT;

    if ( hashenter( bindhash, (HASHDATA * *)&b ) )
        b->name = newstr( target );  /* never freed */

    if ( b->progress != BIND_INIT )
        goto afterscanning;

    b->progress = BIND_NOENTRY;

    /* Not found - have to scan for it. */
    path_parse( target, &f1 );

    /* Scan directory if not already done so. */
    {
        BINDING binding;
        BINDING * b = &binding;

        f2 = f1;
        f2.f_grist.len = 0;
        path_parent( &f2 );
        path_build( &f2, buf, 0 );

        b->name = buf->value;
        b->time = b->flags = 0;
        b->progress = BIND_INIT;

        if ( hashenter( bindhash, (HASHDATA * *)&b ) )
            b->name = newstr( buf->value );  /* never freed */

        if ( !( b->flags & BIND_SCANNED ) )
        {
            file_dirscan( buf->value, time_enter, bindhash );
            b->flags |= BIND_SCANNED;
        }
    }

    /* Scan archive if not already done so. */
    if ( f1.f_member.len )
    {
        BINDING binding;
        BINDING * b = &binding;

        f2 = f1;
        f2.f_grist.len = 0;
        f2.f_member.len = 0;
        string_truncate( buf, 0 );
        path_build( &f2, buf, 0 );

        b->name = buf->value;
        b->time = b->flags = 0;
        b->progress = BIND_INIT;

        if ( hashenter( bindhash, (HASHDATA * *)&b ) )
            b->name = newstr( buf->value );  /* never freed */

        if ( !( b->flags & BIND_SCANNED ) )
        {
            file_archscan( buf->value, time_enter, bindhash );
            b->flags |= BIND_SCANNED;
        }
    }

    afterscanning:

    if ( b->progress == BIND_SPOTTED )
    {
         b->progress = file_time( b->name, &b->time ) < 0
            ? BIND_MISSING
            : BIND_FOUND;
    }

    *time = b->progress == BIND_FOUND ? b->time : 0;
        string_free( buf );
#ifdef DOWNSHIFT_PATHS
    string_free( &path );
#endif

    PROFILE_EXIT( timestamp );
}


static void time_enter( void * closure, char * target, int found, time_t time )
{
    BINDING binding;
    BINDING * b = &binding;
    struct hash * bindhash = (struct hash *)closure;

#ifdef DOWNSHIFT_PATHS
    char path[ MAXJPATH ];
    char * p = path;

    do *p++ = tolower( *target );
    while ( *target++ );

    target = path;
#endif

    b->name = target;
    b->flags = 0;

    if ( hashenter( bindhash, (HASHDATA * *)&b ) )
        b->name = newstr( target );  /* never freed */

    b->time = time;
    b->progress = found ? BIND_FOUND : BIND_SPOTTED;

    if ( DEBUG_BINDSCAN )
        printf( "time ( %s ) : %s\n", target, time_progress[ b->progress ] );
}


/*
 * stamps_done() - free timestamp tables.
 */

void stamps_done()
{
    hashdone( bindhash );
}