• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 
26 #include <diskconfig/diskconfig.h>
27 #include <log/log.h>
28 
29 struct write_list *
alloc_wl(uint32_t data_len)30 alloc_wl(uint32_t data_len)
31 {
32     struct write_list *item;
33 
34     if (!(item = malloc(sizeof(struct write_list) + data_len))) {
35         ALOGE("Unable to allocate memory.");
36         return NULL;
37     }
38 
39     item->len = data_len;
40     return item;
41 }
42 
43 void
free_wl(struct write_list * item)44 free_wl(struct write_list *item)
45 {
46     if (item)
47         free(item);
48 }
49 
50 struct write_list *
wlist_add(struct write_list ** lst,struct write_list * item)51 wlist_add(struct write_list **lst, struct write_list *item)
52 {
53     item->next = (*lst);
54     *lst = item;
55     return item;
56 }
57 
58 void
wlist_free(struct write_list * lst)59 wlist_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 
69 int
wlist_commit(int fd,struct write_list * lst,int test)70 wlist_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             ALOGE("Cannot seek to the specified position (%lld).", (long long)lst->offset);
75             goto fail;
76         }
77 
78         if (!test) {
79             if (write(fd, lst->data, lst->len) != (int)lst->len) {
80                 ALOGE("Failed writing %u bytes at position %lld.", lst->len,
81                      (long long)lst->offset);
82                 goto fail;
83             }
84         } else
85             ALOGI("Would write %d bytes @ offset %lld.", lst->len, (long long)lst->offset);
86     }
87 
88     return 0;
89 
90 fail:
91     return -1;
92 }
93