1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.data.update;
19
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.util.Properties;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30
31
32
33
34 public abstract class LocalDataSource implements CachedWebDataSource {
35
36
37
38
39 private static final Logger LOGGER = LoggerFactory.getLogger(LocalDataSource.class);
40
41
42
43
44
45
46
47 protected void saveLastUpdated(File repo, long timestamp) {
48 final File timestampFile = new File(repo + ".properties");
49 try (OutputStream out = new FileOutputStream(timestampFile)) {
50 final Properties prop = new Properties();
51 prop.setProperty("LAST_UPDATED", String.valueOf(timestamp));
52 prop.store(out, null);
53 } catch (IOException ex) {
54 throw new RuntimeException(ex);
55 }
56 }
57
58
59
60
61
62
63
64
65 protected long getLastUpdated(File repo) {
66 long lastUpdatedOn = 0;
67 final File timestampFile = new File(repo + ".properties");
68 if (timestampFile.isFile()) {
69 try (InputStream is = new FileInputStream(timestampFile)) {
70 final Properties props = new Properties();
71 props.load(is);
72 lastUpdatedOn = Integer.parseInt(props.getProperty("LAST_UPDATED", "0"));
73 } catch (IOException | NumberFormatException ex) {
74 LOGGER.debug("error reading timestamp file", ex);
75 }
76 if (lastUpdatedOn <= 0) {
77
78 lastUpdatedOn = repo.lastModified();
79 }
80 }
81 return lastUpdatedOn;
82 }
83 }