Re: Windows to Linux Porting - timeCreated and timeLastAccessed

2018-05-07 Thread Jesse Phillips via Digitalmars-d-learn

On Monday, 7 May 2018 at 14:31:23 UTC, Jesse Phillips wrote:
I wouldn't use time created. It can be newer than last modified 
this wholey inacurate. Last accessed could be a much more 
appopriate choice if trying to determine what is important.


Sorry, to answer your actual question, I do believe that using 
the OS version specification to select the function is correct.


Re: Windows to Linux Porting - timeCreated and timeLastAccessed

2018-05-07 Thread Jesse Phillips via Digitalmars-d-learn

On Friday, 4 May 2018 at 11:49:24 UTC, Vino wrote:

Hi All,

  Request your help, I have a D program written on Windows 
platform and the program is working as expected, now i am 
trying to port the same program to Linux, my program use the 
function "timeCreated" from std.file for Windows hugely where 
as in Linux we do not have the same function hence planned to 
use the function "timeLastAccessed" from std.file, so what is 
the best approach to port the program. I tried the below code 
but not working, so can you one please guide me on the right 
method to port the program to linux, below is the example code.


I wouldn't use time created. It can be newer than last modified 
this wholey inacurate. Last accessed could be a much more 
appopriate choice if trying to determine what is important.


Re: Windows to Linux Porting - timeCreated and timeLastAccessed

2018-05-07 Thread wjoe via Digitalmars-d-learn

On Friday, 4 May 2018 at 15:42:56 UTC, wjoe wrote:
I think that's not possible. You can't query information that 
hasn't been stored.


I stand corrected.

As Russel Winder points out there are file systems that store 
this information and since Linux 4.11 you can query it via 
statx(2).


Re: Windows to Linux Porting - timeCreated and timeLastAccessed

2018-05-05 Thread Russel Winder via Digitalmars-d-learn
On Fri, 2018-05-04 at 08:02 -0600, Jonathan M Davis via Digitalmars-d-learn
wrote:
> 
[…]
> 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
[…]

Linux itself doesn't but most filesystems certainly do.  Ext4 definitely does.


https://unix.stackexchange.com/questions/7562/what-file-systems-on-linux-store
-the-creation-time

-- 
Russel.
==
Dr Russel Winder  t: +44 20 7585 2200
41 Buckmaster Roadm: +44 7770 465 077
London SW11 1EN, UK   w: www.russel.org.uk


signature.asc
Description: This is a digitally signed message part


Re: Windows to Linux Porting - timeCreated and timeLastAccessed

2018-05-04 Thread Kagamin via Digitalmars-d-learn
If you just want to clean logs, then use modification time on all 
oses:


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.timeLastModified)));

 return dFiles;
}


Re: Windows to Linux Porting - timeCreated and timeLastAccessed

2018-05-04 Thread wjoe via Digitalmars-d-learn

On Friday, 4 May 2018 at 15:30:26 UTC, Vino wrote:

On Friday, 4 May 2018 at 15:16:23 UTC, wjoe wrote:

[...]


Hi Wjoe,

  Thank you very much, but what i am expecting is something 
like OS switch, based of OS type switch the funciton eg:


If OS is windows use the funciton timeCreated else if the OS is 
linux use the function timeLastAccessed in the below example 
program, something similar as stated in the link


https://dlang.org/spec/declaration.html#alias

Eg1:
version (Win32)
{
alias myfoo = win32.foo;
}
version (linux)
{
alias myfoo = linux.bar;
}



auto clogClean (string LogDir ) {
Array!(Tuple!(string, SysTime)) dFiles;


version (Windows) {  alias sTimeStamp = 
std.file.DirEntry.timeCreated;} else version (linux) { alias 
sTimeStamp = std.file.DirEntry.timeLastAccessed; }



dFiles.insert(dirEntries(LogDir, SpanMode.shallow).filter!(a => 
a.isFile).map!(a => tuple(a.name, a.sTimeStamp)));

 return dFiles;
}

From,
Vino.B


I think that's not possible. You can't query information that 
hasn't been stored.


Your best bet in a Change-Function-Name-Port would probably be to 
use access time and mount the file system with options that won't 
touch that anymore after creation. But me thinks such a solution 
would be rather unreliable and not worth the time investment.


Re: Windows to Linux Porting - timeCreated and timeLastAccessed

2018-05-04 Thread Vino via Digitalmars-d-learn

On Friday, 4 May 2018 at 15:16:23 UTC, wjoe wrote:

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:

[...]


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.


Hi Wjoe,

  Thank you very much, but what i am expecting is something like 
OS switch, based of OS type switch the funciton eg:


If OS is windows use the funciton timeCreated else if the OS is 
linux use the function timeLastAccessed in the below example 
program, something similar as stated in the link


https://dlang.org/spec/declaration.html#alias

Eg1:
version (Win32)
{
alias myfoo = win32.foo;
}
version (linux)
{
alias myfoo = linux.bar;
}



auto clogClean (string LogDir ) {
Array!(Tuple!(string, SysTime)) dFiles;


version (Windows) {  alias sTimeStamp = 
std.file.DirEntry.timeCreated;} else version (linux) { alias 
sTimeStamp = std.file.DirEntry.timeLastAccessed; }



dFiles.insert(dirEntries(LogDir, SpanMode.shallow).filter!(a => 
a.isFile).map!(a => tuple(a.name, a.sTimeStamp)));

 return dFiles;
}

From,
Vino.B


Re: Windows to Linux Porting - timeCreated and timeLastAccessed

2018-05-04 Thread wjoe via Digitalmars-d-learn

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.




Re: Windows to Linux Porting - timeCreated and timeLastAccessed

2018-05-04 Thread Vino via Digitalmars-d-learn

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


Re: Windows to Linux Porting - timeCreated and timeLastAccessed

2018-05-04 Thread Jonathan M Davis via Digitalmars-d-learn
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



Re: Windows to Linux Porting - timeCreated and timeLastAccessed

2018-05-04 Thread Vino via Digitalmars-d-learn

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.




From,
Vino.B



Re: Windows to Linux Porting

2018-05-04 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, May 04, 2018 10:25:28 Russel Winder via Digitalmars-d-learn 
wrote:
> On Fri, 2018-05-04 at 08:47 +, Vino via Digitalmars-d-learn wrote:
>
> […]
>
> >   Was able to resolve the issue, the issue was the letter "L" in
> >
> > version (Linux) where is should be version (linux).
>
> It would have helped if I had read the code first rather than jumped to a
> conclusion.
>
> :-)

It happens to us all from time to time, and the casing of the version
identifiers can be easy to screw up and easy to miss the mistakes -
especially since many of them are based on the names that get used with
#ifdef in C/C++ rather than having consistent casing across the various
version identifiers.

- Jonathan M Davis




Re: Windows to Linux Porting - timeCreated and timeLastAccessed

2018-05-04 Thread Adam D. Ruppe via Digitalmars-d-learn
What are you actually trying to do with it? These functions are 
probably the wholly wrong approach.


Windows to Linux Porting - timeCreated and timeLastAccessed

2018-05-04 Thread Vino via Digitalmars-d-learn

Hi All,

  Request your help, I have a D program written on Windows 
platform and the program is working as expected, now i am trying 
to port the same program to Linux, my program use the function 
"timeCreated" from std.file for Windows hugely where as in Linux 
we do not have the same function hence planned to use the 
function "timeLastAccessed" from std.file, so what is the best 
approach to port the program. I tried the below code but not 
working, so can you one please guide me on the right method to 
port the program to linux, below is the example code.


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




Re: Windows to Linux Porting

2018-05-04 Thread Vino via Digitalmars-d-learn

On Friday, 4 May 2018 at 09:25:28 UTC, Russel Winder wrote:
On Fri, 2018-05-04 at 08:47 +, Vino via Digitalmars-d-learn 
wrote:



[…]
  Was able to resolve the issue, the issue was the letter "L" 
in

version (Linux) where is should be version (linux).


It would have helped if I had read the code first rather than 
jumped to a conclusion.


:-)


Hi Russel,
 No issue, and thank you for your help.

From,
Vino.B




Re: Windows to Linux Porting

2018-05-04 Thread Russel Winder via Digitalmars-d-learn
On Fri, 2018-05-04 at 08:47 +, Vino via Digitalmars-d-learn wrote:
> 
[…]
>   Was able to resolve the issue, the issue was the letter "L" in 
> version (Linux) where is should be version (linux).

It would have helped if I had read the code first rather than jumped to a
conclusion.

:-)



-- 
Russel.
==
Dr Russel Winder  t: +44 20 7585 2200
41 Buckmaster Roadm: +44 7770 465 077
London SW11 1EN, UK   w: www.russel.org.uk


signature.asc
Description: This is a digitally signed message part


Re: Windows to Linux Porting

2018-05-04 Thread Vino via Digitalmars-d-learn

On Friday, 4 May 2018 at 07:43:39 UTC, Russel Winder wrote:
On Fri, 2018-05-04 at 03:30 +, Vino via Digitalmars-d-learn 
wrote:

[...]


`./nasconfig.txt`

perhaps: Linux uses / (as does Windows in fact) for directory 
separator.



[...]


Hi Russel,

 Was able to resolve the issue, the issue was the letter "L" in 
version (Linux) where is should be version (linux).



From,
Vino.B


Re: Windows to Linux Porting

2018-05-04 Thread Russel Winder via Digitalmars-d-learn
On Fri, 2018-05-04 at 03:30 +, Vino via Digitalmars-d-learn wrote:
> Hi All,
> 
>Request you help on the below code, the below code always state 
> the file does not exist even if the file do exist.
> 
> Code:
> 
> import core.stdc.stdlib: exit;
> import std.stdio;
> import std.file;
> import std.path;
> 
> auto osSwitch () {
> string ConfigFile;
> version (Windows) { ConfigFile = absolutePath(`.\nasconfig.txt`); 

`./nasconfig.txt`

perhaps: Linux uses / (as does Windows in fact) for directory separator. 

> } else version (Linux) { ConfigFile = 
> absolutePath(`nasconfig.txt`); }
> return ConfigFile;
> }
> void main () {
> auto ConfigFile = osSwitch;
> if (!ConfigFile.exists) { writeln("The Configuration File ", 
> buildNormalizedPath(ConfigFile), " do to exist, Terminating the 
> execution.."); exit(-1);}
> else { writeln(ConfigFile); }
> }
> 
> From,
> Vino.B
> 
-- 
Russel.
==
Dr Russel Winder  t: +44 20 7585 2200
41 Buckmaster Roadm: +44 7770 465 077
London SW11 1EN, UK   w: www.russel.org.uk


signature.asc
Description: This is a digitally signed message part


Windows to Linux Porting

2018-05-03 Thread Vino via Digitalmars-d-learn

Hi All,

  Request you help on the below code, the below code always state 
the file does not exist even if the file do exist.


Code:

import core.stdc.stdlib: exit;
import std.stdio;
import std.file;
import std.path;

auto osSwitch () {
string ConfigFile;
version (Windows) { ConfigFile = absolutePath(`.\nasconfig.txt`); 
} else version (Linux) { ConfigFile = 
absolutePath(`nasconfig.txt`); }

return ConfigFile;
}
void main () {
auto ConfigFile = osSwitch;
if (!ConfigFile.exists) { writeln("The Configuration File ", 
buildNormalizedPath(ConfigFile), " do to exist, Terminating the 
execution.."); exit(-1);}

else { writeln(ConfigFile); }
}

From,
Vino.B