<commit_before>// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015 Oslo and Akershus University College of Applied Sciences
// and <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
//1 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#define OS_TERMINATE_ON_CONTRACT_VIOLATION
#include <os>

#include <fs/disk.hpp>
std::shared_ptr<fs::Disk> disk;

void list_partitions(decltype(disk));

#define MYINFO(X,...) INFO("VirtioBlk",X,##__VA_ARGS__)

void Service::start(const std::string&)
{
  // instantiate memdisk with FAT filesystem
  auto& device = hw::Devices::drive(0);
  disk = std::make_shared<fs::Disk> (device);
  // assert that we have a disk
  CHECKSERT(disk, "Disk created");
  // if the disk is empty, we can't mount a filesystem
  CHECKSERT(not disk->empty(), "Disk is not empty");

  // list extended partitions
  list_partitions(disk);

  // Initialize first valid partition (auto-detect and init)
  disk->init_fs(
  [] (fs::error_t err) {
    if (err) {
      printf("Could not mount filesystem\n");
      panic("init_fs() failed");
    }
    CHECKSERT (not err, "Was able to mount filesystem");

    // async ls
    disk->fs().ls("/",
    [] (fs::error_t err, auto ents) {
      if (err) {
        printf("Could not list '/' directory\n");
        panic("ls() failed");
      }

      // go through directory entries
      for (auto& e : *ents) {
        printf("%s: %s\t of size %llu bytes (CL: %llu)\n",
               e.type_string().c_str(), e.name().c_str(), e.size(), e.block());

        if (e.is_file()) {
          printf("*** Read %s\n", e.name().c_str());
          disk->fs().read(e, 0, e.size(),
          [e] (fs::error_t err, fs::buffer_t buffer, size_t len) {
            if (err) {
              printf("Failed to read %s!\n", e.name().c_str());
              panic("read() failed");
            }

            std::string contents((const char*) buffer.get(), len);
            printf("[%s contents]:\n%s\nEOF\n\n",
                   e.name().c_str(), contents.c_str());
            // ---
            INFO("Virtioblk Test", "SUCCESS");
          });

        } // is_file

      } // ents

    }); // ls

  }); // disk->auto_detect()
}

void list_partitions(decltype(disk) disk)
{
  disk->partitions([] (fs::error_t err, auto& parts) {

      CHECKSERT (not err, "Was able to fetch partition table");

      for (auto& part : parts)
        printf("[Partition]  '%s' at LBA %u\n",
               part.name().c_str(), part.lba());

    });
}
<commit_msg>fixed virtio_block integration test<commit_after>// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015 Oslo and Akershus University College of Applied Sciences
// and <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
//1 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#define OS_TERMINATE_ON_CONTRACT_VIOLATION
#include <os>

#include <fs/disk.hpp>
std::shared_ptr<fs::Disk> disk;

void list_partitions(decltype(disk));

#define MYINFO(X,...) INFO("VirtioBlk",X,##__VA_ARGS__)

void Service::start(const std::string&)
{
  // instantiate memdisk with FAT filesystem
  auto& device = hw::Devices::drive(0);
  disk = std::make_shared<fs::Disk> (device);
  // assert that we have a disk
  CHECKSERT(disk, "Disk created");
  // if the disk is empty, we can't mount a filesystem
  CHECKSERT(not disk->empty(), "Disk is not empty");

  // list extended partitions
  list_partitions(disk);

  // Initialize first valid partition (auto-detect and init)
  disk->init_fs(
  [] (fs::error_t err) {
    if (err) {
      printf("Could not mount filesystem\n");
      panic("init_fs() failed");
    }
    CHECKSERT (not err, "Was able to mount filesystem");

    // async ls
    disk->fs().ls("/",
    [] (fs::error_t err, auto ents) {
      if (err) {
        printf("Could not list '/' directory\n");
        panic("ls() failed");
      }

      // go through directory entries
      for (auto& e : *ents) {
        printf("%s: %s\t of size %llu bytes (CL: %llu)\n",
               e.type_string().c_str(), e.name().c_str(), e.size(), e.block());

        if (e.is_file()) {
          printf("*** Read %s\n", e.name().c_str());
          disk->fs().read(
            e,
            0,
            e.size(),
            [e_name = e.name()]
            (fs::error_t err, fs::buffer_t buffer, size_t len)
            {
              if (err) {
                printf("Failed to read %s!\n", e_name.c_str());
                panic("read() failed");
              }

              std::string contents((const char*) buffer.get(), len);
              printf("[%s contents]:\n%s\nEOF\n\n",
                     e_name.c_str(), contents.c_str());
              // ---
              INFO("Virtioblk Test", "SUCCESS");
            }
          );

        } // is_file

      } // ents

    }); // ls

  }); // disk->auto_detect()
}

void list_partitions(decltype(disk) disk)
{
  disk->partitions([] (fs::error_t err, auto& parts) {

      CHECKSERT (not err, "Was able to fetch partition table");

      for (auto& part : parts)
        printf("[Partition]  '%s' at LBA %u\n",
               part.name().c_str(), part.lba());

    });
}

{'fraction_non_alphanumeric': 0.15656731084179495, 'fraction_numerical': 0.0036030134294136916, 'mean_word_length': 2.4328274311410905, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1205822', 'n_tokens_mistral': 1941, 'n_tokens_neox': 1880, 'n_words': 581}