1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <stdint.h>
20 
21 #define MKID(a, b, c, d) ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
22 
23 #define ID_LSTAT_V1 MKID('S', 'T', 'A', 'T')
24 #define ID_STAT_V2 MKID('S', 'T', 'A', '2')
25 #define ID_LSTAT_V2 MKID('L', 'S', 'T', '2')
26 
27 #define ID_LIST_V1 MKID('L', 'I', 'S', 'T')
28 #define ID_LIST_V2 MKID('L', 'I', 'S', '2')
29 #define ID_DENT_V1 MKID('D', 'E', 'N', 'T')
30 #define ID_DENT_V2 MKID('D', 'N', 'T', '2')
31 
32 #define ID_SEND_V1 MKID('S', 'E', 'N', 'D')
33 #define ID_SEND_V2 MKID('S', 'N', 'D', '2')
34 #define ID_RECV_V1 MKID('R', 'E', 'C', 'V')
35 #define ID_RECV_V2 MKID('R', 'C', 'V', '2')
36 #define ID_DONE MKID('D', 'O', 'N', 'E')
37 #define ID_DATA MKID('D', 'A', 'T', 'A')
38 #define ID_OKAY MKID('O', 'K', 'A', 'Y')
39 #define ID_FAIL MKID('F', 'A', 'I', 'L')
40 #define ID_QUIT MKID('Q', 'U', 'I', 'T')
41 
42 struct SyncRequest {
43     uint32_t id;           // ID_STAT, et cetera.
44     uint32_t path_length;  // <= 1024
45     // Followed by 'path_length' bytes of path (not NUL-terminated).
46 } __attribute__((packed));
47 
48 struct __attribute__((packed)) sync_stat_v1 {
49     uint32_t id;
50     uint32_t mode;
51     uint32_t size;
52     uint32_t mtime;
53 };
54 
55 struct __attribute__((packed)) sync_stat_v2 {
56     uint32_t id;
57     uint32_t error;
58     uint64_t dev;
59     uint64_t ino;
60     uint32_t mode;
61     uint32_t nlink;
62     uint32_t uid;
63     uint32_t gid;
64     uint64_t size;
65     int64_t atime;
66     int64_t mtime;
67     int64_t ctime;
68 };
69 
70 struct __attribute__((packed)) sync_dent_v1 {
71     uint32_t id;
72     uint32_t mode;
73     uint32_t size;
74     uint32_t mtime;
75     uint32_t namelen;
76 };  // followed by `namelen` bytes of the name.
77 
78 struct __attribute__((packed)) sync_dent_v2 {
79     uint32_t id;
80     uint32_t error;
81     uint64_t dev;
82     uint64_t ino;
83     uint32_t mode;
84     uint32_t nlink;
85     uint32_t uid;
86     uint32_t gid;
87     uint64_t size;
88     int64_t atime;
89     int64_t mtime;
90     int64_t ctime;
91     uint32_t namelen;
92 };  // followed by `namelen` bytes of the name.
93 
94 enum SyncFlag : uint32_t {
95     kSyncFlagNone = 0,
96     kSyncFlagBrotli = 1,
97     kSyncFlagLZ4 = 2,
98     kSyncFlagZstd = 4,
99     kSyncFlagDryRun = 0x8000'0000U,
100 };
101 
102 enum class CompressionType {
103     None,
104     Any,
105     Brotli,
106     LZ4,
107     Zstd,
108 };
109 
110 // send_v1 sent the path in a buffer, followed by a comma and the mode as a string.
111 // send_v2 sends just the path in the first request, and then sends another syncmsg (with the
112 // same ID!) with details.
113 struct __attribute__((packed)) sync_send_v2 {
114     uint32_t id;
115     uint32_t mode;
116     uint32_t flags;
117 };
118 
119 // Likewise, recv_v1 just sent the path without any accompanying data.
120 struct __attribute__((packed)) sync_recv_v2 {
121     uint32_t id;
122     uint32_t flags;
123 };
124 
125 struct __attribute__((packed)) sync_data {
126     uint32_t id;
127     uint32_t size;
128 };  // followed by `size` bytes of data.
129 
130 struct __attribute__((packed)) sync_status {
131     uint32_t id;
132     uint32_t msglen;
133 };  // followed by `msglen` bytes of error message, if id == ID_FAIL.
134 
135 union syncmsg {
136     sync_stat_v1 stat_v1;
137     sync_stat_v2 stat_v2;
138     sync_dent_v1 dent_v1;
139     sync_dent_v2 dent_v2;
140     sync_data data;
141     sync_status status;
142     sync_send_v2 send_v2_setup;
143     sync_recv_v2 recv_v2_setup;
144 };
145 
146 #define SYNC_DATA_MAX (64 * 1024)
147