BlockGuardOs.java revision 1e8d508f2d97e19f2fc8a709330ea97e1e9f203a
1/*
2 * Copyright (C) 2011 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
17package libcore.io;
18
19import dalvik.system.BlockGuard;
20import java.io.FileDescriptor;
21import java.net.SocketException;
22import java.nio.ByteBuffer;
23import static libcore.io.OsConstants.*;
24
25/**
26 * Informs BlockGuard of any activity it should be aware of.
27 */
28public class BlockGuardOs extends ForwardingOs {
29    public BlockGuardOs(Os os) {
30        super(os);
31    }
32
33    @Override
34    public void fdatasync(FileDescriptor fd) throws ErrnoException {
35        BlockGuard.getThreadPolicy().onWriteToDisk();
36        os.fdatasync(fd);
37    }
38
39    @Override
40    public void fsync(FileDescriptor fd) throws ErrnoException {
41        BlockGuard.getThreadPolicy().onWriteToDisk();
42        os.fsync(fd);
43    }
44
45    @Override
46    public void ftruncate(FileDescriptor fd, long length) throws ErrnoException {
47        BlockGuard.getThreadPolicy().onWriteToDisk();
48        os.ftruncate(fd, length);
49    }
50
51    @Override
52    public FileDescriptor open(String path, int flags, int mode) throws ErrnoException {
53        BlockGuard.getThreadPolicy().onReadFromDisk();
54        if ((mode & O_ACCMODE) != O_RDONLY) {
55            BlockGuard.getThreadPolicy().onWriteToDisk();
56        }
57        return os.open(path, flags, mode);
58    }
59
60    @Override
61    public int read(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException {
62        BlockGuard.getThreadPolicy().onReadFromDisk();
63        return os.read(fd, buffer);
64    }
65
66    @Override
67    public int read(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException {
68        BlockGuard.getThreadPolicy().onReadFromDisk();
69        return os.read(fd, bytes, byteOffset, byteCount);
70    }
71
72    @Override
73    public int readv(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException {
74        BlockGuard.getThreadPolicy().onReadFromDisk();
75        return os.readv(fd, buffers, offsets, byteCounts);
76    }
77
78    @Override
79    public FileDescriptor socket(int domain, int type, int protocol) throws ErrnoException {
80        final FileDescriptor fd = os.socket(domain, type, protocol);
81        try {
82            BlockGuard.tagSocketFd(fd);
83        } catch (SocketException e) {
84            throw new ErrnoException("socket", EINVAL, e);
85        }
86        return fd;
87    }
88
89    @Override
90    public int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException {
91        BlockGuard.getThreadPolicy().onWriteToDisk();
92        return os.write(fd, buffer);
93    }
94
95    @Override
96    public int write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException {
97        BlockGuard.getThreadPolicy().onWriteToDisk();
98        return os.write(fd, bytes, byteOffset, byteCount);
99    }
100
101    @Override
102    public int writev(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException {
103        BlockGuard.getThreadPolicy().onWriteToDisk();
104        return os.writev(fd, buffers, offsets, byteCounts);
105    }
106}
107