blob: c3d5c0ac1b9c83123293b1c26847c63a0a97255a [file] [log] [blame]
San Mehata6391f12010-03-10 12:46:00 -08001/* libs/diskconfig/write_lst.c
2 *
3 * Copyright 2008, The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#define LOG_TAG "write_lst"
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070019
San Mehata6391f12010-03-10 12:46:00 -080020#include <stdint.h>
21#include <stdio.h>
22#include <stdlib.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070023#include <sys/types.h>
San Mehata6391f12010-03-10 12:46:00 -080024#include <unistd.h>
25
San Mehata6391f12010-03-10 12:46:00 -080026#include <diskconfig/diskconfig.h>
Mark Salyzyn30f991f2017-01-10 13:19:54 -080027#include <log/log.h>
San Mehata6391f12010-03-10 12:46:00 -080028
29struct write_list *
30alloc_wl(uint32_t data_len)
31{
32 struct write_list *item;
33
34 if (!(item = malloc(sizeof(struct write_list) + data_len))) {
Steve Block01dda202012-01-06 14:13:42 +000035 ALOGE("Unable to allocate memory.");
San Mehata6391f12010-03-10 12:46:00 -080036 return NULL;
37 }
38
39 item->len = data_len;
40 return item;
41}
42
43void
44free_wl(struct write_list *item)
45{
46 if (item)
47 free(item);
48}
49
50struct write_list *
51wlist_add(struct write_list **lst, struct write_list *item)
52{
53 item->next = (*lst);
54 *lst = item;
55 return item;
56}
57
58void
59wlist_free(struct write_list *lst)
60{
61 struct write_list *temp_wr;
62 while (lst) {
63 temp_wr = lst->next;
64 free_wl(lst);
65 lst = temp_wr;
66 }
67}
68
69int
70wlist_commit(int fd, struct write_list *lst, int test)
71{
72 for(; lst; lst = lst->next) {
73 if (lseek64(fd, lst->offset, SEEK_SET) != (loff_t)lst->offset) {
Ying Wang9d4c76f2014-04-23 18:28:14 -070074 ALOGE("Cannot seek to the specified position (%lld).", (long long)lst->offset);
San Mehata6391f12010-03-10 12:46:00 -080075 goto fail;
76 }
77
78 if (!test) {
79 if (write(fd, lst->data, lst->len) != (int)lst->len) {
Steve Block01dda202012-01-06 14:13:42 +000080 ALOGE("Failed writing %u bytes at position %lld.", lst->len,
Ying Wang9d4c76f2014-04-23 18:28:14 -070081 (long long)lst->offset);
San Mehata6391f12010-03-10 12:46:00 -080082 goto fail;
83 }
84 } else
Ying Wang9d4c76f2014-04-23 18:28:14 -070085 ALOGI("Would write %d bytes @ offset %lld.", lst->len, (long long)lst->offset);
San Mehata6391f12010-03-10 12:46:00 -080086 }
87
88 return 0;
89
90fail:
91 return -1;
92}