{"id":1131,"date":"2024-01-24T21:57:23","date_gmt":"2024-01-24T13:57:23","guid":{"rendered":"https:\/\/imuslab.com\/wordpress\/?p=1131"},"modified":"2024-01-24T21:57:23","modified_gmt":"2024-01-24T13:57:23","slug":"golang-get-folder-size-speedup","status":"publish","type":"post","link":"https:\/\/imuslab.com\/wordpress\/2024\/01\/24\/golang-get-folder-size-speedup\/","title":{"rendered":"Golang &#8211; \u52a0\u901f\u6293\u8cc7\u6599\u593e\u7684\u7e3d\u5927\u5c0f\u7684\u65b9\u6cd5"},"content":{"rendered":"\n<p>\u6700\u8fd1\u56e0\u70ba\u4e00\u4e9b\u610f\u5916\u6240\u4ee5\u8981\u983b\u7e41\u8dd1\u91ab\u9662\u56de\u8a3a\uff0c\u7d50\u679c\u5c31\u662f\u5728\u7b49\u5f85\u8ddf\u4f86\u56de\u7684\u6642\u9593\u524d\u5f8c\u591a\u4e86\u5f88\u591a\u96f6\u788e\u7684\u6642\u9593\uff0c\u6240\u4ee5\u6211\u5c31\u4e0d\u6d6a\u8cbb\u9019\u4e9b\u6642\u9593\u4f86\u5e6b\u6211\u7528\u4e86\u5feb 6 &#8211; 7 \u5e74\u7684 ArozOS \u7cfb\u7d71\u505a\u4e86\u9ede\u66f4\u65b0\u4e86\u3002\u4f46\u662f\u9019\u7bc7\u90e8\u843d\u683c\u4e26\u4e0d\u662f\u5728\u8b1b\u6211\u66f4\u65b0\u4e86\u751a\u9ebc\uff08\u96d6\u7136\u67d0\u7a0b\u5ea6\u4e0a\u4e5f\u662f\uff09\uff0c\u800c\u662f\u8b1b\u4e00\u4e0b\u95dc\u65bc Golang \u8655\u7406 exec \u8f38\u51fa\u7684\u4e00\u500b\u5c0f\u767c\u73fe\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u80cc\u666f\u6545\u4e8b &#8211; ArozOS \u7684 Disk Properties \u8f09\u5f88\u6162<\/h3>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/imuslab.com\/wordpress\/wp-content\/uploads\/2024\/01\/\u5716\u7247.png\" alt=\"\" class=\"wp-image-1132\" width=\"435\" height=\"611\" srcset=\"https:\/\/imuslab.com\/wordpress\/wp-content\/uploads\/2024\/01\/\u5716\u7247.png 435w, https:\/\/imuslab.com\/wordpress\/wp-content\/uploads\/2024\/01\/\u5716\u7247-214x300.png 214w\" sizes=\"auto, (max-width: 435px) 100vw, 435px\" \/><figcaption>ArozOS \u7684 Disk Properties<\/figcaption><\/figure>\n\n\n\n<p>ArozOS \u56e0\u70ba\u63a1\u7528\u534a\u865b\u64ec\u78c1\u789f\u67b6\u69cb\u7684\u8a2d\u8a08\uff0c\u6240\u4ee5\u6bcf\u6b21\u8981\u5217\u51fa disk space \u7684\u6642\u5019\uff0c\u9664\u4e86\u4e00\u822c\u50cf Windows \u90a3\u6a23\u6703\u5217\u51fa\u7528\u4e86\u591a\u5c11\u8ddf\u5168\u90e8\u7a7a\u9593\u662f\u591a\u5c11\u4ee5\u5916\uff0c\u5982\u679c\u78c1\u789f\u7a7a\u9593\u662f\u4f7f\u7528\u8005\u5206\u9694\u7684\uff0c\u5c31\u8981\u591a\u8a08\u7b97\u4f7f\u7528\u8005\u4f54\u7528\u7684\u7a7a\u9593\u6bd4\uff08\u5716\u4e0a\u9ec3\u8272\u689d\uff09<\/p>\n\n\n\n<p>\u9019\u500b\u529f\u80fd\u539f\u672c\u662f\u4f7f\u7528 Walk function \u4f86\u5be6\u73fe\u7684\u3002\u56e0\u70ba ArozOS \u7684 vfs architecture \u95dc\u7cfb\uff0c\u9019\u88e1\u6211\u4f7f\u7528\u7684\u662f ArozOS \u5c08\u7528\u7684 filepath module \uff08\u4f60\u53ef\u4ee5\u628a\u5b83\u60f3\u50cf\u6210 Go \u539f\u672c\u7684 filepath.Walk<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var size int64 = 0\nfshAbs.Walk(rpath, func(_ string, info os.FileInfo, err error) error {\n    if err != nil {\n        return err\n    }\n    if !info.IsDir() {\n        size += info.Size()\n    }\n    return err\n})\nfilesize = size<\/code><\/pre>\n\n\n\n<p>fshAbs \u662f file system handler abstraction \u7684\u610f\u601d\uff0c\u5927\u6982\u5c31\u662f\u300c\u9019\u500b\u865b\u64ec\u78c1\u789f\u7684\u6a94\u6848\u7cfb\u7d71\u5c64\u300d\u7684 object\u3002<\/p>\n\n\n\n<p>\u53e6\u5916 rpath \u5c31\u662f\u5f9e user space translate \u904e\u4f86\u5230\u9019\u500b abstraction layer \u7684\u5be6\u8cea\u8def\u5f91\uff0c\u5982\u679c\u662f\u672c\u6a5f\u78c1\u789f\u7684\u8a71\u5c31\u6703\u662f\u597d\u50cf \/media\/storage1\/users\/{username}\/myfile.txt \u9019\u6a23\u7684 path\uff1b\u5982\u679c\u662f network drive \u7684\u8a71\u6703\u662f\u8ddf mount-point \u7684\u76f8\u5c0d\u8def\u5f91\uff0c\u4f8b\u5982 WebDAV \u6216\u662f FTP \u4e0b\u5c31\u53ef\u80fd\u6703\u51fa\u73fe \/myfile.txt\uff0c\u6216\u662f SMB \u4e0b\u7684 \/Share\/myfile.txt \u4e4b\u985e\u3002<\/p>\n\n\n\n<p>\u9019\u6a23\u7684 implementation \u4f60\u4e00\u770b\u5c31\u53ef\u4ee5\u77e5\u9053\u9019\u500b\u5728 Network Drive \u4e0a\u662f\u4e0d\u53ef\u80fd\u5be6\u73fe\u7684\uff0c\u56e0\u70ba\u6bcf\u4e00\u500b\u6a94\u6848 Go \u90fd\u8981\u6293\u5b83\u7684 Stat\uff0c\u5373\u662f\u9060\u7aef\u6709\u591a\u5c11\u500b\u6a94\u6848\u5c31\u6703\u9001\u591a\u5c11\u500b file request \u904e\u53bb\uff0c\u6240\u4ee5\u81ea\u5f88\u65e9\u4e4b\u524d\u5c31 ArozOS \u7684 File Manager \u5c31\u52a0\u5165\u4e86 \u9019\u500b\u7c21\u55ae\u7c97\u66b4\u7684\u89e3\u6c7a\u65b9\u6cd5\u2026<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if fsh.IsNetworkDrive() {\n    filesize = -1\n}<\/code><\/pre>\n\n\n\n<p>\u7136\u800c\u96a8\u8457\u6211\u5728\u7528\u7684 NAS \u6771\u897f\u8d8a\u4f86\u8d8a\u591a\uff0c\u9019\u500b walk function \u5373\u4f7f\u5728 local disk \u4e0b\u4e5f\u958b\u59cb\u8b8a\u5f97\u5f88\u6162\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u90a3\u6539\u7528 kernel \u5167\u7f6e\u7684 API \u4f86\u6293\u4e0d\u5c31\u597d\u4e86\u55ce\uff1f<\/h2>\n\n\n\n<p>\u78ba\u5be6\u5982\u6b64\u3002Linux kernel \u88e1\u5176\u5be6\u6709\u5167\u7f6e\u4e00\u500b\u6eff\u597d\u7528\u7684\u6307\u4ee4\u53eb du\uff0c\u7528\u8d77\u4f86\u4e5f\u8d85\u7c21\u55ae\uff0c\u53ea\u8981\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>du -sb \/media\/storage1<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>481491222874 \/media\/storage1<\/code><\/pre>\n\n\n\n<p>\u90a3\u9019\u500b\u8cc7\u6599\u593e\u7e3d\u5927\u5c0f\uff08byte size\uff09\u5c31\u51fa\u4f86\u4e86\u3002\u5982\u679c\u4f60\u60f3\u8981 block size \u7684\u8a71\u53ef\u4ee5\u628a b \u53bb\u6389\u3002\u7136\u5f8c\u5c31\u662f\u5beb\u4e00\u500b\u7c21\u55ae\u7684 Go function \u53bb\u628a\u8cc7\u6599\u6293\u51fa\u4f86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*\nGet Directory Size with native syscall (local drive only)\nfaster than GetDirectorySize if system support du\n*\/\nfunc GetDirectorySizeNative(filename string)(int64, error) {\n    d, err: = apt.PackageExists(\"du\")\n    if err != nil || !d {\n        return 0, err\n    }\n\n    \/\/Convert the filename to absolute path\n    abspath, err: = filepath.Abs(filename)\n    if err != nil {\n        return 0, err\n    }\n\n    \/\/du command exists\n    \/\/use native syscall to get disk size\n    cmd: = exec.Command(\"du\", \"-sb\", abspath)\n    out, err: = cmd.CombinedOutput()\n    if err != nil {\n        return 0, err\n    }\n\n    \/\/Return value is something like 481491222874    \/media\/storage2\n    \/\/We need to trim off the spaces\n    tmp: = string(out)\n    tmp = strings.TrimSpace(tmp)\n    tmp = strings.ReplaceAll(tmp, \"\\t\", \" \")\n    for strings.Contains(tmp, \"  \") {\n        tmp = strings.ReplaceAll(tmp, \"  \", \" \")\n    }\n\n    chunks: = strings.Split(tmp, \" \")\n    if len(chunks) &lt;= 1 {\n        return 0, errors.New(\"malformed output\")\n    }\n\n    \/\/The first chunk should be the size in bytes\n    size, err: = strconv.Atoi(chunks&#91;0])\n    if err != nil {\n        return 0, errors.New(\"malformed output\")\n    }\n\n    return int64(size), nil\n\n}<\/code><\/pre>\n\n\n\n<p>\u5c0d\uff0c\u8a18\u5f97\u8981\u628a \\t (Tab) \u8ddf &#8221; &#8221; (Space) \u53bb\u6389\uff0c\u9084\u8981\u505a\u4e00\u500b for loop \u628a double space \u8b8a\u6210 single space \u9019\u6a23\u4e4b\u5f8c\u6bd4\u8f03\u597d split\u3002<s>\u561b\u96d6\u7136\u8aaa\u4e5f\u662f\u53ef\u4ee5\u7528 regex \u628a\u5b83\u5207\u6210 slice\uff0c\u4f46\u662f\u6211\u662f\u89ba\u5f97\u5982\u679c\u7528 regex \u7684\u8a71\u672a\u4f86\u7684\u6211\u770b\u5230\u61c9\u8a72\u6703\u60f3\u7a7f\u8d8a\u56de\u4f86\u6247\u73fe\u5728\u7684\u6211\u4e00\u5df4\u638c\u3002<\/s><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if fsh.IsLocalDrive() {\n    \/\/Try using native syscall to grab directory size\n    nativeSize, err: = filesystem.GetDirectorySizeNative(fsh.Path)\n    if err == nil {\n        usefallback = false\n        filesize = nativeSize\n    }\n}\n\/\/ \u5982\u679c fallback \u7684\u8a71\u7528\u539f\u672c\u7684 Walk function<\/code><\/pre>\n\n\n\n<p>\u4e4b\u5f8c\u5c31\u662f\u5728\u539f\u5148\u7684 code \u88e1\u52a0\u5165\u9019\u6a23\u7684\u4e00\u500b\u529f\u80fd\uff0c\u5982\u679c\u662f local drive \u7684\u8a71\u5c31\u53bb\u7528 du \u4f86\u6293\uff0c\u6293\u4e0d\u5230\u7684\u8a71\u9084\u662f\u6703\u8dd1\u56de\u53bb\u7528 Go \u63d0\u4f9b\u7684 Walk implementation \u70ba fallback\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7d50\u679c\u901f\u5ea6\u600e\u6a23\uff1f<\/h2>\n\n\n\n<p>\u7d50\u679c\u5c31\u662f\u7531\u539f\u672c Walk \u7684 12 &#8211; 13 \u79d2\u7e2e\u77ed\u5230\u4e4b\u5f8c\u7684 5\u79d2\uff0c\u7b97\u662f\u6709\u8b8a\u5feb\u4e86\u4e00\u9ede\u9ede\uff08\uff1f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u56e0\u70ba\u4e00\u4e9b\u610f\u5916\u6240\u4ee5\u8981\u983b\u7e41\u8dd1\u91ab\u9662\u56de\u8a3a\uff0c\u7d50\u679c\u5c31\u662f\u5728\u7b49\u5f85\u8ddf\u4f86\u56de\u7684\u6642\u9593\u524d\u5f8c\u591a\u4e86\u5f88\u591a\u96f6\u788e\u7684\u6642\u9593\uff0c\u6240\u4ee5\u6211\u5c31\u4e0d\u6d6a\u8cbb\u9019\u4e9b\u6642\u9593\u4f86\u5e6b\u6211\u7528\u4e86\u5feb 6 &#8211; 7 \u5e74\u7684 ArozOS \u7cfb\u7d71\u505a\u4e86\u9ede\u66f4\u65b0\u4e86\u3002\u4f46\u662f\u9019\u7bc7\u90e8\u843d\u683c\u4e26\u4e0d\u662f\u5728\u8b1b\u6211\u66f4\u65b0\u4e86\u751a\u9ebc\uff08\u96d6\u7136\u67d0\u7a0b\u5ea6\u4e0a\u4e5f\u662f\uff09\uff0c\u800c\u662f\u8b1b\u4e00\u4e0b\u95dc\u65bc Golang \u8655\u7406 exec \u8f38\u51fa\u7684\u4e00\u500b\u5c0f\u767c\u73fe\u3002 \u80cc\u666f\u6545\u4e8b &#8211; ArozOS \u7684 Disk Properties \u8f09\u5f88\u6162 ArozOS \u56e0\u70ba\u63a1\u7528\u534a\u865b\u64ec\u78c1\u789f\u67b6\u69cb\u7684\u8a2d\u8a08\uff0c\u6240\u4ee5\u6bcf\u6b21\u8981\u5217\u51fa disk space \u7684\u6642\u5019\uff0c\u9664\u4e86\u4e00\u822c\u50cf Windows \u90a3\u6a23\u6703\u5217\u51fa\u7528\u4e86\u591a\u5c11\u8ddf\u5168\u90e8\u7a7a\u9593\u662f\u591a\u5c11\u4ee5\u5916\uff0c\u5982\u679c\u78c1\u789f\u7a7a\u9593\u662f\u4f7f\u7528\u8005\u5206\u9694\u7684\uff0c\u5c31\u8981\u591a\u8a08\u7b97\u4f7f\u7528\u8005\u4f54\u7528\u7684\u7a7a\u9593\u6bd4\uff08\u5716\u4e0a\u9ec3\u8272\u689d\uff09 \u9019\u500b\u529f\u80fd\u539f\u672c\u662f\u4f7f\u7528 Walk function \u4f86\u5be6\u73fe\u7684\u3002\u56e0\u70ba ArozOS \u7684 vfs architecture \u95dc\u7cfb\uff0c\u9019\u88e1\u6211\u4f7f\u7528\u7684\u662f ArozOS \u5c08\u7528\u7684 filepath module \uff08\u4f60\u53ef\u4ee5\u628a\u5b83\u60f3\u50cf\u6210 Go \u539f\u672c\u7684 filepath.Walk fshAbs \u662f file system handler abstraction \u7684\u610f\u601d\uff0c\u5927\u6982\u5c31\u662f\u300c\u9019\u500b\u865b\u64ec\u78c1\u789f\u7684\u6a94\u6848\u7cfb\u7d71\u5c64\u300d\u7684 object\u3002 \u53e6\u5916 rpath \u5c31\u662f\u5f9e user space [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29,5],"tags":[],"class_list":["post-1131","post","type-post","status-publish","format-standard","hentry","category-aroz-online-system","category-development-logs"],"_links":{"self":[{"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/posts\/1131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1131"}],"version-history":[{"count":2,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/posts\/1131\/revisions"}],"predecessor-version":[{"id":1134,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/posts\/1131\/revisions\/1134"}],"wp:attachment":[{"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}