Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cve_bin_tool/cvedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ def copy_db(self, filename, export=True):
if export:
shutil.copy(self.dbpath, filename)
else:
shutil.copy(filename, self.dbpath)
shutil.copy2(filename, self.dbpath)

def remove_cache_backup(self) -> None:
"""Removes the backup if database was successfully loaded."""
Expand Down
44 changes: 44 additions & 0 deletions test/test_cvedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,47 @@ def test_new_database_schema(self):
assert all(column in column_names for column in required_columns[table])

self.cvedb.db_close()

def test_import_preserves_file_timestamp(self):
"""Test that importing database preserves original file timestamp."""
import os
import tempfile
import time

from cve_bin_tool.cvedb import CVEDB

# Create temporary directories for source and target
with tempfile.TemporaryDirectory() as source_dir, tempfile.TemporaryDirectory() as target_dir:

# Create source database with CVEDB
cvedb_source = CVEDB(cachedir=source_dir)
cursor = cvedb_source.db_open_and_get_cursor()
# Create a minimal table to make it a valid database
cursor.execute("CREATE TABLE IF NOT EXISTS test_table (id INTEGER)")
cursor.execute("INSERT INTO test_table (id) VALUES (1)")
cvedb_source.db_close()

source_db = str(cvedb_source.dbpath)

# Set a specific modification time (2 hours ago)
original_mtime = time.time() - 7200
os.utime(source_db, (original_mtime, original_mtime))

# Wait a moment to ensure different timestamp if copy fails
time.sleep(0.1)

# Create target database and import
cvedb_target = CVEDB(cachedir=target_dir)
target_db = str(cvedb_target.dbpath)

# Import the source database (export=False)
cvedb_target.copy_db(source_db, export=False)

# Check that the target file has the same timestamp as source
source_mtime = os.path.getmtime(source_db)
target_mtime = os.path.getmtime(target_db)

# They should be the same (within small tolerance for filesystem precision)
assert (
abs(source_mtime - target_mtime) < 1
), f"File timestamp not preserved: source={source_mtime}, target={target_mtime}"
Loading