BlockGuardOs.java revision 2bad9bff258de6275bd3847e5e9f3169b0a93c61
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.InetAddress;
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 public void connect(FileDescriptor fd, InetAddress address, int port) throws ErrnoException {
34        BlockGuard.getThreadPolicy().onNetwork();
35        os.connect(fd, address, port);
36    }
37
38    @Override public void fdatasync(FileDescriptor fd) throws ErrnoException {
39        BlockGuard.getThreadPolicy().onWriteToDisk();
40        os.fdatasync(fd);
41    }
42
43    @Override public void fsync(FileDescriptor fd) throws ErrnoException {
44        BlockGuard.getThreadPolicy().onWriteToDisk();
45        os.fsync(fd);
46    }
47
48    @Override public void ftruncate(FileDescriptor fd, long length) throws ErrnoException {
49        BlockGuard.getThreadPolicy().onWriteToDisk();
50        os.ftruncate(fd, length);
51    }
52
53    @Override public FileDescriptor open(String path, int flags, int mode) throws ErrnoException {
54        BlockGuard.getThreadPolicy().onReadFromDisk();
55        if ((mode & O_ACCMODE) != O_RDONLY) {
56            BlockGuard.getThreadPolicy().onWriteToDisk();
57        }
58        return os.open(path, flags, mode);
59    }
60
61    @Override public int poll(StructPollfd[] fds, int timeoutMs) throws ErrnoException {
62        // Greater than 0 is a timeout in milliseconds and -1 means "block forever",
63        // but 0 means "poll and return immediately", which shouldn't be subject to BlockGuard.
64        if (timeoutMs != 0) {
65            BlockGuard.getThreadPolicy().onNetwork();
66        }
67        return os.poll(fds, timeoutMs);
68    }
69
70    @Override public int read(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException {
71        BlockGuard.getThreadPolicy().onReadFromDisk();
72        return os.read(fd, buffer);
73    }
74
75    @Override public int read(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException {
76        BlockGuard.getThreadPolicy().onReadFromDisk();
77        return os.read(fd, bytes, byteOffset, byteCount);
78    }
79
80    @Override public int readv(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException {
81        BlockGuard.getThreadPolicy().onReadFromDisk();
82        return os.readv(fd, buffers, offsets, byteCounts);
83    }
84
85    @Override public int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException {
86        BlockGuard.getThreadPolicy().onWriteToDisk();
87        return os.write(fd, buffer);
88    }
89
90    @Override public int write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException {
91        BlockGuard.getThreadPolicy().onWriteToDisk();
92        return os.write(fd, bytes, byteOffset, byteCount);
93    }
94
95    @Override public int writev(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException {
96        BlockGuard.getThreadPolicy().onWriteToDisk();
97        return os.writev(fd, buffers, offsets, byteCounts);
98    }
99}
100