Skip to content

Commit

Permalink
stored: remove Device::IsFifo()
Browse files Browse the repository at this point in the history
This is now implemented via SeekType. The refactored code also catches
some cases when a backend does not override a required method.
  • Loading branch information
arogge committed Nov 7, 2022
1 parent 2c452e1 commit 3ee0e4b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 43 deletions.
110 changes: 69 additions & 41 deletions core/src/stored/dev.cc
Expand Up @@ -184,7 +184,7 @@ static void InitiateDevice(JobControlRecord* jcr, Device* dev)
dev->vol_poll_interval = 60;
}

if (dev->IsFifo()) { dev->SetCap(CAP_STREAM); }
if (dev->GetSeekMode() == SeekMode::NOSEEK) { dev->SetCap(CAP_STREAM); }

/*
* If the device requires mount :
Expand Down Expand Up @@ -633,15 +633,25 @@ bool Device::rewind(DeviceControlRecord* dcr)

if (fd < 0) { return false; }

if (IsFifo()) { return true; }

if (d_lseek(dcr, (boffset_t)0, SEEK_SET) < 0) {
BErrNo be;
dev_errno = errno;
Mmsg2(errmsg, _("lseek error on %s. ERR=%s"), print_name(), be.bstrerror());
return false;
switch (GetSeekMode()) {
case SeekMode::NOSEEK: {
return true;
} break;
case SeekMode::BYTES: {
if (d_lseek(dcr, (boffset_t)0, SEEK_SET) < 0) {
BErrNo be;
dev_errno = errno;
Mmsg2(errmsg, _("lseek error on %s. ERR=%s"), print_name(),
be.bstrerror());
return false;
}
} break;
case SeekMode::FILE_BLOCK: {
dev_errno = EINVAL;
Mmsg(errmsg, "Block addressed backends must override rewind().");
return false;
} break;
}

return true;
}

Expand Down Expand Up @@ -719,7 +729,6 @@ bool Device::eod(DeviceControlRecord* dcr)
bool Device::UpdatePos(DeviceControlRecord* dcr)
{
boffset_t pos;
bool ok = true;

if (!IsOpen()) {
dev_errno = EBADF;
Expand All @@ -728,25 +737,34 @@ bool Device::UpdatePos(DeviceControlRecord* dcr)
return false;
}

if (IsFifo()) { return true; }

file = 0;
file_addr = 0;
pos = d_lseek(dcr, (boffset_t)0, SEEK_CUR);
if (pos < 0) {
BErrNo be;
dev_errno = errno;
Pmsg1(000, _("Seek error: ERR=%s\n"), be.bstrerror());
Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"), print_name(),
be.bstrerror());
ok = false;
} else {
file_addr = pos;
block_num = (uint32_t)pos;
file = (uint32_t)(pos >> 32);
switch (GetSeekMode()) {
case SeekMode::NOSEEK: {
return true;
} break;
case SeekMode::BYTES: {
file = 0;
file_addr = 0;
pos = d_lseek(dcr, (boffset_t)0, SEEK_CUR);
if (pos < 0) {
BErrNo be;
dev_errno = errno;
Pmsg1(000, _("Seek error: ERR=%s\n"), be.bstrerror());
Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"), print_name(),
be.bstrerror());
return false;
} else {
file_addr = pos;
block_num = (uint32_t)pos;
file = (uint32_t)(pos >> 32);
}
} break;
case SeekMode::FILE_BLOCK: {
dev_errno = EINVAL;
Mmsg(errmsg, "Block addressed backends must override UpdatePos().");
return false;
} break;
}

return ok;
return true;
}

char* Device::StatusDev()
Expand Down Expand Up @@ -819,20 +837,30 @@ bool Device::Reposition(DeviceControlRecord* dcr,
return false;
}

if (IsFifo()) { return true; }

boffset_t pos = (((boffset_t)rfile) << 32) | rblock;
Dmsg1(100, "===== lseek to %d\n", (int)pos);
if (d_lseek(dcr, pos, SEEK_SET) == (boffset_t)-1) {
BErrNo be;
dev_errno = errno;
Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"), print_name(),
be.bstrerror());
return false;
switch (GetSeekMode()) {
case SeekMode::NOSEEK: {
return true;
} break;
case SeekMode::BYTES: {
boffset_t pos = (((boffset_t)rfile) << 32) | rblock;
Dmsg1(100, "===== lseek to %d\n", (int)pos);
if (d_lseek(dcr, pos, SEEK_SET) == (boffset_t)-1) {
BErrNo be;
dev_errno = errno;
Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"), print_name(),
be.bstrerror());
return false;
}
file = rfile;
block_num = rblock;
file_addr = pos;
} break;
case SeekMode::FILE_BLOCK: {
dev_errno = EINVAL;
Mmsg(errmsg, "Block addressed backends must override Reposition().");
return false;
} break;
}
file = rfile;
block_num = rblock;
file_addr = pos;
return true;
}

Expand Down
3 changes: 1 addition & 2 deletions core/src/stored/dev.h
Expand Up @@ -177,11 +177,11 @@ enum class SeekMode
BYTES // device uses byte offsets (like a plain file)
};

// incomplete list of device types for GuessMissingDeviceTypes()
struct DeviceType {
static constexpr std::string_view B_DROPLET_DEV = "droplet";
static constexpr std::string_view B_FIFO_DEV = "fifo";
static constexpr std::string_view B_FILE_DEV = "file";
static constexpr std::string_view B_GFAPI_DEV = "gfapi";
static constexpr std::string_view B_TAPE_DEV = "tape";
static constexpr std::string_view B_UNKNOWN_DEV = "";
};
Expand Down Expand Up @@ -299,7 +299,6 @@ class Device {
bool RequiresMount() const { return BitIsSet(CAP_REQMOUNT, capabilities); }
bool IsRemovable() const { return BitIsSet(CAP_REM, capabilities); }
bool IsTape() const { return (dev_type == DeviceType::B_TAPE_DEV); }
bool IsFifo() const { return dev_type == DeviceType::B_FIFO_DEV; }
bool IsOpen() const { return fd >= 0; }
bool IsOffline() const { return BitIsSet(ST_OFFLINE, state); }
bool IsLabeled() const { return BitIsSet(ST_LABEL, state); }
Expand Down

0 comments on commit 3ee0e4b

Please sign in to comment.