You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@guacamole.apache.org by "mike-jumper (via GitHub)" <gi...@apache.org> on 2025/09/02 06:16:45 UTC

[PR] GUACAMOLE-2129: Migrate recording/typescript creation to shared function. [guacamole-server]

mike-jumper opened a new pull request, #615:
URL: https://github.com/apache/guacamole-server/pull/615

   This change defines a new function, `guac_openat()`, which encapsulates the functionality relied upon by both recording and typescript creation:
   
   * A numeric suffix can be appended automatically, if requested.
   * The file can be locked appropriately upon return.
   * The path containing the file is provided separately and can be automatically created.
   
   Opening as a draft while I implement unit tests and double-check the old vs. new approaches.


-- 
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-unsubscribe@guacamole.apache.org

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


Re: [PR] GUACAMOLE-2129: Migrate recording/typescript creation to shared function. [guacamole-server]

Posted by "necouchman (via GitHub)" <gi...@apache.org>.
necouchman merged PR #615:
URL: https://github.com/apache/guacamole-server/pull/615


-- 
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-unsubscribe@guacamole.apache.org

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


Re: [PR] GUACAMOLE-2129: Migrate recording/typescript creation to shared function. [guacamole-server]

Posted by "necouchman (via GitHub)" <gi...@apache.org>.
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: dev-unsubscribe@guacamole.apache.org

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


Re: [PR] GUACAMOLE-2129: Migrate recording/typescript creation to shared function. [guacamole-server]

Posted by "necouchman (via GitHub)" <gi...@apache.org>.
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-unsubscribe@guacamole.apache.org

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


Re: [PR] GUACAMOLE-2129: Migrate recording/typescript creation to shared function. [guacamole-server]

Posted by "mike-jumper (via GitHub)" <gi...@apache.org>.
mike-jumper commented on code in PR #615:
URL: https://github.com/apache/guacamole-server/pull/615#discussion_r2317578404


##########
src/libguac/file-private.h:
##########
@@ -0,0 +1,42 @@
+/*
+ * 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.
+ */
+
+#ifndef GUAC_FILE_PRIVATE_H
+#define GUAC_FILE_PRIVATE_H
+
+/**
+ * Functions used by the internals of "file.h".
+ *
+ * @file file-private.h
+ */
+
+/**
+ * Tests whether the given string is a filename with no other path components
+ * present.
+ *
+ * @param filename
+ *     The filename to check.
+ *
+ * @return
+ *     Zero if the provided string is a filename without any other path
+ *     components, non-zero if at least one path component is present.

Review Comment:
   Ack! You're right. I'll fix that.



-- 
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-unsubscribe@guacamole.apache.org

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


Re: [PR] GUACAMOLE-2129: Migrate recording/typescript creation to shared function. [guacamole-server]

Posted by "necouchman (via GitHub)" <gi...@apache.org>.
necouchman commented on code in PR #615:
URL: https://github.com/apache/guacamole-server/pull/615#discussion_r2315837663


##########
src/libguac/file-private.h:
##########
@@ -0,0 +1,42 @@
+/*
+ * 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.
+ */
+
+#ifndef GUAC_FILE_PRIVATE_H
+#define GUAC_FILE_PRIVATE_H
+
+/**
+ * Functions used by the internals of "file.h".
+ *
+ * @file file-private.h
+ */
+
+/**
+ * Tests whether the given string is a filename with no other path components
+ * present.
+ *
+ * @param filename
+ *     The filename to check.
+ *
+ * @return
+ *     Zero if the provided string is a filename without any other path
+ *     components, non-zero if at least one path component is present.

Review Comment:
   I think this is actually backward from how the function is implemented - looking at the below code in `file.c`, it looks like it returns `0` if a path separate *is* found, and `1` if a path separator is *not* found. Which makes more sense to me given the function name `guac_is_filename()`.



-- 
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-unsubscribe@guacamole.apache.org

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


Re: [PR] GUACAMOLE-2129: Migrate recording/typescript creation to shared function. [guacamole-server]

Posted by "mike-jumper (via GitHub)" <gi...@apache.org>.
mike-jumper commented on code in PR #615:
URL: https://github.com/apache/guacamole-server/pull/615#discussion_r2323013892


##########
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:
   Erg ... not sure where that `NULL` check came from, actually. Only `remove_file()` actually needs that handling. The other functions specific to these unit tests can all safely assume the filename will be provided.



-- 
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-unsubscribe@guacamole.apache.org

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


Re: [PR] GUACAMOLE-2129: Migrate recording/typescript creation to shared function. [guacamole-server]

Posted by "mike-jumper (via GitHub)" <gi...@apache.org>.
mike-jumper commented on PR #615:
URL: https://github.com/apache/guacamole-server/pull/615#issuecomment-3251210187

   OK - I've added unit tests and the documentation for `guac_is_filename()` should be fixed now.


-- 
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-unsubscribe@guacamole.apache.org

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


Re: [PR] GUACAMOLE-2129: Migrate recording/typescript creation to shared function. [guacamole-server]

Posted by "mike-jumper (via GitHub)" <gi...@apache.org>.
mike-jumper commented on code in PR #615:
URL: https://github.com/apache/guacamole-server/pull/615#discussion_r2323003954


##########
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:
   Yep, you're right - this would otherwise leak the file descriptor.



-- 
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-unsubscribe@guacamole.apache.org

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


Re: [PR] GUACAMOLE-2129: Migrate recording/typescript creation to shared function. [guacamole-server]

Posted by "necouchman (via GitHub)" <gi...@apache.org>.
necouchman commented on code in PR #615:
URL: https://github.com/apache/guacamole-server/pull/615#discussion_r2322133191


##########
src/libguac/file.c:
##########
@@ -0,0 +1,245 @@
+/*
+ * 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 "file-private.h"
+#include "guacamole/error.h"
+#include "guacamole/file.h"
+#include "guacamole/mem.h"
+#include "guacamole/string.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+/**
+ * Creates the directory with the given path. Where possible (non-Windows
+ * platforms), this directory is given "rwxr-x---" (0750) permissions. If the
+ * directory cannot be created, errno is set appropriately.
+ *
+ * @param path
+ *     The full path of the directory to create.
+ *
+ * @return
+ *     Zero if the directory was created successfully, non-zero otherwise.
+ */
+static int guac_mkdir(const char* path) {
+#ifndef __MINGW32__
+    return mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP);
+#else
+    return _mkdir(path);
+#endif
+}
+
+/**
+ * Attempts to acquire a lock on the file associated with the given file
+ * descriptor. The type of lock acquired is dictated by the read_lock flag. If
+ * the lock cannot be acquired. errno is set appropriately.

Review Comment:
   `acquired. errno` -> `acquired, errno`



##########
src/libguac/file.c:
##########
@@ -0,0 +1,245 @@
+/*
+ * 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 "file-private.h"
+#include "guacamole/error.h"
+#include "guacamole/file.h"
+#include "guacamole/mem.h"
+#include "guacamole/string.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+/**
+ * Creates the directory with the given path. Where possible (non-Windows
+ * platforms), this directory is given "rwxr-x---" (0750) permissions. If the
+ * directory cannot be created, errno is set appropriately.
+ *
+ * @param path
+ *     The full path of the directory to create.
+ *
+ * @return
+ *     Zero if the directory was created successfully, non-zero otherwise.
+ */
+static int guac_mkdir(const char* path) {
+#ifndef __MINGW32__
+    return mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP);
+#else
+    return _mkdir(path);
+#endif
+}
+
+/**
+ * Attempts to acquire a lock on the file associated with the given file
+ * descriptor. The type of lock acquired is dictated by the read_lock flag. If
+ * the lock cannot be acquired. errno is set appropriately.
+ *
+ * This function currently has no effet under Windows and simply returns
+ * success.
+ *
+ * @param fd
+ *     The file descriptor of the file to lock.
+ *
+ * @param read_lock
+ *     Whether the lock acquired should be a read lock (non-zero) or a write
+ *     lock (zero).
+ *
+ * @return
+ *     Zero if the lock was successfully acquired, non-zero on error.
+ */
+static int guac_flock(int fd, int read_lock) {
+
+#ifndef __MINGW32__
+    /* Translate requested file open flags (read-only vs. read/write) into
+     * the relevant kind of lock */
+    struct flock file_lock = {
+        .l_type   = read_lock ? F_RDLCK : F_WRLCK,
+        .l_whence = SEEK_SET,
+        .l_start  = 0,
+        .l_len    = 0,
+        .l_pid    = getpid()
+    };
+
+    /* Abort if file cannot be locked */
+    return fcntl(fd, F_SETLK, &file_lock) == -1;
+#else
+    return 0;
+#endif
+
+}
+
+int guac_is_filename(const char* filename) {
+
+    /* Verify no references to current or parent directory */
+    if (strcmp(filename, "..") == 0 || strcmp(filename, ".") == 0)
+        return 0;
+
+    /* Verify no path separators are present in filename */
+    for (const char* current = filename; *current != '\0'; current++) {
+        if (*current == '/' || *current == '\\')
+            return 0;
+    }
+
+    return 1;
+
+}
+
+int guac_openat(const char* path, const char* filename,
+        const guac_open_how* how) {
+
+    int dir_fd = -1;
+    int fd = -1;
+
+    /* Verify only the path contains any path separators, etc. */

Review Comment:
   This comment seems a little off to me - maybe `verify that the filename does not contain path separators`?



-- 
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-unsubscribe@guacamole.apache.org

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


Re: [PR] GUACAMOLE-2129: Migrate recording/typescript creation to shared function. [guacamole-server]

Posted by "necouchman (via GitHub)" <gi...@apache.org>.
necouchman commented on code in PR #615:
URL: https://github.com/apache/guacamole-server/pull/615#discussion_r2322279171


##########
src/libguac/file.c:
##########
@@ -0,0 +1,245 @@
+/*
+ * 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 "file-private.h"
+#include "guacamole/error.h"
+#include "guacamole/file.h"
+#include "guacamole/mem.h"
+#include "guacamole/string.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+/**
+ * Creates the directory with the given path. Where possible (non-Windows
+ * platforms), this directory is given "rwxr-x---" (0750) permissions. If the
+ * directory cannot be created, errno is set appropriately.
+ *
+ * @param path
+ *     The full path of the directory to create.
+ *
+ * @return
+ *     Zero if the directory was created successfully, non-zero otherwise.
+ */
+static int guac_mkdir(const char* path) {
+#ifndef __MINGW32__

Review Comment:
   Just one question/comment on this - I see you've consistently used the `#ifndef __MINGW32__` for specifying UNIX variants. Is there some reason you're doing it this way rather than `#ifdef __MINGW32__` for specifying stuff that applies to Windows, and using the `#else` for everything else (= UNIX)? It seems to me that, since Windows is the exception, you'd want to to capture it as the specific case, falling back to POSIX-compliant interfaces for everything that, you know, follows standards :laughing:?



-- 
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-unsubscribe@guacamole.apache.org

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


Re: [PR] GUACAMOLE-2129: Migrate recording/typescript creation to shared function. [guacamole-server]

Posted by "mike-jumper (via GitHub)" <gi...@apache.org>.
mike-jumper commented on code in PR #615:
URL: https://github.com/apache/guacamole-server/pull/615#discussion_r2322987174


##########
src/libguac/file.c:
##########
@@ -0,0 +1,245 @@
+/*
+ * 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 "file-private.h"
+#include "guacamole/error.h"
+#include "guacamole/file.h"
+#include "guacamole/mem.h"
+#include "guacamole/string.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+/**
+ * Creates the directory with the given path. Where possible (non-Windows
+ * platforms), this directory is given "rwxr-x---" (0750) permissions. If the
+ * directory cannot be created, errno is set appropriately.
+ *
+ * @param path
+ *     The full path of the directory to create.
+ *
+ * @return
+ *     Zero if the directory was created successfully, non-zero otherwise.
+ */
+static int guac_mkdir(const char* path) {
+#ifndef __MINGW32__

Review Comment:
   Nope, no particular reason except habit. I agree it makes more sense to have the exception be the exception...



-- 
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-unsubscribe@guacamole.apache.org

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


Re: [PR] GUACAMOLE-2129: Migrate recording/typescript creation to shared function. [guacamole-server]

Posted by "mike-jumper (via GitHub)" <gi...@apache.org>.
mike-jumper commented on PR #615:
URL: https://github.com/apache/guacamole-server/pull/615#issuecomment-3254926758

   OK, I believe I've addressed each of the above via rebase.


-- 
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-unsubscribe@guacamole.apache.org

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


Re: [PR] GUACAMOLE-2129: Migrate recording/typescript creation to shared function. [guacamole-server]

Posted by "necouchman (via GitHub)" <gi...@apache.org>.
necouchman commented on code in PR #615:
URL: https://github.com/apache/guacamole-server/pull/615#discussion_r2322134621


##########
src/libguac/file.c:
##########
@@ -0,0 +1,245 @@
+/*
+ * 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 "file-private.h"
+#include "guacamole/error.h"
+#include "guacamole/file.h"
+#include "guacamole/mem.h"
+#include "guacamole/string.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+/**
+ * Creates the directory with the given path. Where possible (non-Windows
+ * platforms), this directory is given "rwxr-x---" (0750) permissions. If the
+ * directory cannot be created, errno is set appropriately.
+ *
+ * @param path
+ *     The full path of the directory to create.
+ *
+ * @return
+ *     Zero if the directory was created successfully, non-zero otherwise.
+ */
+static int guac_mkdir(const char* path) {
+#ifndef __MINGW32__
+    return mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP);
+#else
+    return _mkdir(path);
+#endif
+}
+
+/**
+ * Attempts to acquire a lock on the file associated with the given file
+ * descriptor. The type of lock acquired is dictated by the read_lock flag. If
+ * the lock cannot be acquired. errno is set appropriately.
+ *
+ * This function currently has no effet under Windows and simply returns

Review Comment:
   `effet` -> `effect`



-- 
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-unsubscribe@guacamole.apache.org

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