Skip to content

Commit

Permalink
Changed tabs to spaces and reformatted code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeinzi committed Jul 25, 2019
1 parent 93535dc commit cb2070f
Show file tree
Hide file tree
Showing 4 changed files with 328 additions and 302 deletions.
228 changes: 119 additions & 109 deletions src/dropbox-communication.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,130 +38,140 @@

gboolean dropbox_connect(int * sock)
{
int flags = 0;
int retval = -1;
struct sockaddr_un address;
socklen_t address_length = 0;
struct timeval tv;
*sock = -1;

// Initialize address structure.
memset(&address, 0x0, sizeof(address));
address.sun_family = AF_UNIX;
g_snprintf(address.sun_path, sizeof(address.sun_path),
"%s/.dropbox/command_socket", g_get_home_dir());

// Calculate the length of the address.
address_length = sizeof(address) - sizeof(address.sun_path) +
strlen(address.sun_path);

// Create socket.
retval = socket(PF_UNIX, SOCK_STREAM, 0);
if (retval < 0)
goto failed;
*sock = retval;

// Set connect timeout.
tv.tv_sec = 0;
tv.tv_usec = 1000 * 50;
retval = setsockopt(*sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
if (retval < 0)
goto failed;
retval = setsockopt(*sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
if (retval < 0)
goto failed;

// Set native non-blocking, for connect timeout.
retval = fcntl(*sock, F_GETFL, 0);
if (retval < 0)
goto failed;
flags = retval;
retval = fcntl(*sock, F_SETFL, flags | O_NONBLOCK);
if (retval < 0)
goto failed;

// Connect.
retval = connect(*sock, (struct sockaddr*)&address, address_length);
if (retval < 0 && errno == EINPROGRESS) {
fd_set writers;
tv.tv_sec = 1;
tv.tv_usec = 0;

FD_ZERO(&writers);
FD_SET(*sock, &writers);

// Wait for the socket to be ready.
retval = select((*sock)+1, NULL, &writers, NULL, &tv);
if (retval == 0)
goto failed;

// Try to connect again.
retval = connect(*sock, (struct sockaddr*)&address, address_length);
if (retval < 0)
goto failed;
}
else if (retval < 0) {
goto failed;
}

// Set socket to blocking.
retval = fcntl(*sock, F_SETFL, flags);
if (retval < 0)
goto failed;

return TRUE;
int flags = 0;
int retval = -1;
socklen_t address_length = 0;
struct timeval tv;
struct sockaddr_un address;
*sock = -1;

// Initialize address structure.
memset(&address, 0x0, sizeof(address));
address.sun_family = AF_UNIX;
g_snprintf(
address.sun_path,
sizeof(address.sun_path),
"%s/.dropbox/command_socket",
g_get_home_dir()
);

// Calculate the length of the address.
address_length = sizeof(address)
- sizeof(address.sun_path)
+ strlen(address.sun_path);

// Create socket.
retval = socket(PF_UNIX, SOCK_STREAM, 0);
if (retval < 0)
goto failed;
*sock = retval;

// Set connect timeout.
tv.tv_sec = 0;
tv.tv_usec = 1000 * 50;
retval = setsockopt(*sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
if (retval < 0)
goto failed;
retval = setsockopt(*sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
if (retval < 0)
goto failed;

// Set native non-blocking, for connect timeout.
retval = fcntl(*sock, F_GETFL, 0);
if (retval < 0)
goto failed;
flags = retval;
retval = fcntl(*sock, F_SETFL, flags | O_NONBLOCK);
if (retval < 0)
goto failed;

// Connect.
retval = connect(*sock, (struct sockaddr*)&address, address_length);
if (retval < 0 && errno == EINPROGRESS) {
fd_set writers;
tv.tv_sec = 1;
tv.tv_usec = 0;

FD_ZERO(&writers);
FD_SET(*sock, &writers);

// Wait for the socket to be ready.
retval = select((*sock)+1, NULL, &writers, NULL, &tv);
if (retval == 0)
goto failed;

// Try to connect again.
retval = connect(*sock, (struct sockaddr*)&address, address_length);
if (retval < 0)
goto failed;
}
else if (retval < 0) {
goto failed;
}

// Set socket to blocking.
retval = fcntl(*sock, F_SETFL, flags);
if (retval < 0)
goto failed;

return TRUE;

failed:
if (*sock != -1)
close(*sock);
*sock = -1;
return FALSE;
if (*sock != -1)
close(*sock);
*sock = -1;
return FALSE;
}

void dropbox_write(GIOChannel * io_channel, char * str)
{
gsize bytes_written;
GIOStatus status;

do {
status = g_io_channel_write_chars(io_channel, str, -1,
&bytes_written, NULL);
} while (status == G_IO_STATUS_AGAIN);

if (status == G_IO_STATUS_ERROR) {
fprintf(stderr, "dropbox_write() - G_IO_STATUS_ERROR\n");
exit(EXIT_FAILURE);
}
gsize bytes_written;
GIOStatus status;

do {
status = g_io_channel_write_chars(
io_channel,
str,
-1,
&bytes_written,
NULL
);
} while (status == G_IO_STATUS_AGAIN);

if (status == G_IO_STATUS_ERROR) {
fprintf(stderr, "dropbox_write() - G_IO_STATUS_ERROR\n");
exit(EXIT_FAILURE);
}
}


void dropbox_do_verb(gchar * verb, GList * files)
{
GList * lp;
int socket = 0;
GIOChannel * io_channel = NULL;
GList * lp;
int socket = 0;
GIOChannel * io_channel = NULL;

if (!dropbox_connect(&socket)) {
fprintf(stderr, "Connecting failed\n");
return;
}
if (!dropbox_connect(&socket)) {
fprintf(stderr, "Connecting failed\n");
return;
}

io_channel = g_io_channel_unix_new(socket);
g_io_channel_set_close_on_unref(io_channel, TRUE);
g_io_channel_set_line_term(io_channel, "\n", -1);
io_channel = g_io_channel_unix_new(socket);
g_io_channel_set_close_on_unref(io_channel, TRUE);
g_io_channel_set_line_term(io_channel, "\n", -1);

dropbox_write(io_channel, "icon_overlay_context_action\n");
dropbox_write(io_channel, "paths");
dropbox_write(io_channel, "icon_overlay_context_action\n");
dropbox_write(io_channel, "paths");

for (lp = files; lp != NULL; lp = lp->next) {
dropbox_write(io_channel, "\t");
dropbox_write(io_channel, lp->data);
}
for (lp = files; lp != NULL; lp = lp->next) {
dropbox_write(io_channel, "\t");
dropbox_write(io_channel, lp->data);
}

dropbox_write(io_channel, "\nverb\t");
dropbox_write(io_channel, verb);
dropbox_write(io_channel, "\ndone\n");
dropbox_write(io_channel, "\nverb\t");
dropbox_write(io_channel, verb);
dropbox_write(io_channel, "\ndone\n");

g_io_channel_flush(io_channel, NULL);
g_io_channel_unref(io_channel);
g_io_channel_flush(io_channel, NULL);
g_io_channel_unref(io_channel);
}
Loading

0 comments on commit cb2070f

Please sign in to comment.