Skip to content

Commit 59ab3f3

Browse files
committed
publish: use libc for screenshot extraction syscalls
1 parent d8903b4 commit 59ab3f3

3 files changed

Lines changed: 26 additions & 31 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ flat-manager-common = { path = "common" }
3939
hex = "0.4"
4040
jwt = { package = "jsonwebtoken", version = "10.3.0", features = ["aws_lc_rs"] }
4141
log = "0.4"
42+
libc = "0.2"
4243
libostree = { package = "ostree", version = "0.20.5", features = ["v2021_5"] }
4344
r2d2 = "0.8"
4445
serde = "1.0"

src/jobs/publish_job.rs

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use std::ffi::{CString, OsStr, OsString};
88
use std::fs::{self, File};
99
use std::io::{self, Write};
1010
use std::os::fd::{AsRawFd, FromRawFd, RawFd};
11-
use std::os::raw::{c_char, c_int, c_uint};
1211
use std::os::unix::ffi::OsStrExt;
1312
use std::path::{Component, Path};
1413
use std::process::Command;
@@ -28,23 +27,6 @@ use super::utils::{
2827
schedule_update_job,
2928
};
3029

31-
const O_RDONLY: c_int = 0;
32-
const O_WRONLY: c_int = 1;
33-
const O_CREAT: c_int = 0o100;
34-
const O_EXCL: c_int = 0o200;
35-
const O_DIRECTORY: c_int = 0o200000;
36-
const O_NOFOLLOW: c_int = 0o400000;
37-
const O_CLOEXEC: c_int = 0o2000000;
38-
const O_PATH: c_int = 0o10000000;
39-
const ELOOP: i32 = 40;
40-
41-
unsafe extern "C" {
42-
fn open(pathname: *const c_char, flags: c_int, ...) -> c_int;
43-
fn openat(dirfd: c_int, pathname: *const c_char, flags: c_int, ...) -> c_int;
44-
fn mkdirat(dirfd: c_int, pathname: *const c_char, mode: c_uint) -> c_int;
45-
fn unlinkat(dirfd: c_int, pathname: *const c_char, flags: c_int) -> c_int;
46-
}
47-
4830
#[derive(Debug)]
4931
pub struct PublishJobInstance {
5032
pub job_id: i32,
@@ -366,7 +348,7 @@ fn open_or_create_destination_directory(
366348
Ok(directory) => Ok(directory),
367349
Err(err) if err.kind() == io::ErrorKind::NotFound => {
368350
let name = c_string_for_path_component(name, destination_path)?;
369-
let result = unsafe { mkdirat(parent_fd, name.as_ptr(), 0o755) };
351+
let result = unsafe { libc::mkdirat(parent_fd, name.as_ptr(), 0o755) };
370352
if result != 0 {
371353
let err = io::Error::last_os_error();
372354
if err.kind() != io::ErrorKind::AlreadyExists {
@@ -401,7 +383,7 @@ fn destination_directory_error(
401383
destination_path: &Path,
402384
err: io::Error,
403385
) -> JobError {
404-
if err.raw_os_error() == Some(ELOOP) {
386+
if err.raw_os_error() == Some(libc::ELOOP) {
405387
return unsafe_screenshot_destination(destination_path, "is a symlink");
406388
}
407389

@@ -425,10 +407,15 @@ fn destination_directory_error(
425407

426408
fn open_source_file(path: &Path) -> JobResult<File> {
427409
let path_name = c_string_for_path(path)?;
428-
let fd = unsafe { open(path_name.as_ptr(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW) };
410+
let fd = unsafe {
411+
libc::open(
412+
path_name.as_ptr(),
413+
libc::O_RDONLY | libc::O_CLOEXEC | libc::O_NOFOLLOW,
414+
)
415+
};
429416
if fd < 0 {
430417
let err = io::Error::last_os_error();
431-
if err.raw_os_error() == Some(ELOOP) {
418+
if err.raw_os_error() == Some(libc::ELOOP) {
432419
return Err(unsafe_screenshot_source(path, "is a symlink"));
433420
}
434421
return Err(unsafe_screenshot_source(
@@ -480,7 +467,7 @@ fn remove_existing_destination_file(
480467
destination_path: &Path,
481468
) -> JobResult<()> {
482469
let existing = openat_path_no_follow(parent_dir.as_raw_fd(), name).map_err(|err| {
483-
if err.raw_os_error() == Some(ELOOP) {
470+
if err.raw_os_error() == Some(libc::ELOOP) {
484471
unsafe_screenshot_destination(destination_path, "is a symlink")
485472
} else {
486473
unsafe_screenshot_destination(
@@ -518,7 +505,7 @@ fn remove_existing_destination_file(
518505
drop(existing);
519506

520507
let name = c_string_for_path_component(name, destination_path)?;
521-
let result = unsafe { unlinkat(parent_dir.as_raw_fd(), name.as_ptr(), 0) };
508+
let result = unsafe { libc::unlinkat(parent_dir.as_raw_fd(), name.as_ptr(), 0) };
522509
if result != 0 {
523510
return Err(unsafe_screenshot_destination(
524511
destination_path,
@@ -536,9 +523,9 @@ fn open_dir_path_no_follow(path: &Path) -> Result<File, io::Error> {
536523
let path_name = c_string_for_path(path)
537524
.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err.to_string()))?;
538525
let fd = unsafe {
539-
open(
526+
libc::open(
540527
path_name.as_ptr(),
541-
O_RDONLY | O_CLOEXEC | O_DIRECTORY | O_NOFOLLOW,
528+
libc::O_RDONLY | libc::O_CLOEXEC | libc::O_DIRECTORY | libc::O_NOFOLLOW,
542529
)
543530
};
544531
if fd < 0 {
@@ -552,10 +539,10 @@ fn openat_dir_no_follow(parent_fd: RawFd, name: &OsStr) -> Result<File, io::Erro
552539
let name = CString::new(name.as_bytes())
553540
.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err.to_string()))?;
554541
let fd = unsafe {
555-
openat(
542+
libc::openat(
556543
parent_fd,
557544
name.as_ptr(),
558-
O_RDONLY | O_CLOEXEC | O_DIRECTORY | O_NOFOLLOW,
545+
libc::O_RDONLY | libc::O_CLOEXEC | libc::O_DIRECTORY | libc::O_NOFOLLOW,
559546
)
560547
};
561548
if fd < 0 {
@@ -568,7 +555,13 @@ fn openat_dir_no_follow(parent_fd: RawFd, name: &OsStr) -> Result<File, io::Erro
568555
fn openat_path_no_follow(parent_fd: RawFd, name: &OsStr) -> Result<File, io::Error> {
569556
let name = CString::new(name.as_bytes())
570557
.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err.to_string()))?;
571-
let fd = unsafe { openat(parent_fd, name.as_ptr(), O_PATH | O_CLOEXEC | O_NOFOLLOW) };
558+
let fd = unsafe {
559+
libc::openat(
560+
parent_fd,
561+
name.as_ptr(),
562+
libc::O_PATH | libc::O_CLOEXEC | libc::O_NOFOLLOW,
563+
)
564+
};
572565
if fd < 0 {
573566
Err(io::Error::last_os_error())
574567
} else {
@@ -580,10 +573,10 @@ fn openat_create_new(parent_fd: RawFd, name: &OsStr) -> Result<File, io::Error>
580573
let name = CString::new(name.as_bytes())
581574
.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err.to_string()))?;
582575
let fd = unsafe {
583-
openat(
576+
libc::openat(
584577
parent_fd,
585578
name.as_ptr(),
586-
O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC | O_NOFOLLOW,
579+
libc::O_WRONLY | libc::O_CREAT | libc::O_EXCL | libc::O_CLOEXEC | libc::O_NOFOLLOW,
587580
0o644,
588581
)
589582
};

0 commit comments

Comments
 (0)