trexio_exit_code
@@ -426,13 +426,26 @@ The file is written when closed, or when the flush function is called.
/* If directory doesn't exist, create it in write mode */
struct stat st;
- if (stat(file->file_name, &st) == 0 && S_ISDIR(st.st_mode)) {
- /* Do nothing */
- } else {
- if (file->mode == 'r') return TREXIO_READONLY;
+ int rc = stat(file->file_name, &st);
- if (mkdir(file->file_name, 0777) != 0) {
- return TREXIO_FAILURE;
+ bool file_exists = rc == 0;
+
+ if (file_exists) {
+
+ bool is_a_directory = st.st_mode & S_IFDIR;
+ if (!is_a_directory) {
+ return TREXIO_FILE_ERROR;
+ }
+
+ } else {
+
+ if (file->mode == 'r') {
+ return TREXIO_READONLY;
+ }
+
+ rc = mkdir(file->file_name, 0777);
+ if (rc != 0) {
+ return TREXIO_ERRNO;
}
}
@@ -451,7 +464,24 @@ The file is written when closed, or when the flush function is called.
f->lock_file = open(file_name,O_WRONLY|O_CREAT|O_TRUNC, 0644);
if (f->lock_file <= 0) {
- return TREXIO_FAILURE;
+ if (file->mode != 'r') {
+ return TREXIO_ERRNO;
+ } else {
+ if (errno == EACCES) {
+ /* The directory is read-only and the lock file can't be written.
+ Create a dummy temporary file for dummy locking.
+ */
+ char dirname[TREXIO_MAX_FILENAME_LENGTH] = "/tmp/trexio.XXXXXX";
+ mkdtemp(dirname);
+ strncpy (file_name, dirname, TREXIO_MAX_FILENAME_LENGTH);
+ strncat (file_name, lock_file_name, TREXIO_MAX_FILENAME_LENGTH-strlen(lock_file_name));
+ f->lock_file = open(file_name,O_WRONLY|O_CREAT|O_TRUNC, 0644);
+ remove(file_name);
+ rmdir(dirname);
+ } else {
+ return TREXIO_ERRNO;
+ }
+ }
}
return TREXIO_SUCCESS;
@@ -510,8 +540,8 @@ The file is written when closed, or when the flush function is called.