write_lst.c revision fe71a61e5b0cb666675900d206251a7c18ed944b
1/* 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"
19#include <sys/types.h>
20#include <stdint.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <unistd.h>
24
25#include <cutils/log.h>
26
27#include <diskconfig/diskconfig.h>
28
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))) {
35        LOGE("Unable to allocate memory.");
36        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) {
74            LOGE("Cannot seek to the specified position (%lld).", lst->offset);
75            goto fail;
76        }
77
78        if (!test) {
79            if (write(fd, lst->data, lst->len) != (int)lst->len) {
80                LOGE("Failed writing %u bytes at position %lld.", lst->len,
81                     lst->offset);
82                goto fail;
83            }
84        } else
85            ALOGI("Would write %d bytes @ offset %lld.", lst->len, lst->offset);
86    }
87
88    return 0;
89
90fail:
91    return -1;
92}
93