necouchman commented on code in PR #615:
URL: https://github.com/apache/guacamole-server/pull/615#discussion_r2322582836


##########
src/libguac/tests/file/openat.c:
##########
@@ -0,0 +1,407 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <CUnit/CUnit.h>
+#include <guacamole/file.h>
+
+#include <fcntl.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+/**
+ * Closes the given file descriptor if it is not an error code from a previous
+ * call to open() or openat().
+ *
+ * @param fd
+ *     The file descriptor to close.
+ */
+static void close_if_necessary(int fd) {
+    if (fd != -1)
+        close(fd);
+}
+
+/**
+ * Returns whether a file with the given filename exists beneath the given
+ * path.
+ *
+ * @param path
+ *     The path to the directory that may contain the file.
+ *
+ * @param filename
+ *     The filename to test.
+ *
+ * @return
+ *     Non-zero if a file exists with the given filename beneath the given
+ *     path, zero otherwise.
+ */
+static int exists(const char* path, const char* filename) {
+
+    int result = 0;
+
+    int dir_fd = open(path, O_RDONLY);
+    if (dir_fd == -1)
+        return 0;
+
+    if (filename != NULL) {
+        struct stat file_info;
+        result = fstatat(dir_fd, filename, &file_info, 0);
+    }
+
+    close_if_necessary(dir_fd);
+    return result != -1;
+
+}
+
+/**
+ * Removes the file with the given filename beneath the given path. If NULL is
+ * provided instead of a filename, the final component of the given path is
+ * removed as a directory.
+ *
+ * @param path
+ *     The path to the directory that contains the file to remove.
+ *
+ * @param filename
+ *     The filename to remove, or NULL if the containing directory should be
+ *     removed instead.
+ *
+ * @return
+ *     Non-zero if the operation succeeded, zero otherwise.
+ */
+static int remove_file(const char* path, const char* filename) {
+
+    if (filename == NULL)
+        return !rmdir(path);
+
+    int dir_fd = open(path, O_RDONLY);
+    if (dir_fd == -1)
+        return 0;
+
+    if (unlinkat(dir_fd, filename, 0))
+        return 0;
+
+    close(dir_fd);
+    return 1;
+
+}
+
+/**
+ * Returns whether the file with the given filename beneath the given path has
+ * the given permissions (mode). Any permission bits that apply to the file but
+ * which are greater than the least-significant 12 bits are ignored.
+ *
+ * @param path
+ *     The path to the directory that contains the file to test.
+ *
+ * @param filename
+ *     The filename to test.
+ *
+ * @param mode
+ *     The permissions (mode) that the file is expected to have.
+ *
+ * @return
+ *     Non-zero if the file does have the given permissions, zero otherwise.
+ */
+static int has_mode(const char* path, const char* filename, mode_t mode) {
+
+
+    int dir_fd = open(path, O_RDONLY);
+    if (dir_fd == -1)
+        return 0;
+
+    struct stat file_info = { 0 };
+    int result = fstatat(dir_fd, filename, &file_info, 0);

Review Comment:
   Are there any circumstances under which `filename` could be `NULL`? If so, 
will `fstatat()` handle that cleanly, or should there be a check for that (as 
in `exists()` above)?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to