]> git.ipfire.org Git - pakfire.git/commitdiff
pakfire: Check if path exists
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 13 Jan 2021 12:01:49 +0000 (12:01 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 13 Jan 2021 12:01:49 +0000 (12:01 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/pakfire.c
src/libpakfire/include/pakfire/util.h
src/libpakfire/libpakfire.sym
src/libpakfire/pakfire.c
src/libpakfire/util.c

index 6c107a757976c48728854127ec097b0fe98f9a9d..7a7db1db3027408c41d28bee080856dc608cd8a8 100644 (file)
@@ -61,6 +61,12 @@ static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) {
                                PyErr_SetString(PyExc_ValueError, "Invalid architecture");
                                break;
 
+                       // path does not exist
+                       case -ENOENT:
+                               PyErr_Format(PyExc_FileNotFoundError,
+                                       "%s does not exist or is not a directory", path);
+                               break;
+
                        // Anything else
                        default:
                                PyErr_SetNone(PyExc_OSError);
index 8c2a16a1d110a641e839f3971998b734bbd00ce0..83c62af01acba979ecf616bd688121a7ebd9bbda 100644 (file)
@@ -44,6 +44,7 @@ char* pakfire_format_date(time_t t);
 
 char* pakfire_path_join(const char* first, const char* second);
 const char* pakfire_path_relpath(const char* root, const char* path);
+int pakfire_path_isdir(const char* path);
 
 char* pakfire_basename(const char* path);
 char* pakfire_dirname(const char* path);
index 2455e7a0f8832aebd3fdccd735bcc473fa7f83e4..4dfae478d7e8a97992c3a1cc80ba238d56e59eed 100644 (file)
@@ -373,6 +373,7 @@ global:
        pakfire_free;
        pakfire_get_errno;
        pakfire_partition_string;
+       pakfire_path_isdir;
        pakfire_path_join;
        pakfire_path_relpath;
        pakfire_read_file_into_buffer;
index 4dfbdcfd735679dcedc878cc8afb3ded33921bb9..ed89ac4c8f0a572a2fb940fbd746aec27d22af8e 100644 (file)
@@ -92,6 +92,12 @@ PAKFIRE_EXPORT Pakfire pakfire_create(const char* path, const char* arch) {
                return NULL;
        }
 
+       // Check if path exists
+       if (!pakfire_path_isdir(path)) {
+               errno = -ENOENT;
+               return NULL;
+       }
+
        Pakfire pakfire = pakfire_calloc(1, sizeof(*pakfire));
        if (pakfire) {
                pakfire->nrefs = 1;
index 24b0f9db529d1b7b2a6fc196676f07c885c5e3d4..38d9d5ec0d7f9179c9e81a867da6cd4b6168c4b8 100644 (file)
@@ -155,6 +155,20 @@ PAKFIRE_EXPORT const char* pakfire_path_relpath(const char* root, const char* pa
        return NULL;
 }
 
+PAKFIRE_EXPORT int pakfire_path_isdir(const char* path) {
+       struct stat s;
+
+       if (stat(path, &s) != 0) {
+               // Does not seem to exist
+               return 0;
+       }
+
+       if (S_ISDIR(s.st_mode))
+               return 1;
+
+       return 0;
+}
+
 PAKFIRE_EXPORT char* pakfire_basename(const char* path) {
        char* name = pakfire_strdup(path);