Split g_file_open() into _ro() and _rw() variants
Rename g_file_open() to g_file_open_rw(), and add a new g_file_open_ro() call that wraps the common g_file_open_ex(file, 1, 0, 0, 0) idiom. This will make the file access mode more explicit in the code. Change all calls to g_file_open() to the _ro() or _rw() variant as appropriate, and replace g_file_open_ex(file, 1, 0, 0, 0) with the _ro() call. Lastly, add tests for the two new calls to test_os_calls.c (code courteously provided by matt335672).
This commit is contained in:
+10
-12
@@ -2086,24 +2086,14 @@ g_memcmp(const void *s1, const void *s2, int len)
|
||||
/*****************************************************************************/
|
||||
/* returns -1 on error, else return handle or file descriptor */
|
||||
int
|
||||
g_file_open(const char *file_name)
|
||||
g_file_open_rw(const char *file_name)
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
return (int)CreateFileA(file_name, GENERIC_READ | GENERIC_WRITE,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
||||
0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
|
||||
#else
|
||||
int rv;
|
||||
|
||||
rv = open(file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
|
||||
|
||||
if (rv == -1)
|
||||
{
|
||||
/* can't open read / write, try to open read only */
|
||||
rv = open(file_name, O_RDONLY);
|
||||
}
|
||||
|
||||
return rv;
|
||||
return open(file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -2145,6 +2135,14 @@ g_file_open_ex(const char *file_name, int aread, int awrite,
|
||||
#endif
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/* returns -1 on error, else return handle or file descriptor */
|
||||
int
|
||||
g_file_open_ro(const char *file_name)
|
||||
{
|
||||
return g_file_open_ex(file_name, 1, 0, 0, 0);
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/* returns error, always 0 */
|
||||
int
|
||||
|
||||
Reference in New Issue
Block a user