Skip to content

Commit 1b532d9

Browse files
committed
Fixed the output file check issue introduced in 4c4e3b7.
1 parent b49d579 commit 1b532d9

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

src/util.c

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
#include <regex.h>
4949
#include <pthread.h>
5050
#include <unistd.h>
51+
#include <libgen.h>
52+
#include <limits.h>
5153

5254
#include <netinet/in.h>
5355
#include <sys/socket.h>
@@ -1225,22 +1227,47 @@ unescape_str (const char *src) {
12251227

12261228
int
12271229
is_writable_path (const char *path) {
1228-
/* Path is writable */
1229-
if (access (path, W_OK) == 0)
1230+
char *copy = NULL, *dir_path = NULL;
1231+
char dir_path_copy[PATH_MAX] = { 0 };
1232+
int result = 0;
1233+
1234+
if (path == NULL) {
1235+
fprintf (stderr, "Path is NULL\n");
1236+
return 0;
1237+
}
1238+
/* Make a copy of the path because dirname might modify it */
1239+
copy = strdup (path);
1240+
if (copy == NULL) {
1241+
fprintf (stderr, "Memory allocation failed\n");
1242+
return 0;
1243+
}
1244+
/* Get the directory part of the path */
1245+
dir_path = dirname (copy);
1246+
strncpy (dir_path_copy, dir_path, PATH_MAX);
1247+
1248+
/* Check if the directory is writable */
1249+
result = access (dir_path, W_OK);
1250+
free (copy);
1251+
1252+
if (result == 0) {
1253+
/* Directory exists and is writable */
12301254
return 1;
1255+
}
12311256

12321257
switch (errno) {
12331258
case ENOENT:
1234-
fprintf (stderr, "Path does not exist: %s\n", path);
1235-
return 0;
1259+
fprintf (stderr, "Directory does not exist: %s\n", dir_path_copy);
1260+
break;
12361261
case EACCES:
1237-
fprintf (stderr, "No write permission for path: %s\n", path);
1238-
return 0;
1262+
fprintf (stderr, "No write permission for directory: %s\n", dir_path_copy);
1263+
break;
12391264
case EROFS:
1240-
fprintf (stderr, "Path is on a read-only file system: %s\n", path);
1241-
return 0;
1265+
fprintf (stderr, "Directory is on a read-only file system: %s\n", dir_path_copy);
1266+
break;
12421267
default:
1243-
fprintf (stderr, "Unknown error (errno %d) for path: %s\n", errno, path);
1244-
return 0;
1268+
fprintf (stderr, "Unknown error (errno %d) for directory: %s\n", errno, dir_path_copy);
1269+
break;
12451270
}
1271+
1272+
return 0;
12461273
}

0 commit comments

Comments
 (0)