{"id":3513,"date":"2017-08-12T08:02:59","date_gmt":"2017-08-12T00:02:59","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=3513"},"modified":"2017-08-12T08:02:59","modified_gmt":"2017-08-12T00:02:59","slug":"linux%e4%b8%8bc%e8%af%ad%e8%a8%80%e4%b8%adinotify%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e6%96%87%e4%bb%b6%e5%85%a8%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/3513.html","title":{"rendered":"Linux\u4e0bC\u8bed\u8a00\u4e2dinotify\u5982\u4f55\u83b7\u53d6\u6587\u4ef6\u5168\u8def\u5f84"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"Linux\u4e0bC\u8bed\u8a00\u4e2dinotify\u5982\u4f55\u83b7\u53d6\u6587\u4ef6\u5168\u8def\u5f84-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u5b66\u4e60\u63d0\u9ad8\u9700\u8981<\/p>\n<h4 id=\"Linux\u4e0bC\u8bed\u8a00\u4e2dinotify\u5982\u4f55\u83b7\u53d6\u6587\u4ef6\u5168\u8def\u5f84-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"Linux\u4e0bC\u8bed\u8a00\u4e2dinotify\u5982\u4f55\u83b7\u53d6\u6587\u4ef6\u5168\u8def\u5f84-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<blockquote><p>You&#8217;re watching a file. You have passed its name to inotify_add_watch, and got a watch descriptor. If you get an event, you can figure out the file by the watch descriptor.<\/p>\n<p>You&#8217;re watching a directory. Again, you have passed the directory name to inotify_add_watch, and can find which name it was, based on the watch ID. Now inotify_event.name contains the file name. Concatenate the two and you&#8217;re done.<\/p><\/blockquote>\n<p>\u7ffb\u8bd1\u8fc7\u6765\u5c31\u662f\uff1a<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u7ed9 inotify_add_watch() \u4f20\u9012\u7684\u53c2\u6570\u662f\u6587\u4ef6\u6765\u8bf4\uff0c\u5f53I\/O\u4e8b\u4ef6\u53d1\u751f\u65f6 inotify_event.name \u4e3a NULL \uff1b<\/li>\n<li>\u5bf9\u4e8e\u7ed9 inotify_add_watch() \u4f20\u9012\u7684\u53c2\u6570\u662f\u76ee\u5f55\u6765\u8bf4\uff0c\u5f53I\/O\u4e8b\u4ef6\u53d1\u751f\u65f6 inotify_event.name \u4e3a \u6587\u4ef6\u540d(\u4f46\u4e0d\u5305\u62ec\u8def\u5f84) \uff1b<\/li>\n<\/ul>\n<p>\u5728C\u8bed\u8a00\u4e2d\u7684\u4e00\u4e2a\u89e3\u51b3\u529e\u6cd5\u5c31\u662f\u65b0\u5efa\u4e00\u4e2a\u7ed3\u6784\u4f53\u7528\u4e8e\u5b58\u653e\u6587\u4ef6\u63cf\u8ff0\u7b26fd\u548c\u5bf9\u5e94\u7684\u4f20\u5165\u7684\u6587\u4ef6\/\u76ee\u5f55\u540dpathname\uff08\u5982\u679c\u4f20\u7ed9inotify_add_watch()\u51fd\u6570\u7684\u5c31\u662f\u76f8\u5bf9\u8def\u5f84\uff0c\u4e14\u4f60\u5e0c\u671b\u83b7\u53d6\u5bf9\u5e94\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u53ef\u4ee5\u8003\u8651\u7528\u4e00\u4e2a\u51fd\u6570realpath()\u6765\u5b9e\u73b0\u8fd9\u4e2a\u8f6c\u6362\uff09\uff1a<\/p>\n<div class=\"code panel pdl conf-macro output-block\" data-hasbody=\"true\" data-macro-name=\"code\">\n<div class=\"codeContent panelContent pdl\">\n<div>\n<div id=\"highlighter_393565\" class=\"syntaxhighlighter sh-confluence nogutter java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"code\">\n<div class=\"container\" title=\"Hint: double-click to select code\">\n<div class=\"line number1 index0 alt2\"><code class=\"java plain\">#ifndef PATH_MAX<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java plain\">#define PATH_MAX\u00a0<\/code><code class=\"java value\">128<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java plain\">#endif<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java plain\">typedef struct {<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"java spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"java keyword\">int<\/code>\u00a0<code class=\"java plain\">fd;<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"java spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"java keyword\">char<\/code>\u00a0<code class=\"java plain\">pathname[PATH_MAX];<\/code><\/div>\n<div class=\"line number7 index6 alt2\"><code class=\"java plain\">} inotify_fd_path;<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u5728C++\u4e2d\u5e94\u8be5\u662f\u6709map\u8fd9\u6837\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\uff0c\u53c2\u8003\uff1a<a class=\"external-link\" href=\"https:\/\/gist.githubusercontent.com\/pkrnjevic\/6016356\/raw\/3d2e3e739460b56335b80ec56bb091e9660f7e2e\/inotify-example.cpp\" rel=\"nofollow\">inotify-example.cpp<\/a>\u3002<\/p>\n<h5 id=\"Linux\u4e0bC\u8bed\u8a00\u4e2dinotify\u5982\u4f55\u83b7\u53d6\u6587\u4ef6\u5168\u8def\u5f84-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a class=\"external-link\" href=\"https:\/\/stackoverflow.com\/questions\/9313206\/retrieve-the-full-path-name-from-inotify-event\" rel=\"nofollow\">https:\/\/stackoverflow.com\/questions\/9313206\/retrieve-the-full-path-name-from-inotify-event<\/a><\/li>\n<li><a class=\"external-link\" href=\"https:\/\/gist.githubusercontent.com\/pkrnjevic\/6016356\/raw\/3d2e3e739460b56335b80ec56bb091e9660f7e2e\/inotify-example.cpp\" rel=\"nofollow\">https:\/\/gist.githubusercontent.com\/pkrnjevic\/6016356\/raw\/3d2e3e739460b56335b80ec56bb091e9660f7e2e\/inotify-example.cpp<\/a><\/li>\n<li>\u4f7f\u7528 inotify \u76d1\u63a7\u6587\u4ef6\u7cfb\u7edf\u7684\u6d3b\u52a8<br \/>\n<a class=\"external-link\" href=\"https:\/\/www.ibm.com\/developerworks\/cn\/linux\/l-ubuntu-inotify\/index.html\" rel=\"nofollow\">https:\/\/www.ibm.com\/developerworks\/cn\/linux\/l-ubuntu-inotify\/index.html<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u5b66\u4e60\u63d0\u9ad8\u9700\u8981 \u6b63\u6587\uff1a \u53c2\u8003\u89e3\u7b54\uff1a You&#8217;re watching a fi [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,7],"tags":[100,620,30,416],"class_list":["post-3513","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-programing","tag-c","tag-inotify","tag-linux","tag-struct"],"views":4323,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3513","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=3513"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3513\/revisions"}],"predecessor-version":[{"id":3514,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3513\/revisions\/3514"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=3513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=3513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=3513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}