-
Notifications
You must be signed in to change notification settings - Fork 593
/
FileHandlePlatform.scala
177 lines (142 loc) · 6.94 KB
/
FileHandlePlatform.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Copyright (c) 2013 Functional Streams for Scala
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package fs2
package io
package file
import java.nio.ByteBuffer
import java.nio.channels.{FileChannel, FileLock, SeekableByteChannel}
import java.nio.file.{OpenOption, Path => JPath}
import cats.effect.kernel.{Async, Resource, Sync}
private[file] trait FileHandlePlatform[F[_]] {
/** Opaque type representing an exclusive lock on a file. */
type Lock
/** Acquire an exclusive lock on the underlying file.
* @return a lock object which can be used to unlock the file.
*/
def lock: F[Lock]
/** Acquire a lock on the specified region of the underlying file.
* @param position the start of the region to lock.
* @param size the size of the region to lock.
* @param shared to request a shared lock across process boundaries (may be converted to an exclusive lock on some operating systems).
* @return a lock object which can be used to unlock the region.
*/
def lock(position: Long, size: Long, shared: Boolean): F[Lock]
/** Attempt to acquire an exclusive lock on the underlying file.
* @return if the lock could be acquired, a lock object which can be used to unlock the file.
*/
def tryLock: F[Option[Lock]]
/** Attempt to acquire a lock on the specified region of the underlying file.
* @param position the start of the region to lock.
* @param size the size of the region to lock.
* @param shared to request a shared lock across process boundaries (may be converted to an exclusive lock on some operating systems).
* @return if the lock could be acquired, a lock object which can be used to unlock the region.
*/
def tryLock(position: Long, size: Long, shared: Boolean): F[Option[Lock]]
/** Unlock the (exclusive or regional) lock represented by the supplied `Lock`.
* @param lock the lock object which represents the locked file or region.
*/
def unlock(lock: Lock): F[Unit]
}
private[file] trait FileHandleCompanionPlatform {
@deprecated("Use Files[F].open", "3.0.0")
def fromPath[F[_]: Async](path: JPath, flags: Seq[OpenOption]): Resource[F, FileHandle[F]] =
Files.forAsync[F].open(path, flags)
@deprecated("Use Files[F].openFileChannel", "3.0.0")
def fromFileChannel[F[_]: Async](channel: F[FileChannel]): Resource[F, FileHandle[F]] =
Files.forAsync[F].openFileChannel(channel)
/** Creates a `FileHandle[F]` from a `java.nio.channels.FileChannel`. */
private[file] def make[F[_]](
chan: FileChannel
)(implicit F: Sync[F]): FileHandle[F] =
new FileHandle[F] {
type Lock = FileLock
override def force(metaData: Boolean): F[Unit] =
F.blocking(chan.force(metaData))
override def lock: F[Lock] =
F.blocking(chan.lock)
override def lock(position: Long, size: Long, shared: Boolean): F[Lock] =
F.blocking(chan.lock(position, size, shared))
override def read(numBytes: Int, offset: Long): F[Option[Chunk[Byte]]] =
F.blocking {
val buf = ByteBuffer.allocate(numBytes)
val len = chan.read(buf, offset)
if (len < 0) None
else if (len == 0) Some(Chunk.empty)
else Some(Chunk.array(buf.array, 0, len))
}
override def size: F[Long] =
F.blocking(chan.size)
override def truncate(size: Long): F[Unit] =
F.blocking { chan.truncate(size); () }
override def tryLock: F[Option[Lock]] =
F.blocking(Option(chan.tryLock()))
override def tryLock(position: Long, size: Long, shared: Boolean): F[Option[Lock]] =
F.blocking(Option(chan.tryLock(position, size, shared)))
override def unlock(f: Lock): F[Unit] =
F.blocking(f.release())
override def write(bytes: Chunk[Byte], offset: Long): F[Int] =
F.blocking(chan.write(bytes.toByteBuffer, offset))
}
/** Creates a `FileHandle[F]` from a `java.nio.channels.SeekableByteChannel`. Because a `SeekableByteChannel` doesn't provide all the functionalities required by `FileHandle` some features like locking will be unavailable. */
private[file] def makeFromSeekableByteChannel[F[_]](
chan: SeekableByteChannel,
unsupportedOperationException: => Throwable
)(implicit F: Sync[F]): FileHandle[F] =
new FileHandle[F] {
type Lock = Unit
override def force(metaData: Boolean): F[Unit] =
F.raiseError(unsupportedOperationException)
override def lock: F[Lock] =
F.raiseError(unsupportedOperationException)
override def lock(position: Long, size: Long, shared: Boolean): F[Lock] =
F.raiseError(unsupportedOperationException)
override def read(numBytes: Int, offset: Long): F[Option[Chunk[Byte]]] =
F.blocking {
val buf = ByteBuffer.allocate(numBytes)
val len = chan.synchronized {
// don't set position on sequential operations because not all file systems support setting the position
if (chan.position() != offset) { chan.position(offset); () }
chan.read(buf)
}
if (len < 0) None
else if (len == 0) Some(Chunk.empty)
else Some(Chunk.array(buf.array, 0, len))
}
override def size: F[Long] =
F.blocking(chan.size)
override def truncate(size: Long): F[Unit] =
F.blocking { chan.truncate(size); () }
override def tryLock: F[Option[Lock]] =
F.raiseError(unsupportedOperationException)
override def tryLock(position: Long, size: Long, shared: Boolean): F[Option[Lock]] =
F.raiseError(unsupportedOperationException)
override def unlock(f: Lock): F[Unit] =
F.unit
override def write(bytes: Chunk[Byte], offset: Long): F[Int] =
F.blocking {
chan.synchronized {
// don't set position on sequential operations because not all file systems support setting the position
if (chan.position() != offset) { chan.position(offset); () }
chan.write(bytes.toByteBuffer)
}
}
}
}