Page MenuHome

fileops_file_is_writable.patch

fileops_file_is_writable.patch

commit e1a60030390527f7fe9e1f808f4e58bf6403de8b
Author: Lawrence D'Oliveiro <ldo@geek-central.gen.nz>
Date: Mon Feb 18 02:45:51 2013 +0000
More efficient implementation of BLI_file_is_writable using access(2) instead of actually opening file
diff --git a/source/blender/blenlib/intern/fileops.c b/source/blender/blenlib/intern/fileops.c
index 1111d0d..6968ed2 100644
--- a/source/blender/blenlib/intern/fileops.c
+++ b/source/blender/blenlib/intern/fileops.c
@@ -156,33 +156,33 @@ char *BLI_file_ungzip_to_mem(const char *from_file, int *size_r)
}
-/* return 1 when file can be written */
bool BLI_file_is_writable(const char *filename)
-{
- int file;
-
- /* first try to open without creating */
- file = BLI_open(filename, O_BINARY | O_RDWR, 0666);
-
- if (file < 0) {
- /* now try to open and create. a test without actually
- * creating a file would be nice, but how? */
- file = BLI_open(filename, O_BINARY | O_RDWR | O_CREAT, 0666);
-
- if (file < 0) {
- return false;
- }
- else {
- /* success, delete the file we create */
- close(file);
- BLI_delete(filename, false, false);
- return true;
- }
- }
- else {
- close(file);
- return true;
- }
+/* returns true iff the file with the specified name can be written.
+This implementation uses access(2), which makes the check according
+to the real UID and GID of the process, not its effective UID and GID.
+This shouldn't matter for Blender, which is not going to run privileged
+anyway. */
+{
+ bool writable;
+ if (access(filename, W_OK) == 0)
+ {
+ /* file exists and I can write to it */
+ writable = true;
+ }
+ else if (errno != ENOENT)
+ {
+ /* most likely file doesn't exist or containing directory cannot be accessed */
+ writable = false;
+ }
+ else
+ {
+ /* check I can create file in parent directory */
+ char parent[FILE_MAX];
+ BLI_split_dirfile(filename, parent, NULL, sizeof parent, 0);
+ writable = access(parent, X_OK | W_OK) == 0;
+ } /*if*/
+ return
+ writable;
}
bool BLI_file_touch(const char *file)

File Metadata

Mime Type
text/x-diff
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
46/c3/5b58916bf1d1a3131ba5d2aae6dd

Event Timeline