Search code examples
rfilefilesystemsfsfile-attributes

Difference between modification_time and change_time in fs::file_info()


The R package fs provides the function file_info() which returns a tibble containing i.a. the variables:

  • modification_time The time of last data modification, as a POSIXct datetime.
  • access_time The time of last access - as a POSIXct datetime.
  • change_time The time of last file status change - as a POSIXct datetime.

Now I'm wondering what are the differences between modification_time and change_time?

What I noticed so far is that change_time seems to be immune to "chronological inconsistencies". Example:

library(magrittr)
fs::file_create("test_file")
fs::file_info("test_file") %>% dplyr::select(modification_time, access_time, change_time)
#> # A tibble: 1 x 3
#>   modification_time   access_time         change_time        
#>   <dttm>              <dttm>              <dttm>             
#> 1 2019-10-24 13:23:35 2019-10-24 13:23:35 2019-10-24 13:23:35

# change access and modification times to current Sys.time()
# -> both modification_time and change_time will be updated
Sys.sleep(1)
fs::file_touch("test_file")
fs::file_info("test_file") %>% dplyr::select(modification_time, access_time, change_time)
#> # A tibble: 1 x 3
#>   modification_time   access_time         change_time        
#>   <dttm>              <dttm>              <dttm>             
#> 1 2019-10-24 13:23:37 2019-10-24 13:23:37 2019-10-24 13:23:37

# change access and modification times to the past (-2 min)
# -> only modification_time will be updated
fs::file_touch("test_file", Sys.time() - 120)
fs::file_info("test_file") %>% dplyr::select(modification_time, access_time, change_time)
#> # A tibble: 1 x 3
#>   modification_time   access_time         change_time        
#>   <dttm>              <dttm>              <dttm>             
#> 1 2019-10-24 13:21:37 2019-10-24 13:21:37 2019-10-24 13:23:37

# change  access and modification times to the future (+5 min)
# -> only modification_time will be updated
fs::file_touch("test_file", Sys.time() + 300)
fs::file_info("test_file") %>% dplyr::select(modification_time, access_time, change_time)
#> # A tibble: 1 x 3
#>   modification_time   access_time         change_time        
#>   <dttm>              <dttm>              <dttm>             
#> 1 2019-10-24 13:28:37 2019-10-24 13:28:37 2019-10-24 13:23:37

Created on 2019-10-24 by the reprex package (v0.3.0)

The R base function file.mtime() only returns the modification_time from above.


Solution

  • I guess the answer is twofold:

    1. The example above is misleading! The change_time always changes when access_time or modification_time is changed – it's just not perceptible from the output above because the time resolution is too low (seconds).

    2. modification_time gives the file modification timestamp. This (as well as access_time) can be overwritten by the user employing tools like touch or the fs::file_touch() function.

      change_time on the other hand gives the metadata modification timestamp and can't be directly modified by the user. Metadata refers to the last access time, last modification time, permissions and other properties listed in this answer to another Stack Overflow question.