Windows to Linux Porting - timeCreated and timeLastAccessed

wjoe none at example.com
Fri May 4 15:16:23 UTC 2018


On Friday, 4 May 2018 at 14:24:36 UTC, Vino wrote:
> On Friday, 4 May 2018 at 14:02:24 UTC, Jonathan M Davis wrote:
>> On Friday, May 04, 2018 13:17:36 Vino via Digitalmars-d-learn 
>> wrote:
>>> On Friday, 4 May 2018 at 12:38:07 UTC, Adam D. Ruppe wrote:
>>> > What are you actually trying to do with it? These functions 
>>> > are probably the wholly wrong approach.
>>>
>>> Hi Adam,
>>>
>>>   The existing program in Windows do few task's eg: Delete 
>>> files
>>> older that certain days, and now we are trying to port to 
>>> Linux,
>>> and above was just a example, hence asked the right approach 
>>> for
>>> porting.
>>
>> Linux does not keep track of the creation time of a file. So, 
>> it will not work to have a program on Linux ask a file how 
>> long it's been since the file was created. If you want that 
>> information, you'll have to store it elsewhere somehow (and 
>> that generally only works if you created the file in the first 
>> place).
>>
>> The modification time of the file is the time that the file 
>> was last changed (which would be the creation time if it were 
>> only ever written to once, but in the general case, it has no 
>> relation to the creation time at all). So, you could use 
>> std.file.timeLastModified to find out if a file has been 
>> changed within the last x number of days, but there is no way 
>> to find out the creation time of a file by asking the 
>> filesystem.
>>
>> - Jonathan M Davis
>
> Hi Jonathan,
>
>   Thank you,  i got your point from the other forum topic which 
> was raised by me earlier, hence decided to use modification 
> time, the request is on how and the best approach to port the 
> code from windows to Linux eg program below
>
> Example Code:
> import std.stdio: writeln;
> import std.container.array;
> import std.file: dirEntries,isFile, SpanMode;
> import std.algorithm: filter, map;
> import std.typecons: Tuple, tuple;
> import std.datetime.systime: SysTime;
>
>
> version (Windows) { alias sTimeStamp = timeCreated; } else 
> version (linux) { alias sTimeStamp = timeLastAccessed; }
>
> auto clogClean (string LogDir ) {
> Array!(Tuple!(string, SysTime)) dFiles;
>  dFiles.insert(dirEntries(LogDir, SpanMode.shallow).filter!(a 
> => a.isFile).map!(a => tuple(a.name, a.sTimeStamp)));
>  return dFiles;
> }
>
> void main () {
> string LogDir;
> LogDir = "//DScript/Test";       //  Error: undefined 
> identifier timeLastAccessed on Linux
> LogDir = "C:\\DScript\\Others";  //  Error: undefined 
> identifier timeCreated on Windows.
> writeln(clogClean(LogDir));
> }
>
> From,
> Vino.B

Unlike NTFS for Windows there's a plethora of different file 
systems available to use for Linux, one of which doesn't even 
support deletion of files. You also have to keep in mind that 
even if the same file system is used, there is no guarantee that 
you have the same set of metadata available for a mount point 
each and every time.
Consider a file system that's mounted with the 'noatime' option, 
for instance, which doesn't log access times.

As far as I understand you are globing files, check times and 
then act upon that.
If I were to port this to Linux, or any other OS for that matter, 
I wouldn't depend on a feature of an OS.
Instead, since you have to look at a file either way to get the 
meta data (which you query with the stat family of functions), I 
would build my own database or cache with that information.
Glob the directory and add files not yet present with the current 
date (and any other meta data you might need).
Then query all the files of interest and do whatever you want to 
do with them and remove the entry.

Downside is you have possibly another dependency. On the plus 
side you could easily query all files older than X days or 
whatever with a single select and batch process them.



More information about the Digitalmars-d-learn mailing list