[Pkg-owncloud-commits] [owncloud-client] 271/498: Moved the makeWinLongPath function to c_path and rename to c_path_to_UNC
Sandro Knauß
hefee-guest at moszumanska.debian.org
Tue Aug 11 14:48:57 UTC 2015
This is an automated email from the git hooks/post-receive script.
hefee-guest pushed a commit to branch master
in repository owncloud-client.
commit 5214189eb2d9382abd821bbab756ead9f39e1b1d
Author: Klaas Freitag <freitag at owncloud.com>
Date: Thu Jul 2 16:53:48 2015 +0200
Moved the makeWinLongPath function to c_path and rename to c_path_to_UNC
Other cleanups like streamlined allocation and more.
---
csync/src/std/c_path.c | 45 ++++++++++++++++++++
csync/src/std/c_path.h | 13 ++++++
csync/src/std/c_string.c | 65 ++---------------------------
csync/tests/encoding_tests/check_encoding.c | 11 +++--
4 files changed, 66 insertions(+), 68 deletions(-)
diff --git a/csync/src/std/c_path.c b/csync/src/std/c_path.c
index f9a7dcc..6fe36b4 100644
--- a/csync/src/std/c_path.c
+++ b/csync/src/std/c_path.c
@@ -389,3 +389,48 @@ int c_parse_uri(const char *uri,
return -1;
}
+
+/*
+ * This function takes a path and converts it to a UNC representation of the
+ * string. That means that it prepends a \\?\ and convertes all slashes to
+ * backslashes.
+ *
+ * Note the following:
+ * - The string must be absolute.
+ * - it needs to contain a drive character to be a valid UNC
+ * - A conversion is only done if the path len is larger than 245. Otherwise
+ * the windows API functions work with the normal "unixoid" representation too.
+ *
+ * Since the function reallocs memory that it can not free itself, the number of
+ * newly allocated bytes are returned in parameter mem_reserved. The calling
+ * function will call free on the result pointer if mem_reserved is > 0.
+ *
+ */
+ const char *c_path_to_UNC(const char *str)
+ {
+ int len = 0;
+ char *longStr = NULL;
+ int i = 4; // index where to start changing "/"=>"\"
+
+ len = strlen(str);
+ longStr = c_malloc(len+5);
+ *longStr = '\0';
+
+ // prepend \\?\ and convert '/' => '\' to support long names
+ if( str[0] == '/' ) {
+ strncpy( longStr, "\\\\?", 4);
+ i=3;
+ } else {
+ strncpy( longStr, "\\\\?\\", 5); // prepend string by this four magic chars.
+ }
+ strncat( longStr, str, len );
+
+ /* replace all occurences of / with the windows native \ */
+ while(longStr[i] != '\0') {
+ if(longStr[i] == '/') {
+ longStr[i] = '\\';
+ }
+ i++;
+ }
+ return longStr;
+ }
diff --git a/csync/src/std/c_path.h b/csync/src/std/c_path.h
index 479dbdf..00a3f64 100644
--- a/csync/src/std/c_path.h
+++ b/csync/src/std/c_path.h
@@ -107,6 +107,19 @@ typedef struct
char * extension;
} C_PATHINFO;
+/**
+ * @brief c_path_to_UNC converts a unixoid path to UNC format.
+ *
+ * It converts the '/' to '\' and prepends \\?\ to the path.
+ *
+ * A proper windows path has to have a drive letter, otherwise it is not
+ * valid UNC.
+ *
+ * @param str The path to convert
+ *
+ * @return a pointer to the converted string. Caller has to free it.
+ */
+const char *c_path_to_UNC(const char *str);
/**
* }@
diff --git a/csync/src/std/c_string.c b/csync/src/std/c_string.c
index 7015da6..0da268c 100644
--- a/csync/src/std/c_string.c
+++ b/csync/src/std/c_string.c
@@ -32,6 +32,7 @@
#include <wchar.h>
#include "c_string.h"
+#include "c_path.h"
#include "c_alloc.h"
#include "c_macro.h"
@@ -274,63 +275,6 @@ char* c_utf8_from_locale(const mbchar_t *wstr)
return dst;
}
-/*
- * This function takes a path and converts it to a UNC representation of the
- * string. That means that it prepends a \\?\ and convertes all slashes to
- * backslashes.
- *
- * Note the following:
- * - The string must be absolute.
- * - it needs to contain a drive character to be a valid UNC
- * - A conversion is only done if the path len is larger than 245. Otherwise
- * the windows API functions work with the normal "unixoid" representation too.
- *
- * Since the function reallocs memory that it can not free itself, the number of
- * newly allocated bytes are returned in parameter mem_reserved. The calling
- * function will call free on the result pointer if mem_reserved is > 0.
- *
- */
- const char *makeLongWinPath(const char *str, int *mem_reserved)
-{
- int len = 0;
- char *longStr = NULL;
-
- if( mem_reserved ) {
- *mem_reserved = 0;
- }
-
- len = strlen(str);
- // prepend \\?\ and convert '/' => '\' to support long names
- if( len > 245 ) { // Only do realloc for long pathes. Shorter pathes are fine.
- int i = 4;
- // reserve mem for a new string with the prefix
- if( mem_reserved ) {
- *mem_reserved = len + 5;
- }
- longStr = c_malloc(len+5);
- *longStr = '\0';
-
- if( str[0] == '/' ) {
- strcpy( longStr, "\\\\?");
- i=3;
- } else {
- strcpy( longStr, "\\\\?\\"); // prepend string by this four magic chars.
- }
- strcat( longStr, str );
-
- /* replace all occurences of / with the windows native \ */
- while(longStr[i] != '\0') {
- if(longStr[i] == '/') {
- longStr[i] = '\\';
- }
- i++;
- }
- return longStr;
- } else {
- return str;
- }
-}
-
/* Convert a an UTF8 string to multibyte */
mbchar_t* c_utf8_to_locale(const char *str)
{
@@ -339,7 +283,6 @@ mbchar_t* c_utf8_to_locale(const char *str)
size_t len = 0;
int size_needed = 0;
const char *longStr = NULL;
- int mem_reserved = 0;
#endif
if (str == NULL ) {
@@ -347,7 +290,7 @@ mbchar_t* c_utf8_to_locale(const char *str)
}
#ifdef _WIN32
- longStr = makeLongWinPath(str, &mem_reserved);
+ longStr = c_path_to_UNC(str);
if( longStr ) {
len = strlen(longStr);
@@ -359,9 +302,7 @@ mbchar_t* c_utf8_to_locale(const char *str)
MultiByteToWideChar(CP_UTF8, 0, longStr, -1, dst, size_needed);
}
- if( mem_reserved > 0 ) { // Free mem reserved in hte makeLongWinPath function
- SAFE_FREE(longStr);
- }
+ SAFE_FREE(longStr);
}
#else
#ifdef WITH_ICONV
diff --git a/csync/tests/encoding_tests/check_encoding.c b/csync/tests/encoding_tests/check_encoding.c
index d20758a..2d35c8e 100644
--- a/csync/tests/encoding_tests/check_encoding.c
+++ b/csync/tests/encoding_tests/check_encoding.c
@@ -20,6 +20,7 @@
#include "torture.h"
#include <stdio.h>
#include "c_string.h"
+#include "c_path.h"
#ifdef _WIN32
#include <string.h>
@@ -145,14 +146,13 @@ static void check_to_multibyte(void **state)
static void check_long_win_path(void **state)
{
- int mem_reserved = 0;
const char *path = "C://DATA/FILES/MUSIC/MY_MUSIC.mp3"; // check a short path
- const char *new_short = makeLongWinPath(path, &mem_reserved);
+ const char *exp_path = "\\\\?\\C:\\\\DATA\\FILES\\MUSIC\\MY_MUSIC.mp3";
+ const char *new_short = c_path_to_UNC(path);
(void) state; /* unused */
- assert_string_equal(new_short, path);
- assert_int_equal(mem_reserved, 0);
+ assert_string_equal(new_short, exp_path);
const char *longPath = "D://alonglonglonglong/blonglonglonglong/clonglonglonglong/dlonglonglonglong/"
"elonglonglonglong/flonglonglonglong/glonglonglonglong/hlonglonglonglong/ilonglonglonglong/"
@@ -163,11 +163,10 @@ static void check_long_win_path(void **state)
"jlonglonglonglong\\klonglonglonglong\\llonglonglonglong\\mlonglonglonglong\\nlonglonglonglong\\"
"olonglonglonglong\\file.txt";
- const char *new_long = makeLongWinPath(longPath, &mem_reserved);
+ const char *new_long = c_path_to_UNC(longPath);
// printf( "XXXXXXXXXXXX %s %d\n", new_long, mem_reserved);
assert_string_equal(new_long, longPathConv);
- assert_int_equal(mem_reserved, 287);
// printf( "YYYYYYYYYYYY %ld\n", strlen(new_long));
assert_int_equal( strlen(new_long), 286);
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud-client.git
More information about the Pkg-owncloud-commits
mailing list