forked from spiriou/microADB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_sync_service.c
849 lines (691 loc) · 21 KB
/
file_sync_service.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
/*
* Copyright (C) 2020 Simon Piriou. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include "adb.h"
#include "file_sync_service.h"
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#define htoll(x) (x)
#define ltohl(x) (x)
#define MKID(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
#define ID_STAT MKID('S','T','A','T')
#define ID_LIST MKID('L','I','S','T')
#define ID_ULNK MKID('U','L','N','K')
#define ID_SEND MKID('S','E','N','D')
#define ID_RECV MKID('R','E','C','V')
#define ID_DENT MKID('D','E','N','T')
#define ID_DONE MKID('D','O','N','E')
#define ID_DATA MKID('D','A','T','A')
#define ID_OKAY MKID('O','K','A','Y')
#define ID_FAIL MKID('F','A','I','L')
#define ID_QUIT MKID('Q','U','I','T')
#define min(a,b) ((a) < (b) ? (a):(b))
#define SYNC_TEMP_BUFF_SIZE PATH_MAX
/****************************************************************************
* Private types
****************************************************************************/
union syncmsg {
unsigned id;
struct {
unsigned id;
unsigned namelen;
} req;
struct {
unsigned id;
unsigned mode;
unsigned size;
unsigned time;
} stat;
struct {
unsigned id;
unsigned mode;
unsigned size;
unsigned time;
unsigned namelen;
} dent;
struct {
unsigned id;
unsigned size;
} data;
struct {
unsigned id;
unsigned msglen;
} status;
};
enum {
AFS_STATE_WAIT_CMD,
AFS_STATE_WAIT_CMD_DATA,
AFS_STATE_PROCESS_RECV,
AFS_STATE_PROCESS_LIST,
AFS_STATE_PROCESS_SEND_FILE_HDR,
AFS_STATE_PROCESS_SEND_FILE_DATA,
AFS_STATE_PROCESS_SEND_SYM_HDR,
AFS_STATE_PROCESS_SEND_SYM_DATA
};
typedef struct afs_service_s {
adb_service_t service;
uint8_t *packet_ptr;
uint8_t state;
unsigned cmd;
unsigned namelen;
union {
struct {
DIR *d;
char *path;
char *file_ptr;
} list;
struct {
int fd;
} send_file;
struct {
char *path;
} send_link;
struct {
int fd;
} recv;
};
unsigned size;
char buff[SYNC_TEMP_BUFF_SIZE];
} afs_service_t;
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/* Helpers */
static int read_from_packet(afs_service_t *svc, apacket *p, unsigned int size);
static int create_path_directories(char *name);
static void prepare_fail_message(afs_service_t *svc, apacket *p, const char *reason);
static void prepare_fail_errno(afs_service_t *svc, apacket *p);
static void prepare_okay_message(afs_service_t *svc, apacket *p);
/* Generic states for file service */
static int state_wait_cmd(afs_service_t *svc, apacket *p);
static int state_wait_cmd_data(afs_service_t *svc, apacket *p);
/* State init functions */
static int state_init_stat(afs_service_t *svc, apacket *p);
static int state_init_list(afs_service_t *svc, apacket *p);
static int state_init_send(afs_service_t *svc, apacket *p);
static int state_init_send_file(afs_service_t *svc, apacket *p, mode_t mode);
static int state_init_send_link(afs_service_t *svc, apacket *p);
static int state_init_recv(afs_service_t *svc, apacket *p);
/* State process functions */
static int state_process_send_header(afs_service_t *svc, apacket *p);
static int state_process_send_file(afs_service_t *svc, apacket *p);
#ifdef CONFIG_ADBD_FILE_SYMLINK
static int state_process_send_sym(afs_service_t *svc, apacket *p);
#endif
static int state_process_recv(afs_service_t *svc, apacket *p);
static int state_process_list(afs_service_t *svc, apacket *p);
/* Reset service state */
static void state_reset(afs_service_t *svc);
/* Frame processing */
static int file_sync_on_ack(adb_service_t *service, apacket *p);
static int file_sync_on_write(adb_service_t *service, apacket *p);
static void file_sync_on_close(struct adb_service_s *service);
/****************************************************************************
* Private Functions
****************************************************************************/
static void prepare_fail_message(afs_service_t *svc, apacket *p, const char *reason)
{
UNUSED(svc);
int len;
union syncmsg *msg = (union syncmsg*)(p->data + p->write_len);
adb_err("sync: failure: %s\n", reason);
len = min(strlen(reason),
CONFIG_ADBD_PAYLOAD_SIZE - sizeof(msg->data) - p->write_len);
memcpy((char*)(&msg->data+1), reason, len);
msg->data.id = ID_FAIL;
msg->data.size = htoll(len);
p->write_len += sizeof(msg->data) + len;
}
static void prepare_fail_errno(afs_service_t *svc, apacket *p)
{
prepare_fail_message(svc, p, strerror(errno));
}
static void prepare_okay_message(afs_service_t *svc, apacket *p)
{
UNUSED(svc);
union syncmsg *msg = (union syncmsg*)(p->data + p->write_len);
msg->status.id = ID_OKAY;
msg->status.msglen = 0;
p->write_len += sizeof(msg->status);
}
static int read_from_packet(afs_service_t *svc, apacket *p, unsigned int size)
{
int ret = -EINVAL;
if (svc->size > size || size > SYNC_TEMP_BUFF_SIZE) {
goto exit_reset;
}
unsigned int chunk_size = min(size-svc->size, p->msg.data_length);
memcpy(svc->buff+svc->size, svc->packet_ptr, chunk_size);
svc->size += chunk_size;
p->msg.data_length -= chunk_size;
svc->packet_ptr += chunk_size;
if (svc->size == size) {
ret = 0;
goto exit_reset;
}
return -EAGAIN;
exit_reset:
svc->size = 0;
return ret;
}
static int create_path_directories(char *name)
{
int ret;
char *x = name;
unsigned int mode = 0775;
if(name[0] != '/') return -1;
for(;;) {
x = strchr(++x, '/');
if(!x) return 0;
*x = 0;
ret = mkdir(name, mode);
*x = '/';
if((ret < 0) && (errno != EEXIST)) {
adb_err("mkdir <%s> failed: %d\n", name, errno);
return ret;
}
}
return 0;
}
static void state_reset(afs_service_t *svc)
{
switch (svc->state) {
case AFS_STATE_PROCESS_LIST:
free(svc->list.path);
closedir(svc->list.d);
break;
case AFS_STATE_PROCESS_RECV:
close(svc->recv.fd);
break;
case AFS_STATE_PROCESS_SEND_SYM_HDR:
case AFS_STATE_PROCESS_SEND_SYM_DATA:
free(svc->send_link.path);
break;
case AFS_STATE_PROCESS_SEND_FILE_HDR:
case AFS_STATE_PROCESS_SEND_FILE_DATA:
close(svc->send_file.fd);
/* TODO handle file unlink if transfer incomplete */
break;
default:
break;
}
svc->state = AFS_STATE_WAIT_CMD;
svc->size = 0;
}
static int state_init_stat(afs_service_t *svc, apacket *p)
{
struct stat st;
union syncmsg *msg = (union syncmsg*)(p->data + p->write_len);
msg->stat.id = ID_STAT;
p->write_len += sizeof(msg->stat);
if(!stat(svc->buff, &st)) {
msg->stat.mode = htoll(st.st_mode);
msg->stat.size = htoll(st.st_size);
msg->stat.time = htoll(st.st_mtime);
/* There may be more data to process in current frame */
svc->state = AFS_STATE_WAIT_CMD;
return 1;
}
/* File not found */
msg->stat.mode = 0;
msg->stat.size = 0;
msg->stat.time = 0;
return 0;
}
static int state_init_list(afs_service_t *svc, apacket *p)
{
union syncmsg *msg = (union syncmsg*)(p->data + p->write_len);
int len = strlen(svc->buff);
/* PATH_MAX + "/" + 1 char filename at least + "\x0" => -3 */
if (len >= PATH_MAX-3) {
adb_err("Dir name too big\n");
goto exit_done;
}
svc->list.path = (char*)malloc(PATH_MAX);
if (svc->list.path == NULL) {
adb_err("Cannot allocate dirname\n");
goto exit_done;
}
memcpy(svc->list.path, svc->buff, len);
svc->list.path[len] = '/';
svc->list.file_ptr = svc->list.path+len +1;
svc->list.d = opendir(svc->buff);
if(svc->list.d == NULL) {
goto exit_free;
}
svc->state = AFS_STATE_PROCESS_LIST;
/* Fill first frame */
return state_process_list(svc, p);
exit_free:
free(svc->list.path);
exit_done:
msg->dent.id = ID_DONE;
msg->dent.mode = 0;
msg->dent.size = 0;
msg->dent.time = 0;
msg->dent.namelen = 0;
p->write_len += sizeof(msg->dent);
return 0;
}
static int state_process_list(afs_service_t *svc, apacket *p)
{
int ret;
struct dirent *de;
struct stat st;
union syncmsg *msg = (union syncmsg*)(p->data + p->write_len);
/* Only send items one by one to ensure there is no overflow in packet */
de = readdir(svc->list.d);
if (de == NULL) {
msg->dent.id = ID_DONE;
msg->dent.mode = 0;
msg->dent.size = 0;
msg->dent.time = 0;
msg->dent.namelen = 0;
p->write_len += sizeof(msg->dent);
return 0;
}
int len = strlen(de->d_name);
int remaining = PATH_MAX -
(int)(svc->list.file_ptr - svc->list.path);
if (len >= remaining) {
adb_err("filename <%s> too long to stat: %d/%d\n",
de->d_name, len, remaining);
msg->dent.mode = 0;
msg->dent.size = 0;
msg->dent.time = 0;
}
else {
/* Try to stat file */
memcpy(svc->list.file_ptr, de->d_name, len);
svc->list.file_ptr[len] = 0;
/* Do not follow symlinks */
if((ret = lstat(svc->list.path, &st))) {
adb_err("stat failed <%s> %d %d\n", svc->list.path, ret, errno);
st.st_mode = 0;
st.st_size = 0;
st.st_mtime = 0;
}
msg->dent.mode = htoll(st.st_mode);
msg->dent.size = htoll(st.st_size);
msg->dent.time = htoll(st.st_mtime);
}
remaining = (int)(CONFIG_ADBD_PAYLOAD_SIZE - sizeof(msg->dent) - p->write_len);
if (len > remaining) {
adb_err("filename <%s> too long: %d/%d\n",
de->d_name,
len,
remaining);
len = remaining;
}
msg->dent.id = ID_DENT;
msg->dent.namelen = htoll(len);
memcpy((&msg->dent)+1, de->d_name, len);
p->write_len += sizeof(msg->dent) + len;
return 1;
}
static int state_init_send(afs_service_t *svc, apacket *p)
{
unsigned int mode;
bool is_link;
char* tmp = strrchr(svc->buff,',');
if(tmp) {
*tmp = 0;
mode = strtoul(tmp + 1, NULL, 0);
is_link = S_ISLNK((mode_t)mode);
mode &= 0777;
}
else {
mode = 0644;
is_link = 0;
}
/* TODO always unlink or stat file ?
* Useless for regular files (O_CREAT | O_TRUNC)
* but may be required for symlinks or folders ?
*/
unlink(svc->buff);
if(create_path_directories(svc->buff) != 0) {
adb_err("failed to create path <%s>\n", svc->buff);
prepare_fail_errno(svc, p);
return 0;
}
if (is_link) {
return state_init_send_link(svc, p);
}
return state_init_send_file(svc, p, mode);
}
static int state_init_send_file(afs_service_t *svc, apacket *p, mode_t mode)
{
svc->send_file.fd = open(svc->buff, O_WRONLY | O_CREAT | O_TRUNC, mode);
if(svc->send_file.fd < 0) {
adb_err("failed to open file <%s> (fd=%d)\n",
svc->buff, svc->send_file.fd);
prepare_fail_errno(svc, p);
return 0;
}
svc->state = AFS_STATE_PROCESS_SEND_FILE_HDR;
return 1;
}
static int state_init_send_link(afs_service_t *svc, apacket *p)
{
int len;
len = strlen(svc->buff);
if (len >= PATH_MAX) {
prepare_fail_message(svc, p, "path too long");
return 0;
}
svc->send_link.path = (char*)malloc(len+1);
if (svc->send_link.path == NULL) {
adb_err("Cannot allocate dirname\n");
prepare_fail_message(svc, p, "out of memory");
return 0;
}
memcpy(svc->send_link.path, svc->buff, len);
svc->send_link.path[len] = 0;
svc->state = AFS_STATE_PROCESS_SEND_SYM_HDR;
return 1;
}
static int state_process_send_header(afs_service_t *svc, apacket *p)
{
int ret;
union syncmsg *msg = (union syncmsg*)svc->buff;
ret = read_from_packet(svc, p, sizeof(msg->data));
if (ret != 0) {
if (ret == -EAGAIN) {
return 1;
}
prepare_fail_message(svc, p, "read error");
return 0;
}
if(msg->data.id != ID_DATA) {
if(msg->data.id == ID_DONE) {
prepare_okay_message(svc, p);
return 0;
}
adb_log("Data message is 0x%x\n", msg->data.id);
prepare_fail_message(svc, p, "invalid data message");
return 0;
}
/* Read file length */
svc->namelen = ltohl(msg->data.size);
svc->state += 1;
return 1;
}
static int state_process_send_file(afs_service_t *svc, apacket *p)
{
int ret;
int block_size = min(p->msg.data_length, svc->namelen);
uint8_t *write_ptr = svc->packet_ptr;
svc->namelen -= block_size;
p->msg.data_length -= block_size;
svc->packet_ptr += block_size;
if (svc->send_file.fd >= 0) {
/* Write data to file */
while (block_size > 0) {
ret = write(svc->send_file.fd, write_ptr, block_size);
if (ret > 0) {
write_ptr += ret;
block_size -= ret;
continue;
}
if (ret < 0 && errno == EINTR) {
continue;
}
/* TODO handle nonblocking io */
adb_err("write error %d %d\n", ret, errno);
prepare_fail_message(svc, p, "write error");
return 0;
}
}
/* Wait for DONE frame */
if (svc->namelen == 0)
svc->state -= 1;
return 1;
}
#ifdef CONFIG_ADBD_FILE_SYMLINK
static int state_process_send_sym(afs_service_t *svc, apacket *p) {
int ret;
ret = read_from_packet(svc, p, svc->namelen);
if (ret != 0) {
if (ret == -EAGAIN) {
return 1;
}
prepare_fail_message(svc, p, "read error");
return 0;
}
if (svc->namelen >= SYNC_TEMP_BUFF_SIZE) {
prepare_fail_message(svc, p, "symlink target too long");
return 0;
}
svc->buff[svc->namelen] = 0;
ret = symlink(svc->buff, svc->send_link.path);
if (ret) {
adb_err("symlink failed %d %d\n", ret, errno);
prepare_fail_message(svc, p, "symlink call failed");
return 0;
}
/* Wait for DONE frame */
svc->state -= 1;
return 1;
}
#endif
static int state_init_recv(afs_service_t *svc, apacket *p)
{
svc->recv.fd = open(svc->buff, O_RDONLY);
if(svc->recv.fd < 0) {
adb_err("Cannot open file <%s> for read %d\n",
svc->buff, errno);
prepare_fail_message(svc, p, "file does not exist");
return 0;
}
/* FIXME handle non blocking io ? */
// flags = fcntl(svc->recv.fd, F_GETFL, 0);
// fcntl(svc->recv.fd, F_SETFL, flags | O_NONBLOCK);
svc->state = AFS_STATE_PROCESS_RECV;
return state_process_recv(svc, p);
}
static int state_process_recv(afs_service_t *svc, apacket *p)
{
int ret;
union syncmsg *msg = (union syncmsg*)(p->data + p->write_len);
ret = read(svc->recv.fd,
(&msg->data)+1,
CONFIG_ADBD_PAYLOAD_SIZE - sizeof(msg->data) - p->write_len);
if (ret > 0) {
msg->data.id = ID_DATA;
msg->data.size = htoll(ret);
p->write_len += sizeof(msg->data) + ret;
return 1;
}
if (ret == 0) {
msg->status.id = ID_DONE;
msg->status.msglen = 0;
p->write_len += sizeof(msg->status);
return 0;
}
/* TODO handle non blocking and EAGAIN */
adb_err("read failed %d %d\n", ret, errno);
prepare_fail_message(svc, p, "read failed");
return 0;
}
static int state_wait_cmd(afs_service_t *svc, apacket *p)
{
int ret;
union syncmsg *msg;
ret = read_from_packet(svc, p, sizeof(msg->req));
if (ret != 0) {
if (ret == -EAGAIN) {
return 1;
}
return -1;
}
msg = (union syncmsg*)svc->buff;
if (msg->req.id == ID_QUIT) {
/* Return error code so service gets closed */
return -1;
}
if (msg->req.namelen >= PATH_MAX) {
adb_err("Fail path too big (%d)\n", msg->req.namelen);
return -1;
}
svc->cmd = msg->req.id;
svc->namelen = msg->req.namelen;
svc->state = AFS_STATE_WAIT_CMD_DATA;
return 1;
}
static int state_wait_cmd_data(afs_service_t *svc, apacket *p)
{
int ret;
ret = read_from_packet(svc, p, svc->namelen);
if (ret != 0) {
if (ret == -EAGAIN) {
return 1;
}
return -1;
}
if (svc->namelen >= SYNC_TEMP_BUFF_SIZE) {
return -1;
}
svc->buff[svc->namelen] = 0;
switch(svc->cmd) {
case ID_STAT:
ret = state_init_stat(svc, p);
break;
case ID_LIST:
ret = state_init_list(svc, p);
break;
case ID_SEND:
ret = state_init_send(svc, p);
break;
case ID_RECV:
ret = state_init_recv(svc, p);
break;
case ID_QUIT:
// adb_log("got QUIT command\n");
ret = 0;
break;
default:
adb_err("Unexpected command 0x%x\n", svc->cmd);
ret = -1;
}
return ret;
}
static int file_sync_on_write(adb_service_t *service, apacket *p) {
int ret = 0;
afs_service_t *svc = container_of(service, afs_service_t, service);
svc->packet_ptr = p->data;
/* Process all packet data */
while (p->msg.data_length > 0 && ret >= 0) {
switch(svc->state) {
case AFS_STATE_WAIT_CMD:
ret = state_wait_cmd(svc, p);
break;
case AFS_STATE_WAIT_CMD_DATA:
ret = state_wait_cmd_data(svc, p);
break;
case AFS_STATE_PROCESS_SEND_FILE_HDR:
case AFS_STATE_PROCESS_SEND_SYM_HDR:
ret = state_process_send_header(svc, p);
break;
case AFS_STATE_PROCESS_SEND_FILE_DATA:
ret = state_process_send_file(svc, p);
break;
case AFS_STATE_PROCESS_SEND_SYM_DATA:
#ifdef CONFIG_ADBD_FILE_SYMLINK
ret = state_process_send_sym(svc, p);
#else
adb_err("symlink not supported\n");
ret = -1;
#endif
break;
default:
adb_err("Unexpected state %d\n", svc->state);
ret = -1;
}
/* process done or error, reset state */
if (ret <= 0) {
state_reset(svc);
}
}
/* process ok, wait for next frame */
return ret >= 0 ? 0 : ret;
}
static int file_sync_on_ack(adb_service_t *service, apacket *p) {
int ret;
afs_service_t *svc = container_of(service, afs_service_t, service);
svc->packet_ptr = p->data;
/* No data in notify packet */
switch (svc->state) {
case AFS_STATE_PROCESS_RECV:
ret = state_process_recv(svc, p);
break;
case AFS_STATE_PROCESS_LIST:
ret = state_process_list(svc, p);
break;
case AFS_STATE_PROCESS_SEND_FILE_HDR:
case AFS_STATE_PROCESS_SEND_SYM_HDR:
case AFS_STATE_WAIT_CMD:
/* Nothing to do */
ret = 0;
break;
default:
adb_err("ERROR state %d\n", svc->state);
ret = -1;
break;
}
if (ret > 0) {
/* process ok, continue with next frame */
return 0;
}
/* process done or error, reset state */
state_reset(svc);
return ret;
}
static void file_sync_on_close(struct adb_service_s *service) {
afs_service_t *svc = container_of(service, afs_service_t, service);
state_reset(svc);
free(svc);
}
static const adb_service_ops_t file_sync_ops = {
.on_write_frame = file_sync_on_write,
.on_ack_frame = file_sync_on_ack,
.on_kick = NULL,
.on_close = file_sync_on_close
};
/****************************************************************************
* Public Functions
****************************************************************************/
adb_service_t* file_sync_service(const char *params)
{
UNUSED(params);
afs_service_t *service =
(afs_service_t*)malloc(sizeof(afs_service_t));
if (service == NULL) {
return NULL;
}
service->size = 0;
service->state = AFS_STATE_WAIT_CMD;
service->service.ops = &file_sync_ops;
return &service->service;
}