Fixed warnings and formatting.

This commit is contained in:
Armin Novak 2017-11-14 13:57:00 +01:00
parent 1fd6308ef5
commit 7b58495e7b

View File

@ -61,14 +61,13 @@ static struct posix_file* make_posix_file(const char* local_name, const WCHAR* r
{ {
struct posix_file* file = NULL; struct posix_file* file = NULL;
struct stat statbuf; struct stat statbuf;
file = calloc(1, sizeof(*file)); file = calloc(1, sizeof(*file));
if (!file) if (!file)
return NULL; return NULL;
file->fd = -1; file->fd = -1;
file->offset = 0; file->offset = 0;
file->local_name = _strdup(local_name); file->local_name = _strdup(local_name);
file->remote_name = _wcsdup(remote_name); file->remote_name = _wcsdup(remote_name);
@ -84,14 +83,11 @@ static struct posix_file* make_posix_file(const char* local_name, const WCHAR* r
file->is_directory = S_ISDIR(statbuf.st_mode); file->is_directory = S_ISDIR(statbuf.st_mode);
file->size = statbuf.st_size; file->size = statbuf.st_size;
return file; return file;
error: error:
free(file->local_name); free(file->local_name);
free(file->remote_name); free(file->remote_name);
free(file); free(file);
return NULL; return NULL;
} }
@ -142,7 +138,6 @@ static BOOL decode_percent_encoded_byte(const char* str, const char* end, char*
*value |= hex_to_dec(str[1], &valid); *value |= hex_to_dec(str[1], &valid);
*value <<= 4; *value <<= 4;
*value |= hex_to_dec(str[2], &valid); *value |= hex_to_dec(str[2], &valid);
return valid; return valid;
} }
@ -152,9 +147,9 @@ static char* decode_percent_encoded_string(const char* str, size_t len)
char* next = NULL; char* next = NULL;
const char* cur = str; const char* cur = str;
const char* lim = str + len; const char* lim = str + len;
/* Percent decoding shrinks data length, so len bytes will be enough. */ /* Percent decoding shrinks data length, so len bytes will be enough. */
buffer = calloc(len + 1, sizeof(char)); buffer = calloc(len + 1, sizeof(char));
if (!buffer) if (!buffer)
return NULL; return NULL;
@ -180,10 +175,8 @@ static char* decode_percent_encoded_string(const char* str, size_t len)
} }
return buffer; return buffer;
error: error:
free(buffer); free(buffer);
return NULL; return NULL;
} }
@ -220,7 +213,6 @@ static WCHAR* convert_local_name_component_to_remote(const char* local_name)
} }
return remote_name; return remote_name;
error: error:
free(remote_name); free(remote_name);
return NULL; return NULL;
@ -231,18 +223,16 @@ static char* concat_local_name(const char* dir, const char* file)
size_t len_dir = 0; size_t len_dir = 0;
size_t len_file = 0; size_t len_file = 0;
char* buffer = NULL; char* buffer = NULL;
len_dir = strlen(dir); len_dir = strlen(dir);
len_file = strlen(file); len_file = strlen(file);
buffer = calloc(len_dir + 1 + len_file + 1, sizeof(char)); buffer = calloc(len_dir + 1 + len_file + 1, sizeof(char));
if (!buffer) if (!buffer)
return NULL; return NULL;
memcpy(buffer, dir, len_dir * sizeof(char)); memcpy(buffer, dir, len_dir * sizeof(char));
buffer[len_dir] = '/'; buffer[len_dir] = '/';
memcpy(buffer + len_dir + 1, file, len_file * sizeof(char)); memcpy(buffer + len_dir + 1, file, len_file * sizeof(char));
return buffer; return buffer;
} }
@ -251,18 +241,16 @@ static WCHAR* concat_remote_name(const WCHAR* dir, const WCHAR* file)
size_t len_dir = 0; size_t len_dir = 0;
size_t len_file = 0; size_t len_file = 0;
WCHAR* buffer = NULL; WCHAR* buffer = NULL;
len_dir = _wcslen(dir); len_dir = _wcslen(dir);
len_file = _wcslen(file); len_file = _wcslen(file);
buffer = calloc(len_dir + 1 + len_file + 1, sizeof(WCHAR)); buffer = calloc(len_dir + 1 + len_file + 1, sizeof(WCHAR));
if (!buffer) if (!buffer)
return NULL; return NULL;
memcpy(buffer, dir, len_dir * sizeof(WCHAR)); memcpy(buffer, dir, len_dir * sizeof(WCHAR));
buffer[len_dir] = L'\\'; buffer[len_dir] = L'\\';
memcpy(buffer + len_dir + 1, file, len_file * sizeof(WCHAR)); memcpy(buffer + len_dir + 1, file, len_file * sizeof(WCHAR));
return buffer; return buffer;
} }
@ -281,6 +269,7 @@ static BOOL add_directory_entry_to_list(const char* local_dir_name, const WCHAR*
return TRUE; return TRUE;
remote_base_name = convert_local_name_component_to_remote(entry->d_name); remote_base_name = convert_local_name_component_to_remote(entry->d_name);
if (!remote_base_name) if (!remote_base_name)
return FALSE; return FALSE;
@ -293,7 +282,6 @@ static BOOL add_directory_entry_to_list(const char* local_dir_name, const WCHAR*
free(remote_base_name); free(remote_base_name);
free(remote_name); free(remote_name);
free(local_name); free(local_name);
return result; return result;
} }
@ -313,16 +301,16 @@ static BOOL do_add_directory_contents_to_list(const char* local_name, const WCHA
* musl). We should not be breaking people's builds because of that, * musl). We should not be breaking people's builds because of that,
* so we do nothing and proceed with fingers crossed. * so we do nothing and proceed with fingers crossed.
*/ */
for (;;) for (;;)
{ {
struct dirent* entry = NULL; struct dirent* entry = NULL;
errno = 0; errno = 0;
entry = readdir(dirp); entry = readdir(dirp);
if (!entry) if (!entry)
{ {
int err = errno; int err = errno;
if (!err) if (!err)
break; break;
@ -342,10 +330,9 @@ static BOOL add_directory_contents_to_list(const char* local_name, const WCHAR*
{ {
BOOL result = FALSE; BOOL result = FALSE;
DIR* dirp = NULL; DIR* dirp = NULL;
WLog_VRB(TAG, "adding directory: %s", local_name); WLog_VRB(TAG, "adding directory: %s", local_name);
dirp = opendir(local_name); dirp = opendir(local_name);
if (!dirp) if (!dirp)
{ {
int err = errno; int err = errno;
@ -360,6 +347,7 @@ static BOOL add_directory_contents_to_list(const char* local_name, const WCHAR*
int err = errno; int err = errno;
WLog_WARN(TAG, "failed to close directory: %s", strerror(err)); WLog_WARN(TAG, "failed to close directory: %s", strerror(err));
} }
out: out:
return result; return result;
} }
@ -367,17 +355,15 @@ out:
static BOOL add_file_to_list(const char* local_name, const WCHAR* remote_name, wArrayList* files) static BOOL add_file_to_list(const char* local_name, const WCHAR* remote_name, wArrayList* files)
{ {
struct posix_file* file = NULL; struct posix_file* file = NULL;
WLog_VRB(TAG, "adding file: %s", local_name); WLog_VRB(TAG, "adding file: %s", local_name);
file = make_posix_file(local_name, remote_name); file = make_posix_file(local_name, remote_name);
if (!file) if (!file)
return FALSE; return FALSE;
if (ArrayList_Add(files, file) < 0) if (ArrayList_Add(files, file) < 0)
{ {
free_posix_file(file); free_posix_file(file);
return FALSE; return FALSE;
} }
@ -413,22 +399,19 @@ static BOOL process_file_name(const char* local_name, wArrayList* files)
BOOL result = FALSE; BOOL result = FALSE;
const char* base_name = NULL; const char* base_name = NULL;
WCHAR* remote_name = NULL; WCHAR* remote_name = NULL;
/* /*
* Start with the base name of the file. text/uri-list contains the * Start with the base name of the file. text/uri-list contains the
* exact files selected by the user, and we want the remote files * exact files selected by the user, and we want the remote files
* to have names relative to that selection. * to have names relative to that selection.
*/ */
base_name = get_basename(local_name); base_name = get_basename(local_name);
remote_name = convert_local_name_component_to_remote(base_name); remote_name = convert_local_name_component_to_remote(base_name);
if (!remote_name) if (!remote_name)
return FALSE; return FALSE;
result = add_file_to_list(local_name, remote_name, files); result = add_file_to_list(local_name, remote_name, files);
free(remote_name); free(remote_name);
return result; return result;
} }
@ -436,7 +419,6 @@ static BOOL process_uri(const char* uri, size_t uri_len, wArrayList* files)
{ {
BOOL result = FALSE; BOOL result = FALSE;
char* name = NULL; char* name = NULL;
WLog_VRB(TAG, "processing URI: %.*s", uri_len, uri); WLog_VRB(TAG, "processing URI: %.*s", uri_len, uri);
if ((uri_len < strlen("file://")) || strncmp(uri, "file://", strlen("file://"))) if ((uri_len < strlen("file://")) || strncmp(uri, "file://", strlen("file://")))
@ -446,13 +428,13 @@ static BOOL process_uri(const char* uri, size_t uri_len, wArrayList* files)
} }
name = decode_percent_encoded_string(uri + strlen("file://"), uri_len - strlen("file://")); name = decode_percent_encoded_string(uri + strlen("file://"), uri_len - strlen("file://"));
if (!name) if (!name)
goto out; goto out;
result = process_file_name(name, files); result = process_file_name(name, files);
out: out:
free(name); free(name);
return result; return result;
} }
@ -462,9 +444,7 @@ static BOOL process_uri_list(const char* data, size_t length, wArrayList* files)
const char* lim = data + length; const char* lim = data + length;
const char* start = NULL; const char* start = NULL;
const char* stop = NULL; const char* stop = NULL;
WLog_VRB(TAG, "processing URI list:\n%.*s", length, data); WLog_VRB(TAG, "processing URI list:\n%.*s", length, data);
ArrayList_Clear(files); ArrayList_Clear(files);
/* /*
@ -478,9 +458,7 @@ static BOOL process_uri_list(const char* data, size_t length, wArrayList* files)
while (cur < lim) while (cur < lim)
{ {
BOOL comment = (*cur == '#'); BOOL comment = (*cur == '#');
start = cur; start = cur;
stop = cur;
for (stop = cur; stop < lim; stop++) for (stop = cur; stop < lim; stop++)
{ {
@ -490,14 +468,17 @@ static BOOL process_uri_list(const char* data, size_t length, wArrayList* files)
cur = stop + 2; cur = stop + 2;
else else
cur = stop + 1; cur = stop + 1;
break; break;
} }
if (*stop == '\n') if (*stop == '\n')
{ {
cur = stop + 1; cur = stop + 1;
break; break;
} }
} }
if (stop == lim) if (stop == lim)
cur = lim; cur = lim;
@ -515,7 +496,6 @@ static BOOL convert_local_file_to_filedescriptor(const struct posix_file* file,
FILEDESCRIPTOR* descriptor) FILEDESCRIPTOR* descriptor)
{ {
size_t remote_len = 0; size_t remote_len = 0;
descriptor->dwFlags = FD_ATTRIBUTES | FD_FILESIZE | FD_SHOWPROGRESSUI; descriptor->dwFlags = FD_ATTRIBUTES | FD_FILESIZE | FD_SHOWPROGRESSUI;
if (file->is_directory) if (file->is_directory)
@ -532,6 +512,7 @@ static BOOL convert_local_file_to_filedescriptor(const struct posix_file* file,
} }
remote_len = _wcslen(file->remote_name); remote_len = _wcslen(file->remote_name);
if (remote_len + 1 > ARRAYSIZE(descriptor->cFileName)) if (remote_len + 1 > ARRAYSIZE(descriptor->cFileName))
{ {
WLog_ERR(TAG, "file name too long (%"PRIuz" characters)", remote_len); WLog_ERR(TAG, "file name too long (%"PRIuz" characters)", remote_len);
@ -539,7 +520,6 @@ static BOOL convert_local_file_to_filedescriptor(const struct posix_file* file,
} }
memcpy(descriptor->cFileName, file->remote_name, remote_len * sizeof(WCHAR)); memcpy(descriptor->cFileName, file->remote_name, remote_len * sizeof(WCHAR));
return TRUE; return TRUE;
} }
@ -548,10 +528,9 @@ static FILEDESCRIPTOR* convert_local_file_list_to_filedescriptors(wArrayList* fi
int i; int i;
int count = 0; int count = 0;
FILEDESCRIPTOR* descriptors = NULL; FILEDESCRIPTOR* descriptors = NULL;
count = ArrayList_Count(files); count = ArrayList_Count(files);
descriptors = calloc(count, sizeof(descriptors[0])); descriptors = calloc(count, sizeof(descriptors[0]));
if (!descriptors) if (!descriptors)
goto error; goto error;
@ -564,10 +543,8 @@ static FILEDESCRIPTOR* convert_local_file_list_to_filedescriptors(wArrayList* fi
} }
return descriptors; return descriptors;
error: error:
free(descriptors); free(descriptors);
return NULL; return NULL;
} }
@ -586,13 +563,12 @@ static void* convert_uri_list_to_filedescriptors(wClipboard* clipboard, UINT32 f
return NULL; return NULL;
descriptors = convert_local_file_list_to_filedescriptors(clipboard->localFiles); descriptors = convert_local_file_list_to_filedescriptors(clipboard->localFiles);
if (!descriptors) if (!descriptors)
return NULL; return NULL;
*pSize = ArrayList_Count(clipboard->localFiles) * sizeof(FILEDESCRIPTOR); *pSize = ArrayList_Count(clipboard->localFiles) * sizeof(FILEDESCRIPTOR);
clipboard->fileListSequenceNumber = clipboard->sequenceNumber; clipboard->fileListSequenceNumber = clipboard->sequenceNumber;
return descriptors; return descriptors;
} }
@ -600,13 +576,14 @@ static BOOL register_file_formats_and_synthesizers(wClipboard* clipboard)
{ {
UINT32 file_group_format_id; UINT32 file_group_format_id;
UINT32 local_file_format_id; UINT32 local_file_format_id;
file_group_format_id = ClipboardRegisterFormat(clipboard, "FileGroupDescriptorW"); file_group_format_id = ClipboardRegisterFormat(clipboard, "FileGroupDescriptorW");
local_file_format_id = ClipboardRegisterFormat(clipboard, "text/uri-list"); local_file_format_id = ClipboardRegisterFormat(clipboard, "text/uri-list");
if (!file_group_format_id || !local_file_format_id) if (!file_group_format_id || !local_file_format_id)
goto error; goto error;
clipboard->localFiles = ArrayList_New(FALSE); clipboard->localFiles = ArrayList_New(FALSE);
if (!clipboard->localFiles) if (!clipboard->localFiles)
goto error; goto error;
@ -618,7 +595,6 @@ static BOOL register_file_formats_and_synthesizers(wClipboard* clipboard)
goto error_free_local_files; goto error_free_local_files;
return TRUE; return TRUE;
error_free_local_files: error_free_local_files:
ArrayList_Free(clipboard->localFiles); ArrayList_Free(clipboard->localFiles);
clipboard->localFiles = NULL; clipboard->localFiles = NULL;
@ -638,7 +614,6 @@ static UINT posix_file_get_size(const struct posix_file* file, INT64* size)
} }
*size = statbuf.st_size; *size = statbuf.st_size;
return NO_ERROR; return NO_ERROR;
} }
@ -656,6 +631,7 @@ static UINT posix_file_request_size(wClipboardDelegate* delegate,
return ERROR_INVALID_STATE; return ERROR_INVALID_STATE;
file = ArrayList_GetItem(delegate->clipboard->localFiles, request->listIndex); file = ArrayList_GetItem(delegate->clipboard->localFiles, request->listIndex);
if (!file) if (!file)
return ERROR_INDEX_ABSENT; return ERROR_INDEX_ABSENT;
@ -680,6 +656,7 @@ static UINT posix_file_read_open(struct posix_file* file)
return NO_ERROR; return NO_ERROR;
file->fd = open(file->local_name, O_RDONLY); file->fd = open(file->local_name, O_RDONLY);
if (file->fd < 0) if (file->fd < 0)
{ {
int err = errno; int err = errno;
@ -696,10 +673,8 @@ static UINT posix_file_read_open(struct posix_file* file)
file->offset = 0; file->offset = 0;
file->size = statbuf.st_size; file->size = statbuf.st_size;
WLog_VRB(TAG, "open file %d -> %s", file->fd, file->local_name); WLog_VRB(TAG, "open file %d -> %s", file->fd, file->local_name);
WLog_VRB(TAG, "file %d size: %"PRIu64" bytes", file->fd, file->size); WLog_VRB(TAG, "file %d size: %"PRIu64" bytes", file->fd, file->size);
return NO_ERROR; return NO_ERROR;
} }
@ -732,10 +707,9 @@ static UINT posix_file_read_perform(struct posix_file* file, UINT32 size,
{ {
BYTE* buffer = NULL; BYTE* buffer = NULL;
ssize_t amount = 0; ssize_t amount = 0;
WLog_VRB(TAG, "file %d request read %"PRIu32" bytes", file->fd, size); WLog_VRB(TAG, "file %d request read %"PRIu32" bytes", file->fd, size);
buffer = malloc(size); buffer = malloc(size);
if (!buffer) if (!buffer)
{ {
WLog_ERR(TAG, "failed to allocate %"PRIu32" buffer bytes", size); WLog_ERR(TAG, "failed to allocate %"PRIu32" buffer bytes", size);
@ -743,6 +717,7 @@ static UINT posix_file_read_perform(struct posix_file* file, UINT32 size,
} }
amount = read(file->fd, buffer, size); amount = read(file->fd, buffer, size);
if (amount < 0) if (amount < 0)
{ {
int err = errno; int err = errno;
@ -753,15 +728,11 @@ static UINT posix_file_read_perform(struct posix_file* file, UINT32 size,
*actual_data = buffer; *actual_data = buffer;
*actual_size = amount; *actual_size = amount;
file->offset += amount; file->offset += amount;
WLog_VRB(TAG, "file %d actual read %"PRIu32" bytes (offset %"PRIu64")", file->fd, WLog_VRB(TAG, "file %d actual read %"PRIu32" bytes (offset %"PRIu64")", file->fd,
amount, file->offset); amount, file->offset);
return NO_ERROR; return NO_ERROR;
error: error:
free(buffer); free(buffer);
return ERROR_READ_FAULT; return ERROR_READ_FAULT;
} }
@ -779,6 +750,7 @@ static UINT posix_file_read_close(struct posix_file* file)
int err = errno; int err = errno;
WLog_WARN(TAG, "failed to close fd %d: %s", file->fd, strerror(err)); WLog_WARN(TAG, "failed to close fd %d: %s", file->fd, strerror(err));
} }
file->fd = -1; file->fd = -1;
} }
@ -789,22 +761,26 @@ static UINT posix_file_get_range(struct posix_file* file, UINT64 offset, UINT32
BYTE** actual_data, UINT32* actual_size) BYTE** actual_data, UINT32* actual_size)
{ {
UINT error = NO_ERROR; UINT error = NO_ERROR;
error = posix_file_read_open(file); error = posix_file_read_open(file);
if (error) if (error)
goto out; goto out;
error = posix_file_read_seek(file, offset); error = posix_file_read_seek(file, offset);
if (error) if (error)
goto out; goto out;
error = posix_file_read_perform(file, size, actual_data, actual_size); error = posix_file_read_perform(file, size, actual_data, actual_size);
if (error) if (error)
goto out; goto out;
error = posix_file_read_close(file); error = posix_file_read_close(file);
if (error) if (error)
goto out; goto out;
out: out:
return error; return error;
} }
@ -825,6 +801,7 @@ static UINT posix_file_request_range(wClipboardDelegate* delegate,
return ERROR_INVALID_STATE; return ERROR_INVALID_STATE;
file = ArrayList_GetItem(delegate->clipboard->localFiles, request->listIndex); file = ArrayList_GetItem(delegate->clipboard->localFiles, request->listIndex);
if (!file) if (!file)
return ERROR_INDEX_ABSENT; return ERROR_INDEX_ABSENT;
@ -840,26 +817,29 @@ static UINT posix_file_request_range(wClipboardDelegate* delegate,
WLog_WARN(TAG, "failed to report file range result: 0x%08X", error); WLog_WARN(TAG, "failed to report file range result: 0x%08X", error);
free(data); free(data);
return NO_ERROR; return NO_ERROR;
} }
static UINT dummy_file_size_success(wClipboardDelegate* delegate, const wClipboardFileSizeRequest* request, UINT64 fileSize) static UINT dummy_file_size_success(wClipboardDelegate* delegate,
const wClipboardFileSizeRequest* request, UINT64 fileSize)
{ {
return ERROR_NOT_SUPPORTED; return ERROR_NOT_SUPPORTED;
} }
static UINT dummy_file_size_failure(wClipboardDelegate* delegate, const wClipboardFileSizeRequest* request, UINT errorCode) static UINT dummy_file_size_failure(wClipboardDelegate* delegate,
const wClipboardFileSizeRequest* request, UINT errorCode)
{ {
return ERROR_NOT_SUPPORTED; return ERROR_NOT_SUPPORTED;
} }
static UINT dummy_file_range_success(wClipboardDelegate* delegate, const wClipboardFileRangeRequest* request, const BYTE* data, UINT32 size) static UINT dummy_file_range_success(wClipboardDelegate* delegate,
const wClipboardFileRangeRequest* request, const BYTE* data, UINT32 size)
{ {
return ERROR_NOT_SUPPORTED; return ERROR_NOT_SUPPORTED;
} }
static UINT dummy_file_range_failure(wClipboardDelegate* delegate, const wClipboardFileRangeRequest* request, UINT errorCode) static UINT dummy_file_range_failure(wClipboardDelegate* delegate,
const wClipboardFileRangeRequest* request, UINT errorCode)
{ {
return ERROR_NOT_SUPPORTED; return ERROR_NOT_SUPPORTED;
} }
@ -869,7 +849,6 @@ static void setup_delegate(wClipboardDelegate* delegate)
delegate->ClientRequestFileSize = posix_file_request_size; delegate->ClientRequestFileSize = posix_file_request_size;
delegate->ClipboardFileSizeSuccess = dummy_file_size_success; delegate->ClipboardFileSizeSuccess = dummy_file_size_success;
delegate->ClipboardFileSizeFailure = dummy_file_size_failure; delegate->ClipboardFileSizeFailure = dummy_file_size_failure;
delegate->ClientRequestFileRange = posix_file_request_range; delegate->ClientRequestFileRange = posix_file_request_range;
delegate->ClipboardFileRangeSuccess = dummy_file_range_success; delegate->ClipboardFileRangeSuccess = dummy_file_range_success;
delegate->ClipboardFileRangeFailure = dummy_file_range_failure; delegate->ClipboardFileRangeFailure = dummy_file_range_failure;
@ -884,6 +863,5 @@ BOOL ClipboardInitPosixFileSubsystem(wClipboard* clipboard)
return FALSE; return FALSE;
setup_delegate(&clipboard->delegate); setup_delegate(&clipboard->delegate);
return TRUE; return TRUE;
} }