I am saving some data into .txt
file like this:
void save_file()
{
char *file_name = "my_file.txt";
FILE *f = fopen(file_name, "w");
if (f == NULL)
return;
// some fprintf instructions
fclose(f);
}
Everything works perfectly. However, I would like this file to have read-only property enabled. (Windows 10)
Is there a solution for my problem using standard libraries only?
C does not have the concept of file security out of the box. Since you are using Windows, you can use Windows' security APIs to set the ACL for particular users or groups to "R" (read-only). Here is how to do it for a specific user:
#include <Windows.h>
#include <aclapi.h>
DWORD WINAPI MakeUserAccessReadOnly(
LPWSTR lpPath,
SE_OBJECT_TYPE objType,
LPWSTR lpUser
)
{
PSECURITY_DESCRIPTOR pSD;
PACL pCurrentACL, pNewACL;
EXPLICIT_ACCESSW ea;
DWORD dwError;
dwError = GetNamedSecurityInfoW(lpPath, objType, DACL_SECURITY_INFORMATION, NULL, NULL, &pCurrentACL, NULL, &pSD);
if (dwError != ERROR_SUCCESS)
{
return dwError;
}
ZeroMemory(&ea, sizeof(EXPLICIT_ACCESSW));
ea.grfAccessPermissions = GENERIC_READ;
ea.grfAccessMode = SET_ACCESS;
ea.grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
ea.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
ea.Trustee.TrusteeType = TRUSTEE_IS_USER;
ea.Trustee.ptstrName = lpUser;
dwError = SetEntriesInAclW(1, &ea, pCurrentACL, &pNewACL);
if (dwError != ERROR_SUCCESS)
{
return dwError;
}
dwError = SetNamedSecurityInfoW(lpPath, objType, DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION, NULL, NULL, pNewACL, NULL);
LocalFree((HLOCAL)pNewACL);
return dwError;
}
You can then call the function like so:
MakeUserAccessReadOnly(L"C:\\Path\\FileToMakeReadOnly.ABC", SE_FILE_OBJECT, L"PC-NAME\\UserName");
Then, assuming that the user named UserName
doesn't have write access to the file through, say, membership in a group that also has write access to the file, they will only have read access to the file.