Post by Scott LurndalPost by Thiago Adams(Followup set to comp.unix.programmer)
How to copy a file on linux keeping the original file information?
timestamp etc?
Or in other words the equivalent of CopyFileA from windows.
comp.unix.programmer is likely to give you better answers. I don't
think POSIX defines any functions that copy files.
No, AFAIK, POSIX doesn't define an "all-in-one" file copy function.
However, Linux (the OS Thiago asks about) defines a number of them.
If I uunderstand correctly, you want to do the equivalent of "cp -p",
Which /can/ be accomplished with POSIX calls
- the standard I/O functions (fopen()/open(), fread()/read(),
fwrite()/write(), fclose()/close() ) to copy the file data
- stat() to obtain the "original file information", and
- utime() to set the copy timestamps,
- chmod() to set the copy file permissions
- chown() to set the file ownership
But, perhaps Thiago would be satisfied with just a hardlinked file.
but from C rather than from a shell. You might consider using system(),
but that has some drawbacks, and there are probably better ways.
I am using this to create a "deploy" folder with all files necessary to
run a program. So on windows I am coping dlls and I think it is a good
idea to keep the original file attributes . (I am not sure if zip files
keep that.. I need to check)
Both the tar(1) and cpio(1) utilities can be used to create a
compressed deployable archive. They're the most portable way
to distribute anything and they keep and restore the attributes
(although the ownership attributes uid and gid can only be
restored by root if they differ from the current users).
Windows, well 'nuf said.
For Windows, you need more than a USENETter.
I know of some programmers who have "carved out a niche", but, they
haven't tried all the packaging methods. They may not make good
candidates for bootstrapping (they may not know the absolute best
install method). It is difficult to find people "who have tried it all".
In the past, these guys were FOSS and they only did enough Windows
to get by. This is their page. MSI is a semi-popular option for
packaging (pretty complicated), and MSIX is the latest version
(not all that popular, only Windows might ship materials like that).
"NSIS"
https://firefox-source-docs.mozilla.org/browser/installer/windows/installer/index.html
That's not going to teach you very much. I don't know whether the installer
scripts are in the Firefox tarball or not. Some of their materials
are kept private.
When msiexec installs the materials in an MSI file, the dates and so on
would be preserved. If you compiled in 2015, the 2015 date is going to be
on the file when it is installed on my machine.
An example of a preservative copy routine, would be the Windows-bundled "robocopy.exe",
which was an employee programming effort that eventually went from
hobby-ware, to being fully supported in the OS. The "DATSO" option
would make a verbatim copy of a 2015 executable for example, nicely
stamped as 2015.
But these folders, these are owned by TrustedInstaller:
C:\Program Files
C:\Program Files (x86)
and you have to copy the TrustedInstaller token from a running "msiexec",
to have TrustedInstaller privileges and write to those folders. You can
attempt to write there, and your writes are "redirected". Without going
into more detail, "bootleg" copying really isn't supported, and even if
you developed a recipe, it could be broken tomorrow because it is not
supported. Nobody does installs using Robocopy.
I have a program that used to be able to make a program
"trusted installer", but that program as well as psexec, no longer work.
Similarly, some of the easy holes that allowed you to hack administrator,
no longer work (I helped one guy on USENET regain his administrator
account using one of those holes -- users can erase Administrator and
lose control!). That is a very nice thing for tech support purposes,
for remote work, but they've been steadily whittling away at the holes.
It means, if I know of one that still works, I have
to be careful to not "advertise it". Or they'll close the hole.
This is why installer packages are popular, as they grind down the
sharp edges for you. Using an actual installer, a popular installer,
Microsoft likely try not to break those.
My mention of Robocopy, is only so you can see a program make
a verbatim copy for you. Robocopy is really a folder-copying program,
so you have to be aware of the purpose, to craft a good syntax.
Be very careful with options such as /MIR (mirror), as mirror has
been used to erase the destination folder. It can do terrible damage,
and do it quickly, with a mis-crafted command.
Manual page:
https://ss64.com/nt/robocopy.html
Actually using it:
https://serverfault.com/questions/420105/robocopy-and-permissions
Paul