OSDN Git Service

Add darwin support for the host tools
[android-x86/system-extras.git] / f2fs_utils / f2fs_ioutils.c
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #define _LARGEFILE64_SOURCE
30
31 #include <assert.h>
32 #include <dlfcn.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #ifdef __linux__
36 #include <asm/types.h>
37 #include <linux/fs.h>
38 #endif
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>  /* memset() */
42 #include <sys/param.h>
43 #include <sys/stat.h>
44 #include <sys/types.h>
45 #include <unistd.h>
46
47 #include <f2fs_fs.h>
48 #include <f2fs_format_utils.h>
49
50 #include <sparse/sparse.h>
51
52 struct selabel_handle;
53
54 #include "make_f2fs.h"
55
56 #ifdef USE_MINGW
57
58 #include <winsock2.h>
59
60 /* These match the Linux definitions of these flags.
61    L_xx is defined to avoid conflicting with the win32 versions.
62 */
63 #define L_S_IRUSR 00400
64 #define L_S_IWUSR 00200
65 #define L_S_IXUSR 00100
66 #define S_IRWXU (L_S_IRUSR | L_S_IWUSR | L_S_IXUSR)
67 #define S_IRGRP 00040
68 #define S_IWGRP 00020
69 #define S_IXGRP 00010
70 #define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
71 #define S_IROTH 00004
72 #define S_IWOTH 00002
73 #define S_IXOTH 00001
74 #define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
75 #define S_ISUID 0004000
76 #define S_ISGID 0002000
77 #define S_ISVTX 0001000
78
79 #else
80
81 #include <selinux/selinux.h>
82 #include <selinux/label.h>
83
84 #define O_BINARY 0
85
86 #endif
87
88 struct f2fs_configuration config;
89 struct sparse_file *f2fs_sparse_file;
90
91 struct buf_item {
92         void *buf;
93         size_t len;
94         struct buf_item *next;
95 };
96
97 struct buf_item *buf_list;
98
99 static int dev_write_fd(void *buf, __u64 offset, size_t len)
100 {
101         if (lseek64(config.fd, (off64_t)offset, SEEK_SET) < 0)
102                 return -1;
103         ssize_t written = write(config.fd, buf, len);
104         if (written == -1)
105                 return -1;
106         if ((size_t)written != len)
107                 return -1;
108         return 0;
109 }
110
111 void flush_sparse_buffs()
112 {
113         while (buf_list) {
114                 struct buf_item *bi = buf_list;
115                 buf_list = buf_list->next;
116                 free(bi->buf);
117                 free(bi);
118         }
119 }
120
121 static int dev_write_sparse(void *buf, __u64 byte_offset, size_t byte_len)
122 {
123         struct buf_item *bi = calloc(1, sizeof(struct buf_item));
124
125         if (bi == NULL) {
126                 return -ENOMEM;
127         }
128         bi->buf = malloc(byte_len);
129         if (bi->buf == NULL) {
130                 free(bi);
131                 return -ENOMEM;
132         }
133
134         bi->len = byte_len;
135         memcpy(bi->buf, buf, byte_len);
136         bi->next = buf_list;
137         buf_list = bi;
138
139         sparse_file_add_data(f2fs_sparse_file, bi->buf, byte_len, byte_offset/F2FS_BLKSIZE);
140         return 0;
141 }
142
143 void init_sparse_file(unsigned int block_size, int64_t len)
144 {
145         f2fs_sparse_file = sparse_file_new(block_size, len);
146 }
147
148 void finalize_sparse_file(int fd)
149 {
150         sparse_file_write(f2fs_sparse_file, fd, /*gzip*/0, /*sparse*/1, /*crc*/0);
151         sparse_file_destroy(f2fs_sparse_file);
152 }
153
154 void f2fs_finalize_device(struct f2fs_configuration *c)
155 {
156 }
157
158 int f2fs_trim_device()
159 {
160         return 0;
161 }
162
163 /*
164  * IO interfaces
165  */
166 int dev_read_version(void *buf, __u64 offset, size_t len)
167 {
168         return 0;
169 }
170
171 int dev_read(void  *buf, __u64 offset, size_t len)
172 {
173         return 0;
174 }
175
176 int dev_write(void *buf, __u64 offset, size_t len)
177 {
178         if (config.fd >= 0) {
179                 return dev_write_fd(buf, offset, len);
180         } else {
181                 return dev_write_sparse(buf, offset, len);
182         }
183 }
184
185
186 int dev_fill(void *buf, __u64 offset, size_t len)
187 {
188         int ret;
189         if (config.fd >= 0) {
190                 return dev_write_fd(buf, offset, len);
191         }
192         // sparse file fills with zero by default.
193         // return sparse_file_add_fill(f2fs_sparse_file, ((__u8*)(bi->buf))[0], byte_len, byte_offset/F2FS_BLKSIZE);
194         return 0;
195 }
196
197 int dev_read_block(void *buf, __u64 blk_addr)
198 {
199         assert(false); // Must not be invoked.
200         return 0;
201 }
202
203 int dev_read_blocks(void *buf, __u64 addr, __u32 nr_blks)
204 {
205         assert(false); // Must not be invoked.
206         return 0;
207 }
208