server: proxy: capture: refactor path creation
authorkubistika <kmizrachi18@gmail.com>
Wed, 23 Oct 2019 09:39:58 +0000 (12:39 +0300)
committerakallabeth <akallabeth@users.noreply.github.com>
Thu, 24 Oct 2019 10:19:31 +0000 (12:19 +0200)
server/proxy/pf_capture.c
server/proxy/pf_capture.h

index b52b42f..b9b99e4 100644 (file)
 
 #include <stdio.h>
 #include <string.h>
+#include <winpr/image.h>
+#include <winpr/sysinfo.h>
+#include <winpr/path.h>
+#include <winpr/file.h>
 
 #include "pf_capture.h"
 
@@ -30,6 +34,82 @@ static BOOL pf_capture_create_dir_if_not_exists(const char* path)
        return CreateDirectoryA(path, NULL);
 }
 
+static BOOL pf_capture_create_user_captures_dir(const char* base_dir, const char* username)
+{
+       int rc;
+       size_t size;
+       char* buf = NULL;
+       BOOL ret = FALSE;
+
+       /* create sub-directory in base captures directory for current username, if it doesn't already exists. */
+       rc = _snprintf(NULL, 0, "%s/%s", base_dir, username);
+       if (rc < 0)
+               return FALSE;
+
+       size = (size_t) rc;
+
+       buf = malloc(size + 1);
+       if (!buf)
+               return FALSE;
+
+       rc = sprintf(buf, "%s/%s", base_dir, username);
+       if (rc < 0 || (size_t) rc != size)
+               goto out;
+
+       if (!pf_capture_create_dir_if_not_exists(buf))
+               goto out;
+
+       ret = TRUE;
+
+out:
+       free(buf);
+       return ret;
+}
+
+static BOOL pf_capture_create_current_session_captures_dir(pClientContext* pc)
+{
+       proxyConfig* config = pc->pdata->config;
+       rdpSettings* settings = pc->context.settings;
+       const char* fmt = "%s/%s/%s_%02u-%02u-%"PRIu16"_%02u-%02u-%02u-%03u";
+       int rc;
+       size_t size;
+       SYSTEMTIME localTime;
+
+       GetLocalTime(&localTime);
+
+       /* create sub-directory in current user's captures directory, for the specific session. */
+       rc = _snprintf(NULL, 0, fmt, config->CapturesDirectory, settings->Username,
+                 settings->ServerHostname, localTime.wDay, localTime.wMonth, localTime.wYear,
+                 localTime.wHour, localTime.wMinute, localTime.wSecond, localTime.wMilliseconds);
+
+       if (rc < 0)
+               return FALSE;
+
+       size = (size_t) rc;
+
+       /* `pc->frames_dir` will be used by proxy client for saving frames to storage. */
+       pc->frames_dir = malloc(size + 1);
+       if (!pc->frames_dir)
+               return FALSE;
+
+       rc = sprintf(pc->frames_dir, fmt, config->CapturesDirectory, settings->Username,
+                 settings->ServerHostname, localTime.wDay, localTime.wMonth, localTime.wYear,
+                 localTime.wHour, localTime.wMinute, localTime.wSecond, localTime.wMilliseconds);
+
+       if (rc < 0 || (size_t) rc != size)
+               goto error;
+
+       if (!pf_capture_create_dir_if_not_exists(pc->frames_dir))
+               goto error;
+
+       return TRUE;
+
+error:
+       free(pc->frames_dir);
+       return FALSE;
+}
+
+
 /* creates a directory to store captured session frames.
  *
  * @context: current session.
@@ -44,34 +124,45 @@ BOOL pf_capture_create_session_directory(pClientContext* pc)
 {
        proxyConfig* config = pc->pdata->config;
        rdpSettings* settings = pc->context.settings;
-       SYSTEMTIME localTime;
-       char tmp[MAX_PATH];
-       const char* fmt = "%s/%s/%s_%02u-%02u-%"PRIu16"_%02u-%02u-%02u-%03u";
 
-       _snprintf(tmp, sizeof(tmp), "%s/%s", config->CapturesDirectory, settings->Username);
-       if (!pf_capture_create_dir_if_not_exists(tmp))
+       if (!pf_capture_create_user_captures_dir(config->CapturesDirectory, settings->Username))
                return FALSE;
 
-       pc->frames_dir = malloc(MAX_PATH);
-       if (!pc->frames_dir)
+       if (!pf_capture_create_current_session_captures_dir(pc))
                return FALSE;
 
-       GetLocalTime(&localTime);
-       sprintf_s(pc->frames_dir, MAX_PATH, fmt, config->CapturesDirectory, settings->Username,
-                 settings->ServerHostname, localTime.wDay, localTime.wMonth, localTime.wYear,
-                 localTime.wHour, localTime.wMinute, localTime.wSecond, localTime.wMilliseconds);
-
-       return pf_capture_create_dir_if_not_exists(pc->frames_dir);
+       return TRUE;
 }
 
 /* saves a captured frame in a BMP format. */
 BOOL pf_capture_save_frame(pClientContext* pc, const BYTE* frame)
 {
        rdpSettings* settings = pc->context.settings;
+       int rc;
        const char* fmt = "%s/%"PRIu64".bmp";
-       char file_path[MAX_PATH];
+       char* file_path = NULL;
+       size_t size;
+
+       if (!pc->frames_dir)
+               return FALSE;
 
-       sprintf_s(file_path, sizeof(file_path), fmt, pc->frames_dir, pc->frames_count++);
-       return winpr_bitmap_write(file_path, frame, settings->DesktopWidth, settings->DesktopHeight,
+       rc = _snprintf(NULL, 0, fmt, pc->frames_dir, pc->frames_count++);
+       if (rc < 0)
+               return FALSE;
+
+       size = (size_t) rc;
+       file_path = malloc(size + 1);
+       if (!file_path)
+               return FALSE;
+
+       rc = sprintf(file_path, fmt, pc->frames_dir, pc->frames_count++);
+       if (rc < 0 || (size_t) rc != size)
+               goto out;
+
+       rc = winpr_bitmap_write(file_path, frame, settings->DesktopWidth, settings->DesktopHeight,
                                  settings->ColorDepth);
+
+out:
+       free(file_path);
+       return rc;
 }
index 92aaf1c..93a6010 100644 (file)
 #ifndef FREERDP_SERVER_PROXY_CAPTURE_H
 #define FREERDP_SERVER_PROXY_CAPTURE_H
 
-#include <winpr/image.h>
-#include <winpr/sysinfo.h>
-#include <winpr/path.h>
-#include <winpr/file.h>
-
 #include "pf_context.h"
 
 BOOL pf_capture_create_session_directory(pClientContext* context);