checkFileSystem: Check functions for file system objects

Description Usage Arguments Details Value Functions Examples

Description

A check function test something about its first (required) parameter and returns either an empty string if the check succeeds or a non-empty string if the check fails, even when a test fails due to an error. The check functions described here test file-system related properties.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
checkIsPath(path)

checkIsNotPath(path)

checkIsFile(path, path.info = NULL)

checkIsNotFile(path, path.info = NULL)

checkIsDir(path, path.info = NULL)

checkIsNotDir(path, path.info = NULL)

checkIsLink(path, okBadLink = FALSE)

checkIsNotLink(path)

Arguments

path

A file system path to check.

path.info

The data frame returned by the call file.info(path). By default this is NULL and will be looked up during execution. However, this file system operation can be slow; when doing multiple checks on the same file, might want to do this once and pass the result to each check that needs it. As the point of providing this is speed, little validation is done, so if this is not an info data frame for the correct path, weird and unexpected behavior may result.

okBadLink

By default this is FALSE and links with missing targets will result in a failure message. Set TRUE to allow bad links.

Details

Since file systems change asynchronously with respect to running R code, success or failure of a test does not guarantee that even the next line of code will work if that code depends on the previously checked file system state. This is called a race condition. However, checking up front will in practice be highly correlated with the state of the file system for some reasonable time frame, and it is simpler to handle errors caught early when there is a lot less code in progress that needs to be unwound. As a pre-flight check, these are very useful. Just don't count on the results once the plane is in the air.

Value

All functions return either an empty string if the check succeeds or a non-empty string if the check fails (including if checking causes an error.)

Functions

Examples

  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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# Create file system objects for examples.
emptyFile  <- tempfile()
emptyDir   <- tempfile()
noSuchFile <- tempfile()
file.create( emptyFile )
dir.create(  emptyDir  )

linkToEmptyFile <- tempfile()
linkToEmptyDir  <- tempfile()
linkToNowhere   <- tempfile()
linkToLinkToEmptyFile <- tempfile()
linkToLinkToNowhere   <- tempfile()
okLink <- file.symlink( emptyFile,       linkToEmptyFile       ) &&
          file.symlink( emptyDir,        linkToEmptyDir        ) &&
          file.symlink( noSuchFile,      linkToNowhere         ) &&
          file.symlink( linkToEmptyFile, linkToLinkToEmptyFile ) &&
          file.symlink( linkToNowhere,   linkToLinkToNowhere   )

checkIsPath( emptyFile )
#=> [1] ""
checkIsPath( emptyDir )
#=> [1] ""
checkIsPath( noSuchFile )
#=> [1] "No such path."
checkIsPath( NA )    # file.exists(NA) is an error
#=> [1] "Checking for a path failed with the following error:
#=> invalid 'file' argument"

if (okLink) {
   checkIsPath( linkToEmptyFile )
   #=> [1] ""
   checkIsPath( linkToEmptyDir )
   #=> [1] ""
   checkIsPath( linkToNowhere )
   #=> [1] "No such path."

   checkIsPath( linkToLinkToEmptyFile )
   #=> [1] ""
   checkIsPath( linkToLinkToNowhere )
   #=> [1] "No such path."
}

checkIsNotPath( emptyFile )
#=> [1] "Path exists."
checkIsNotPath( emptyDir )
#=> [1] "Path exists."
checkIsNotPath( noSuchFile )
#=> [1] ""
checkIsNotPath( NA )    # file.exists(NA) is an error
#=> [1] "Checking for a path failed with the following error:
#=> invalid 'file' argument"

if (okLink) {
   checkIsNotPath( linkToEmptyFile )
   #=> [1] "Path exists."
   checkIsNotPath( linkToEmptyDir )
   #=> [1] "Path exists."
   checkIsNotPath( linkToNowhere )
   #=> [1] ""

   checkIsNotPath( linkToLinkToEmptyFile )
   #=> [1] "Path exists."
   checkIsNotPath( linkToLinkToNowhere )
   #=> [1] ""
}

checkIsFile( emptyFile )
#=> [1] ""
checkIsFile( emptyDir )
#=> [1] "Not a file."
checkIsFile( noSuchFile )
#=> [1] "No such path."
checkIsFile( NA )    # file.exists(NA) is an error
#=> [1] "Checking for a file failed with the following error:
#=> invalid 'file' argument"

checkIsFile( emptyFile, path.info= file.info( emptyFile ))
#=> [1] ""
checkIsFile( emptyFile, path.info= file.info( emptyDir ))  # Oops.
#=> [1] "Not a file." # Not true!

if (okLink) {
   checkIsFile( linkToEmptyFile )
   #=> [1] ""
   checkIsFile( linkToEmptyDir )
   #=> [1] "Not a file."
   checkIsFile( linkToNowhere )
   #=> [1] "No such path."

   checkIsFile( linkToLinkToEmptyFile )
   #=> [1] ""
   checkIsFile( linkToLinkToNowhere )
   #=> [1] "No such path."
}

checkIsNotFile( emptyFile )
#=> [1] "Is a file."
checkIsNotFile( emptyDir )
#=> [1] ""
checkIsNotFile( noSuchFile )
#=> [1] "No such path."
checkIsNotFile( NA )    # file.exists(NA) is an error
#=> [1] "Checking for a file failed with the following error:
#=> invalid 'file' argument"

checkIsNotFile( emptyFile, path.info= file.info( emptyFile ))
#=> [1] "Is a file."
checkIsNotFile( emptyFile, path.info= file.info( emptyDir ))  # Oops.
#=> [1] "" # Not true!

if (okLink) {
   checkIsNotFile( linkToEmptyFile )
   #=> [1] "Is a file."
   checkIsNotFile( linkToEmptyDir )
   #=> [1] ""
   checkIsNotFile( linkToNowhere )
   #=> [1] "No such path."

   checkIsNotFile( linkToLinkToEmptyFile )
   #=> [1] "Is a file."
   checkIsNotFile( linkToLinkToNowhere )
   #=> [1] "No such path."
}

checkIsDir( emptyFile )
#=> [1] "Not a directory."
checkIsDir( emptyDir )
#=> [1] ""
checkIsDir( noSuchFile )
#=> [1] "No such path."
checkIsDir( NA )    # file.exists(NA) is an error
#=> [1] "Checking for a directory failed with the following error:
#=> invalid 'file' argument"

checkIsDir( emptyFile, path.info= file.info( emptyFile ))
#=> [1] "Not a directory."
checkIsDir( emptyFile, path.info= file.info( emptyDir ))  # Oops.
#=> [1] "" # Not true!

if (okLink) {
   checkIsDir( linkToEmptyFile )
   #=> [1] "Not a directory."
   checkIsDir( linkToEmptyDir )
   #=> [1] ""
   checkIsDir( linkToNowhere )
   #=> [1] "No such path."

   checkIsDir( linkToLinkToEmptyFile )
   #=> [1] "Not a directory."
   checkIsDir( linkToLinkToNowhere )
   #=> [1] "No such path."
}

checkIsNotDir( emptyFile )
#=> [1] ""
checkIsNotDir( emptyDir )
#=> [1] "Is a directory."
checkIsNotDir( noSuchFile )
#=> [1] "No such path."
checkIsNotDir( NA )    # file.exists(NA) is an error
#=> [1] "Checking for a directory failed with the following error:
#=> invalid 'file' argument"

checkIsNotDir( emptyFile, path.info= file.info( emptyFile ))
#=> [1] ""
checkIsNotDir( emptyFile, path.info= file.info( emptyDir ))  # Oops.
#=> [1] "Is a directory." # Not true!

if (okLink) {
   checkIsNotDir( linkToEmptyFile )
   #=> [1] ""
   checkIsNotDir( linkToEmptyDir )
   #=> [1] "Is a directory."
   checkIsNotDir( linkToNowhere )
   #=> [1] "No such path."

   checkIsNotDir( linkToLinkToEmptyFile )
   #=> [1] ""
   checkIsNotDir( linkToLinkToNowhere )
   #=> [1] "No such path."
}

checkIsLink( emptyFile )
#=> [1] "Not a link"
checkIsLink( emptyDir )
#=> [1] "Not a link"
checkIsLink( noSuchFile )
#=> [1] "No such path."
checkIsLink( NA )    # file.exists(NA) is an error
#=> [1] "Checking for a link failed with the following error:
#=> invalid 'file' argument"

if (okLink) {
   checkIsLink( linkToEmptyFile )
   #=> [1] ""
   checkIsLink( linkToEmptyDir )
   #=> [1] ""
   checkIsLink( linkToNowhere )
   #=> [1] "Bad link."
   checkIsLink( linkToNowhere, okBadLink= TRUE )
   #=> [1] ""

   checkIsLink( linkToLinkToEmptyFile )
   #=> [1] ""
   checkIsLink( linkToLinkToNowhere )
   #=> [1] "Bad link."
   checkIsLink( linkToLinkToNowhere, okBadLink= TRUE )
   #=> [1] ""
}

checkIsNotLink( emptyFile )
#=> [1] ""
checkIsNotLink( emptyDir )
#=> [1] ""
checkIsNotLink( noSuchFile )
#=> [1] "No such path."
checkIsNotLink( NA )    # file.exists(NA) is an error
#=> [1] "Checking for a link failed with the following error:
#=> invalid 'file' argument"

if (okLink) {
   checkIsNotLink( linkToEmptyFile )
   #=> [1] "Is a link."
   checkIsNotLink( linkToEmptyDir )
   #=> [1] "Is a link."
   checkIsNotLink( linkToNowhere )
   #=> [1] "No such path"

   checkIsNotLink( linkToLinkToEmptyFile )
   #=> [1] "Is a link."
   checkIsNotLink( linkToLinkToNowhere )
   #=> [1] "No such path."
}

# cleanup
unlink( emptyFile )
unlink ( emptyDir, recursive= TRUE )
unlink( linkToEmptyFile )
unlink( linkToEmptyDir )
unlink( linkToNowhere )
unlink( linkToLinkToEmptyFile )
unlink( linkToLinkToNowhere )

jefferys/DataRepo documentation built on May 19, 2019, 3:58 a.m.