fopen() file path in C - c

I have clone a git repository in C:/Repo. I am trying to open a file which lies in the git at some location ,for example, git/program/slm/error.txt. I am using fopen() API in C language to open the file and the filepath I am specifying is "C:/Repo/git/program/slm/error.txt". The program seems to be working while providing the above mentioned path. But,I want to make this program generic so that it can work on other systems as well as in other systems it is not necessary that the git will be cloned in C:/Repo only,this is local to my system. git/program/slm/error.txt is the relative path and will be common for all the system.
Can anyone please help me how what filepathname should I provide to make it generic so that it works on all the systems?

Take your repository path as commandline argument and then when you want to access the file append the repository path and the relative path that you want to access.

Related

Docker File No such file or directory - Absolute Path issue

Writing because I have a stranger problem with Docker File process
The problem is regarding Docker File Context. As far as I understood the directory context that I can access from Dockerfiles is one directory up and one directory down
Example Directory Tree
A - B - C - D - E
If my docketfile is on C
I can access B D
But I can’t access A E
I have a problem because this is my case
My Docker file is on C
And I need to access files from B D E
And I really don’t know how to do it
I need to access it
Becaiuse my target jar is on E
And I need to do an ADD to this file to implementing docket hot deploy with Spring Dev Tools
Somenthing like on Docker
ADD .\D\E\jar.file jar.file
ENtrypoint xxx
Expose xxx
And I still need to access B to get some other files.
Was Clear?
Sorry I know is strange
If you can do something it does not mean it is right or if is something not recommended so it means the issue can arise.
If you read General guidelines and recommendations, It will recommend keeping the thing in context, then why you need to copy thing from the different drive? Btw it is not possible in Linux as docker need to copy from the context so better to keep your jar file in dockerfile context.
Understand build context
When you issue a docker build command, the current working directory
is called the build context. By default, the Dockerfile is assumed to
be located here, but you can specify a different location with the
file flag (-f). Regardless of where the Dockerfile actually lives, all
recursive contents of files and directories in the current directory
are sent to the Docker daemon as the build context.

File extracted from ZIP not recognized until re-save

Q: Why would re-saving a file be different vs a direct extraction from a zip file? Particularly on Windows?
Context
I have an angular application that prepares a text file for import into a commercial machine. For user convenience, we provide the file inside a zip file so that the required folder structure can be provided to the user. They write this file to a USB drive and use that to import into the machine.
Problem
If the downloaded zip file is extracted directly onto the USB (to get the file and the required folder structure), the machine cannot recognize the embedded text file.
Troubleshooting
If I open the file in any text editor, add a space, delete the space, and re-save the file on the USB, then the machine will recognize the file. Alternatively, if I extract the zip onto the local file system, then copy the folder structure from the local file system to the USB, then the machine also will recognize it.
If I switch to Linux, then a 'write out' from nano will fix the file. If I use the touch command on the file, the problem remains.
Suspecting a whitespace/line-ending issue, I've tried several diff tools which reveal no apparent differences:
$ diff original.txt resaved.txt (Linux)
$ vbindiff original.txt resaved.txt (Linux)
> fc /b original.txt resaved.txt (Windows 7)
Other info:
Angular version: 5.2.10
Zip Utility in angular: JSZip 3.1.5
Unzip Utils: 7-Zip and Native Windows Explorer extract
JSZip code:
const zip = new JSZip();
zip.folder('FolderA/FolderB/FolderC').file('FILE.TXT', new File([contentString], 'TEMP.TXT', { type: 'text/plain' }));
zip.generateAsync({ type: 'blob' })
.then(function (content) {
saveAs(content, 'ZipFile.ZIP');
});
At this point, I'm out of ideas. Hoping someone here may have some insight into this odd behavior.
TL;DR: Check the file attributes (e.g. Archive, Read-Only, Hidden, System, etc).
Our system was specifically looking for the Archive bit and modifying the file in any way set this bit.
This was an ugly one to ferret out, but chatting with our embedded systems programmer for a bit led to the answer.
Our machine was specifically searching for the archive bit (Windows file attribute) when it was searching for files to import. This bit is a relic from Windows NTFS and is near obsolete. For all intents and purposes it is a dirty flag used to point out files that should be archived/backed up in the next backup run. There are much better ways to do this, so it has fallen out of style.
However, for whatever reason, our system is searching only for files with that bit set. That's why opening/copying/moving the file would fix the problem, because altering it in any way set this archive bit (dirty flag).
If you want to learn more about it, see here and here.
So, the moral of the story is to check these file attributes if you have a similar issue.
We are using the Harmony USB driver from Microchip, so this may be a nuance of that tool (or maybe just an artifact from one of the online examples).
You can see it this using the file properties in Windows Explorer or with the > attrib <file> command in Windows command prompt.
To fix:
Windows: You can set the value from the command prompt using > attrib +a <file> or remove it using > attrib -a <file>.
If using node.js on a Windows host, you can use the winattr library from NPM to manipulate these attributes.
Linux: You can use $ getfattr and $ setfattr to set the bit (see here and here).
Note: the answers I linked say to use $ setfattr -h -v 0x00000020 -n system.ntfs_attrib_be <target-file> but I got an operation not supported when I tried to do the same. I ended up using the java solution, but when I inspected the file afterward, it seemed the equivalent command would have been $ setfattr -n user.DOSATTRIB -v 0sMHgyMAA= <target-file>. Your mileage may vary but I offer it in case it helps anyone.
Java: You can also use Java from any system.

Escaping from chroot()

I'm working on a webserver in UNIX environment with C language. Currently, I have done jailing the process but now I cannot use syslog and logging to a file option and that's basically due to the changed root path.
New root path for the program is it's directory. I could not escape from it to real root "/" in order to use these functions and go back to jail root path again.
Are there any other alternatives or solutions to this?
The whole point of using chroot() is to make the real root inaccessible, so easy formula: If you can break it, you don't need it.
So, you should make syslog accessible from within your chroot environment instead. How? Just openlog() prior to chroot(). After that, you can syslog() even though you wouldn't be able to openlog() it anymore.
If your root is the working directory, don't use chroot, and remove the '/' at the beggining of all the relative path you use, or add '.' before this '/'.
Use chroot only if you want to fully work as if it was your system root.
If both env are on the same file system, you can use hard links so that under the chroot'ed env you see files "outside". It may not be so easy to configure everything to work, but it is possible. Change your viewpoint: don't try to escape from chroot, try to include things into.

where ShellExecute found the exe files

I am writing a program in pure C, using win32 api.
I need to know the full path of a registered program.
For example if I write
ShellExecute(0,0,"chrome",0,0,SW_SHOW)
the chrome browser starts. How can I obtain "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" from "chrome" as ShellExecute does?
In this case Chrome has registered itself in the App Paths registry section. More details over on MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/ee872121.aspx
Finding an Application Executable
When the ShellExecuteEx function is called with the name of an
executable file in its lpFile parameter, there are several places
where the function looks for the file. We recommend registering your
application in the App Paths registry subkey. Doing so avoids the need
for applications to modify the system PATH environment variable.
The file is sought in the following locations:
The current working directory.
The Windows directory only (no subdirectories are searched).
The Windows\System32 directory.
Directories listed in the PATH environment variable.
Recommended: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
The documentation tells you how the shell searches, and you can replicate that search.
The function SHEvaluateSystemCommandTemplate does exactly that. It performs the exact same search algorithm that is performed by ShellExecute. You just pass "chrome.exe" (or even just "chrome"), and the full path to the chrome executable is returned in the ppszApplication parameter.

Which function is called when changing a directory in FUSE?

I'm making a filesystem using FUSE, and know I have a doubt. When I use the "cd" command in the new filesystem, it changes to directories that doesn't exist.
For example, if the directory "m" doesn't exist, and I make a "cd m" it changes to that directory.
Which is the function that FUSE calls when the directory is changed? Why is the app doing the problem I describe?
Thanks!
Are you implementing getattr? and if so, are you making sure to return -ENOENT if the path they give you doesn't correspond to a file or directory in your system?

Resources