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


##########
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;
+

Review Comment:
   Does `dir_fd` get automatically closed in the case where the `unlinkat()` 
call fails? Or will this leave `dir_fd` hanging open? In the above 
implementation of `exists()` you recorded the result of the `fstatat()` call 
and then did the `close_if_necessary()` call, returning the result. Should the 
same logic be applied, here?



-- 
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: dev-unsubscr...@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to