Annotation of nono/host/hostwindrv.cpp, revision 1.1.1.3

1.1       root        1: //
                      2: // nono
                      3: // Copyright (C) 2023 nono project
                      4: // Licensed under nono-license.txt
                      5: //
                      6: 
                      7: //
                      8: // Windrv (相当) ホストドライバ
                      9: //
                     10: 
                     11: #include "hostwindrv.h"
                     12: #include <dirent.h>
                     13: #include <inttypes.h>
                     14: #include <fcntl.h>
                     15: #include <sys/types.h>
                     16: #include <sys/stat.h>
                     17: #if defined(HAVE_STATVFS)
                     18: #include <sys/statvfs.h>
                     19: #elif defined(HAVE_STATFS)
                     20: #include <sys/statfs.h>
                     21: #endif
                     22: #include <sys/time.h>
                     23: #include <algorithm>
                     24: 
                     25: // コンストラクタ
                     26: HostWindrv::HostWindrv(WindrvDevice *windrv)
                     27:        : inherited(OBJ_HOSTWINDRV)
                     28: {
                     29:        // ホストデバイスのログレベルは常に VM デバイス(親)に従属なので
                     30:        // ドライバオブジェクトにログエイリアスは不要。
                     31:        ClearAlias();
                     32: 
                     33:        // コンストラクト後ただちにログ出力できるようここで一度追従しておく。
                     34:        // 以降はホストデバイスの SetLogLevel() で変更する。
                     35:        loglevel = windrv->loglevel;
                     36: }
                     37: 
                     38: // デストラクタ
                     39: HostWindrv::~HostWindrv()
                     40: {
                     41: }
                     42: 
                     43: // ルートパスの設定。
1.1.1.2   root       44: bool
1.1       root       45: HostWindrv::InitRootPath(const std::string& rootpath_)
                     46: {
                     47:        rootpath = rootpath_;
                     48: 
                     49:        // ルートパスのほうは末尾の '/' なし。
                     50:        // ゲストパスが必ず '/' から始まるため。
1.1.1.3 ! root       51:        while (rootpath.empty() == false && rootpath.back() == '/') {
1.1       root       52:                rootpath.pop_back();
                     53:        }
                     54:        if (rootpath.empty()) {
                     55:                rootpath = ".";
                     56:        }
1.1.1.2   root       57: 
                     58:        struct stat st;
                     59:        int r = stat(rootpath.c_str(), &st);
                     60:        if (r < 0) {
                     61:                putmsg(1, "stat '%s': %s", rootpath.c_str(), strerror(errno));
                     62:                return false;
                     63:        }
                     64:        if (!S_ISDIR(st.st_mode)) {
                     65:                putmsg(1, "stat '%s': Not directory", rootpath.c_str());
                     66:                return false;
                     67:        }
                     68:        r = access(rootpath.c_str(), R_OK | X_OK);
                     69:        if (r != 0) {
                     70:                putmsg(1, "access '%s': %s", rootpath.c_str(), strerror(errno));
                     71:                return false;
                     72:        }
                     73: 
                     74:        return true;
1.1       root       75: }
                     76: 
                     77: // 初期化コマンド。
                     78: // 戻り値はステータスコード。
                     79: uint32
                     80: HostWindrv::Initialize()
                     81: {
                     82:        vdirs.clear();
                     83: 
                     84:        return 0;
                     85: }
                     86: 
                     87: // ゲストパスのディレクトリ path に対応する VDir をオープンして返す。
                     88: // 見付からなければ NULL を返す。
                     89: // path はゲストパスなので "/BIN/" を指定して、マッチしたホストディレクトリ
                     90: // (および VDir の検索キー) が "/bin/" とかはある。
                     91: VDir *
                     92: HostWindrv::OpenVDir(const std::string& path)
                     93: {
                     94:        putmsg(2, "%s: '%s'", __func__, path.c_str());
                     95:        std::vector<std::string> search_names = SplitPath(path);
                     96: 
                     97:        std::string cwd = "/";
                     98:        VDir *vdir;
                     99:        for (auto it = search_names.begin();;) {
                    100:                // cwd に対する dir を取得。
                    101:                vdir = FindVDir(cwd);
                    102:                if (vdir) {
                    103:                        // 見付かれば更新。
                    104:                        UpdateVDir(vdir);
                    105:                } else {
                    106:                        // キャッシュになければ、ホストのディレクトリを読んで作成。
                    107:                        putmsg(2, "%s: add '%s'", __func__, cwd.c_str());
                    108:                        vdir = AddVDir(cwd);
                    109:                        if (vdir == NULL) {
                    110:                                // それでもなければ、ない。
                    111:                                putmsg(2, "%s: vdir not found", __func__);
                    112:                                return NULL;
                    113:                        }
                    114:                }
                    115: 
                    116:                // サブディレクトリへ降りる必要がなければここまで。
                    117:                if (it == search_names.end()) {
                    118:                        break;
                    119:                }
                    120: 
                    121:                const auto& name = *it;
                    122:                const VDirent *ent = vdir->MatchName(name, true);
                    123:                if (ent == NULL) {
                    124:                        putmsg(2, "%s: '%s' not found", __func__, name.c_str());
                    125:                        return NULL;
                    126:                }
                    127:                if (ent->IsDir() == false) {
                    128:                        putmsg(2, "%s: '%s' is not dir", __func__, name.c_str());
                    129:                        return NULL;
                    130:                }
                    131:                // サブディレクトリが見付かったので、ここに降りて次へ。
                    132:                ++it;
                    133:                cwd += name;
                    134:                cwd += '/';
                    135:        }
                    136: 
                    137:        vdir->Acquire();
                    138:        return vdir;
                    139: }
                    140: 
                    141: // ホスト相対パス path に対応する VDir を返す。
                    142: // 見付からなければ NULL を返す。
                    143: // この時点では VDir のタイムスタンプは更新しない。
                    144: VDir *
                    145: HostWindrv::FindVDir(const std::string& path) const
                    146: {
                    147:        std::string nmpath = NormalizePath(path);
                    148:        auto it = vdirs.find(nmpath);
                    149:        if (it == vdirs.end()) {
                    150:                return NULL;
                    151:        }
                    152:        return it->second;
                    153: }
                    154: 
                    155: // 仮想ディレクトリ dir を必要なら更新。
                    156: bool
                    157: HostWindrv::UpdateVDir(VDir *vdir)
                    158: {
                    159:        return FillVDir(vdir, __func__, false);
                    160: }
                    161: 
                    162: // ホスト相対パス path に対応する VDir を作成して追加。
                    163: VDir *
                    164: HostWindrv::AddVDir(const std::string& path)
                    165: {
                    166:        std::string nmpath = NormalizePath(path);
                    167: 
                    168:        VDir *new_vdir = new VDir(this, nmpath);
                    169:        if (FillVDir(new_vdir, __func__, true) == false) {
                    170:                delete new_vdir;
                    171:                return NULL;
                    172:        }
                    173: 
                    174:        // vdirs に追加。
                    175:        // この時点で存在しないはず。
                    176:        assert(vdirs.find(nmpath) == vdirs.end());
                    177: 
                    178:        // 一杯なら、参照した時間がもっとも古いものを削除。
                    179:        // 本当は Refcount がゼロなやつでないとおかしいはずだが、
                    180:        // そんなに使用中のはずはないのでいいか…。
                    181:        if (vdirs.size() >= MaxVDirs) {
                    182:                auto it = std::min_element(vdirs.begin(), vdirs.end(),
                    183:                        [](const auto& a, const auto& b) {
                    184:                                const auto av = a.second;
                    185:                                const auto bv = b.second;
                    186:                                if (av->Refcount() < bv->Refcount())
                    187:                                        return true;
                    188:                                if (av->Refcount() > bv->Refcount())
                    189:                                        return false;
                    190:                                return av->GetATime() < bv->GetATime();
                    191:                        }
                    192:                );
                    193:                auto old_vdir = it->second;
                    194:                putmsg(2, "%s Purge '%s'", __func__, old_vdir->GetPath().c_str());
                    195:                delete old_vdir;
                    196:                vdirs.erase(it);
                    197:        }
                    198:        vdirs[nmpath] = new_vdir;
                    199:        return new_vdir;
                    200: }
                    201: 
                    202: // vdir のディレクトリエントリを(再)構成する。
                    203: // caller は呼び出し元関数名。ログ表示のため。
                    204: // force が true なら常に作成、false なら更新があったときだけ作成。
                    205: bool
                    206: HostWindrv::FillVDir(VDir *vdir, const char *caller, bool force)
                    207: {
                    208:        struct stat st;
                    209: 
                    210:        std::string path = vdir->GetPath();
                    211:        std::string fullpath = rootpath + path;
                    212:        int r = stat(fullpath.c_str(), &st);
                    213:        if (r < 0) {
                    214:                if (errno == ENOENT) {
                    215:                        putmsg(2, "%s: '%s' (%s) not found", caller,
                    216:                                path.c_str(), fullpath.c_str());
                    217:                } else {
                    218:                        putmsg(2, "%s: '%s' (%s): %s", caller,
                    219:                                path.c_str(), fullpath.c_str(), strerror(errno));
                    220:                }
                    221:                return false;
                    222:        }
                    223: 
                    224: #if defined(HAVE_STAT_ST_TIMESPEC)
                    225:        // 更新判定は st_mtimespec がある環境でだけ行う。
                    226:        // st_mtime だと同一の1秒内に更新したことが分からないため。(Linux)
                    227:        if (force == false) {
                    228:                // OS のディレクトリが VDir より新しいか調べる。
                    229:                if (vdir->GetMTime() == timespec2nsec(st.st_mtimespec)) {
                    230:                        return true;
                    231:                }
                    232:                putmsg(2, "%s: '%s' (%s) has been updated", caller,
                    233:                        path.c_str(), fullpath.c_str());
                    234:        }
                    235: #endif
                    236: 
                    237:        auto& list = vdir->list;
                    238:        list.clear();
                    239: 
                    240:        // ホストのディレクトリエントリから仮想ディレクトリエントリを作成。
                    241:        DIR *dir = opendir(fullpath.c_str());
                    242:        if (dir == NULL) {
                    243:                putmsg(2, "%s: opendir '%s' (%s): %s", caller,
                    244:                        path.c_str(), fullpath.c_str(), strerror(errno));
                    245:                return false;
                    246:        }
                    247:        for (struct dirent *d; (d = readdir(dir)) != NULL; ) {
                    248:                // VDirent の属性はディレクトリかファイルか、のみ。
                    249:                bool isdir;
                    250:                if (d->d_type == DT_DIR) {
                    251:                        isdir = true;
                    252:                } else if (d->d_type == DT_REG) {
                    253:                        isdir = false;
                    254:                } else if (d->d_type == DT_LNK) {
                    255:                        // リンク先を調べる
                    256:                        std::string linkname = fullpath + std::string(d->d_name);
                    257:                        struct stat linkst;
                    258:                        r = stat(linkname.c_str(), &linkst);
                    259:                        if (r < 0) {
                    260:                                continue;
                    261:                        }
                    262:                        if (S_ISDIR(linkst.st_mode)) {
                    263:                                isdir = true;
                    264:                        } else if (S_ISREG(linkst.st_mode)) {
                    265:                                isdir = false;
                    266:                        } else {
                    267:                                continue;
                    268:                        }
                    269:                } else {
                    270:                        continue;
                    271:                }
                    272: 
                    273:                if (IsHuman68kFilename(d->d_name) == false) {
                    274:                        continue;
                    275:                }
                    276:                list.emplace_back(std::string(d->d_name), isdir);
                    277:        }
                    278:        closedir(dir);
                    279: 
                    280:        // ファイル名をチェック。
                    281:        if (Windrv::option_case_sensitive == false) {
                    282:                // 大文字小文字を区別しないモードでは "a.bak" と "a.BAK" は
                    283:                // 同一なので、どちらもゲストに出現させない。
                    284:                for (int i = 0, iend = list.size() - 1; i < iend; i++) {
                    285:                        if (list[i].IsValid() == false) {
                    286:                                continue;
                    287:                        }
                    288:                        for (int j = i + 1, jend = list.size(); j < jend; j++) {
                    289:                                if (strcasecmp(list[i].name.c_str(),
                    290:                                               list[j].name.c_str()) == 0)
                    291:                                {
                    292:                                        list[i].Invalidate();
                    293:                                        list[j].Invalidate();
                    294:                                }
                    295:                        }
                    296:                }
                    297:        }
                    298: 
                    299:        if (loglevel >= 3) {
                    300:                putmsgn("%s: Enumerating '%s'...", caller, path.c_str());
                    301:                for (int i = 0; i < list.size(); i++) {
                    302:                        putmsgn(" %c%c%c '%s'",
                    303:                                (list[i].IsValid()   ? '-' : 'I'),
                    304:                                (list[i].IsArchive() ? 'A' : '-'),
                    305:                                (list[i].IsDir()     ? 'D' : '-'),
                    306:                                list[i].name.c_str());
                    307:                }
                    308:        }
                    309: 
                    310:        // タイムスタンプを更新。
                    311: #if defined(HAVE_STAT_ST_TIMESPEC)
                    312:        vdir->SetMTime(timespec2nsec(st.st_mtimespec));
                    313: #endif
                    314:        vdir->UpdateATime();
                    315: 
                    316:        return true;
                    317: }
                    318: 
                    319: // パスを分解して返す。
                    320: // "/a/bc/" なら { "a", "bc" } に。
                    321: // "/" なら { } を返す。
                    322: /*static*/ std::vector<std::string>
                    323: HostWindrv::SplitPath(const std::string& path)
                    324: {
                    325:        // 先頭と末尾の '/' を取り除く。
                    326:        int s = 0;
                    327:        int e = path.size();
                    328:        for (; s < e; s++) {
                    329:                if (path[s] != '/') {
                    330:                        break;
                    331:                }
                    332:        }
                    333:        for (e--; e >= s; e--) {
                    334:                if (path[e] != '/') {
                    335:                        break;
                    336:                }
                    337:        }
                    338:        std::string tmppath = path.substr(s, e - s + 1);
                    339: 
                    340:        // '/' で分解。
                    341:        // path が "/a/bc/" なら tmppath は "a/bc" になっているので
                    342:        // これで { "a", "bc" } になる。
                    343:        std::vector<std::string> sep = string_split(tmppath, '/');
                    344:        return sep;
                    345: }
                    346: 
                    347: // 相対パス文字列を正規化する。
                    348: // vdirs[] のキーに使う場合用。
                    349: /*static*/ std::string
                    350: HostWindrv::NormalizePath(const std::string& path)
                    351: {
                    352:        std::string src = "/" + path + "/";
                    353:        std::string dst;
                    354: 
                    355:        bool sep = false;
                    356:        for (auto c : src) {
                    357:                if (sep) {
                    358:                        if (c != '/') {
                    359:                                sep = false;
                    360:                                dst += c;
                    361:                        }
                    362:                } else {
                    363:                        if (c == '/') {
                    364:                                sep = true;
                    365:                        }
                    366:                        dst += c;
                    367:                }
                    368:        }
                    369:        return dst;
                    370: }
                    371: 
                    372: // vdir をクローズする。
                    373: void
                    374: HostWindrv::CloseVDir(VDir *vdir)
                    375: {
                    376:        if (vdir) {
                    377:                vdir->Release();
                    378:        }
                    379: }
                    380: 
                    381: // fname が Human68k で使えるファイル名なら true を返す。
                    382: bool
                    383: HostWindrv::IsHuman68kFilename(const char *fname) const
                    384: {
                    385:        if (strcmp(fname, ".") == 0 || strcmp(fname, "..") == 0) {
                    386:                return true;
                    387:        }
                    388: 
                    389:        const std::string name = fname; //ToGuestCharset(fname);
                    390: 
                    391:        for (auto c : name) {
                    392:                // TODO: 記号どこまで使えるか調べる
                    393:                if (!isalnum((int)c) && strchr("-._", c) == NULL) {
                    394:                        return false;
                    395:                }
                    396:        }
                    397: 
                    398:        const char *p = strchr(name.c_str(), '.');
                    399:        if (p == NULL) {
                    400:                // '.' がない場合は 8+10 文字以内でなければいけない。
                    401:                if (name.size() > 18) {
                    402:                        return false;
                    403:                }
                    404:        } else {
                    405:                const char *e = strrchr(name.c_str(), '.');
                    406:                if (p == e) {
                    407:                        // '.' が1つなら、1.1 文字以上 8+10.3 文字以内でなければいけない。
                    408:                        int len1 = p - name.c_str();
                    409:                        int len2 = strlen(p + 1);
                    410:                        if (len1 < 1 || len1 > 18) {
                    411:                                return false;
                    412:                        }
                    413:                        if (len2 < 1 || len2 > 3) {
                    414:                                return false;
                    415:                        }
                    416:                } else {
                    417:                        // '.' が複数は未対応にするか。
                    418:                        return false;
                    419:                }
                    420:        }
                    421: 
                    422:        return true;
                    423: }
                    424: 
                    425: // ゲスト文字コードの文字列をホスト文字コードに変換。
                    426: std::string
                    427: HostWindrv::ToHostCharset(const std::string& src) const
                    428: {
                    429:        // XXX Not supported yet
                    430:        return src;
                    431: }
                    432: 
                    433: // ホスト文字コードの文字列をゲスト文字コードに変換。
                    434: std::string
                    435: HostWindrv::ToGuestCharset(const std::string& src) const
                    436: {
                    437:        // XXX Not supported yet
                    438:        return src;
                    439: }
                    440: 
                    441: // メディア容量を取得する。
                    442: // 成功すれば cap にパラメータを書き出して 0 を返す。
                    443: // 失敗すればステータスコードを返す。
                    444: uint32
                    445: HostWindrv::GetCapacity(Human68k::capacity *cap)
                    446: {
                    447:        uint64 totalbytes;
                    448:        uint64 availbytes;
                    449:        uint bytes_per_sector;
                    450:        uint sectors_per_cluster;
                    451:        uint total_clusters;
                    452:        uint avail_clusters;
                    453: 
                    454:        // メディアの総容量と空き容量を取得。
                    455: #if defined(HAVE_STATVFS)
                    456:        struct statvfs buf;
                    457:        int r = statvfs(rootpath.c_str(), &buf);
                    458:        if (r < 0) {
                    459:                putmsg(1, "statvfs: %s: %s", rootpath.c_str(), strerror(errno));
                    460:                return Human68k::RES_INVALID_PARAM;
                    461:        }
                    462: #elif defined(HAVE_STATFS)
                    463:        struct statfs buf;
                    464:        int r = statfs(rootpath.c_str(), &buf);
                    465:        if (r < 0) {
                    466:                putmsg(1, "statfs: %s: %s", rootpath.c_str(), strerror(errno));
                    467:                return Human68k::RES_INVALID_PARAM;
                    468:        }
                    469: #else
                    470:        putmsg(0, "%s: No statfs() nor statvfs()", __func__);
                    471:        return Human68k::RES_INVALID_PARAM;
                    472: #endif
                    473:        totalbytes = (uint64)buf.f_bsize * (uint64)buf.f_blocks;
                    474:        availbytes = (uint64)buf.f_bsize * (uint64)buf.f_bavail;
                    475: 
                    476:        // 空き容量/総容量ともに 2GB に制限する。
                    477:        if (availbytes >= 0x8000'0000U) {
                    478:                availbytes = 0x7fff'ffff;
                    479:        }
                    480:        if (totalbytes >= 0x8000'0000U) {
                    481:                totalbytes = 0x8000'0000U;
                    482:        }
                    483:        // クラスタを適当にでっちあげる。
                    484:        bytes_per_sector = 512;
                    485:        sectors_per_cluster = 128;
                    486:        total_clusters = totalbytes / (sectors_per_cluster * bytes_per_sector);
                    487:        avail_clusters = availbytes / (sectors_per_cluster * bytes_per_sector);
                    488: 
                    489:        cap->avail_clusters = avail_clusters;
                    490:        cap->total_clusters = total_clusters;
                    491:        cap->sectors_per_cluster = sectors_per_cluster;
                    492:        cap->bytes_per_sector = bytes_per_sector;
                    493: 
                    494:        return 0;
                    495: }
                    496: 
                    497: // ホストパス path の示すファイルの Human68k 属性を返す。
                    498: // エラーの場合はステータスコードを返す。
                    499: // stp があれば stat(2) の結果を書き戻す。
                    500: uint32
                    501: HostWindrv::GetAttributeInternal(const std::string& fullpath,
                    502:        struct stat *stp) const
                    503: {
                    504:        struct stat stbuf;
                    505:        uint32 attr;
                    506:        int r;
                    507: 
                    508:        if (stp == NULL) {
                    509:                stp = &stbuf;
                    510:        }
                    511: 
                    512:        r = stat(fullpath.c_str(), stp);
                    513:        if (r < 0) {
                    514:                putmsg(1, "%s: stat: %s: %s",
                    515:                        __func__, fullpath.c_str(), strerror(errno));
                    516:                return Human68k::RES_INVALID_PARAM; // 何返すのがいいか
                    517:        }
                    518: 
                    519:        // 属性。
                    520:        if (S_ISDIR(stp->st_mode)) {
                    521:                attr = Human68k::ATTR_DIR;
                    522:        } else if (S_ISREG(stp->st_mode)) {
                    523:                attr = Human68k::ATTR_ARCHIVE;
                    524:        } else {
                    525:                putmsg(1, "%s: st_mode is %o", __func__, stp->st_mode);
                    526:                return Human68k::RES_INVALID_PARAM; // 何返すのがいいか
                    527:        }
                    528: 
                    529:        // 自分に書き込み権がないものは全部 ReadOnly。
                    530:        r = access(fullpath.c_str(), W_OK);
                    531:        if (r < 0) {
                    532:                if (errno == EACCES || errno == EROFS) {
                    533:                        attr |= Human68k::ATTR_RDONLY;
                    534:                } else {
                    535:                        putmsg(1, "%s: access: %s: %s",
                    536:                                __func__, fullpath.c_str(), strerror(errno));
                    537:                        return Human68k::RES_INVALID_PARAM; // 何返すのがいいか
                    538:                }
                    539:        }
                    540: 
                    541:        return attr;
                    542: }
                    543: 
                    544: // path の示すファイルについて FILES の所定のフィールドを埋めて返す。
                    545: bool
                    546: HostWindrv::GetFileStat(Human68k::FILES *files, const std::string& path) const
                    547: {
                    548:        struct stat st;
                    549: 
                    550:        assert(files);
                    551: 
                    552:        std::string fullpath = rootpath + path;
                    553:        uint32 attr = GetAttributeInternal(fullpath, &st);
                    554:        if ((int32)attr < 0) {
                    555:                return false;
                    556:        }
                    557:        files->attr = attr;
                    558: 
                    559:        uint32 datetime = Human68k::Unixtime2DateTime(st.st_mtime);
                    560:        files->date = datetime >> 16;
                    561:        files->time = datetime & 0xffff;
                    562: 
                    563:        // 2GB を超えるファイルは扱わないことにする。
                    564:        if (st.st_size >= 0x8000'0000) {
                    565:                putmsg(1, "%s: %s: size exceeds", __func__, fullpath.c_str());
                    566:                return false;
                    567:        }
                    568:        files->size = (uint32)st.st_size;
                    569: 
                    570:        return true;
                    571: }
                    572: 
                    573: // ホスト相対パス path で示されるディレクトリを作成する。
                    574: uint32
                    575: HostWindrv::MakeDir(const std::string& path)
                    576: {
                    577:        std::string fullpath = rootpath + path;
                    578:        int r = mkdir(fullpath.c_str(), 0755);
                    579:        if (r < 0) {
                    580:                putmsg(1, "%s: mkdir %s: %s", __func__,
                    581:                        fullpath.c_str(), strerror(errno));
                    582:                return -1; // ?
                    583:        }
                    584:        return 0;
                    585: }
                    586: 
                    587: // ホスト相対パス path で示されるディレクトリを削除する。
                    588: // 削除するディレクトリは空でなければならない。
                    589: uint32
                    590: HostWindrv::RemoveDir(const std::string& path)
                    591: {
                    592:        std::string fullname = rootpath + path;
                    593:        int r = rmdir(fullname.c_str());
                    594:        if (r < 0) {
                    595:                putmsg(1, "%s: rmdir %s: %s", __func__,
                    596:                        fullname.c_str(), strerror(errno));
                    597:                if (errno == ENOTEMPTY) {
                    598:                        return Human68k::RES_CANNOT_DELETE;
                    599:                }
                    600:                return -1; // ?
                    601:        }
                    602:        return 0;
                    603: }
                    604: 
                    605: // ホスト相対パス oldpath を newpath にリネームもしくは移動する。
                    606: uint32
                    607: HostWindrv::Rename(const std::string& oldpath, const std::string& newpath)
                    608: {
                    609:        std::string oldfullpath = rootpath + oldpath;
                    610:        std::string newfullpath = rootpath + newpath;
                    611:        int r = rename(oldfullpath.c_str(), newfullpath.c_str());
                    612:        if (r < 0) {
                    613:                putmsg(1, "%s: rename %s %s: %s", __func__,
                    614:                        oldfullpath.c_str(), newfullpath.c_str(), strerror(errno));
                    615:                return -1; // ?
                    616:        }
                    617:        return 0;
                    618: }
                    619: 
                    620: // ホスト相対パス path で示されるファイルを削除する。
                    621: // ディレクトリは削除出来ない。
                    622: uint32
                    623: HostWindrv::Delete(const std::string& path)
                    624: {
                    625:        std::string fullname = rootpath + path;
                    626:        int r = unlink(fullname.c_str());
                    627:        if (r < 0) {
                    628:                putmsg(1, "%s: unlink %s: %s", __func__,
                    629:                        fullname.c_str(), strerror(errno));
                    630:                return -1; // ?
                    631:        }
                    632:        return 0;
                    633: }
                    634: 
                    635: // ホスト相対パス path で示されるファイルの属性を返す。
                    636: // 属性は Human68k::ATTR_*。
                    637: // エラーの場合はステータスコードを返す。
                    638: uint32
                    639: HostWindrv::GetAttribute(const std::string& path)
                    640: {
                    641:        std::string fullname = rootpath + path;
                    642:        uint32 result = GetAttributeInternal(fullname, NULL);
                    643:        return result;
                    644: }
                    645: 
                    646: // ホスト相対パス path で示されるファイルの属性を attr に変更する。
                    647: // attr は Human68k::ATTR_*。
                    648: // 現状 RDONLY の上げ下げのみ対応。
                    649: uint32
                    650: HostWindrv::SetAttribute(const std::string& path, uint8 newattr)
                    651: {
                    652:        struct stat st;
                    653: 
                    654:        std::string fullname = rootpath + path;
                    655:        uint32 attr = GetAttributeInternal(fullname, &st);
                    656: 
                    657:        attr &= Human68k::ATTR_RDONLY;
                    658:        newattr &= Human68k::ATTR_RDONLY;
                    659: 
                    660:        mode_t newmode;
                    661:        if (attr == 0 && newattr != 0) {
                    662:                newmode = st.st_mode & ~0200;
                    663:        } else if (attr != 0 && newattr == 0) {
                    664:                newmode = st.st_mode | 0200;
                    665:        } else {
                    666:                return 0;
                    667:        }
                    668: 
                    669:        int r = chmod(fullname.c_str(), newmode);
                    670:        if (r < 0) {
                    671:                putmsg(1, "%s: chmod(%03o) %s: %s", __func__,
                    672:                        newmode & 0777, fullname.c_str(), strerror(errno));
                    673:                return -1; // ?
                    674:        }
                    675:        return 0;
                    676: }
                    677: 
                    678: // ホスト相対パス path で示されるファイルを作成する。
                    679: // 属性 attr は今の所 RDONLY しか見ていない。
                    680: uint32
                    681: HostWindrv::CreateFile(Windrv::FCB *fcb, const std::string& path, uint8 attr)
                    682: {
                    683:        assert(fcb);
                    684: 
                    685:        int mode = 0666;
                    686:        if ((attr & Human68k::ATTR_RDONLY)) {
                    687:                mode &= ~0222;
                    688:        }
                    689: 
                    690:        std::string fullname = rootpath + path;
                    691:        fcb->fd = open(fullname.c_str(), O_CREAT | O_TRUNC | O_WRONLY, mode);
                    692:        if (fcb->fd < 0) {
                    693:                putmsg(1, "%s: create %s: %s", __func__,
                    694:                        fullname.c_str(), strerror(errno));
                    695:                return Human68k::RES_INVALID_FUNC; // ?
                    696:        }
                    697: 
                    698:        return 0;
                    699: }
                    700: 
                    701: // ホスト相対パス path で示されるファイルをオープンする。
                    702: // fcb->mode は Human68k::OPEN_* のいずれか。
                    703: // オープン出来れば、fcb に諸々をセットして 0 を返す。
                    704: //  o fcb->date, fcb->time に MS-DOS 形式の日付時刻
                    705: //  o fcb->size にファイルサイズ
                    706: //  o fcb->fd にディスクリプタ
                    707: // 失敗すればステータスコードを返す。
                    708: uint32
                    709: HostWindrv::Open(Windrv::FCB *fcb, const std::string& path)
                    710: {
                    711:        assert(fcb);
                    712: 
                    713:        // 全世界的に同じ値だと思うけど…。
                    714:        int openmode;
                    715:        switch (fcb->GetMode()) {
                    716:         case Human68k::OPEN_RDONLY:    openmode = O_RDONLY;    break;
                    717:         case Human68k::OPEN_WRONLY:    openmode = O_WRONLY;    break;
                    718:         case Human68k::OPEN_RDWR:              openmode = O_RDWR;              break;
                    719:         default:
                    720:                return Human68k::RES_INVALID_MODE;
                    721:        }
                    722: 
                    723:        std::string fullname = rootpath + path;
                    724:        fcb->fd = open(fullname.c_str(), openmode);
                    725:        if (fcb->fd < 0) {
                    726:                putmsg(1, "%s: open %s: %s", __func__,
                    727:                        fullname.c_str(), strerror(errno));
                    728:                return Human68k::RES_INVALID_FUNC; // ?
                    729:        }
                    730: 
                    731:        struct stat st;
                    732:        int r = fstat(fcb->fd, &st);
                    733:        if (r < 0) {
                    734:                putmsg(1, "%s: fstat %s: %s", __func__,
                    735:                        fullname.c_str(), strerror(errno));
                    736:                Close(fcb);
                    737:                return Human68k::RES_INVALID_FUNC; // ?
                    738:        }
                    739: 
                    740:        fcb->SetDateTime(Human68k::Unixtime2DateTime(st.st_mtime));
                    741:        fcb->SetSize(st.st_size);
                    742:        return 0;
                    743: }
                    744: 
                    745: // FCB をクローズする。
                    746: void
                    747: HostWindrv::Close(Windrv::FCB *fcb)
                    748: {
                    749:        if (fcb->fd >= 0) {
                    750:                close(fcb->fd);
                    751:                fcb->fd = -1;
                    752:        }
                    753: }
                    754: 
                    755: // fcb(->fd) から dst に len バイト読み込む。
                    756: // ここでは fcb は更新不要。
                    757: uint32
                    758: HostWindrv::Read(Windrv::FCB *fcb, void *dst, uint32 len)
                    759: {
                    760:        assert(fcb);
                    761: 
                    762:        auto n = read(fcb->fd, dst, len);
                    763:        if (n < 0) {
                    764:                putmsg(1, "%s: %s", __func__, strerror(errno));
                    765:                return -1;
                    766:        }
                    767:        return (uint32)n;
                    768: }
                    769: 
                    770: // fcb(->fd) に src から len バイトを書き出す。
                    771: // ここでは fcb は更新不要。
                    772: uint32
                    773: HostWindrv::Write(Windrv::FCB *fcb, const void *src, uint32 len)
                    774: {
                    775:        assert(fcb);
                    776: 
                    777:        auto n = write(fcb->fd, src, len);
                    778:        if (n < 0) {
                    779:                putmsg(1, "%s: %s", __func__, strerror(errno));
                    780:                return -1;
                    781:        }
                    782:        return (uint32)n;
                    783: }
                    784: 
                    785: // シークする。
                    786: // 戻り値はシーク後の先頭からの現在位置。
                    787: // ここでは fcb は更新不要。
                    788: uint32
                    789: HostWindrv::Seek(Windrv::FCB *fcb, int32 offset, int whence)
                    790: {
                    791:        assert(fcb);
                    792: 
                    793:        auto n = lseek(fcb->fd, (off_t)offset, whence);
                    794:        if (n < 0) {
                    795:                putmsg(1, "%s: lseek($%x, %d): %s", __func__,
                    796:                        offset, whence, strerror(errno));
                    797:                return -1;
                    798:        }
                    799: 
                    800:        return (uint32)n;
                    801: }
                    802: 
                    803: // フラッシュ。
                    804: uint32
                    805: HostWindrv::Flush()
                    806: {
                    807:        return 0;
                    808: }
                    809: 
                    810: // ファイルの更新日時(MS-DOS形式)を取得する。
                    811: uint32
                    812: HostWindrv::GetFileTime(Windrv::FCB *fcb)
                    813: {
                    814:        assert(fcb);
                    815: 
                    816:        struct stat st;
                    817:        int r = fstat(fcb->fd, &st);
                    818:        if (r < 0) {
                    819:                putmsg(1, "%s: fstat: %s", __func__, strerror(errno));
                    820:                return -1;
                    821:        }
                    822: 
                    823:        uint32 result = Human68k::Unixtime2DateTime(st.st_mtime);
                    824:        return result;
                    825: }
                    826: 
                    827: // ファイルの更新日時を設定する。
                    828: // datetime は MS-DOS 形式。
                    829: uint32
                    830: HostWindrv::SetFileTime(Windrv::FCB *fcb, uint32 datetime)
                    831: {
                    832:        assert(fcb);
                    833: 
                    834:        // [0] が atime、[1] が mtime。
                    835:        struct timeval times[2];
                    836:        times[1].tv_sec = Human68k::DateTime2Unixtime(datetime);
                    837:        times[1].tv_usec = 0;
                    838:        gettimeofday(&times[0], NULL);
                    839:        int r = futimes(fcb->fd, times);
                    840:        if (r < 0) {
                    841:                putmsg(1, "%s: futimes: %s", __func__, strerror(errno));
                    842:                return -1;
                    843:        }
                    844:        return 0;
                    845: }
                    846: 
                    847: #if defined(HAVE_STAT_ST_TIMESPEC)
                    848: /*static*/ uint64
                    849: HostWindrv::timespec2nsec(const struct timespec& ts)
                    850: {
                    851:        return (uint64)ts.tv_sec * 1000'000'000 + (uint64)ts.tv_nsec;
                    852: }
                    853: #endif

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.