View Javadoc
1   /*
2    * This file is part of dependency-check-core.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Copyright (c) 2024 Jeremy Long. All Rights Reserved.
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   * @author Jeremy Long
33   */
34  public abstract class LocalDataSource implements CachedWebDataSource {
35  
36      /**
37       * Static logger.
38       */
39      private static final Logger LOGGER = LoggerFactory.getLogger(LocalDataSource.class);
40  
41      /**
42       * Saves the timestamp in a properties file next to the provided repo file
43       *
44       * @param repo the local file data source
45       * @param timestamp the epoch timestamp to store
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       * Retrieves the last updated date from the local file system (in a file
60       * next to the repo file).
61       *
62       * @param repo the local file data source
63       * @return the epoch timestamp of the last updated date/time
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                  //fall back on conversion from file last modified to storing in the db.
78                  lastUpdatedOn = repo.lastModified();
79              }
80          }
81          return lastUpdatedOn;
82      }
83  }