12#include <sys/socket.h>
14#include <netinet/in.h>
23#include "fileproto.pb.h"
34 if (ostream !=
NULL && field->tag == ListFilesResponse_file_tag)
36 DIR *dir = *(DIR**)field->pData;
38 FileInfo fileinfo = {};
40 while ((file = readdir(dir)) !=
NULL)
42 fileinfo.inode = file->d_ino;
43 strncpy(fileinfo.name, file->d_name,
sizeof(fileinfo.name));
44 fileinfo.name[
sizeof(fileinfo.name) - 1] =
'\0';
70 DIR *directory =
NULL;
74 ListFilesRequest request = {};
83 directory = opendir(request.path);
84 printf(
"Listing directory: %s\n", request.path);
89 ListFilesResponse response = {};
92 if (directory ==
NULL)
97 response.has_path_error =
true;
98 response.path_error =
true;
103 response.has_path_error =
false;
104 response.file = directory;
113 if (directory !=
NULL)
119 int listenfd, connfd;
120 struct sockaddr_in servaddr;
124 listenfd = socket(AF_INET, SOCK_STREAM, 0);
125 setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &reuse,
sizeof(reuse));
127 memset(&servaddr, 0,
sizeof(servaddr));
128 servaddr.sin_family = AF_INET;
129 servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
130 servaddr.sin_port = htons(1234);
131 if (bind(listenfd, (
struct sockaddr*)&servaddr,
sizeof(servaddr)) != 0)
137 if (listen(listenfd, 5) != 0)
146 connfd = accept(listenfd,
NULL,
NULL);
154 printf(
"Got connection.\n");
158 printf(
"Closing connection.\n");
pb_ostream_t pb_ostream_from_socket(int fd)
pb_istream_t pb_istream_from_socket(int fd)
MOSAPI char * strncpy(char *__restrict dest, const char *__restrict src, size_t n)
#define PB_GET_ERROR(stream)
#define pb_decode_delimited(s, f, d)
bool pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_iter_t *field)
bool pb_encode_submessage(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct)
#define pb_encode_delimited(s, f, d)
static void * memset(void *s, int c, size_t n)
bool ListFilesResponse_callback(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_iter_t *field)
void handle_connection(int connfd)