ursa-engine 0.13.0

Ursa's SQL engine session: a DataFusion session that reads files by path, locally or in object storage.
Documentation
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
//! Turning a name in `FROM` into a file location.
//!
//! `SELECT * FROM 'edges.parquet'` names a file where a table would go. A name
//! that reads as a location (it has a scheme, a path separator, an extension or
//! a glob) is opened as a listing table; anything else is left to the catalog,
//! so a misspelt table name still reports "table not found".
//!
//! DataFusion's [`ListingTableUrl::parse`] expands a glob only in a scheme-less
//! local path. [`table_url`] also expands one in a URL (`s3://bucket/dir/*.parquet`,
//! `file:///data/*.csv`): the URL is split at the path segment where the glob
//! begins, the part before it becomes the listing prefix, and the rest filters
//! the listing.
//!
//! A relative path resolves against the session's [`Root`]: a directory
//! natively, a URL in the browser, or, natively with none, the working
//! directory. A session with a directory root also writes only under it
//! ([`write_target`], [`check_write`]), and a read-only session writes
//! nothing ([`set_read_only`]).

use std::path::{Component, Path, PathBuf};
use std::sync::Arc;

use datafusion::datasource::listing::ListingTableUrl;
use datafusion::error::{DataFusionError, Result};
use datafusion::prelude::{SessionConfig, SessionContext};
use glob::Pattern;
use url::Url;

/// The characters that start a glob, as DataFusion's local-path parser has them.
const GLOB_START_CHARS: [char; 3] = ['*', '?', '['];

/// Whether a table name reads as a file location rather than a table name.
pub fn looks_like_location(name: &str) -> bool {
    name.contains(['/', '\\', '.']) || name.contains(GLOB_START_CHARS) || name.starts_with('~')
}

/// `~` or `~/…` with the home directory put in for the `~`.
pub fn expand_tilde(location: &str) -> String {
    if location == "~" || location.starts_with("~/") {
        if let Some(home) = std::env::var_os("HOME").and_then(|h| h.into_string().ok()) {
            if !home.is_empty() {
                return format!("{home}{}", &location[1..]);
            }
        }
    }
    location.to_string()
}

/// The listing table URL for a location: a local path (with globs, as DataFusion
/// expands them), or a URL whose path may contain a glob. A relative path is
/// relative to the process's working directory; [`session_table_url`]
/// resolves it against a session's [`Root`].
pub fn table_url(location: &str) -> Result<ListingTableUrl> {
    table_url_under(None, location)
}

/// [`table_url`] in the session with `config`: a relative path resolves
/// against the session's [`Root`], if it has one.
pub fn session_table_url(config: &SessionConfig, location: &str) -> Result<ListingTableUrl> {
    table_url_under(root(config).as_ref(), location)
}

fn table_url_under(root: Option<&Root>, location: &str) -> Result<ListingTableUrl> {
    let location = resolve_under(root, location)?;
    if let Some((base, pattern)) = split_url_glob(&location) {
        let url = Url::parse(base)
            .map_err(|e| DataFusionError::Plan(format!("'{location}' is not a valid URL: {e}")))?;
        let glob = Pattern::new(pattern).map_err(|e| {
            DataFusionError::Plan(format!(
                "'{pattern}' in '{location}' is not a valid glob: {e}"
            ))
        })?;
        return ListingTableUrl::try_new(url, Some(glob));
    }
    ListingTableUrl::parse(&location)
        .map_err(|e| DataFusionError::Plan(format!("'{location}' is not a valid path or URL: {e}")))
}

/// For a URL with a scheme, split it at the start of the path segment that holds
/// the first glob character: `s3://b/data/*.parquet` gives `("s3://b/data/",
/// "*.parquet")`. `None` for a local path (DataFusion expands its globs itself),
/// a URL without a glob, and `http(s)` URLs, where `?` starts a query and a
/// server cannot be listed anyway.
pub fn split_url_glob(location: &str) -> Option<(&str, &str)> {
    let scheme_end = location.find("://")?;
    let scheme = &location[..scheme_end];
    // A one-letter "scheme" is a Windows drive letter.
    if scheme.len() < 2
        || !scheme
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.'))
        || matches!(scheme, "http" | "https")
    {
        return None;
    }
    let authority_start = scheme_end + 3;
    let path_start = authority_start + location[authority_start..].find('/')?;
    let path = &location[path_start..];
    let glob_at = path.find(GLOB_START_CHARS)?;
    // `path` starts with '/', so there is always a separator before the glob.
    let split = path_start + path[..glob_at].rfind('/')? + 1;
    Some((&location[..split], &location[split..]))
}

/// Where a session's relative paths resolve: its root.
///
/// The root travels with the session, as an extension of its
/// [`SessionConfig`] ([`set_root`], [`with_root`]), so two sessions in one
/// process can each have their own.
///
/// - **No root:** natively, a relative path is relative to the process's
///   working directory, as the CLI has always read it. The browser has no
///   working directory, so there a relative path is an error.
/// - **[`Root::Dir`]:** a directory, natively (`ursa serve DIR`). `'edges.csv'`
///   is `DIR/edges.csv`. Reads are not confined: an absolute path, `~/…` or a
///   URL reads what it names, as anywhere else in the native engine. Writes
///   are: `COPY … TO` and `.output` write only under the root
///   ([`write_target`]), and `INSERT INTO` a table of files only into files
///   under it ([`check_write`]).
/// - **[`Root::Url`]:** a URL, such as the browser shell's `https://…/data/`.
///   A relative path, or an absolute one (`/data/x.csv`), is joined to it as
///   a browser joins a link. It confines no write: a browser writes only the
///   session's own files (`crate::session_files`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Root {
    /// A directory. A relative one is made absolute (against the working
    /// directory) when it is set.
    Dir(PathBuf),
    /// A URL. One that names a directory ends in `/`, as in a browser.
    Url(Url),
}

/// The session's root, as its config holds it.
#[derive(Debug)]
struct SessionRoot(Option<Root>);

/// `config` with `root` as its session's root (`None`: none).
pub fn with_root(config: SessionConfig, root: Option<Root>) -> SessionConfig {
    config.with_extension(Arc::new(SessionRoot(root.map(absolute))))
}

/// Set (or clear) the root of the session `ctx`. It applies from the next
/// statement on.
pub fn set_root(ctx: &SessionContext, root: Option<Root>) {
    let state = ctx.state_ref();
    let mut state = state.write();
    state
        .config_mut()
        .set_extension(Arc::new(SessionRoot(root.map(absolute))));
}

/// The root of the session with `config`, if it has one.
pub fn root(config: &SessionConfig) -> Option<Root> {
    config
        .get_extension::<SessionRoot>()
        .and_then(|root| root.0.clone())
}

/// The directory a native session's relative local paths are relative to:
/// its root, if that is a directory, else the working directory. `None` if
/// the working directory cannot be read (or the root is a URL).
pub fn base_dir(config: &SessionConfig) -> Option<PathBuf> {
    match root(config) {
        Some(Root::Dir(dir)) => Some(dir),
        Some(Root::Url(_)) => None,
        None => std::env::current_dir().ok(),
    }
}

fn absolute(root: Root) -> Root {
    match root {
        Root::Dir(dir) if dir.is_relative() => Root::Dir(std::path::absolute(&dir).unwrap_or(dir)),
        root => root,
    }
}

/// Whether `location` is a URL: it has a scheme of two letters or more (a
/// one-letter one is a Windows drive).
fn has_scheme(location: &str) -> bool {
    matches!(Url::parse(location), Ok(url) if url.scheme().len() > 1)
}

/// `location` (a path, glob or URL, as `FROM` names it) resolved in the
/// session with `config`: `~` expanded, then a relative path joined to the
/// session's [`Root`]. With no root, a relative path is left as it is (the
/// working directory's), except in the browser, which has none.
pub fn resolve(config: &SessionConfig, location: &str) -> Result<String> {
    resolve_under(root(config).as_ref(), location)
}

fn resolve_under(root: Option<&Root>, location: &str) -> Result<String> {
    let location = expand_tilde(location);
    if has_scheme(&location) {
        return Ok(location);
    }
    match root {
        Some(Root::Url(base)) => base.join(&location).map(String::from).map_err(|e| {
            DataFusionError::Plan(format!("'{location}' is not a valid path or URL: {e}"))
        }),
        Some(Root::Dir(_)) if Path::new(&location).is_absolute() => Ok(location),
        Some(Root::Dir(dir)) => dir
            .join(&location)
            .into_os_string()
            .into_string()
            .map_err(|_| {
                DataFusionError::Plan(format!(
                    "'{location}' cannot be read: the session's root, {}, is not valid UTF-8",
                    dir.display()
                ))
            }),
        None if cfg!(target_arch = "wasm32") => Err(DataFusionError::Plan(format!(
            "'{location}' is a relative path, and the browser has no current directory; \
             use an http:// or https:// URL"
        ))),
        None => Ok(location),
    }
}

/// Whether a session writes no files ([`set_read_only`]).
#[derive(Debug)]
struct ReadOnly(bool);

/// Make the session `ctx` read-only (`ursa serve --read-only`): it writes no
/// files. [`write_target`] and [`check_write`] refuse every write, and
/// `ursa-sql` refuses `COPY … TO` (also to the session's files in memory),
/// `INSERT INTO` a table that is not in memory, and `ATTACH` of a database
/// that does not exist; it opens one that does read-only. Reads, and what a
/// session defines in memory (views, tables, graphs), are as before.
pub fn set_read_only(ctx: &SessionContext, on: bool) {
    let state = ctx.state_ref();
    let mut state = state.write();
    state.config_mut().set_extension(Arc::new(ReadOnly(on)));
}

/// Whether the session with `config` is read-only ([`set_read_only`]).
pub fn is_read_only(config: &SessionConfig) -> bool {
    config.get_extension::<ReadOnly>().is_some_and(|on| on.0)
}

/// Refuse a write in a read-only session ([`set_read_only`]), with a reason
/// that does not name what was refused, for the caller to say that.
pub fn check_writable(config: &SessionConfig) -> Result<()> {
    if is_read_only(config) {
        return Err(DataFusionError::Plan(READ_ONLY.into()));
    }
    Ok(())
}

/// Why a read-only session refuses a write.
pub const READ_ONLY: &str =
    "this session is read-only (ursa serve --read-only), and writes no files";

/// Where a write to `target` (`COPY … TO 'target'`, `.output target`) goes in
/// the session with `config`: `Ok(None)` to write it as named, in a session
/// with no directory [`Root`]; in a session with one, the absolute path under
/// the root that `target` names, or why it may not be written (a reason
/// that does not repeat `target`, for the caller to say what was refused).
/// A read-only session ([`set_read_only`]) refuses every target.
///
/// A rooted session writes only under its root. `target` must be a relative
/// path with no `..` (not absolute, not `~/…`, not a URL), and no part of it
/// that exists may be a symbolic link that leads outside the root. The check
/// is made before the write, not during it: it keeps a client of the session
/// (a page, an agent) inside the root, not another process that swaps a
/// directory for a link in between.
pub fn write_target(config: &SessionConfig, target: &str) -> Result<Option<String>> {
    check_writable(config)?;
    match root(config) {
        Some(Root::Dir(dir)) => under_root(&dir, target).map(Some),
        _ => Ok(None),
    }
}

fn under_root(dir: &Path, target: &str) -> Result<String> {
    let outside = |why: &str| {
        DataFusionError::Plan(format!(
            "it is outside this session's root, {} ({why}); a session with a root writes \
             only under it, to a relative path such as 'out/result.parquet'",
            dir.display()
        ))
    };
    if has_scheme(target) {
        return Err(outside("it is a URL"));
    }
    if target.starts_with('~') {
        return Err(outside("it is in the home directory"));
    }
    let mut parts = Vec::new();
    for component in Path::new(target).components() {
        match component {
            Component::Normal(part) => parts.push(part),
            Component::CurDir => {}
            Component::ParentDir => return Err(outside("it has '..' in it")),
            Component::RootDir | Component::Prefix(_) => {
                return Err(outside("it is an absolute path"))
            }
        }
    }
    if parts.is_empty() {
        return Err(outside("it names the root itself"));
    }
    let root = std::fs::canonicalize(dir).map_err(|e| {
        DataFusionError::Plan(format!(
            "the session's root, {}, cannot be read: {e}",
            dir.display()
        ))
    })?;
    let mut path = root.clone();
    path.extend(&parts);
    match resolve_links(&path) {
        Ok(real) if real.starts_with(&root) => {}
        Ok(_) => return Err(outside("a symbolic link in it leads outside the root")),
        Err(e) => {
            return Err(DataFusionError::Plan(format!(
                "a symbolic link in it cannot be followed: {e}"
            )))
        }
    }
    let mut written = path.into_os_string().into_string().map_err(|_| {
        DataFusionError::Plan(format!(
            "the session's root, {}, is not valid UTF-8",
            dir.display()
        ))
    })?;
    // `COPY … TO 'out/'` writes a directory of files.
    if target.ends_with(['/', std::path::MAIN_SEPARATOR]) {
        written.push(std::path::MAIN_SEPARATOR);
    }
    Ok(written)
}

/// Refuse a write to the files at `url` (an `INSERT INTO` a table of files)
/// in a session with a directory [`Root`], unless they are under it: a local
/// path whose links stay inside the root. Any write is allowed in a session
/// without one, unless it is read-only ([`set_read_only`]).
pub fn check_write(config: &SessionConfig, url: &Url) -> Result<()> {
    if is_read_only(config) {
        return Err(DataFusionError::Plan(format!(
            "cannot write to {url}: {READ_ONLY}"
        )));
    }
    let Some(Root::Dir(dir)) = root(config) else {
        return Ok(());
    };
    let refused = || {
        DataFusionError::Plan(format!(
            "cannot write to {url}: it is outside this session's root, {}, and a session \
             with a root writes only under it",
            dir.display()
        ))
    };
    let path = local_path(url).ok_or_else(refused)?;
    let root = std::fs::canonicalize(&dir).map_err(|_| refused())?;
    match resolve_links(&path) {
        Ok(real) if real.starts_with(&root) => Ok(()),
        _ => Err(refused()),
    }
}

/// The local path a `file://` URL names.
#[cfg(not(target_arch = "wasm32"))]
fn local_path(url: &Url) -> Option<PathBuf> {
    (url.scheme() == "file").then(|| url.to_file_path().ok())?
}

/// The browser has no local paths.
#[cfg(target_arch = "wasm32")]
fn local_path(_: &Url) -> Option<PathBuf> {
    None
}

/// `path` (absolute, without `..`) with its symbolic links resolved as far as
/// it exists, and the rest, which does not exist yet, as it is. An error for
/// a link that cannot be followed (one that leads nowhere, or a loop): a
/// write would create its target, wherever it is.
fn resolve_links(path: &Path) -> std::io::Result<PathBuf> {
    match std::fs::canonicalize(path) {
        Ok(real) => Ok(real),
        Err(e) => {
            // It is there (a link), but cannot be followed.
            if std::fs::symlink_metadata(path).is_ok() {
                return Err(e);
            }
            match (path.parent(), path.file_name()) {
                (Some(parent), Some(name)) => Ok(resolve_links(parent)?.join(name)),
                _ => Err(e),
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn locations_and_table_names() {
        assert!(looks_like_location("edges.csv"));
        assert!(looks_like_location("data/edges"));
        assert!(looks_like_location("s3://bucket/x"));
        assert!(looks_like_location("*.parquet"));
        assert!(looks_like_location("~/edges"));
        assert!(!looks_like_location("edges"));
        assert!(!looks_like_location("my_table"));
    }

    #[test]
    fn splits_url_globs() {
        assert_eq!(
            split_url_glob("s3://bucket/data/*.parquet"),
            Some(("s3://bucket/data/", "*.parquet"))
        );
        assert_eq!(
            split_url_glob("gs://bucket/a/b/part-?.csv"),
            Some(("gs://bucket/a/b/", "part-?.csv"))
        );
        assert_eq!(
            split_url_glob("s3://bucket/year=*/x.parquet"),
            Some(("s3://bucket/", "year=*/x.parquet"))
        );
        assert_eq!(
            split_url_glob("file:///tmp/x/[ab].csv"),
            Some(("file:///tmp/x/", "[ab].csv"))
        );
        assert_eq!(split_url_glob("s3://bucket/data/x.parquet"), None);
        assert_eq!(split_url_glob("https://host/x.csv?y=1"), None);
        assert_eq!(split_url_glob("data/*.csv"), None);
        assert_eq!(split_url_glob("C://data/*.csv"), None);
    }

    #[test]
    fn remote_glob_url_lists_its_prefix() {
        let url = table_url("s3://bucket/data/*.parquet").unwrap();
        assert_eq!(url.scheme(), "s3");
        assert_eq!(url.prefix().as_ref(), "data");
        assert!(url.is_collection());
        assert_eq!(url.get_glob().as_ref().unwrap().as_str(), "*.parquet");
    }

    fn rooted(dir: &Path) -> SessionConfig {
        with_root(SessionConfig::new(), Some(Root::Dir(dir.to_path_buf())))
    }

    #[cfg(unix)]
    #[test]
    fn a_relative_path_resolves_against_the_root() {
        let config = rooted(Path::new("/srv/data"));
        assert_eq!(
            resolve(&config, "edges.csv").unwrap(),
            "/srv/data/edges.csv"
        );
        assert_eq!(
            resolve(&config, "./sub/*.csv").unwrap(),
            "/srv/data/./sub/*.csv"
        );
        assert_eq!(
            resolve(&config, "/abs/edges.csv").unwrap(),
            "/abs/edges.csv"
        );
        assert_eq!(
            resolve(&config, "s3://bucket/x.parquet").unwrap(),
            "s3://bucket/x.parquet"
        );
        let url = with_root(
            SessionConfig::new(),
            Some(Root::Url(Url::parse("https://example.com/data/").unwrap())),
        );
        assert_eq!(
            resolve(&url, "karate_edges.csv").unwrap(),
            "https://example.com/data/karate_edges.csv"
        );
        assert_eq!(
            resolve(&url, "/other/x.csv").unwrap(),
            "https://example.com/other/x.csv"
        );
        // No root: as written, the working directory's (natively).
        assert_eq!(
            resolve(&SessionConfig::new(), "edges.csv").unwrap(),
            "edges.csv"
        );
        let table = session_table_url(&config, "edges.csv").unwrap();
        assert_eq!(table.get_url().as_str(), "file:///srv/data/edges.csv");
    }

    #[test]
    fn a_relative_root_is_made_absolute() {
        let config = with_root(SessionConfig::new(), Some(Root::Dir("data".into())));
        let Some(Root::Dir(dir)) = root(&config) else {
            panic!("a directory root")
        };
        assert_eq!(dir, std::env::current_dir().unwrap().join("data"));
    }

    #[test]
    fn writes_stay_under_the_root() {
        let dir = tempfile::tempdir().unwrap();
        let root_dir = std::fs::canonicalize(dir.path()).unwrap();
        let config = rooted(dir.path());
        let target = |t: &str| write_target(&config, t);
        assert_eq!(
            target("out.parquet").unwrap().unwrap(),
            root_dir.join("out.parquet").to_str().unwrap()
        );
        assert_eq!(
            target("./new/dir/out.csv").unwrap().unwrap(),
            root_dir.join("new/dir/out.csv").to_str().unwrap()
        );
        assert!(target("parts/").unwrap().unwrap().ends_with("parts/"));
        for (refused, why) in [
            ("../out.csv", "'..'"),
            ("a/../../out.csv", "'..'"),
            ("/tmp/out.csv", "absolute"),
            ("~/out.csv", "home directory"),
            ("file:///tmp/out.csv", "URL"),
            ("s3://bucket/out.csv", "URL"),
            ("memory://uploads/out.csv", "URL"),
            (".", "the root itself"),
        ] {
            let e = target(refused).unwrap_err().to_string();
            assert!(
                e.contains("outside this session's root") && e.contains(why),
                "{refused}: {e}"
            );
        }
        // No root: as written.
        assert_eq!(
            write_target(&SessionConfig::new(), "/tmp/x.csv").unwrap(),
            None
        );
    }

    #[cfg(unix)]
    #[test]
    fn a_symbolic_link_out_of_the_root_is_refused() {
        let dir = tempfile::tempdir().unwrap();
        let outside = tempfile::tempdir().unwrap();
        let root_dir = dir.path().join("root");
        std::fs::create_dir(&root_dir).unwrap();
        std::fs::create_dir(root_dir.join("inside")).unwrap();
        std::os::unix::fs::symlink(outside.path(), root_dir.join("away")).unwrap();
        std::os::unix::fs::symlink(root_dir.join("inside"), root_dir.join("here")).unwrap();
        std::os::unix::fs::symlink(outside.path().join("x.csv"), root_dir.join("dangling.csv"))
            .unwrap();
        std::os::unix::fs::symlink(outside.path().join("y.csv"), root_dir.join("inside/y.csv"))
            .unwrap();
        let config = rooted(&root_dir);
        for refused in ["away/x.csv", "away/new/x.csv", "here/y.csv"] {
            let e = write_target(&config, refused).unwrap_err().to_string();
            assert!(e.contains("symbolic link"), "{refused}: {e}");
        }
        // A link that leads nowhere would be created wherever it points.
        let e = write_target(&config, "dangling.csv")
            .unwrap_err()
            .to_string();
        assert!(e.contains("cannot be followed"), "{e}");
        // A link that stays inside the root is fine.
        assert!(write_target(&config, "here/z.csv").unwrap().is_some());
        // A root that is itself a link is followed.
        let linked = dir.path().join("linked");
        std::os::unix::fs::symlink(&root_dir, &linked).unwrap();
        assert!(write_target(&rooted(&linked), "inside/z.csv")
            .unwrap()
            .is_some());

        let inside = Url::from_file_path(root_dir.join("inside/t.csv")).unwrap();
        check_write(&config, &inside).unwrap();
        let away = Url::from_file_path(root_dir.join("away/t.csv")).unwrap();
        assert!(check_write(&config, &away).is_err());
        let s3 = Url::parse("s3://bucket/t.csv").unwrap();
        assert!(check_write(&config, &s3).is_err());
        check_write(&SessionConfig::new(), &s3).unwrap();
    }

    #[test]
    fn a_read_only_session_writes_nothing() {
        let dir = tempfile::tempdir().unwrap();
        let read_only = |config: SessionConfig| config.with_extension(Arc::new(ReadOnly(true)));
        for config in [
            read_only(rooted(dir.path())),
            read_only(SessionConfig::new()),
        ] {
            assert!(is_read_only(&config));
            let e = write_target(&config, "out.csv").unwrap_err().to_string();
            assert!(e.contains("read-only"), "{e}");
            let inside = Url::from_file_path(dir.path().join("t.csv")).unwrap();
            let e = check_write(&config, &inside).unwrap_err().to_string();
            assert!(e.contains("read-only"), "{e}");
        }
        assert!(!is_read_only(&rooted(dir.path())));
        assert!(write_target(&rooted(dir.path()), "out.csv")
            .unwrap()
            .is_some());
    }
}